Using Eco Sauce SDK with Coding Agents: A Swap-and-Supply Tutorial
Sauce lets an agent express a transaction as a program: execute contract calls, inspect intermediate results, and enforce a final outcome. This tutorial uses a 10 USDC swap on Base followed by an Aave V3 supply to demonstrate the workflow.
The agent discovers pools, compares routes, and compiles SauceScript. The Sauce runtime executes the compiled program atomically. A failed swap, insufficient output, or failed Aave supply reverts the complete swap-and-supply transaction.
The examples follow the development compiler used in the recorded demo: sauce-compiler, resolved from the Sauce repository’s dev-tools package, with viem for contract calls. The runtime entry point is cook(bytes[] ingredients). Sauce SDK is able to run forks for chains for demonstration purposes.
Sauce’s built-in simulation lets an agent test a transaction against the chain state at a specific historical block and inspect what it would have done. For this swap-and-supply workflow, that means checking the swap output, Aave supply, and transaction constraints against a reproducible snapshot before submitting a live transaction. The result describes execution against that snapshot; live execution still depends on the state when the transaction is included.
The workspace requires Node.js, Foundry, the Sauce checkout with its development dependencies and built runtime artifacts, the demo’s ABI files, and the eco-sauce skill available to the agent. The TypeScript runners use tsx: this development compiler resolves to TypeScript inside node_modules and does not run directly under plain Node.
Start with a bounded agent request. A useful prompt specifies the input, routing objective, destination action, constraints, and evidence:
Use the eco-sauce skill to build a Base-fork demo.
Swap exactly 10 native USDC for WETH.
Compare supported direct pools and split allocations.
Rank candidate routes by output after estimated fees.
Supply all purchased WETH to Aave V3 for the caller.
Enforce 0.5% slippage and a five-minute deadline.
Return the SauceScript, route allocation, receipt,
and decoded execution trace. Use local test funds only.The skill supplies workflow context; the installed compiler, contract ABIs, and runtime source determine the executable API. In this demo, the agent inspected those interfaces before generating code.
Separate route planning from execution. The TypeScript runner discovers verified USDC/WETH pools and collects quotes against one state snapshot. Exact concentrated-liquidity quotes execute the pool’s swap logic in simulation and revert before payment, capturing tick crossings.
The search covered 23 direct pools across Uniswap V3, PancakeSwap V3, Aerodrome Slipstream, and Uniswap V2. Dynamic programming compared allocations in 0.01 USDC increments. A subsequent fee comparison ranked all single-pool alternatives and the highest-output split.
| Candidate | Estimated gas units | Estimated net output, WETH equivalent |
|---|---|---|
| Uniswap V3 0.01%, 100% allocation | 896,896 | 0.004187898476 |
| Five-pool split | 2,045,216 | 0.004181380427 |
The split produced more WETH before fees. The Uniswap route retained more value after estimated costs at a sampled Base gas price of 0.006 gwei. The comparison included estimated L1 data fees and excluded deployment and separate approval costs. “Best” therefore means best among the evaluated alternatives under those assumptions.
Express the outcome in SauceScript. The demo program accepts canonical token and Aave addresses, the recipient, input amount, aggregate minimum output, deadline, and route legs. Each leg contains:
[poolType, poolAddress, amountIn, sqrtPriceLimitX96]For the selected V3 route, poolType is 1 and the exact-input amount is positive. The program checks that the caller owns the proceeds and that all allocations sum to the input. It snapshots existing router balances, pulls the USDC, and executes each leg through the router.
After the swaps, the program calculates purchased WETH as the increase over the starting balance. It checks the aggregate minimum, approves only that amount for Aave, supplies it for the caller, and clears the allowance. Final assertions verify preserved router balances and the expected aWETH gain. This passes the actual swap result into the next action without relying on the earlier quote as the deposit amount.
Compile the verified plan. The following excerpt uses the compiler API from the working demo. SAUCE identifies the local Sauce checkout; HERE contains the program and its ABI files. The addresses, quote, selected pool, and price limit come from the verified route plan.
import { createRequire } from "node:module";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { parseAbi, toHex } from "viem";
const { compile } = createRequire(
join(SAUCE, "dev-tools/package.json")
)("sauce-compiler");
const amountIn = 10_000_000n; // USDC has 6 decimals
const minimum = quotedWeth * 9950n / 10000n;
const deadline = quoteBlock.timestamp + 300n;
const legs = [[
1n, BigInt(pool), amountIn, sqrtPriceLimitX96
]];
const compiled = compile(
readFileSync(
join(HERE, "optimized-swap-supply.sauce.js"),
"utf8"
),
{
baseDir: HERE,
target: "v1",
args: [
USDC, WETH, AAVE, AWETH, account.address,
amountIn, minimum, deadline, legs
]
}
);
const ingredients = compiled.bytecodes.map(
(bytes: Uint8Array) => toHex(bytes)
);The compiler returns multiple bytecode segments, including the invocation of main with its arguments. Every segment is passed to cook. The v1 target matches the Solidity runtime used in this demo; compiler target and deployed runtime must agree.
Simulate before local execution. The runner verifies the loopback endpoint, development chain ID 31337, and Base fork source before using the public Anvil test account. It deploys the runtime locally and seeds test balances. An exact 10 USDC approval precedes cook as a separate transaction.
With the clients, approval, and runtime prepared, the execution sequence is below. The wallet client is configured with the local test account and development chain; client setup is omitted from this excerpt.
const cookAbi = parseAbi([
"function cook(bytes[] ingredients) payable returns (bytes)"
]);
await client.simulateContract({
account: account.address,
address: sauce,
abi: cookAbi,
functionName: "cook",
args: [ingredients]
});
const gas = await client.estimateContractGas({
account: account.address,
address: sauce,
abi: cookAbi,
functionName: "cook",
args: [ingredients]
});
const hash = await wallet.writeContract({
address: sauce,
abi: cookAbi,
functionName: "cook",
args: [ingredients],
gas: gas * 120n / 100n
});
const receipt =
await client.waitForTransactionReceipt({ hash });The gas limit includes a 20% buffer. Slippage protection remains a separate on-chain constraint: output must meet the minimum, the deadline must remain valid, and each leg must spend its exact allocation.
Verify the complete outcome. A successful receipt establishes transaction success; transfer events, the Aave Supply event, and balance checks establish what happened. The local trace recorded:
cook → pull 10 USDC → Uniswap V3 swap
→ receive 0.004193283723433545 WETH
→ supply that WETH to Aave
→ receive 0.004193283723433544 aWETH
→ clear allowance → verify final balancesThe one-wei difference was consistent with Aave rounding. The transaction consumed 866,596 gas units. Negative tests confirmed rollback for insufficient output, expiry, an incorrect beneficiary, an allocation mismatch, and failed Aave supply.
The agent’s deliverable is a reproducible program and its evidence: discovered pools, quote snapshot, selected allocations, compiled constraints, receipt, decoded trace, and reconciled balances. The demo source bundle contains the SauceScript, ABI files, TypeScript runners, recorded results, and reproduction instructions. The source implementation is optimized-swap-supply.sauce.js; the runners are compare-execute-fork.ts, compare-gas.ts, and execute-net-fork.ts.
This demo establishes an agent-to-compiler-to-runtime workflow for composing dependent actions. Its routing search excludes multihop routes, Uniswap V4, RFQs, and exhaustive optimization of every split after fees. The local runtime addresses and calldata belong to the fork; production execution requires a verified deployment and freshly validated transaction parameters.
Reference implementation: Eco Sauce repository. Pool and lending addresses were checked against Uniswap’s Base deployments and the Aave V3 Base address book.
About Eco
Eco connects digital asset markets through stablecoins. Developers and enterprises use a single integration to program money movement across blockchains, tokenization platforms, and institutional liquidity venues, with real-time routing and pricing intelligence. Whether for trading, treasury, or payment flows, Eco delivers superior price control and all-or-nothing execution against live market conditions and customer-defined requirements.