# Pay many wallets on different chains

> Pay several wallets, each in its own token on its own chain, from one wallet in one transaction.

- Status: live. Compiles on Composer today.
- Who it's for: Payroll, airdrops and market makers
- Page: https://composer-experiences.li.quest/templates/multi-wallet-payouts
- Working example, run with your own key and wallet: https://composer-experiences.li.quest/templates/multi-wallet-payouts/live

## What you get

- Several payouts in one transaction
- Each wallet gets the token it wants, on its chain

## 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. One funding wallet (Base)
2. Split the funding (Base). Ops: `core.splitAt ×n`
3. Pay on this chain (Base). Ops: `core.transfer ×n`
4. Bridge to the others (Base to Arbitrum, Optimism). Ops: `lifi.bridge ×n`
5. Escrow pays each wallet (Arbitrum, Optimism): Minimum checked on chain. Ops: `continuation.settle ×n`

## What you control

| Parameter | Set by |
| --- | --- |
| Recipients | Address, chain, token, amount |
| Funding token | Fixed by you |

## 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 `multi-wallet-payouts.ts`. It names the flow `tpl:multi-wallet-payouts@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: `multi-wallet-payouts.ts`

```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

```ts
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 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:multi-wallet-payouts@1",
    "chainId": 8453,
    "inputs": [
      {
        "name": "funding",
        "resource": {
          "kind": "erc20",
          "token": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
          "chainId": 8453
        }
      }
    ],
    "nodes": [
      {
        "id": "split1",
        "op": "core.splitAt",
        "bind": {
          "source": {
            "$ref": "input.funding"
          }
        },
        "config": {
          "amount": "1200000000"
        }
      },
      {
        "id": "split2",
        "op": "core.splitAt",
        "bind": {
          "source": {
            "$ref": "split1.tail"
          }
        },
        "config": {
          "amount": "800000000"
        }
      },
      {
        "id": "bridge1",
        "op": "lifi.bridge",
        "bind": {
          "amountIn": {
            "$ref": "split1.head"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
            "chainId": 42161
          },
          "slippage": 0.01,
          "bridges": {
            "deny": [
              "frax",
              "near"
            ]
          }
        }
      },
      {
        "id": "settle1",
        "op": "continuation.settle",
        "bind": {
          "input": {
            "$ref": "bridge1.amountOut"
          }
        },
        "config": {
          "outcomes": [
            {
              "token": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
              "destination": "0x4c2c0F0bB2631B02aC9299C59690914ee7A200B8",
              "chainId": 42161
            }
          ],
          "continuationFlow": {
            "version": 1,
            "id": "tpl:multi-wallet-payouts@1",
            "chainId": 42161,
            "inputs": [
              {
                "name": "funding",
                "resource": {
                  "kind": "erc20",
                  "token": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
                  "chainId": 42161
                }
              }
            ],
            "nodes": []
          },
          "validity": {
            "expiresAtMs": 1794937428256
          }
        }
      },
      {
        "id": "bridge2",
        "op": "lifi.bridge",
        "bind": {
          "amountIn": {
            "$ref": "split2.head"
          }
        },
        "config": {
          "resourceOut": {
            "kind": "erc20",
            "token": "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
            "chainId": 10
          },
          "slippage": 0.01,
          "bridges": {
            "deny": [
              "frax",
              "near"
            ]
          }
        }
      },
      {
        "id": "settle2",
        "op": "continuation.settle",
        "bind": {
          "input": {
            "$ref": "bridge2.amountOut"
          }
        },
        "config": {
          "outcomes": [
            {
              "token": "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
              "destination": "0x650b971A75064140d68457d838e334f280707b70",
              "chainId": 10
            }
          ],
          "continuationFlow": {
            "version": 1,
            "id": "tpl:multi-wallet-payouts@1",
            "chainId": 10,
            "inputs": [
              {
                "name": "funding",
                "resource": {
                  "kind": "erc20",
                  "token": "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
                  "chainId": 10
                }
              }
            ],
            "nodes": []
          },
          "validity": {
            "expiresAtMs": 1794937428256
          }
        }
      },
      {
        "id": "transfer3",
        "op": "core.transfer",
        "bind": {
          "amount": {
            "$ref": "split2.tail"
          },
          "recipient": {
            "kind": "address",
            "value": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
          }
        },
        "config": {}
      }
    ]
  },
  "run": {
    "inputs": {
      "funding": {
        "kind": "directDeposit",
        "amount": "2500000000"
      }
    },
    "signer": "0x64816e8e3E65f69b2F7a85ab9deC8FA93Ce6E15d",
    "maxPriceImpactBps": 0
  }
}
```

### Prompt for a coding agent

```text
Goal: add "Pay many wallets on different chains" 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: One funding wallet → Split the funding (core.splitAt ×n) → Pay on this chain (core.transfer ×n) → Bridge to the others (lifi.bridge ×n) → Escrow pays each wallet (continuation.settle ×n).
   What can change: Recipients, Funding token.
4. Name the flow "tpl:multi-wallet-payouts@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.
