Skip to main content
The program reads the price of ETH in USDC from Chainlink’s ETH/USD feed, the Uniswap V3 WETH/USDC pools at 0.05% and 0.3%, and the Uniswap V2 WETH/USDC pair, and returns the four prices, the feed’s age and the gaps between them. It reverts when amount is 0, the feed has no price, a V3 pool is missing or has no liquidity, or the pair is missing. Target chains: Ethereum. Run this read-only example with sauce simulate; it does not require a funded wallet or send a transaction.

Prerequisites

You need the sauce command (npm install -g @eco-incorp/sauce). No wallet and no tokens: the simulation signs nothing and the program moves no token. See Installation.

Program

price-survey.sauce.ts

How it works

  • v3Price squares slot0().sqrtPriceX96 with Math.mulDiv(sqrtPriceX96, sqrtPriceX96, 1 << 96), which keeps the full product before dividing. WETH < USDC compares two registry addresses, so it is fixed for the chain and picks which way up the ratio is read.
  • v2 is not the pair’s mid price. It is what the pair pays for amount WETH after its 0.3% fee (amount * 997 against reserve * 1000), per whole WETH, so it includes that trade’s price impact: a larger amount gives a lower v2.
  • The feed’s answer is an Int256, the ABI’s signed int256, and it does not mix with the unsigned prices. The program requires it above 0, then converts it with Uint256(round.answer), a checked conversion that reverts on a negative value, and divides by 100 to rescale it from 8 to 6 decimals. It is still a USD price: oracleGapBps compares it with the pool’s USDC price as if 1 USDC = 1 USD.
  • oracleAge is ctx.timestamp() - round.updatedAt, the seconds since the feed’s last update. spreadBps is the gap between the highest and lowest of the three pool prices, and oracleGapBps the gap between the 0.05% pool and the feed, each in basis points of the lower price.

Run the example

1

Compile

2

Simulate

Any address works as the owner: the simulation deploys its Pot first if it has none and gives it the cook’s value. The result lists what main returned, by name.

Simulation result

Simulated on a local fork of Ethereum at block 26252095 with the current contracts. Nothing was signed or sent; values change with the block.
On the fork the feed’s price was 2,381.351312 USD per ETH, updated 1,306 seconds earlier. The 0.05% pool priced WETH at 2,382.646964 USDC, the 0.3% pool at 2,387.231158, and the V2 pair paid 2,379.962093 for 1 WETH: 30 basis points between the highest and lowest pool, and 5 between the 0.05% pool and the feed. No token moved. The full output lists two native transfers of 0.00003728943124295 ETH: the cook’s value from the owner to the Pot, and the Kitchen’s fee from the Pot to the Kitchen.

Next steps