Build: deposit into a vault on another chain

Three steps from here to your app. Run it from your backend, your frontend or your agent.

1

Get your API key

Create a key on the LI.FI portal. It ties usage and your fees to your app.

Get API key ↗
2

Add the flow

Install
npm install @lifi/composer-sdk@0.16.0 @lifi/compose-spec@0.16.0
cross-chain-vault.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
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 full working module, not an excerpt. It names the flow tpl:cross-chain-vault@1 so LI.FI can see which template it came from.

3

Choose what you control

Fix these once, or pass them in from your user on every call.

Source chain and tokenPassed by your user
VaultFixed by you
RefundUSDC on Base

Before you go live

Need it changed?

A protocol we don't cover yet, custom logic, or a launch with a partner. We build these with you.

Talk to the team