# Deposit any token into a vault

> Users deposit whatever they hold. It arrives as vault shares in one transaction.

- Status: live. Compiles on Composer today, and Jumper runs it in production.
- Who it's for: Wallets, neobanks and exchanges with an Earn tab
- Page: https://composer-experiences.li.quest/templates/vault-deposit
- Working example, run with your own key and wallet: https://composer-experiences.li.quest/templates/vault-deposit/live

## What you get

- Any token in, vault shares out
- Simulated before signing, with a minimum on the shares
- Runs on Jumper today

## What runs, in order

A step without ops is work outside the flow, such as the user paying in. `×n` marks an op the flow repeats.

1. User pays (Base): Any token
2. Swap to the vault token (Base). Ops: `lifi.swap`
3. Deposit (Base): Morpho, Aave, Euler and more. Ops: `lifi.zap`

## What you control

| Parameter | Set by |
| --- | --- |
| From token | Passed by your user |
| Vault | Fixed by you, or user picks |
| Slippage | 0.5% |

## Build it

1. Get an API key at https://portal.li.fi. Keep it on your server.
2. Install these exact versions; npm's `latest` tag lags production:

```sh
npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
```

3. Add the module below as `vault-deposit.ts`. It names the flow `tpl:vault-deposit@1`, so LI.FI can see which template it came from.
4. Compile with `simulationPolicy: 'allow-revert'`. Before the user signs, show `outputs[<key>].amount.minimum.value`: the least they receive. It is optional; when it is absent, the flow has no floor.

### The module: `vault-deposit.ts`

```ts
/**
 * Deposit any token into a vault in one transaction: the user's token is
 * swapped to the vault's underlying token, which is then zapped into the vault,
 * and the vault shares go to the user. A user who already pays with the
 * underlying token skips the swap. Call it with
 * `const { flow, run } = buildFlow(sdk, inputs); const result = await flow.compile(run);`.
 */
import {
  type Address,
  type ComposeSdk,
  guards,
  materialisers,
  resources,
} from '@lifi/composer-sdk';

/** Every address is checksummed (viem's `getAddress`). */
export interface VaultDepositInputs {
  chainId: number;
  /** The token the user pays with. */
  tokenIn: Address;
  /** In `tokenIn`'s smallest unit. */
  amountIn: bigint;
  /** The vault's share token, which the user receives. */
  vault: Address;
  /** The token the vault accepts, e.g. USDC. */
  underlying: Address;
  user: Address;
}

export const buildFlow = (
  sdk: ComposeSdk,
  inputs: VaultDepositInputs,
  name = 'tpl:vault-deposit@1'
) => {
  const { chainId } = inputs;
  const flow = sdk.flow(chainId, {
    name,
    inputs: { amountIn: resources.erc20(inputs.tokenIn, chainId) },
  });

  // Swap whatever the user pays with into the vault's underlying token. A swap
  // from a token to itself is rejected, so the vault's own token skips it.
  const paysUnderlying = inputs.tokenIn === inputs.underlying;
  const underlyingAmount = paysUnderlying
    ? flow.inputs.amountIn
    : flow.lifi.swap('swap', {
        bind: { amountIn: flow.inputs.amountIn },
        config: {
          resourceOut: resources.erc20(inputs.underlying, chainId),
          slippage: 0.005,
        },
      }).amountOut;

  // Deposit the underlying amount into the vault; the guard floors the shares.
  flow.lifi.zap('deposit', {
    bind: { amountIn: underlyingAmount },
    config: { resourceOut: resources.erc20(inputs.vault, chainId) },
    guards: [guards.slippage({ port: 'amountOut', bps: 50 })],
  });

  const run = {
    signer: inputs.user,
    inputs: {
      amountIn: materialisers.directDeposit({ amount: inputs.amountIn }),
    },
    sweepTo: inputs.user,
  };
  return { flow, run };
};
```

### Call it

```ts
import { createComposeSdk } from '@lifi/composer-sdk';

import { buildFlow } from './vault-deposit';

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

// inputs: the values your user picks; see the module's inputs interface.
const { flow, run } = buildFlow(sdk, inputs);
const result = await flow.compile({ ...run, simulationPolicy: 'allow-revert' });
// Show the outputs' amount.minimum to the user, then have them sign
// result.transaction.
```

### The same call over REST

Compile and simulate in one call, for sample values:

```http
POST https://composer.li.quest/compose
content-type: application/json
x-lifi-api-key: <your key>

{
  "flow": {
    "version": 1,
    "id": "tpl:vault-deposit@1",
    "chainId": 8453,
    "inputs": [
      {
        "name": "amountIn",
        "resource": {
          "kind": "erc20",
          "token": "0x4200000000000000000000000000000000000006",
          "chainId": 8453
        }
      }
    ],
    "nodes": [
      {
        "id": "swap",
        "op": "lifi.swap",
        "bind": {
          "amountIn": {
            "$ref": "input.amountIn"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
            "chainId": 8453
          },
          "slippage": 0.005
        }
      },
      {
        "id": "deposit",
        "op": "lifi.zap",
        "bind": {
          "amountIn": {
            "$ref": "swap.amountOut"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0xbeeF010f9cb27031ad51e3333f9aF9C6B1228183",
            "chainId": 8453
          }
        },
        "guards": [
          {
            "kind": "slippage",
            "port": "amountOut",
            "bps": 50
          }
        ]
      }
    ]
  },
  "run": {
    "inputs": {
      "amountIn": {
        "kind": "directDeposit",
        "amount": "100000000000000000"
      }
    },
    "signer": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    "sweepTo": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
  }
}
```

### Prompt for a coding agent

```text
Goal: add "Deposit any token into a vault" to my app with LI.FI Composer.
1. Install exactly: npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
2. Read https://composer.li.quest/llms.txt. The manifest at https://composer.li.quest/compose/manifest is the source of truth for operations.
3. The flow: User pays → Swap to the vault token (lifi.swap) → Deposit (lifi.zap).
   What can change: From token, Vault, Slippage.
4. Name the flow "tpl:vault-deposit@1" so LI.FI can see which template it came from.
5. Compile with POST /compose and simulationPolicy: 'allow-revert'.
   Before the user signs, show outputs[<key>].amount.minimum.value: the least they receive.
   It is optional; when it is absent, the flow has no floor.
6. The API key comes from https://portal.li.fi. Keep it on the server.
7. If you get stuck, POST https://composer.li.quest/feedback with what you tried.
```

## Composer references

- [Composer API llms.txt](https://composer.li.quest/llms.txt)
- [Operation manifest](https://composer.li.quest/compose/manifest): the source of truth for operations
- [Docs](https://docs.li.fi/composer/overview.md)
- Stuck? `POST https://composer.li.quest/feedback` with what you tried; it needs no key.
