# Withdraw to any chain

> Exit a position and receive USDC on the chain the user wants, in one transaction.

- Status: live. Compiles on Composer today.
- Who it's for: Wallets and apps where users hold positions on several chains
- Page: https://composer-experiences.li.quest/templates/withdraw-anywhere
- Working example, run with your own key and wallet: https://composer-experiences.li.quest/templates/withdraw-anywhere/live

## What you get

- Withdraw and bridge in one transaction
- The user picks where the money lands

## 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. Position (Base): Aave USDC
2. Withdraw (Base). Ops: `lifi.zap`
3. Bridge (Base to Arbitrum). Ops: `lifi.bridge`
4. USDC to the user (Arbitrum)

## What you control

| Parameter | Set by |
| --- | --- |
| Position | Passed by your user |
| Destination chain | Passed by your user |
| 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 `withdraw-anywhere.ts`. It names the flow `tpl:withdraw-anywhere@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: `withdraw-anywhere.ts`

```ts
/**
 * Exit a vault position and receive USDC on another chain, in one transaction:
 * redeem the position's shares for USDC, then bridge the USDC to the user on
 * the chain they pick.
 *
 *   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 WithdrawAnywhereInputs {
  chainId: number;
  /** The position's share token, such as Aave's aToken. */
  position: Address;
  /** In `position`'s smallest unit. */
  amount: bigint;
  /** USDC on the position's chain, which the exit produces. */
  usdc: Address;
  destinationChainId: number;
  /** USDC on the destination chain, which the user receives. */
  destinationUsdc: Address;
  /** Signs on the position's chain and receives the USDC. */
  user: Address;
  slippageBps: number;
}

export const buildFlow = (
  sdk: ComposeSdk,
  inputs: WithdrawAnywhereInputs,
  name = 'tpl:withdraw-anywhere@1'
) => {
  const { chainId, user } = inputs;
  const flow = sdk.flow(chainId, {
    name,
    inputs: { position: resources.erc20(inputs.position, chainId) },
  });

  // Redeem the shares for USDC; the guard floors what the bridge receives.
  const exit = flow.lifi.zap('withdraw', {
    bind: { amountIn: flow.inputs.position },
    config: { resourceOut: resources.erc20(inputs.usdc, chainId) },
    guards: [guards.slippage({ port: 'amountOut', bps: inputs.slippageBps })],
  });

  // The bridge pays the user directly on the destination chain.
  flow.lifi.bridge('bridge', {
    bind: { amountIn: exit.amountOut },
    config: {
      resourceOut: resources.erc20(
        inputs.destinationUsdc,
        inputs.destinationChainId
      ),
      destination: user,
    },
  });

  const run = {
    signer: user,
    inputs: {
      position: materialisers.directDeposit({ amount: inputs.amount }),
    },
    // The exit usually yields more than the guard's floor, which the bridge
    // quotes on; the rest comes back to the user on the position's chain.
    sweepTo: user,
  } satisfies ComposeRunInput;
  return { flow, run };
};
```

### Call it

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

import { buildFlow } from './withdraw-anywhere';

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:withdraw-anywhere@1",
    "chainId": 8453,
    "inputs": [
      {
        "name": "position",
        "resource": {
          "kind": "erc20",
          "token": "0x4e65fE4DbA92790696d040ac24Aa414708F5c0AB",
          "chainId": 8453
        }
      }
    ],
    "nodes": [
      {
        "id": "withdraw",
        "op": "lifi.zap",
        "bind": {
          "amountIn": {
            "$ref": "input.position"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
            "chainId": 8453
          }
        },
        "guards": [
          {
            "kind": "slippage",
            "port": "amountOut",
            "bps": 50
          }
        ]
      },
      {
        "id": "bridge",
        "op": "lifi.bridge",
        "bind": {
          "amountIn": {
            "$ref": "withdraw.amountOut"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
            "chainId": 42161
          },
          "destination": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
        }
      }
    ]
  },
  "run": {
    "inputs": {
      "position": {
        "kind": "directDeposit",
        "amount": "500000000"
      }
    },
    "signer": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    "sweepTo": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
  }
}
```

### Prompt for a coding agent

```text
Goal: add "Withdraw to any 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: Position → Withdraw (lifi.zap) → Bridge (lifi.bridge) → USDC to the user.
   What can change: Position, Destination chain, Slippage.
4. Name the flow "tpl:withdraw-anywhere@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.
