targetBps, the program trades toward the target. Inside the band, it returns without trading.
The trade applies a price limit based on maxSlippageBps. The cook reverts if the pool is unavailable, the Pot holds neither token, or the average trade price exceeds the configured limit including the pool fee.
Target chains: Base and Ethereum. The same source compiles for both; the recorded simulation uses a Base fork.
This program evaluates conditions only when called. To run it repeatedly, use an external scheduler or caller that submits an authorized transaction.
Prerequisites
Install the Sauce CLI to compile and simulate. To send the transaction, configure a wallet with native coin for gas and the Kitchen fee. Fund the Pot once it is deployed, or use--fund; the program trades the WETH and USDC the Pot holds.
Program
pot is the Pot the program runs on, available without declaring it; pot.USDC and pot.WETH are its balances (Names).
rebalance.sauce.ts
How it works
priceAtandwethSharerun twice: before the trade, to decide whether to trade, and after it, to report the new share from the pool’s new price.sqrtPriceAtruns once, in the branch taken, to turn the floor or the ceiling into the swap’s price limit.- The trade size is computed from the price read before the swap. It ignores the pool fee and the price impact of the swap itself, so it aims at
targetBpswithout guaranteeing it, and the price limit can stop the swap early. In either case the next run starts from the new balances. amountInandamountOutare the changes in the Pot’s balances. Therequirein each branch compares them with the limit price, with the pool fee applied, so a fill worse than the limit plus the fee reverts the cook.- Both tokens stay in the Pot: the swap’s recipient is
pot.
Run the example
1
Compile
2
Simulate
--assume-balance flags give the Pot 1 WETH and 500 USDC for this simulation only, about 82% WETH by value, outside the 45% to 55% band.3
Send
sauce tx rebalance.sauce.ts --chain base --owner <your-address> --args 5000 500 30 prints the unsigned transactions instead, for your own signer or scheduler.Simulation result
Simulated on a local fork of Base at block 53159700 with the current contracts, using the Simulate command above. Nothing was signed or sent.before: 8231), outside the band, so the program sold 0.392574970052759156 WETH for 913.126172 USDC. after is 5000, but wethShare rounds down by integer division, so that figure alone does not show whether the 0.3% price limit stopped the swap before the target was reached.
Next steps
- Dollar-cost averaging: buy WETH one tranche at a time under a price limit.
- Holdings per chain: read what the Pot holds before deciding to trade.
- Distributed swap: split one order across several pools.
sauce simulate,sauce tx, The Pot.
