> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eco.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap and supply as an actions document

> A JSON actions document that swaps WETH for USDC on a Uniswap V3 pool and supplies the USDC to Aave V3, compiled and simulated on Base.

This example describes a swap and lending deposit as a JSON actions document. `sauce actions` converts it to a SauceScript program that swaps WETH for USDC on Uniswap V3, approves the USDC received, and supplies it to Aave V3 for the Pot.

The cook reverts if the pool is missing, the swap delivers less than the document's 2,000 USDC minimum, or the approval or supply fails. Set the minimum for your input amount and market conditions before sending.

**Target chains:** Base. The document names no chain and no address, so it compiles for any chain that registers `WETH`, `USDC`, `UniswapV3.Factory` and `AaveV3.Pool`; it was compiled and simulated on Base.

## Prerequisites

Install the [Sauce CLI](https://docs.eco.com/sauce-sdk/installation.md) to compile and simulate. To send the transaction, configure a wallet with WETH for the program and native coin for gas and the Kitchen fee.

## Document

```json actions-swap-supply.json theme={null}
{
  "version": 1,
  "actions": [
    { "type": "argument", "name": "wethIn" },
    {
      "type": "uniswapV3Swap",
      "tokenIn": "WETH",
      "tokenOut": "USDC",
      "factory": "UniswapV3.Factory",
      "fee": 500,
      "amountOutMin": "2000000000",
      "recipient": "pot",
      "saveOutputAs": "usdc"
    },
    { "type": "approve", "token": "USDC", "spender": "AaveV3.Pool", "amountRef": "usdc" },
    {
      "type": "aaveV3Supply",
      "pool": "AaveV3.Pool",
      "token": "USDC",
      "onBehalfOf": "pot",
      "amountRef": "usdc"
    }
  ]
}
```

`sauce actions actions-swap-supply.json --chain base` prints the program it lowers to. An excerpt, with the inlined approval elided:

```ts theme={null}
// actions-swap-supply.json, lowered for base (chain 8453)
import { uniswapV3 } from "@sauce/router";

function main(a_wethIn: Uint256): void {
  let t_out0 = a_wethIn;
  const pool0 = UniswapV3.Factory.getPool(WETH, USDC, 500);
  require(pool0 != 0, "actions[1]: no pool under that key");
  const amt1 = uniswapV3(pool0, WETH, USDC, t_out0, 0, 2000000000, pot);
  let v_usdc = amt1;
...
  IAaveV3Pool.at(AaveV3.Pool).supply(USDC, v_usdc, pot, 0);
}
```

## How it works

* The `argument` action becomes `main`'s parameter `a_wethIn`, so one compiled document takes any amount through `--args`. `amountOutMin` cannot be an argument: it is a string fixed in the document, so 2,000 USDC is the minimum for every `wethIn`. Set it again from a quote when you change the amount.
* The pool is given by key (`factory` + `fee`). The program reads it from `UniswapV3.Factory.getPool` while it runs and reverts with `actions[1]: no pool under that key` if there is none. The swap is a call to `@sauce/router`'s `uniswapV3`, the same function a SauceScript program imports.
* `saveOutputAs: "usdc"` names the swap's output, and both the approval and the supply spend exactly that amount through `amountRef`. The approval is lowered to an inline checked approve that resets a non-zero allowance first.
* The document cannot do what [the SauceScript version](https://docs.eco.com/quickstarts/sauce/quote-swap-supply.md) does: quote two pools and choose one at run time. The actions format has no quote, comparison or condition action; each swap names its pool when the document is written.

## Run the example

**1. Lower**

```bash theme={null}
sauce actions actions-swap-supply.json --chain base
```

Prints the generated SauceScript, `main`'s parameters and the bytecode (898 bytes on Base).

**2. Compile**

```bash theme={null}
sauce compile actions-swap-supply.json --chain base
```

**3. Simulate**

```bash theme={null}
sauce simulate actions-swap-supply.json --chain base --owner <your-address> \
  --assume-balance WETH:1 --args 1000000000000000000
```

`--assume-balance WETH:1` gives the Pot 1 WETH for this simulation only, and `--args` gives the `argument` actions in the order the document declares them. If your Pot is not deployed yet, the simulation deploys it first.

**4. Send**

```bash theme={null}
sauce run actions-swap-supply.json --chain base --wallet <name> \
  --fund WETH:1 --args 1000000000000000000
```

`--fund WETH:1` pulls 1 WETH from your wallet into the Pot in the same cook. To sign elsewhere, build the unsigned transactions instead:

```bash theme={null}
sauce tx actions-swap-supply.json --chain base --owner <your-address> \
  --fund WETH:1 --args 1000000000000000000
```

## Simulation result

Simulated on a local fork of Base at block 52965104 with the current contracts. Nothing was signed or sent.

```text theme={null}
The cook succeeds from 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266: gas used 367218 for the cook, after the Pot's deploy (1. below), returns 0x
Left in the Pot, not in the owner's wallet:
  2316.836894 aBasUSDC
...
What moved, in order:
...
  [3] 0xd0b5…F224        -> Pot                   2316.836895 USDC
  [4] Pot                -> 0xd0b5…F224           1 WETH
  [5] Pot                -> aBasUSDC 0x4e65…c0AB  2316.836895 USDC
  [6] mint               -> Pot                   2316.836894 aBasUSDC
```

The 0.05% pool (`0xd0b5…F224`) took 1 WETH and paid 2,380.895733 USDC, above the 2,000 USDC minimum, and the Pot supplied all of it to Aave. With `onBehalfOf: "pot"`, the Pot holds the 2,380.895732 aBasUSDC; a later cook on the same Pot can withdraw it. `main` returns nothing, so the cook returns `0x`.

## Next steps

* [Quote two pools, swap on the better one, supply to Aave](https://docs.eco.com/quickstarts/sauce/quote-swap-supply.md): the same flow in SauceScript, choosing the pool at run time.
* [Low-level calls](https://docs.eco.com/quickstarts/sauce/low-level-call.md): raw calldata and a program's own ABI, for what neither the actions format nor the registry covers.
* [`sauce actions`](https://docs.eco.com/sauce-sdk/cli/actions), [`sauce simulate`](https://docs.eco.com/sauce-sdk/cli/simulate), [Protocols reference](https://docs.eco.com/sauce-sdk/reference/protocols.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.