TWAP Oracles
Last updated
Last updated
The TWAP oracles report an of the prices of Principal Tokens, Yield Tokens and LP tokens, quoted in either interest bearing token or underlying asset. They primarily serve as one of the pricing references for lending market integrations.
The BaseOracle
provides the base template of Spectra TWAP oracles. It follows Chainlink's . This template takes the address of the Principal Token and the address of the underlying pool at construction. Currently, we integrate the and the AMMs from Curve.
The prices are fetched through the getRoundData()
and latestRoundData()
functions, both returning the latest version of the price data. The decimals()
method is overriden and specifies the decimals in which the prices are quoted. The _getQuoteAmount()
function is internally overriden and fetches the price data.
This template is used to create the BaseOracleCurveYT
, BaseOracleCurvePT
and BaseOracleCurveLP
contracts, which provide templates for reporting prices of the Yield Token, the Principal Token and the LP token respectively in a quote asset. In our case, the quote asset will either be the corresponding interest bearing token or the underlying asset.
The aforementioned contracts define the virtual methods _YTPrice()
, _PTPrice()
and _LPTPrice()
, whose role is to report the oracle price of the corresponding asset in a given quote asset. These have to be defined in the corresponding Chainlink Feed.
The Chainlink feeds acts as an isolated price data provider for a given base asset and a quote asset. Since we have 3 base assets to report the price of (PT, YT, LP), 2 quote assets (IBT, Underlying) and we support 2 AMMs (twocrypto-ng, stableswap-ng), we need 12 feeds.
The feeds define _YTPrice()
, _PTPrice()
and _LPTPrice()
by making calls to the library CurveOracleLib
. It also specifies the decimals of the data feed, which are the decimals of the quote asset.
This library takes care of fetching the prices from the underlying Curve pool by calling the price_oracle()
method. This method implements an exponential moving average of the spot prices as given by the AMM, along with native oracle manipulation protection. For further information, we refer the reader to the
To deploy a TWAP oracle instance, you must create and deploy a contract inheriting from the appropriate Chainlink feed class. For instance, to deploy a price oracle for a PrincipalToken quoted in the underlying asset within a StableSwap pool, your contract should inherit from BaseFeedCurvePTAssetSNG.
Below is an example of a mock TWAP oracle implementation designed for obtaining PrincipalToken prices quoted in the underlying asset from a StableSwap AMM.
If you do not plan on defining additional logic, you can deploy Spectra's oracles through the .