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

# Swap and deposit

> Swap WETH to USDC, then deposit the USDC into an Aave lending position in a single flow. You pick each step; for a route the server plans, see Deposit with a server-planned route.

## Scenario

A user swaps 1 WETH to USDC and deposits the USDC into Aave as `aEthUSDC`, in one transaction on Ethereum. The [Flow](https://docs.li.fi/composer/composer-api/guides/build-a-flow.md) binds the [`lifi.swap`](https://docs.li.fi/composer/composer-api/ops/lifi-swap) output handle straight into the [`lifi.zap`](https://docs.li.fi/composer/composer-api/ops/lifi-zap) input, and a [`slippage` guard](https://docs.li.fi/composer/composer-api/guards/slippage) floors the deposit.

## Full example

Install the SDK with `npm install @lifi/composer-sdk @lifi/compose-spec` and set `LIFI_API_KEY`; see [Before you start](https://docs.li.fi/composer/composer-api/overview.md#before-you-start). Adapted from [`swapAndZap.ts`](https://github.com/lifinance/composer-sdk-examples/blob/main/examples/swapAndZap.ts).

```ts theme={"system"}
import {
  createComposeSdk,
  guards,
  materialisers,
  resources,
} from '@lifi/composer-sdk';

const WETH = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2';
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
// Aave aEthUSDC receipt token on Ethereum mainnet
const A_ETH_USDC = '0x98C23E9d8f34FEFb1B7BD6a91B7FF122F4e16F5c';

const sdk = createComposeSdk({
  baseUrl: 'https://composer.li.quest',
  apiKey: process.env.LIFI_API_KEY!,
});

const builder = sdk.flow(1, {
  name: 'swap-and-zap-weth-to-aave',
  inputs: {
    amountIn: resources.erc20(WETH, 1),
  },
});

const swapOutputs = builder.lifi.swap('swap', {
  bind: { amountIn: builder.inputs.amountIn },
  config: {
    resourceOut: resources.erc20(USDC, 1),
    slippage: 0.03,
  },
});

builder.lifi.zap('zap', {
  bind: { amountIn: swapOutputs.amountOut },
  config: {
    resourceOut: resources.erc20(A_ETH_USDC, 1),
  },
  guards: [guards.slippage({ port: 'amountOut', bps: 100 })],
});

const result = await builder.compile({
  signer: '0xYourSignerAddress',
  inputs: {
    amountIn: materialisers.directDeposit({
      amount: '1000000000000000000', // 1 WETH (18 decimals)
    }),
  },
  sweepTo: builder.context.sender,
});

console.log('aEthUSDC:', result.outputs['zap.amountOut']?.amount);
console.log('Swap USDC:', result.outputs['swap.amountOut']?.amount);
```

## Run it

Save the file as `swap-and-deposit.mts` and run `node swap-and-deposit.mts`. The script compiles the flow; it sends nothing.

## What to check

* `result.outputs['zap.amountOut']` is the `aEthUSDC` the signer receives (`role: 'terminal'`, swept to the sender). `amount.estimate` is the simulated amount; `amount.minimum` is the 1% guard floor (`basis: 'guard'`).
* `result.outputs['swap.amountOut']` has `role: 'consumed'` and `consumedBy: 'zap'`. Its `amount.minimum` is the swap's own 3% floor (`basis: 'op'`), so the swap needs no guard.
* `result.actions` holds the WETH approval, then `execute`.

## Related

* [Deposit with a server-planned route](https://docs.li.fi/composer/composer-api/recipes/server-planned-route.md): let the server pick the steps.
* [Split a deposit across vaults](https://docs.li.fi/composer/composer-api/recipes/split-deposit.md): one input, two vaults.
* [Bridge and deposit](https://docs.li.fi/composer/composer-api/recipes/bridge-and-deposit.md): the same deposit on another chain.


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