diff --git a/src/strategies/erc20-quadratic-voting/README.md b/src/strategies/erc20-quadratic-voting/README.md new file mode 100644 index 000000000..d4a94f411 --- /dev/null +++ b/src/strategies/erc20-quadratic-voting/README.md @@ -0,0 +1,62 @@ +# ERC20 Quadratic Voting Strategy + +This strategy implements quadratic voting for ERC20 token holders. It applies the square root function to token balances, reducing the voting power of large holders while maintaining proportional representation for smaller holders. + +## How it Works + +The strategy calculates voting power using the formula: +``` +votingPower = √(tokenBalance) +``` + +This means: +- A holder with 100 tokens gets √100 = 10 voting power +- A holder with 400 tokens gets √400 = 20 voting power +- A holder with 900 tokens gets √900 = 30 voting power + +## Parameters + +- `address`: The ERC20 token contract address +- `symbol`: Token symbol (for display purposes) +- `decimals`: Number of token decimals + +## Example Configuration + +```json +{ + "strategies": [ + [ + "erc20-quadratic-voting", + { + "address": "0x6b175474e89094c44da98b954eedeac495271d0f", + "symbol": "DAI", + "decimals": 18 + } + ] + ] +} +``` + +## Benefits + +1. **Decentralization**: Prevents large token holders from dominating governance +2. **Fairness**: Gives smaller holders meaningful voting power +3. **Simplicity**: Easy to understand and implement +4. **Compatibility**: Works with any ERC20 token + +## Limitations + +1. **Sybil Vulnerability**: Can be gamed by splitting tokens across multiple addresses +2. **Reduced Incentives**: May reduce incentives for large stakeholders +3. **Complexity**: More complex than simple token-weighted voting + +## Comparison with Linear Voting + +| Token Balance | Linear Voting Power | Quadratic Voting Power | +|---------------|-------------------|----------------------| +| 100 tokens | 100 | 10 | +| 400 tokens | 400 | 20 | +| 900 tokens | 900 | 30 | +| 1600 tokens | 1600 | 40 | + +This shows how quadratic voting reduces the power gap between large and small holders. \ No newline at end of file diff --git a/src/strategies/erc20-quadratic-voting/examples.json b/src/strategies/erc20-quadratic-voting/examples.json new file mode 100644 index 000000000..5cc18ce21 --- /dev/null +++ b/src/strategies/erc20-quadratic-voting/examples.json @@ -0,0 +1,25 @@ +[ + { + "name": "erc20-quadratic-voting", + "strategy": { + "name": "erc20-quadratic-voting", + "params": { + "address": "0x6b175474e89094c44da98b954eedeac495271d0f", + "symbol": "DAI", + "decimals": 18 + } + }, + "network": "1", + "addresses": [ + "0x0000000000000000000000000000000000000000", + "0x0000000000000000000000000000000000000001", + "0x0000000000000000000000000000000000000002" + ], + "snapshot": 12345678, + "scores": { + "0x0000000000000000000000000000000000000000": 0, + "0x0000000000000000000000000000000000000001": 0, + "0x0000000000000000000000000000000000000002": 0 + } + } +] \ No newline at end of file diff --git a/src/strategies/erc20-quadratic-voting/index.ts b/src/strategies/erc20-quadratic-voting/index.ts new file mode 100644 index 000000000..853b31bd1 --- /dev/null +++ b/src/strategies/erc20-quadratic-voting/index.ts @@ -0,0 +1,58 @@ +import { formatUnits } from '@ethersproject/units'; +import { BigNumber } from '@ethersproject/bignumber'; +import { Multicaller } from '../../utils'; + +const ONE = BigNumber.from(1); +const TWO = BigNumber.from(2); + +// Square root implementation for BigNumber +function sqrt(value: BigNumber): BigNumber { + let z = value.add(ONE).div(TWO); + let y = value; + while (z.sub(y).isNegative()) { + y = z; + z = value.div(z).add(z).div(TWO); + } + return y; +} + +export const author = 'snapshot-labs'; +export const version = '1.0.0'; + +const abi = [ + 'function balanceOf(address account) external view returns (uint256)' +]; + +export async function strategy( + space, + network, + provider, + addresses, + options, + snapshot +): Promise> { + const blockTag = typeof snapshot === 'number' ? snapshot : 'latest'; + + // Validate required options + if (!options.address) { + throw new Error('Token address is required'); + } + if (!options.decimals) { + throw new Error('Token decimals are required'); + } + + // Get token balances for all addresses + const multi = new Multicaller(network, provider, abi, { blockTag }); + addresses.forEach((address) => + multi.call(address, options.address, 'balanceOf', [address]) + ); + const result: Record = await multi.execute(); + + // Apply quadratic voting (square root of balance) + return Object.fromEntries( + Object.entries(result).map(([address, balance]) => [ + address, + parseFloat(formatUnits(sqrt(balance), options.decimals / 2)) + ]) + ); +} \ No newline at end of file diff --git a/src/strategies/index.ts b/src/strategies/index.ts index e26e8fd5a..f50eecb4f 100644 --- a/src/strategies/index.ts +++ b/src/strategies/index.ts @@ -35,6 +35,7 @@ import * as erc20WithBalance from './erc20-with-balance'; import * as erc20BalanceOfDelegation from './erc20-balance-of-delegation'; import * as erc20BalanceOfWithDelegation from './erc20-balance-of-with-delegation'; import * as erc20BalanceOfQuadraticDelegation from './erc20-balance-of-quadratic-delegation'; +import * as erc20QuadraticVoting from './erc20-quadratic-voting'; import * as erc20BalanceOfTopHolders from './erc20-balance-of-top-holders'; import * as erc20BalanceOfWeighted from './erc20-balance-of-weighted'; import * as erc20BalanceStarknet from './erc20-balance-starknet'; @@ -550,6 +551,7 @@ const strategies = { 'erc20-balance-of-delegation': erc20BalanceOfDelegation, 'erc20-balance-of-with-delegation': erc20BalanceOfWithDelegation, 'erc20-balance-of-quadratic-delegation': erc20BalanceOfQuadraticDelegation, + 'erc20-quadratic-voting': erc20QuadraticVoting, 'erc20-balance-of-top-holders': erc20BalanceOfTopHolders, 'erc20-balance-of-weighted': erc20BalanceOfWeighted, 'erc20-balance-of-indexed': erc20BalanceOfIndexed,