Build: deposit any token into a vault

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
vault-deposit.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
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 full working module, not an excerpt. It names the flow tpl:vault-deposit@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.

From tokenPassed by your user
VaultFixed by you, or user picks
Slippage0.5%

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