LFJ Developer Docs
  • Liquidity Book
  • Introduction
  • LB V2.2 Key Changes
  • Guides
    • Swap Tokens
    • Add/Remove Liquidity
    • Tracking Volume
    • Tracking Pool Balances
    • Finding The Best Quote
    • Byte32 Decoding
    • Price From Bin Id
    • Bin Id From Price
    • Finding Liquidity Depth
    • User Balances
  • Concepts
    • Concentrated Liquidity
    • Bin Math
    • Bin Liquidity
    • Swaps
    • Fees
    • Oracle
  • Contracts
    • Interfaces
      • ILBLegacyFactory
      • ILBLegacyToken
      • ILBLegacyPair
      • ILBLegacyRouter
      • ILBFlashLoanCallback
      • IPendingOwnable
      • IJoeFactory
      • IJoePair
      • IJoeRouter01
      • IJoeRouter02
      • IWNATIVE
      • ILBFactory
      • ILBHooks
      • ILBPair
      • ILBRouter
      • ILBToken
    • Libraries
      • Math
        • BitMath
        • Encoded
        • LiquidityConfigurations
        • PackedUint128Math
        • SafeCast
        • SampleMath
        • TreeMath
        • Uint128x128Math
        • Uint256x256Math
      • BinHelper
      • Clone
      • Constants
      • FeeHelper
      • Hooks
      • ImmutableClone
      • JoeLibrary
      • OracleHelper
      • PairParameterHelper
      • PriceHelper
      • ReentrancyGuardUpgradeable
      • TokenHelper
    • LBBaseHooks
    • LBFactory
    • LBPair
    • LBQuoter
    • LBRouter
    • LBToken
  • Deployment Addresses
    • Avalanche C-Chain
    • Fuji Testnet
    • Arbitrum One
    • Binance Smart Chain
    • Binance Smart Chain Testnet
    • Ethereum Mainnet
    • Monad Testnet
  • SDK
    • Introduction
    • Making a Trade
    • Adding Liquidity
    • Removing Liquidity
  • Audits
  • AMM
    • Joe V1 Contracts
    • Joe V1 Audits
  • LFJ DEX API
    • Dex Analytics
    • Pools
    • Rewards
    • User
    • User Lifetime Stats
    • Vaults
    • Models
  • LFJ Aggregator API
    • Default
    • Models
Powered by GitBook
On this page
  • State Variables
  • _MAX_SAMPLE_LIFETIME
  • Functions
  • checkOracleId
  • getSample
  • getActiveSampleAndSize
  • getSampleAt
  • binarySearch
  • setSample
  • update
  • increaseLength
  • _checkOracleId
  • Errors
  • OracleHelper__InvalidOracleId
  • OracleHelper__NewLengthTooSmall
  • OracleHelper__LookUpTimestampTooOld
  • Structs
  • Oracle
  1. Contracts
  2. Libraries

OracleHelper

PreviousJoeLibraryNextPairParameterHelper

Last updated 8 days ago

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

uint256 internal constant _MAX_SAMPLE_LIFETIME = 120 seconds;

Functions

checkOracleId

Modifier to check that the oracle id is valid

modifier checkOracleId(uint16 oracleId);

Parameters

Name
Type
Description

oracleId

uint16

The oracle id

getSample

Returns the sample at the given oracleId

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

oracleId

uint16

The oracle id

Returns

Name
Type
Description

sample

bytes32

The sample

getActiveSampleAndSize

Returns the active sample and the active size of the oracle

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

oracleId

uint16

The oracle id

Returns

Name
Type
Description

activeSample

bytes32

The active sample

activeSize

uint16

The 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

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

oracleId

uint16

The oracle id

lookUpTimestamp

uint40

The timestamp to look up

Returns

Name
Type
Description

lastUpdate

uint40

The last update timestamp

cumulativeId

uint64

The cumulative id

cumulativeVolatility

uint64

The cumulative volatility

cumulativeBinCrossed

uint64

The cumulative bin crossed

binarySearch

Binary search to find the 2 samples surrounding the given timestamp

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

oracleId

uint16

The oracle id

lookUpTimestamp

uint40

The timestamp to look up

length

uint16

The oracle length

Returns

Name
Type
Description

<none>

bytes32

prevSample The previous sample

<none>

bytes32

nextSample The next sample

setSample

Sets the sample at the given oracleId

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

oracleId

uint16

The oracle id

sample

bytes32

The sample

update

Updates the oracle

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

parameters

bytes32

The parameters

activeId

uint24

The active id

Returns

Name
Type
Description

<none>

bytes32

The updated parameters

increaseLength

Increases the oracle length

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

Parameters

Name
Type
Description

oracle

Oracle

The oracle

oracleId

uint16

The oracle id

newLength

uint16

The new length

_checkOracleId

Checks that the oracle id is valid

function _checkOracleId(uint16 oracleId) private pure;

Parameters

Name
Type
Description

oracleId

uint16

The oracle id

Errors

OracleHelper__InvalidOracleId

error OracleHelper__InvalidOracleId();

OracleHelper__NewLengthTooSmall

error OracleHelper__NewLengthTooSmall();

OracleHelper__LookUpTimestampTooOld

error OracleHelper__LookUpTimestampTooOld();

Structs

Oracle

struct Oracle {
    bytes32[65535] samples;
}
Git Source