octopus-network / solana-flux-aggregator

Solana Flux Aggregator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

extend flux aggregator with exponential moving average

defactojob opened this issue · comments

This is how Uniswap's price oracle works:

They provides exponential price average, which allows users to calculate an average price for any arbitrary elapsed time (1h, 1 day, 1week) to track the exponential moving average, uniswap records price * seconds:

price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed; 

and to calculate moving average for a period, a smart contract can record the difference between the price cumulatives, and divide by the time period, like this:

price0Average = FixedPoint.uq112x112(
            uint224((price0Cumulative - price0CumulativeLast) / timeElapsed)
        );