Sub topic 2
Governance tokens: voting rights as a token
This chapter assumes you know what a smart contract is, can read short Solidity or Python, and understand ERC-20 balances. A governance token is an ERC-20 token whose balance also gives its holder voting power over a protocol's decisions.
Governance tokens: voting rights as a token
Most governance tokens use token-weighted voting: one token equals one vote. A holder with 10 percent of the supply controls about 10 percent of the votes. The contract typically uses the balance at the time a proposal starts, so tokens bought during the vote do not count.
From proposal to execution: voting, quorum, and timelocks
A proposal moves through a fixed sequence. Quorum is the minimum share of total supply that must vote for a result to count. A timelock is a contract that delays execution after approval, giving users time to react.
Delegation and vote checkpoints in code
Holding a token does not automatically activate voting power. The holder must delegate, which can be to themselves. The contract records checkpoints so it can read past balances at a given block.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
import {ERC20Votes} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";
import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
contract GovToken is ERC20, ERC20Permit, ERC20Votes {
constructor() ERC20("Gov Token", "GOV") ERC20Permit("Gov Token") {
_mint(msg.sender, 1_000_000 * 10 ** decimals());
}
function _update(address from, address to, uint256 value)
internal
override(ERC20, ERC20Votes)
{
super._update(from, to, value);
}
function nonces(address owner)
public
view
override(ERC20Permit, Nonces)
returns (uint256)
{
return super.nonces(owner);
}
}Why token voting can fail: plutocracy, apathy, and capture
Token voting gives the most power to the largest holders, which is plutocracy. Many holders also skip votes, so a small group can pass proposals, which is apathy. A group that buys or borrows enough tokens can push through changes that favor it, which is capture. Quorum and timelocks reduce these risks but do not remove them.
Liquidity pools and price impact
Many tokens trade in liquidity pools that follow the constant-product rule, x * y = k. A large trade against a thin pool moves the price. The function below computes the output and the price impact for a trade.
def swap_out(reserve_in, reserve_out, amount_in, fee=0.003):
"""Output amount for a constant-product (x * y = k) pool."""
amount_in_after_fee = amount_in * (1 - fee)
return reserve_out * amount_in_after_fee / (reserve_in + amount_in_after_fee)
# A pool holding 100 ETH and 1,000,000 MEME
spot_price = 1_000_000 / 100 # 10,000 MEME per ETH
out = swap_out(100, 1_000_000, 10) # buy with 10 ETH
effective_price = out / 10
price_impact = 1 - effective_price / spot_price
print(round(out), round(price_impact, 4))Exercise
Using the pool above, what is the price impact of buying with 10 ETH?
Show answer
The script prints 90649 and 0.0935, so the trade moves the price about 9.35 percent.