amount WETH in that pool with a price limit derived from the oracle. The USDC and any WETH the limit leaves unsold go to recipient. The cook reverts when the oracle’s price is older than maxAge, the pool’s price is more than maxDeviationBps from it, the pool is already past the limit, or the recipient receives less than minOut USDC.
Target chains: Ethereum. The recorded simulation uses an Ethereum fork.
Prerequisites
Install the Sauce CLI to compile and simulate. To send the transaction, configure a wallet with funds for gas and the Kitchen fee, and provide WETH in your Pot once it is deployed, or through--fund.
Program
pot is the Pot the program runs on, available without declaring it (Names).
oracle-guarded-swap.sauce.ts
How it works
- Two checks run before any swap: the feed’s
updatedAtmust be withinmaxAgeseconds of the block’s timestamp, and the pool’s price withinmaxDeviationBpsof the oracle’s. The second is writtengap * 10000 <= oracle * maxDeviationBps, so no division rounds the bound. The feed’sansweris anInt256; the program requires it above 0 and converts it withUint256(round.answer), a checked conversion, so the unsigned arithmetic after it never sees a negative price. - The price limit comes from the oracle, not the pool.
flooris the oracle’s price lessmaxDeviationBps; as a sqrt price it is moved by √(1 − fee), with the fee read from the pool in the cook, so the last unit the pool fills fetchesfloorafter the 0.05% fee. USDC is token0 on Ethereum, so selling WETH raises the pool’s sqrt price and the limit is a ceiling. - A limit on the wrong side of the pool’s current price makes a Uniswap V3 pool revert with
SPL; the program checks that first and reverts withthe pool is past the limit. receivedis the recipient’s USDC balance change andsoldthe Pot’s WETH balance change. The average is held to the floor (sold below the floor), and onlyamount - soldWETH goes back to the recipient; other WETH in the Pot stays.
Run the example
1
Compile
2
Simulate
--assume-balance WETH:1 gives the Pot 1 WETH for this simulation only. The result lists the oracle’s price, the pool’s price, the WETH sold and the USDC received.3
Send
sauce tx prints the unsigned transaction instead, for your own signer, with the same funding and arguments:Simulation result
Simulated on a local fork of Ethereum at block 26278321 with the current contracts. Nothing was signed or sent; values change with the block.maxDeviationBps at 40:
maxDeviationBps at 0 the pool’s price must equal the oracle’s, and the cook reverts at that check:
Next steps
- Price survey: the same reads with no swap, run with
sauce simulate. - Multi-venue route: two legs on paid-first venues, each with its own minimum.
sauce simulate,sauce price, Built-ins, Names
