Page cover

Price oracle

2.3 Price oracle

The marginal price offered by DEX Raiden (not including fees) at time can be computed by dividing the reserves of asset a by the reserves of asset.

Since arbitrageurs will trade with DEX Raiden if this price is incorrect (by a sufficient amount to make up for the fee), the price offered by DEX Raiden tends to track the relative market price of the assets. This means it can be used as an approximate price oracle. However, DEX Raiden v1 is not safe to use as an on-chain price oracle, because it is very easy to manipulate. Suppose some other contract uses the current ETH-DAI or BSC-DAI price to settle a derivative. An attacker who wishes to manipulate the measured price can buy ETH or BSC from the ETH-DAI or BSC-DAI pair, trigger settlement on the derivative contract (causing it to settle based on the inflated price), and then sell ETH or BSC back to the pair to trade it back to the true price. This might even be done as an atomic transaction, or by a miner who controls the ordering of transactions within a block.

DEX Raiden v1.1 improves this oracle functionality by measuring and recording the price before the first trade of each block (or equivalently, after the last trade of the previous block).

This price is more difficult to manipulate than prices during a block. If the attacker submits a transaction that attempts to manipulate the price at the end of a block, some other arbitrageur may be able to submit another transaction to trade back immediately afterward in the same block. A miner (or an attacker who uses enough gas to fill an entire block) could manipulate the price at the end of a block, but unless they mine the next block as well, they may not have a particular advantage in arbitraging the trade back. Specifically, DEX Raiden v1.1 accumulates this price, by keeping track of the cumulative sum of prices at the beginning of each block in which someone interacts with the contract. Each price is weighted by the amount of time that has passed since the last block in which it was updated, according to the block timestamp.

This means that the accumulator value at any given time (after being updated) should be the sum of the spot price at each second in the history of the contract.

Last updated