Build: pay many wallets on different chains
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.0multi-wallet-payouts.ts
/**
* Pay many wallets from one funding wallet in one transaction. The funding
* token is split into one exact part per recipient: a recipient on the funding
* chain gets a transfer, one on another chain is bridged into an escrow there
* that pays it out. Call it with
* `const { flow, run } = buildFlow(sdk, inputs); const result = await flow.compile(run);`.
*/
import {
type Address,
type Bindable,
type ComposeSdk,
materialisers,
raw,
resources,
} from '@lifi/composer-sdk';
export interface Recipient {
address: Address;
chainId: number;
/** The ERC-20 it receives; on the funding chain, the funding token. */
token: Address;
/** In the funding token's smallest unit. */
amount: bigint;
}
export interface MultiWalletPayoutsInputs {
chainId: number;
fundingToken: Address;
/** Signs the transaction; an escrow that expires unfilled refunds it. */
funder: Address;
recipients: Recipient[];
slippageBps: number;
}
export const buildFlow = (
sdk: ComposeSdk,
inputs: MultiWalletPayoutsInputs,
name = 'tpl:multi-wallet-payouts@1'
) => {
const { chainId, fundingToken, recipients } = inputs;
const funding = resources.erc20(fundingToken, chainId);
const flow = sdk.flow(chainId, { name, inputs: { funding } });
const slippage = inputs.slippageBps / 10_000;
// `frax` takes a native-coin fee no node holds; `near` quotes revert in a flow.
const deny = ['frax', 'near'];
const expiresAtMs = Date.now() + 2 * 60 * 60 * 1000;
// All splits first, so the bridges are quoted together; the last part is the rest.
let rest: Bindable<'resource'> = flow.inputs.funding;
const parts = recipients.map((r, i) => {
if (i === recipients.length - 1) return { ...r, part: rest };
const split = flow.core.splitAt(`split${i + 1}`, {
bind: { source: rest },
config: { amount: r.amount },
});
rest = split.tail;
return { ...r, part: split.head };
});
parts.forEach(({ address: destination, token, part, ...r }, i) => {
const n = i + 1;
if (r.chainId === chainId) {
const recipient = raw.literal('address', destination);
flow.core.transfer(`transfer${n}`, {
bind: { amount: part, recipient },
config: {},
});
return;
}
const tokenOut = resources.erc20(token, r.chainId);
const bridge = flow.lifi.bridge(`bridge${n}`, {
bind: { amountIn: part },
config: { resourceOut: tokenOut, slippage, bridges: { deny } },
});
// The escrow's leg has no nodes: its balance is the payout.
const leg = sdk.flow(r.chainId, { name, inputs: { funding: tokenOut } });
flow.continuation.settle(`settle${n}`, {
bind: { input: bridge.amountOut },
config: {
outcomes: [{ token, destination, chainId: r.chainId }],
continuationFlow: leg.build(),
validity: { expiresAtMs },
},
});
});
const total = recipients.reduce((sum, r) => sum + r.amount, 0n);
const run = {
signer: inputs.funder,
inputs: { funding: materialisers.directDeposit({ amount: total }) },
// A bridged payout is a future amount the price-impact check cannot price.
maxPriceImpactBps: 0,
};
return { flow, run };
};Call it
import { createComposeSdk } from '@lifi/composer-sdk';
import { buildFlow } from './multi-wallet-payouts';
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:multi-wallet-payouts@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.
RecipientsAddress, chain, token, amount
Funding tokenFixed by you
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