solana-flux-aggregator icon indicating copy to clipboard operation
solana-flux-aggregator copied to clipboard

extend flux aggregator with exponential moving average

Open defactojob opened this issue 4 years ago • 0 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)
        );

defactojob avatar Jan 08 '21 02:01 defactojob