Skip to content

OracleHelper ​

Git Source

Author:
Trader Joe

This library contains functions to manage the oracle
The oracle samples are stored in a single bytes32 array.
Each sample is encoded as follows:
0 - 16: oracle length (16 bits)
16 - 80: cumulative id (64 bits)
80 - 144: cumulative volatility accumulator (64 bits)
144 - 208: cumulative bin crossed (64 bits)
208 - 216: sample lifetime (8 bits)
216 - 256: sample creation timestamp (40 bits)

State Variables ​

_MAX_SAMPLE_LIFETIME ​

solidity
uint256 internal constant _MAX_SAMPLE_LIFETIME = 120 seconds;

Functions ​

checkOracleId ​

Modifier to check that the oracle id is valid

solidity
modifier checkOracleId(uint16 oracleId);

Parameters

NameTypeDescription
oracleIduint16The oracle id

getSample ​

Returns the sample at the given oracleId

solidity
function getSample(Oracle storage oracle, uint16 oracleId)
    internal
    view
    checkOracleId(oracleId)
    returns (bytes32 sample);

Parameters

NameTypeDescription
oracleOracleThe oracle
oracleIduint16The oracle id

Returns

NameTypeDescription
samplebytes32The sample

getActiveSampleAndSize ​

Returns the active sample and the active size of the oracle

solidity
function getActiveSampleAndSize(Oracle storage oracle, uint16 oracleId)
    internal
    view
    returns (bytes32 activeSample, uint16 activeSize);

Parameters

NameTypeDescription
oracleOracleThe oracle
oracleIduint16The oracle id

Returns

NameTypeDescription
activeSamplebytes32The active sample
activeSizeuint16The active size of the oracle

getSampleAt ​

Returns the sample at the given timestamp. If the timestamp is not in the oracle, it returns the closest sample

solidity
function getSampleAt(Oracle storage oracle, uint16 oracleId, uint40 lookUpTimestamp)
    internal
    view
    returns (uint40 lastUpdate, uint64 cumulativeId, uint64 cumulativeVolatility, uint64 cumulativeBinCrossed);

Parameters

NameTypeDescription
oracleOracleThe oracle
oracleIduint16The oracle id
lookUpTimestampuint40The timestamp to look up

Returns

NameTypeDescription
lastUpdateuint40The last update timestamp
cumulativeIduint64The cumulative id
cumulativeVolatilityuint64The cumulative volatility
cumulativeBinCrosseduint64The cumulative bin crossed

binarySearch ​

Binary search to find the 2 samples surrounding the given timestamp

solidity
function binarySearch(Oracle storage oracle, uint16 oracleId, uint40 lookUpTimestamp, uint16 length)
    internal
    view
    returns (bytes32, bytes32);

Parameters

NameTypeDescription
oracleOracleThe oracle
oracleIduint16The oracle id
lookUpTimestampuint40The timestamp to look up
lengthuint16The oracle length

Returns

NameTypeDescription
<none>bytes32prevSample The previous sample
<none>bytes32nextSample The next sample

setSample ​

Sets the sample at the given oracleId

solidity
function setSample(Oracle storage oracle, uint16 oracleId, bytes32 sample) internal checkOracleId(oracleId);

Parameters

NameTypeDescription
oracleOracleThe oracle
oracleIduint16The oracle id
samplebytes32The sample

update ​

Updates the oracle

solidity
function update(Oracle storage oracle, bytes32 parameters, uint24 activeId) internal returns (bytes32);

Parameters

NameTypeDescription
oracleOracleThe oracle
parametersbytes32The parameters
activeIduint24The active id

Returns

NameTypeDescription
<none>bytes32The updated parameters

increaseLength ​

Increases the oracle length

solidity
function increaseLength(Oracle storage oracle, uint16 oracleId, uint16 newLength) internal;

Parameters

NameTypeDescription
oracleOracleThe oracle
oracleIduint16The oracle id
newLengthuint16The new length

_checkOracleId ​

Checks that the oracle id is valid

solidity
function _checkOracleId(uint16 oracleId) private pure;

Parameters

NameTypeDescription
oracleIduint16The oracle id

Errors ​

OracleHelper__InvalidOracleId ​

solidity
error OracleHelper__InvalidOracleId();

OracleHelper__NewLengthTooSmall ​

solidity
error OracleHelper__NewLengthTooSmall();

OracleHelper__LookUpTimestampTooOld ​

solidity
error OracleHelper__LookUpTimestampTooOld();

Structs ​

Oracle ​

solidity
struct Oracle {
    bytes32[65535] samples;
}