sirenmarkets / core

SIREN Core Smart Contracts
https://sirenmarkets.com/
GNU General Public License v3.0
42 stars 12 forks source link

Uniswap TWAP on-chain price oracle #176

Open lukaskiss222 opened 2 years ago

lukaskiss222 commented 2 years ago

Instead of using chainlink as price oracle, we can try to use uniswap v3 pools as price oracle using TWAP. (Based on this whitepaper https://uniswap.org/whitepaper-v3.pdf, chapter 5) (https://docs.uniswap.org/protocol/concepts/V3-overview/oracle)

Idea: Tick Accumulator -> The tick accumulator stores the cumulative sum of the active tick at the time of the observation. The tick accumulator value increases monotonically and grows by the value of the current tick - per second.

To derive the arithmetic mean tick over an interval, the caller needs to retrieve two observations, one after the other, take the delta of the two values, and divide by the time elapsed between them. Calculating a TWAP from the tick accumulator is also covered in the whitepaper. Note that using an arithmetic mean tick to derive a price corresponds to a geometric mean price.

We can use: https://github.com/Uniswap/v3-periphery/blob/main/contracts/libraries/OracleLibrary.sol especially consult function.