Sub topic 8
Delegation and Vote Checkpoints in Code
This chapter assumes you understand wallets, smart contracts, ERC-20 balances, and basic Solidity syntax. You will use OpenZeppelin’s ERC20Votes extension to see how a token balance becomes voting power.
Why delegation is required
A governance token can give voting power to its holder. However, counting every holder’s balance directly is not enough. A proposal needs a reliable answer to questions such as:
- How many votes did an address have when voting started?
- Did the address transfer tokens after voting began?
- Can a voter assign voting power to another address?
- Can the contract prevent a balance transfer from changing an earlier vote?
Delegation separates token ownership from voting power. A holder can delegate to their own address or to another address. The delegate receives the holder’s voting power without receiving the tokens.
An address that never delegates usually has zero recorded voting power, even if it owns tokens.
The ERC20Votes implementation
OpenZeppelin’s ERC20Votes extension tracks voting power and stores checkpoints. A checkpoint records an account’s voting power at a particular block. Governance contracts can then query the historical value instead of trusting the current balance.
The following contract mints one million tokens to the deployer. It also supports signed approvals through ERC20Permit and vote tracking through ERC20Votes.
// 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);
}
}The _update override is important because both ERC20 and ERC20Votes need to process transfers, minting, and burning. Calling super._update lets both parent contracts update their state.
What happens during delegation
Suppose Alice owns 500 GOV and calls:
govToken.delegate(alice);The contract records Alice as her own delegate and creates or updates a checkpoint. Alice’s current voting power becomes 500 GOV.
If Alice delegates to Bob instead, Bob receives 500 votes. Alice still owns the tokens, but Alice has no delegated voting power from those tokens.
When Alice transfers 100 GOV, the contract moves voting power from the old delegate to the new delegate. The token balance and the vote checkpoints therefore change together.
| Action | Token owner | Voting power recipient | New checkpoint |
|---|---|---|---|
| Alice delegates to herself | Alice | Alice | Alice’s votes increase |
| Alice delegates to Bob | Alice | Bob | Bob’s votes increase |
| Alice transfers tokens | Alice and recipient | Their delegates | Both records may change |
Reading historical votes
A governance contract can use a function such as:
uint256 votes = govToken.getPastVotes(
voter,
snapshotBlock
);The snapshotBlock fixes the voting power used for a proposal. A voter cannot buy tokens after the snapshot and use them to change that proposal’s result.
Checkpoints are usually written only when an account’s voting power changes. This saves storage compared with recording a value at every block. The contract searches the checkpoint history for the latest value at or before the requested block.
Common errors
- Assuming the current balance equals voting power.
- Forgetting to delegate after receiving tokens.
- Treating delegation as a token transfer.
- Using the current vote total instead of a historical snapshot.
- Ignoring delegated votes when analyzing concentration.
Exercise
Alice owns 1,000 GOV, but she has never delegated. Bob owns 100 GOV and has delegated to himself. If the system uses ERC20Votes, who normally has recorded voting power?
Show answer
Bob normally has 100 votes, while Alice has zero recorded voting power until she delegates.