> ## Documentation Index
> Fetch the complete documentation index at: https://docs.li.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# Consolidate tokens into USDC

> Swap several stablecoin balances into USDC on Ethereum in one transaction, with one input and one lifi.swap per token.

## Scenario

A wallet user holds small balances of USDT, DAI, FRAX and LUSD on Ethereum and wants them as one USDC balance, with one signature. The flow declares one input per stablecoin and swaps each into USDC with its own [`lifi.swap`](https://docs.li.fi/composer/composer-api/ops/lifi-swap). All four swaps share one config. [`sweepTo`](https://docs.li.fi/composer/composer-api/concepts/sweeping-and-amounts#sweepto) sends the USDC to the signer in the same transaction.

## Full example

Install the SDK with `npm install @lifi/composer-sdk @lifi/compose-spec` and set `LIFI_API_KEY`; see [Before you start](https://docs.li.fi/composer/composer-api/overview.md#before-you-start). Adapted from [`consolidateToUsdc.ts`](https://github.com/lifinance/composer-sdk-examples/blob/main/examples/consolidateToUsdc.ts).

```ts theme={"system"}
import { createComposeSdk, materialisers, resources } from '@lifi/composer-sdk';

const CHAIN_ID = 1;
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
const USDT = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const FRAX = '0x853d955aCEf822Db058eb8505911ED77F175b99e';
const LUSD = '0x5f98805A4E8be255a32880FDeC7F6728C6568bA0';

const sdk = createComposeSdk({
  baseUrl: 'https://composer.li.quest',
  apiKey: process.env.LIFI_API_KEY!,
});

// One input per stablecoin on Ethereum.
const builder = sdk.flow(CHAIN_ID, {
  name: 'consolidate-stables-to-usdc',
  inputs: {
    usdt: resources.erc20(USDT, CHAIN_ID),
    dai: resources.erc20(DAI, CHAIN_ID),
    frax: resources.erc20(FRAX, CHAIN_ID),
    lusd: resources.erc20(LUSD, CHAIN_ID),
  },
});

// Swap each stablecoin to USDC with the same config.
const swapConfig = {
  resourceOut: resources.erc20(USDC, CHAIN_ID),
  slippage: 0.03,
};
builder.lifi.swap('swapUsdt', { bind: { amountIn: builder.inputs.usdt }, config: swapConfig });
builder.lifi.swap('swapDai', { bind: { amountIn: builder.inputs.dai }, config: swapConfig });
builder.lifi.swap('swapFrax', { bind: { amountIn: builder.inputs.frax }, config: swapConfig });
builder.lifi.swap('swapLusd', { bind: { amountIn: builder.inputs.lusd }, config: swapConfig });

// directDeposit pulls a fixed amount of each token from the signer.
const result = await builder.compile({
  signer: '0xYourSignerAddress',
  inputs: {
    usdt: materialisers.directDeposit({ amount: '100000000' }), // 100 USDT (6 decimals)
    dai: materialisers.directDeposit({ amount: '100000000000000000000' }), // 100 DAI (18 decimals)
    frax: materialisers.directDeposit({ amount: '100000000000000000000' }), // 100 FRAX
    lusd: materialisers.directDeposit({ amount: '100000000000000000000' }), // 100 LUSD
  },
  sweepTo: builder.context.sender,
});

for (const id of ['swapUsdt', 'swapDai', 'swapFrax', 'swapLusd']) {
  console.log(id, result.outputs[`${id}.amountOut`]?.amount);
}
```

## Run it

Save the file as `consolidate-to-usdc.mts` and run it:

```bash theme={"system"}
LIFI_API_KEY=... npx tsx consolidate-to-usdc.mts
```

The script compiles the flow and prints the result; it sends nothing. To submit, send `result.actions` in order, as in [Build a same-chain flow](https://docs.li.fi/composer/composer-api/guides/build-a-flow.md#wire-in-a-signer).

## What to check

* `result.outputs['<swap id>.amountOut']` is the USDC from each swap: `amount.estimate` is the simulated amount and `amount.minimum` is the swap's 3% floor. `sweepTo` makes the signer their owner.
* `result.actions` holds an approval for each stablecoin the proxy needs, then `execute`.
* A `<swap id>.unspentIn` output appears only if a swap leaves part of its input unsold. `sweepTo` returns it too.

## Related

* [`lifi.swap`](https://docs.li.fi/composer/composer-api/ops/lifi-swap): config, `unspentIn` and errors.
* [Sweeping and amounts](https://docs.li.fi/composer/composer-api/concepts/sweeping-and-amounts.md#sweepto): what `sweepTo` moves.
* [Swap and deposit](https://docs.li.fi/composer/composer-api/recipes/swap-and-deposit.md): chain a deposit onto a swap.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.