Skip to content
Audit finding Intermediate

Uniswap Oracle uses wrong prices

The Uniswap oracle contract used a mock with hard-coded prices, so the price could change from the set price and updating via set would be too slow and gas expensive. An attacker could potentially exploit the stale or incorrect price.

Yield Witch v2 contest

Originally disclosed on Code4rena

The Uniswap oracle uses a mock contract with hard-coded prices to retrieve the price, which is not feasible in production. Also, note that even when using the “real deal” @uniswap/v3-periphery/contracts/libraries/OracleLibrary.sol… it does not, in fact, return the prices.

The price could change from the set price. Meanwhile, always updating new prices with set will be too slow and gas expensive.

Recommend using cumulativeTicks = pool.observe([secondsAgo, 0]) // [a_t1, a_t2] and applying equation 5.5 from the Uniswap V3 whitepaper to compute the token0 TWAP. Note that even the official .consult call seems to only return the averaged cumulative ticks; you’d still need to compute the 1.0001^timeWeightedAverageTick in the function.

albertocuestacanada (Yield) acknowledged:

We probably should have not included this contract; it’s too confusing since, at the time, the Uniswap v3 OracleLibrary was still a mock, and this hasn’t gone real testing.

The price source in a production version would be a Uniswap v3 pool, not one of our mock oracle sources. We never expected to call set in production, but to retrieve the prices from a Uniswap v3 pool using the mentioned library (which was not even merged into main at the start of the contest).

We will check with the Uniswap team what is the recommended way of using their oracles. The equation 5.5 in the whitepaper is problematic because an exponentiation of two fractional numbers in Solidity is neither trivial nor cheap. Our understanding is that one of the goals of the OracleLibrary was to provide a consistent implementation to this formula.

From a conversation with @moodysalem, I understand that the code in getQuoteAtTick might achieve the same result as the 5.5 equation, so maybe we need to retrieve the average tick with consult, and then the actual price with getQuoteAtTick.

albertocuestacanada (Yield) commented:

I’m using the acknowledged label for findings that require further investigation to assess.

The original report as published on Code4rena. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Impact

The price could change from the set price, and updating prices via set would be too slow and gas expensive, potentially leading to incorrect valuations.

What the attacker needed

None

The fix

The fix should involve using a real Uniswap v3 pool and computing the time-weighted average price using the recommended library or equation.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This report is about a smart contract that gets prices from Uniswap, a cryptocurrency exchange. The contract was using fake, fixed prices instead of real market prices, which could cause problems if the real price changes.

More from Yield Witch v2 contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.