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
  • Introduction
  • Samples
  • Time-Weighted Average Values
  1. Concepts

Oracle

PreviousFeesNextContracts

Last updated 9 days ago

Introduction

LBPair stores cumulative values as a sample for the following data:

  • Active bin ID

  • Volatility accumulated ($v_a$)

  • Number of bins crossed ($k$)

The active bin ID can be easily converted into the bin price via helper functions, allowing users to obtain historical prices easily.

Using cumulative values allow us to take the time-weighted average values.

Samples

Samples are stored in a 16-bit circular array. However, by default, the oracle array only stores 2 samples, but it can be extended by anyone by calling increaseOracleLength. This is to shift the burden of gas cost to the oracle user.

Each sample consists of three values: cumulative bin ID, cumulative volatility accumulated and cumultive bins crossed.

Samples are updated at the end of each swap.

A new sample is created if enough time has passed since the creation of the previous sample as defined by the variable oracleSampleLifeTime. To be specific, given a new swap, if the time since creation of sample $i$ has exceeded oracleSampleLifeTime, then we create sample $i+1$. Otherwise, we update the cumulative values in sample $i$.

Time-Weighted Average Values

Sample values can be read by calling getOracleSampleFrom. This will return the three cumulative values at the time specified by the timeDelta input: cumulativeId, cumulativeVolatilityAccumulated and cumulativeBinCrossed.

To calculate the average value of any of those three variables, getOracleSampleFrom needs to called on two different timestamps, and the average value will be:

Time Weighted Average=getOracleSampleFrom(t2).value−getOracleSampleFrom(t1).valuetimestamp(t2)−timestamp(t1)\textbf{Time Weighted Average} = \frac{getOracleSampleFrom(t_2).value - getOracleSampleFrom(t_1).value}{timestamp(t_2) - timestamp(t_1)}Time Weighted Average=timestamp(t2​)−timestamp(t1​)getOracleSampleFrom(t2​).value−getOracleSampleFrom(t1​).value​

To convert time-weighted average ID to time-weighted average price, you can use the getPriceFromId() function from the LBRouter contract.