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 thesauce 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
v3Pricesquaresslot0().sqrtPriceX96withMath.mulDiv(sqrtPriceX96, sqrtPriceX96, 1 << 96), which keeps the full product before dividing.WETH < USDCcompares two registry addresses, so it is fixed for the chain and picks which way up the ratio is read.v2is not the pair’s mid price. It is what the pair pays foramountWETH after its 0.3% fee (amount * 997againstreserve * 1000), per whole WETH, so it includes that trade’s price impact: a largeramountgives a lowerv2.- The feed’s
answeris anInt256, the ABI’s signedint256, and it does not mix with the unsigned prices. The program requires it above 0, then converts it withUint256(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:oracleGapBpscompares it with the pool’s USDC price as if 1 USDC = 1 USD. oracleAgeisctx.timestamp() - round.updatedAt, the seconds since the feed’s last update.spreadBpsis the gap between the highest and lowest of the three pool prices, andoracleGapBpsthe gap between the 0.05% pool and the feed, each in basis points of the lower price.
Run the example
1
Compile
2
Simulate
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.Next steps
- Oracle-guarded swap: the same feed and 0.05% pool reads, used to decide whether to sell WETH.
- Quote two pools, swap, supply: quotes a trade with Uniswap’s QuoterV2 instead of reading pool prices.
sauce simulate,sauce pools, Built-ins, Names
