Skip to main content

DrawAccumulatorLib

Git Source

Author: G9 Software Inc.

This contract distributes tokens over time according to an exponential weighted average. Time is divided into discrete "draws", of which each is allocated tokens.

Constants

MAX_OBSERVATION_CARDINALITY

uint16 constant MAX_OBSERVATION_CARDINALITY = 366;

Functions

add

Adds balance for the given draw id to the accumulator.

function add(Accumulator storage accumulator, uint256 _amount, uint24 _drawId) internal returns (bool);

Parameters

NameTypeDescription
accumulatorAccumulatorThe accumulator to add to
_amountuint256The amount of balance to add
_drawIduint24The draw id to which to add balance to. This must be greater than or equal to the previous addition's draw id.

Returns

NameTypeDescription
<none>boolTrue if a new observation was created, false otherwise.

newestDrawId

Returns the newest draw id from the accumulator.

function newestDrawId(Accumulator storage accumulator) internal view returns (uint256);

Parameters

NameTypeDescription
accumulatorAccumulatorThe accumulator to get the newest draw id from

Returns

NameTypeDescription
<none>uint256The newest draw id

newestObservation

Returns the newest draw id from the accumulator.

function newestObservation(Accumulator storage accumulator) internal view returns (Observation memory);

Parameters

NameTypeDescription
accumulatorAccumulatorThe accumulator to get the newest draw id from

Returns

NameTypeDescription
<none>ObservationThe newest draw id

getDisbursedBetween

Gets the balance that was disbursed between the given start and end draw ids, inclusive.

function getDisbursedBetween(Accumulator storage _accumulator, uint24 _startDrawId, uint24 _endDrawId)
internal
view
returns (uint256);

Parameters

NameTypeDescription
_accumulatorAccumulatorThe accumulator to get the disbursed balance from
_startDrawIduint24The start draw id, inclusive
_endDrawIduint24The end draw id, inclusive

Returns

NameTypeDescription
<none>uint256The disbursed balance between the given start and end draw ids, inclusive

binarySearch

Binary searches an array of draw ids for the given target draw id.

The _targetDrawId MUST exist between the range of draws at _oldestIndex and _newestIndex (inclusive)

function binarySearch(
uint24[366] storage _drawRingBuffer,
uint16 _oldestIndex,
uint16 _newestIndex,
uint16 _cardinality,
uint24 _targetDrawId
)
internal
view
returns (uint16 beforeOrAtIndex, uint24 beforeOrAtDrawId, uint16 afterOrAtIndex, uint24 afterOrAtDrawId);

Parameters

NameTypeDescription
_drawRingBufferuint24[366]The array of draw ids to search
_oldestIndexuint16The oldest index in the ring buffer
_newestIndexuint16The newest index in the ring buffer
_cardinalityuint16The number of items in the ring buffer
_targetDrawIduint24The target draw id to search for

Returns

NameTypeDescription
beforeOrAtIndexuint16The index of the observation occurring at or before the target draw id
beforeOrAtDrawIduint24The draw id of the observation occurring at or before the target draw id
afterOrAtIndexuint16The index of the observation occurring at or after the target draw id
afterOrAtDrawIduint24The draw id of the observation occurring at or after the target draw id

Structs

Accumulator

An accumulator for a draw.

struct Accumulator {
RingBufferInfo ringBufferInfo;
uint24[366] drawRingBuffer;
mapping(uint256 drawId => Observation observation) observations;
}

Properties

NameTypeDescription
ringBufferInfoRingBufferInfoThe metadata for the drawRingBuffer
drawRingBufferuint24[366]The ring buffer of draw ids
observationsmapping(uint256 drawId => Observation observation)The observations for each draw id

Observation

The accumulator observation record

struct Observation {
uint96 available;
uint160 disbursed;
}

Properties

NameTypeDescription
availableuint96The total amount available as of this Observation
disburseduint160The total amount disbursed in the past

RingBufferInfo

The metadata for using the ring buffer.

struct RingBufferInfo {
uint16 nextIndex;
uint16 cardinality;
}