Split one order across N pools. Run the legs in parallel. Recombine against a floor, or revert the whole script.
1import { IERC20 } from "@openzeppelin/contracts/build/contracts/IERC20.json";2import { IV3SwapRouter } from "@uniswap/swap-router-contracts/artifacts/contracts/interfaces/IV3SwapRouter.sol/IV3SwapRouter.json";3import { IUniswapV3Pool } from "@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json";45// Split one order across two Uniswap V3 pools, weighting each leg by the6// pool's live liquidity, swap both legs via SwapRouter02, then require the7// combined output clears the floor — all inside one transaction. Router is8// fixed (Base mainnet); the tokens, amount, caller, pools and floor are9// parameters.1011function main(12 tokenIn: Address, tokenOut: Address, amountIn: Uint256, caller: Address,13 pool1Addr: Address, pool2Addr: Address, minOut: Uint256,14): Uint256 {15 const router: Address = 0x2626664c2603336E57B271c5C0b26F421741e481;1617 // Pull the input tokens in and let the router spend them.18 const token = IERC20.at(tokenIn);19 token.transferFrom(caller, address.self, amountIn);20 token.approve(router, amountIn);2122 // Weight the split by each pool's live liquidity at inclusion.23 const liqA: Uint256 = IUniswapV3Pool.at(pool1Addr).liquidity();24 const liqB: Uint256 = IUniswapV3Pool.at(pool2Addr).liquidity();25 const liqTotal: Uint256 = liqA + liqB;26 if (liqTotal === 0) throw "no liquidity";2728 const legA: Uint256 = (amountIn * liqA) / liqTotal;29 const legB: Uint256 = amountIn - legA;3031 const feeA: Uint256 = IUniswapV3Pool.at(pool1Addr).fee();32 const feeB: Uint256 = IUniswapV3Pool.at(pool2Addr).fee();3334 // Swap each leg. Keys a-g sort alphabetically onto ExactInputSingleParams:35 // a=tokenIn, b=tokenOut, c=fee, d=recipient, e=amountIn, f=amountOutMinimum, g=sqrtPriceLimitX9636 const r = IV3SwapRouter.at(router);37 const outA: Uint256 = r.exactInputSingle({ a: tokenIn, b: tokenOut, c: feeA, d: caller, e: legA, f: 0, g: 0 });38 const outB: Uint256 = r.exactInputSingle({ a: tokenIn, b: tokenOut, c: feeB, d: caller, e: legB, f: 0, g: 0 });3940 // Recombine and enforce the floor across the whole order.41 const outTotal: Uint256 = outA + outB;42 if (outTotal < minOut) throw "min out not met";43 return outTotal;44}Order split takes a large order, splits it across N pools by depth, and runs the legs in parallel. The outputs recombine into a single number that has to clear a floor; if not, the whole script reverts. No partial fills, no orphaned approvals.
const outA = quoter.quoteExactInputSingle({...});const outB = quoter.quoteExactInputSingle({...});// Branch at inclusion on live quotesconst pool = outA > outB ? poolA : poolB;return router.exactInputSingle({...});Quote every venue inside the transaction and route through the winner. Decision is made at inclusion against the live pool state.
@contractexport class ERC20 { balanceOf = new Map<address, uint256>(); transfer(to: address, amt: uint256) { this.balanceOf.sub(msg.sender, amt);Same ERC-20 spec in 11 lines of TypeScript vs 28 lines of Solidity. Identical selectors, identical gas, identical audit surface.
token.transferFrom(caller, THIS_ADDRESS(), amountIn);// adaptive swap across discovered pools…const swapDelta = postBal[0] - preBal[0];outToken.approve(aavePool, swapDelta);IAavePool.at(aavePool).supply(tokenOut, swapDelta, beneficiary, 0);PLACEHOLDER — Swap USDC into WETH across the best Base pools and supply the WETH to Aave V3 for a beneficiary, in one atomic Sauce program.