Skip to content

SampleMath ​

Git Source

Author:
Trader Joe

This library contains functions to encode and decode a sample into a single bytes32
and interact with the encoded bytes32
The 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 ​

OFFSET_ORACLE_LENGTH ​

solidity
uint256 internal constant OFFSET_ORACLE_LENGTH = 0;

OFFSET_CUMULATIVE_ID ​

solidity
uint256 internal constant OFFSET_CUMULATIVE_ID = 16;

OFFSET_CUMULATIVE_VOLATILITY ​

solidity
uint256 internal constant OFFSET_CUMULATIVE_VOLATILITY = 80;

OFFSET_CUMULATIVE_BIN_CROSSED ​

solidity
uint256 internal constant OFFSET_CUMULATIVE_BIN_CROSSED = 144;

OFFSET_SAMPLE_LIFETIME ​

solidity
uint256 internal constant OFFSET_SAMPLE_LIFETIME = 208;

OFFSET_SAMPLE_CREATION ​

solidity
uint256 internal constant OFFSET_SAMPLE_CREATION = 216;

Functions ​

encode ​

Encodes a sample

solidity
function encode(
    uint16 oracleLength,
    uint64 cumulativeId,
    uint64 cumulativeVolatility,
    uint64 cumulativeBinCrossed,
    uint8 sampleLifetime,
    uint40 createdAt
) internal pure returns (bytes32 sample);

Parameters

NameTypeDescription
oracleLengthuint16The oracle length
cumulativeIduint64The cumulative id
cumulativeVolatilityuint64The cumulative volatility
cumulativeBinCrosseduint64The cumulative bin crossed
sampleLifetimeuint8The sample lifetime
createdAtuint40The sample creation timestamp

Returns

NameTypeDescription
samplebytes32The encoded sample

getOracleLength ​

Gets the oracle length from an encoded sample

solidity
function getOracleLength(bytes32 sample) internal pure returns (uint16 length);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 16[: oracle length (16 bits) [16 - 256[: any (240 bits)

Returns

NameTypeDescription
lengthuint16The oracle length

getCumulativeId ​

Gets the cumulative id from an encoded sample

solidity
function getCumulativeId(bytes32 sample) internal pure returns (uint64 id);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 16[: any (16 bits) [16 - 80[: cumulative id (64 bits) [80 - 256[: any (176 bits)

Returns

NameTypeDescription
iduint64The cumulative id

getCumulativeVolatility ​

Gets the cumulative volatility accumulator from an encoded sample

solidity
function getCumulativeVolatility(bytes32 sample) internal pure returns (uint64 volatilityAccumulator);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 80[: any (80 bits) [80 - 144[: cumulative volatility accumulator (64 bits) [144 - 256[: any (112 bits)

Returns

NameTypeDescription
volatilityAccumulatoruint64The cumulative volatility

getCumulativeBinCrossed ​

Gets the cumulative bin crossed from an encoded sample

solidity
function getCumulativeBinCrossed(bytes32 sample) internal pure returns (uint64 binCrossed);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 144[: any (144 bits) [144 - 208[: cumulative bin crossed (64 bits) [208 - 256[: any (48 bits)

Returns

NameTypeDescription
binCrosseduint64The cumulative bin crossed

getSampleLifetime ​

Gets the sample lifetime from an encoded sample

solidity
function getSampleLifetime(bytes32 sample) internal pure returns (uint8 lifetime);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 208[: any (208 bits) [208 - 216[: sample lifetime (8 bits) [216 - 256[: any (40 bits)

Returns

NameTypeDescription
lifetimeuint8The sample lifetime

getSampleCreation ​

Gets the sample creation timestamp from an encoded sample

solidity
function getSampleCreation(bytes32 sample) internal pure returns (uint40 creation);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 216[: any (216 bits) [216 - 256[: sample creation timestamp (40 bits)

Returns

NameTypeDescription
creationuint40The sample creation timestamp

getSampleLastUpdate ​

Gets the sample last update timestamp from an encoded sample

solidity
function getSampleLastUpdate(bytes32 sample) internal pure returns (uint40 lastUpdate);

Parameters

NameTypeDescription
samplebytes32The encoded sample as follows: [0 - 216[: any (216 bits) [216 - 256[: sample creation timestamp (40 bits)

Returns

NameTypeDescription
lastUpdateuint40The sample last update timestamp

getWeightedAverage ​

Gets the weighted average of two samples and their respective weights

solidity
function getWeightedAverage(bytes32 sample1, bytes32 sample2, uint40 weight1, uint40 weight2)
    internal
    pure
    returns (uint64 weightedAverageId, uint64 weightedAverageVolatility, uint64 weightedAverageBinCrossed);

Parameters

NameTypeDescription
sample1bytes32The first encoded sample
sample2bytes32The second encoded sample
weight1uint40The weight of the first sample
weight2uint40The weight of the second sample

Returns

NameTypeDescription
weightedAverageIduint64The weighted average id
weightedAverageVolatilityuint64The weighted average volatility
weightedAverageBinCrosseduint64The weighted average bin crossed

update ​

Updates a sample with the given values

solidity
function update(bytes32 sample, uint40 deltaTime, uint24 activeId, uint24 volatilityAccumulator, uint24 binCrossed)
    internal
    pure
    returns (uint64 cumulativeId, uint64 cumulativeVolatility, uint64 cumulativeBinCrossed);

Parameters

NameTypeDescription
samplebytes32The encoded sample
deltaTimeuint40The time elapsed since the last update
activeIduint24The active id
volatilityAccumulatoruint24The volatility accumulator
binCrosseduint24The bin crossed

Returns

NameTypeDescription
cumulativeIduint64The cumulative id
cumulativeVolatilityuint64The cumulative volatility
cumulativeBinCrosseduint64The cumulative bin crossed