Price Feed Security
You are referring to the Legacy
version of KyberSwap docs.
For the most updated information, please refer to:
Classic
Using Kyber as an on-chain price feed
While using Kyber as an on-chain feed for token prices is viable, note that it is susceptible to price manipulation by malicious parties.
Generally, any price feed should
be averaged over time
have checks to verify that the buy / sell spread is small, and that there is no arbitrage at the time of query
We recommend that adequate measures are in place to verify the rate obtained on Kyber. We outline some of these methods below.
Check for arbitrage and the buy / sell spread
Query for both buy and sell rates.
Check that the spread between the buy / sell rate is within an acceptable range.
Should there be arbitrage, there is a small possibility that a party is manipulating the price feed. We recommend that the transaction be reverted in these cases.
Algorithm
Our suggested algorithm is as follows:
Get expected rate of 1 ETH equivalent worth of source tokens to the destination token
Use the expected rate of (1) to calculate the expected destinations token receivable
Get expected rate of the no. of destination tokens obtained in (2) to the source token
Use the expected rate of (3) to calculate the number of source tokens receivable
If the resulting source token amount is greater than the initial source amount, arbitrage opportunity exists.
Otherwise, the smaller the resulting source token amount, the higher the spread.
Note: The calcDstQty
function used below
Example
srcToken
: USDC (6 decimals)destToken
: WBTC (8 decimals)querySrcAmount
=180 * 10 ** 6
(180 USDC in its token decimals)
A
buyRate
of98426111111111
is obtainedExpected dest amount is
1771669
, or roughly0.0177
WBTC tokens.A
sellRate
of10151860703099732512111
is obtainedExpected source amount is
179857368
, or roughly179.86
USDC tokens.
This means that if we swap 180 USDC to WBTC and back, we will obtain roughly 179.86 USDC in return. The resultingSrcAmount > querySrcAmount
condition thus checks for arbitrage.
We now check the spread between the buy and sell rate.
This means that there is a 0.07% spread. For token to token swaps, we expect spreads to be higher, as ETH is used as the quote currency.
Use multiple price feeds
Consider using other price feeds in tandem with Kyber, if such on-chain sources are available. For example, use Maker's medianizer to obtain ETH/USD rates.
Use actual source quantity
The token conversion rate returned by Kyber varies with different source token quantities. If applicable, use the actual source quantity when calling the getExpectedRate
function.
Last updated