Skip to main content
Quote two Uniswap V3 pools onchain and swap through whichever gives the better output, via SwapRouter02, all decided inside one transaction. The router and quoter are fixed (Base mainnet); the tokens, amount, caller, two pools and minimum output are main parameters supplied at run time. The program imports its contract ABIs as JSON artifacts placed beside the source.

Prerequisites

  • Complete Sauce setup, including access, a Pot and the current Kitchen fee.
  • Use Base mainnet (chain ID 8453) and two Uniswap V3 pools for the same token pair.
  • The caller must hold the input tokens and approve the Pot to spend amountIn.
  • Save these four ABI files beside the program: IERC20, IV3SwapRouter, IUniswapV3Pool, and IQuoterV2.
  • Supply a minimum acceptable output in the output token’s smallest unit. Review the compiler compatibility limitation before execution.

Program

best-pool-swap.sauce.ts

What to notice

  • The two quotes and the swap happen in the same transaction, so the decision is made against the state the swap executes in.
  • Under a Pot delegatecall, ctx.self() is the Pot, so the input tokens are pulled to the Pot and the router is approved from there.
  • The struct arguments use positional keys (a to g). The SauceScript reference explains the naming rules for struct fields.

Run it

Compile with @eco-incorp/sauce-compiler (target: "evm") with the four ABI JSON files next to the source, append the seven main arguments as 32-byte words, and execute through your Pot with the Kitchen fee. See Getting started with Sauce.

Troubleshooting

If execution reverts, check the input balance, allowance to the Pot, pool pair, fee tiers and minimum output. Simulate the complete call and estimate gas before submission. A quote or swap failure reverts the transaction.

Next steps

Review SauceScript contract calls or package the program as an intent.