# Deposit into a vault on another chain

> Users deposit from the chain they are on into a vault on another chain, in one transaction.

- Status: live. Compiles on Composer today.
- Who it's for: Apps whose best vaults sit on a different chain
- Page: https://composer-experiences.li.quest/templates/cross-chain-vault
- Working example, run with your own key and wallet: https://composer-experiences.li.quest/templates/cross-chain-vault/live

## What you get

- One transaction across chains
- A refund on the destination chain if the deposit cannot run

## 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 (Arbitrum): Any token
2. Swap to USDC (Arbitrum). Ops: `lifi.swap`
3. Bridge (Arbitrum to Base). Ops: `lifi.bridge`
4. Lands in escrow (Base). Ops: `continuation.settle`
5. Deposit (Base): Aave USDC. Ops: `lifi.zap`

## What you control

| Parameter | Set by |
| --- | --- |
| Source chain and token | Passed by your user |
| Vault | Fixed by you |
| Refund | USDC on Base |

## 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 `cross-chain-vault.ts`. It names the flow `tpl:cross-chain-vault@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: `cross-chain-vault.ts`

```ts
/**
 * Deposit into a vault on another chain with one signature: swap what the user
 * pays with to USDC, bridge it into an escrow on the vault's chain, and let a
 * solver deposit it there. If the deposit never runs, the escrow refunds the
 * user in the bridged USDC.
 *
 *   const { flow, run } = buildFlow(sdk, inputs);
 *   const result = await flow.compile(run);
 */
import {
  type Address,
  type ComposeRunInput,
  type ComposeSdk,
  guards,
  materialisers,
  resources,
} from '@lifi/composer-sdk';

export interface CrossChainVaultInputs {
  sourceChainId: number;
  /** What the user pays with on the source chain; the zero address is the native coin. */
  payToken: Address;
  /** In `payToken`'s smallest unit. */
  amount: bigint;
  sourceUsdc: Address;
  vaultChainId: number;
  vaultUsdc: Address;
  /** The vault's share token, such as Aave's aToken. */
  vault: Address;
  /** Signs on the source chain, receives the shares, and gets any refund. */
  user: Address;
  slippageBps: number;
}

const NATIVE = '0x0000000000000000000000000000000000000000';

export const buildFlow = (
  sdk: ComposeSdk,
  inputs: CrossChainVaultInputs,
  name = 'tpl:cross-chain-vault@1'
) => {
  const { sourceChainId, sourceUsdc, vaultChainId, vaultUsdc, vault, user } =
    inputs;
  const pay =
    inputs.payToken === NATIVE
      ? resources.native(sourceChainId)
      : resources.erc20(inputs.payToken, sourceChainId);

  // The leg a solver runs from the escrow; its guard is the enforced floor.
  const leg = sdk.flow(vaultChainId, {
    name: `${name}:deposit`,
    inputs: { funding: resources.erc20(vaultUsdc, vaultChainId) },
  });
  leg.lifi.zap('deposit', {
    bind: { amountIn: leg.inputs.funding },
    config: { resourceOut: resources.erc20(vault, vaultChainId) },
    guards: [guards.slippage({ port: 'amountOut', bps: inputs.slippageBps })],
  });

  const flow = sdk.flow(sourceChainId, { name, inputs: { payment: pay } });
  const usdc = flow.lifi.swap('swap', {
    bind: { amountIn: flow.inputs.payment },
    config: { resourceOut: resources.erc20(sourceUsdc, sourceChainId) },
  });
  // No `destination`: the bridge delivers into the settle's escrow.
  const bridge = flow.lifi.bridge('bridge', {
    bind: { amountIn: usdc.amountOut },
    config: { resourceOut: resources.erc20(vaultUsdc, vaultChainId) },
  });
  flow.continuation.settle('settle', {
    bind: { input: bridge.amountOut },
    config: {
      provider: 'intent-factory',
      outcomes: [{ token: vault, destination: user, chainId: vaultChainId }],
      refund: user,
      validity: { expiresAtMs: Date.now() + 2 * 60 * 60 * 1000 },
      continuationFlow: leg.build(),
    },
  });

  const run = {
    signer: user,
    inputs: { payment: materialisers.directDeposit({ amount: inputs.amount }) },
    // The escrowed outcome is unpriceable; the committed floor bounds it.
    maxPriceImpactBps: 0,
  } satisfies ComposeRunInput;
  return { flow, run };
};
```

### Call it

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

import { buildFlow } from './cross-chain-vault';

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:cross-chain-vault@1",
    "chainId": 42161,
    "inputs": [
      {
        "name": "payment",
        "resource": {
          "kind": "native",
          "chainId": 42161
        }
      }
    ],
    "nodes": [
      {
        "id": "swap",
        "op": "lifi.swap",
        "bind": {
          "amountIn": {
            "$ref": "input.payment"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
            "chainId": 42161
          }
        }
      },
      {
        "id": "bridge",
        "op": "lifi.bridge",
        "bind": {
          "amountIn": {
            "$ref": "swap.amountOut"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
            "chainId": 8453
          }
        }
      },
      {
        "id": "settle",
        "op": "continuation.settle",
        "bind": {
          "input": {
            "$ref": "bridge.amountOut"
          }
        },
        "config": {
          "provider": "intent-factory",
          "outcomes": [
            {
              "token": "0x4e65fE4DbA92790696d040ac24Aa414708F5c0AB",
              "destination": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
              "chainId": 8453
            }
          ],
          "refund": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
          "validity": {
            "expiresAtMs": 1794937428240
          },
          "continuationFlow": {
            "version": 1,
            "id": "tpl:cross-chain-vault@1:deposit",
            "chainId": 8453,
            "inputs": [
              {
                "name": "funding",
                "resource": {
                  "kind": "erc20",
                  "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
                  "chainId": 8453
                }
              }
            ],
            "nodes": [
              {
                "id": "deposit",
                "op": "lifi.zap",
                "bind": {
                  "amountIn": {
                    "$ref": "input.funding"
                  }
                },
                "config": {
                  "resourceOut": {
                    "kind": "erc20",
                    "token": "0x4e65fE4DbA92790696d040ac24Aa414708F5c0AB",
                    "chainId": 8453
                  }
                },
                "guards": [
                  {
                    "kind": "slippage",
                    "port": "amountOut",
                    "bps": 50
                  }
                ]
              }
            ]
          }
        }
      }
    ]
  },
  "run": {
    "inputs": {
      "payment": {
        "kind": "directDeposit",
        "amount": "20000000000000000"
      }
    },
    "signer": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    "maxPriceImpactBps": 0
  }
}
```

### Prompt for a coding agent

```text
Goal: add "Deposit into a vault on another chain" 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 USDC (lifi.swap) → Bridge (lifi.bridge) → Lands in escrow (continuation.settle) → Deposit (lifi.zap).
   What can change: Source chain and token, Vault, Refund.
4. Name the flow "tpl:cross-chain-vault@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.
