Tarot Price Oracle: On-Chain & Manipulation-Resistant
We introduce a highly secure price oracle for token pairs on Fantom Opera, designed for robustness and reusability by other DeFi projects.
The wisdom of the oracle echoes into the future that which has been predetermined by fate. Destiny is revealed.
Introduction To Price Oracles
Price oracles are a critical piece of decentralized finance (DeFi) infrastructure. They’re indispensable to many DeFi protocols, including our own Tarot Protocol.
What is a price oracle? A price oracle is any contract that provides on-chain access to price information for a token, usually denominated in terms of another token or an off-chain unit of account such as USD. DeFi products such as synthetic assets, decentralized options, collateralized loans, and algorithmic stablecoins all depend on price oracles to function correctly. As an example, virtually every decentralized lending protocol requires timely and accurate on-chain prices in order to determine the value of collateralized loans, as well as to prevent borrowers from withdrawing more than the value of their collateral at any time.
Throughout the history of DeFi, there have been many oracle implementations with varying degrees of decentralization. The Tarot Price Oracle is a fully decentralized design that complements other price oracles, with a robust and minimalist approach for getting on-chain prices from decentralized exchanges (DEXs) on the Fantom Opera network.
The Tarot Price Oracle
On-chain decentralized price oracles have several desirable properties:
On-chain and permissionless, so anyone can access them
Ensured by smart contract to always calculate prices according to a consistent formula
Highly available due to their decentralized nature
Anyone can add arbitrary token pairs from multiple DEXs
Consistent with off-chain market data for highly liquid token pairs
Able to withstand and overcome adverse scenarios
Guaranteed by smart contract to represent reality at the required point in time
The Tarot Price Oracle works with any Uniswap V2-compatible token pair, commonly supported by DEXs. It calculates the time-weighted average price (TWAP) by using the pair’s built-in price accumulators, over a period of at least 1,200 seconds (20 minutes). This is accomplished using the pair’s current price and a previously-observed price (stored within the oracle’s contract) from at least 20 minutes prior.
Anyone can add a new token pair to the Tarot Price Oracle by calling:
Once a token pair has been initialized and at least 20 minutes have elapsed, the TWAP for the pair can be retrieved by calling:
This function returns the TWAP, along with the number of seconds over which it was calculated (which will be at least 20 minutes).
Subsequent calls to
getResult within 20 minutes will return a new TWAP using the pair’s current price and the same price observation as before. After 20 minutes, a new call to
getResult will update the contract’s price observation for the pair, use it for its TWAP calculation, and continue to use the price observation for at least 20 minutes.
Oracle Risks And Security Considerations
There have been several instances of DeFi protocol hacks and exploits where the underlying oracle implementation was the primary vulnerability. A DeFi project may be susceptible to price manipulation if it relies on a vulnerable, decentralized oracle for on-chain prices.
This is why manipulation resistance is a key requirement for the Tarot Price Oracle. In our case, we need to protect against a scenario where a borrower can withdraw more than the value of the collateral deposited, and thus result in a loss of lenders’ funds.
Price oracles prioritize different characteristics according to their use-cases. The Tarot Price Oracle emphasizes robustness and manipulation resistance, opting for a sufficiently long minimum time period to calculate the TWAP (20 minutes) and being limited to one type of data: the prices for highly liquid token pairs on decentralized exchanges.
Pricing LP Tokens
A liquidity pool is a pool of tokens locked in a smart contract and used to facilitate decentralized trading or other DeFi activities. Liquidity pools for decentralized trading became widely popularized by the Uniswap protocol. This basic type of liquidity pool holds two tokens in its reserves and creates a decentralized trading venue for the token pair according to a constant product market maker formula.
When liquidity is added to a pool, the liquidity provider (LP) receives special tokens called LP tokens representing the liquidity supplied to the pool, relative to the total size of the pool’s reserves. The Tarot Protocol uses these LP tokens as collateral to enable decentralized lending. The ultimate goal is to allow liquidity providers to leverage their LP tokens for significantly enhanced yield farming and staking rewards.
In order to determine how much collateral is necessary for borrowing and leverage, the Tarot Protocol uses the Tarot Price Oracle to reliably calculate the value of LP tokens. The pricing of LP tokens will be explored further in a future article.