Checkpoints
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
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.
struct Checkpoint {
uint32 fromBlock;
uint96 votes;
}
mapping(address => address) _delegates
mapping(address => uint128[]) _checkpoints
uint128[] _totalSupplyCheckpoints
event DelegateChanged(address delegator, address fromDelegate, address toDelegate)
Emitted when an account changes their delegate.
event DelegateVotesChanged(address delegate, uint256 previousBalance, uint256 newBalance)
Emitted when a balance or delegate change results in changes to an account's voting power.
function checkpoints(address account, uint32 pos) public view virtual returns (struct Checkpoints.Checkpoint checkpoint)
function numCheckpoints(address account) public view virtual returns (uint32)
Get number of checkpoints for account
.
function delegates(address account) public view virtual returns (address)
Get the address account
is currently delegating to.
function getVotes(address account) public view returns (uint96)
Gets the current votes balance for account
.
account
address
The address to get votes balance
[0]
uint96
The number of current votes for account
function getPastVotes(address account, uint256 blockNumber) public view returns (uint96)
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.
account
address
The address of the account to check
blockNumber
uint256
The block number to get the vote balance at
[0]
uint96
The number of votes the account had as of the given block
function getPastTotalSupply(uint256 blockNumber) public view returns (uint96)
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
blockNumber
uint256
The block number to get the total supply at
function delegate(address delegator, address delegatee) internal virtual
Change delegation for delegator
to delegatee
.
function moveVotingPower(address src, address dst, uint256 amount) internal
Moves voting power from one delegate to another
src
address
Address of old delegate
dst
address
Address of new delegate
amount
uint256
Voting power amount to transfer between delegates
function writeCheckpoint(uint128[] ckpts, function (uint256,uint256) view returns (uint256) op, uint256 delta) internal returns (uint256 oldWeight, uint256 newWeight)
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.
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
delta
uint256
Variation with respect to last stored value to be used for new checkpoint
function lookupCheckpoint(uint128[] ckpts, uint256 blockNumber) internal view returns (uint96)
Lookup a value in a list of (sorted) checkpoints.
ckpts
uint128[]
The checkpoints array to use
blockNumber
uint256
Block number when we want to get the checkpoint at
function maxSupply() internal view virtual returns (uint96)
Maximum token supply. Defaults to type(uint96).max
(2^96 - 1)
function encodeCheckpoint(uint32 blockNumber, uint96 value) internal pure returns (uint128)
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.
function decodeBlockNumber(uint128 checkpoint) internal pure returns (uint32)
Decodes a block number from a uint128
checkpoint
.
function decodeValue(uint128 checkpoint) internal pure returns (uint96)
Decodes a voting value from a uint128
checkpoint
.
function decodeCheckpoint(uint128 checkpoint) internal pure returns (uint32 blockNumber, uint96 value)
Decodes a block number and voting value from a uint128
checkpoint
.
function add(uint256 a, uint256 b) internal pure returns (uint256)
function subtract(uint256 a, uint256 b) internal pure returns (uint256)