Checkpoints
Checkpoints
Abstract contract to support checkpoints for Compound-like voting and delegation. This implementation supports token supply up to 2^96 - 1. This contract keeps a history (checkpoints) of each account's vote power. Vote power can be delegated either by calling the {delegate} function directly, or by providing a signature to be used with {delegateBySig}. Voting power can be publicly queried through {getVotes} and {getPastVotes}. NOTE: Extracted from OpenZeppelin ERCVotes.sol. This contract is upgrade-safe.
Checkpoint
_delegates
_checkpoints
_totalSupplyCheckpoints
DelegateChanged
Emitted when an account changes their delegate.
DelegateVotesChanged
Emitted when a balance or delegate change results in changes to an account's voting power.
checkpoints
numCheckpoints
Get number of checkpoints for account
.
delegates
Get the address account
is currently delegating to.
getVotes
Gets the current votes balance for account
.
Parameters
Name | Type | Description |
---|---|---|
account | address | The address to get votes balance |
Return Values
Name | Type | Description |
---|---|---|
[0] | uint96 | The number of current votes for |
getPastVotes
Determine the prior number of votes for an account as of a block number.
Block number must be a finalized block or else this function will revert to prevent misinformation.
Parameters
Name | Type | Description |
---|---|---|
account | address | The address of the account to check |
blockNumber | uint256 | The block number to get the vote balance at |
Return Values
Name | Type | Description |
---|---|---|
[0] | uint96 | The number of votes the account had as of the given block |
getPastTotalSupply
Retrieve the totalSupply
at the end of blockNumber
. Note, this value is the sum of all balances, but it is NOT the sum of all the delegated votes!
blockNumber
must have been already mined
Parameters
Name | Type | Description |
---|---|---|
blockNumber | uint256 | The block number to get the total supply at |
delegate
Change delegation for delegator
to delegatee
.
moveVotingPower
Moves voting power from one delegate to another
Parameters
Name | Type | Description |
---|---|---|
src | address | Address of old delegate |
dst | address | Address of new delegate |
amount | uint256 | Voting power amount to transfer between delegates |
writeCheckpoint
Writes a new checkpoint based on operating last stored value with a delta
. Usually, said operation is the add
or subtract
functions from this contract, but more complex functions can be passed as parameters.
Parameters
Name | Type | Description |
---|---|---|
ckpts | uint128[] | The checkpoints array to use |
op | function (uint256,uint256) view returns (uint256) | The function to apply over the last value and the |
delta | uint256 | Variation with respect to last stored value to be used for new checkpoint |
lookupCheckpoint
Lookup a value in a list of (sorted) checkpoints.
Parameters
Name | Type | Description |
---|---|---|
ckpts | uint128[] | The checkpoints array to use |
blockNumber | uint256 | Block number when we want to get the checkpoint at |
maxSupply
Maximum token supply. Defaults to type(uint96).max
(2^96 - 1)
encodeCheckpoint
Encodes a blockNumber
and value
into a single uint128
checkpoint.
blockNumber
is stored in the first 32 bits, while value
in the remaining 96 bits.
decodeBlockNumber
Decodes a block number from a uint128
checkpoint
.
decodeValue
Decodes a voting value from a uint128
checkpoint
.
decodeCheckpoint
Decodes a block number and voting value from a uint128
checkpoint
.
add
subtract
Last updated