Build: withdraw to any 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.0withdraw-anywhere.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
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 full working module, not an excerpt. It names the flow tpl:withdraw-anywhere@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.
PositionPassed by your user
Destination chainPassed by your user
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