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

struct Checkpoint {
  uint32 fromBlock;
  uint96 votes;
}

_delegates

mapping(address => address) _delegates

_checkpoints

mapping(address => uint128[]) _checkpoints

_totalSupplyCheckpoints

uint128[] _totalSupplyCheckpoints

DelegateChanged

event DelegateChanged(address delegator, address fromDelegate, address toDelegate)

Emitted when an account changes their delegate.

DelegateVotesChanged

event DelegateVotesChanged(address delegate, uint256 previousBalance, uint256 newBalance)

Emitted when a balance or delegate change results in changes to an account's voting power.

checkpoints

function checkpoints(address account, uint32 pos) public view virtual returns (struct Checkpoints.Checkpoint checkpoint)

numCheckpoints

function numCheckpoints(address account) public view virtual returns (uint32)

Get number of checkpoints for account.

delegates

function delegates(address account) public view virtual returns (address)

Get the address account is currently delegating to.

getVotes

function getVotes(address account) public view returns (uint96)

Gets the current votes balance for account.

Parameters

NameTypeDescription

account

address

The address to get votes balance

Return Values

NameTypeDescription

[0]

uint96

The number of current votes for account

getPastVotes

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.

Parameters

NameTypeDescription

account

address

The address of the account to check

blockNumber

uint256

The block number to get the vote balance at

Return Values

NameTypeDescription

[0]

uint96

The number of votes the account had as of the given block

getPastTotalSupply

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

Parameters

NameTypeDescription

blockNumber

uint256

The block number to get the total supply at

delegate

function delegate(address delegator, address delegatee) internal virtual

Change delegation for delegator to delegatee.

moveVotingPower

function moveVotingPower(address src, address dst, uint256 amount) internal

Moves voting power from one delegate to another

Parameters

NameTypeDescription

src

address

Address of old delegate

dst

address

Address of new delegate

amount

uint256

Voting power amount to transfer between delegates

writeCheckpoint

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.

Parameters

NameTypeDescription

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

lookupCheckpoint

function lookupCheckpoint(uint128[] ckpts, uint256 blockNumber) internal view returns (uint96)

Lookup a value in a list of (sorted) checkpoints.

Parameters

NameTypeDescription

ckpts

uint128[]

The checkpoints array to use

blockNumber

uint256

Block number when we want to get the checkpoint at

maxSupply

function maxSupply() internal view virtual returns (uint96)

Maximum token supply. Defaults to type(uint96).max (2^96 - 1)

encodeCheckpoint

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.

decodeBlockNumber

function decodeBlockNumber(uint128 checkpoint) internal pure returns (uint32)

Decodes a block number from a uint128 checkpoint.

decodeValue

function decodeValue(uint128 checkpoint) internal pure returns (uint96)

Decodes a voting value from a uint128 checkpoint.

decodeCheckpoint

function decodeCheckpoint(uint128 checkpoint) internal pure returns (uint32 blockNumber, uint96 value)

Decodes a block number and voting value from a uint128 checkpoint.

add

function add(uint256 a, uint256 b) internal pure returns (uint256)

subtract

function subtract(uint256 a, uint256 b) internal pure returns (uint256)

Last updated