Build: sweep small balances into one token

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
dust-sweep.ts
/**
 * Sweep several small balances into one token in one transaction: every token
 * is swapped to `tokenOut` and the total goes to the user. Call it with
 * `const { flow, run } = buildFlow(sdk, inputs); const result = await flow.compile(run);`.
 */
import {
  type Address,
  type ComposeSdk,
  materialisers,
  resources,
} from '@lifi/composer-sdk';

export interface DustSweepInputs {
  chainId: number;
  /** The balances to sweep; each amount is in that token's smallest unit. */
  tokens: { address: Address; amount: bigint }[];
  /** The one token the user ends up with, e.g. USDC. */
  tokenOut: Address;
  user: Address;
}

export const buildFlow = (
  sdk: ComposeSdk,
  inputs: DustSweepInputs,
  name = 'tpl:dust-sweep@1'
) => {
  const { chainId, tokens } = inputs;
  // One flow input per balance: `token0`, `token1`, …
  const flow = sdk.flow(chainId, {
    name,
    inputs: Object.fromEntries(
      tokens.map(({ address }, i) => [
        `token${i}`,
        resources.erc20(address, chainId),
      ])
    ),
  });

  // Swap every balance into the target token.
  tokens.forEach((_, i) => {
    flow.lifi.swap(`swap${i}`, {
      bind: { amountIn: flow.inputs[`token${i}`]! },
      config: {
        resourceOut: resources.erc20(inputs.tokenOut, chainId),
        slippage: 0.05,
      },
    });
  });

  // Each balance is deposited as is; the swapped total is swept to the user.
  const run = {
    signer: inputs.user,
    inputs: Object.fromEntries(
      tokens.map(({ amount }, i) => [
        `token${i}`,
        materialisers.directDeposit({ amount }),
      ])
    ),
    sweepTo: inputs.user,
  };
  return { flow, run };
};
Call it
import { createComposeSdk } from '@lifi/composer-sdk';

import { buildFlow } from './dust-sweep';

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:dust-sweep@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.

Tokens to sweepPicked by your user
ThresholdBelow a value you set
Target tokenUSDC or ETH
SlippageUp to 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