Skip to content

Latest commit

 

History

History
220 lines (147 loc) · 5.52 KB

File metadata and controls

220 lines (147 loc) · 5.52 KB

Solidity API

Checkpoints

_This library defines the Trace* struct, for checkpointing values as they change at different points in time, and later looking up past values by block number. See {Votes} as an example.

To create a history of checkpoints define a variable type Checkpoints.Trace* in your contract, and store a new checkpoint for the current transaction block using the {push} function._

Trace

struct Trace {
  struct Checkpoints.Checkpoint[] _checkpoints;
}

Point

struct Point {
  int128 bias;
  int128 slope;
  int128 permanent;
}

Checkpoint

struct Checkpoint {
  uint48 _key;
  struct Checkpoints.Point _value;
}

CheckpointUnorderedInsertions

error CheckpointUnorderedInsertions()

A value was attempted to be inserted on a past checkpoint.

push

function push(struct Checkpoints.Trace self, uint48 key, struct Checkpoints.Point value) internal returns (struct Checkpoints.Point, struct Checkpoints.Point)

_Pushes a (key, value) pair into a Trace so that it is stored as the checkpoint.

Returns previous value and new value.

IMPORTANT: Never accept key as a user input, since an arbitrary type(uint48).max key set will disable the library._

lowerLookup

function lowerLookup(struct Checkpoints.Trace self, uint48 key) internal view returns (struct Checkpoints.Point)

Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.

upperLookup

function upperLookup(struct Checkpoints.Trace self, uint48 key) internal view returns (bool exists, uint48 _key, struct Checkpoints.Point _value)

Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.

upperLookupRecent

function upperLookupRecent(struct Checkpoints.Trace self, uint48 key) internal view returns (bool exists, uint48 _key, struct Checkpoints.Point _value)

_Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.

NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys)._

latest

function latest(struct Checkpoints.Trace self) internal view returns (struct Checkpoints.Point)

Returns the value in the most recent checkpoint, or zero if there are no checkpoints.

latestCheckpoint

function latestCheckpoint(struct Checkpoints.Trace self) internal view returns (bool exists, uint48 _key, struct Checkpoints.Point _value)

Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value in the most recent checkpoint.

length

function length(struct Checkpoints.Trace self) internal view returns (uint256)

Returns the number of checkpoint.

at

function at(struct Checkpoints.Trace self, uint48 pos) internal view returns (struct Checkpoints.Checkpoint)

Returns checkpoint at given position.

blankPoint

function blankPoint() internal pure returns (struct Checkpoints.Point)

TraceAddress

struct TraceAddress {
  struct Checkpoints.CheckpointAddress[] _checkpoints;
}

CheckpointAddress

struct CheckpointAddress {
  uint48 _key;
  address _value;
}

push

function push(struct Checkpoints.TraceAddress self, uint48 key, address value) internal returns (address, address)

_Pushes a (key, value) pair into a TraceAddress so that it is stored as the checkpoint.

Returns previous value and new value.

IMPORTANT: Never accept key as a user input, since an arbitrary type(uint48).max key set will disable the library._

lowerLookup

function lowerLookup(struct Checkpoints.TraceAddress self, uint48 key) internal view returns (address)

Returns the value in the first (oldest) checkpoint with key greater or equal than the search key, or zero if there is none.

upperLookup

function upperLookup(struct Checkpoints.TraceAddress self, uint48 key) internal view returns (address)

Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.

upperLookupRecent

function upperLookupRecent(struct Checkpoints.TraceAddress self, uint48 key) internal view returns (address)

_Returns the value in the last (most recent) checkpoint with key lower or equal than the search key, or zero if there is none.

NOTE: This is a variant of {upperLookup} that is optimised to find "recent" checkpoint (checkpoints with high keys)._

latest

function latest(struct Checkpoints.TraceAddress self) internal view returns (address)

Returns the value in the most recent checkpoint, or zero if there are no checkpoints.

latestCheckpoint

function latestCheckpoint(struct Checkpoints.TraceAddress self) internal view returns (bool exists, uint48 _key, address _value)

Returns whether there is a checkpoint in the structure (i.e. it is not empty), and if so the key and value in the most recent checkpoint.

length

function length(struct Checkpoints.TraceAddress self) internal view returns (uint256)

Returns the number of checkpoint.

at

function at(struct Checkpoints.TraceAddress self, uint48 pos) internal view returns (struct Checkpoints.CheckpointAddress)

Returns checkpoint at given position.