Batch Swap
The QsnBatchSwap contract enables users to split a single input token across multiple output tokens in one transaction. This is useful for portfolio diversification, multi-token purchases, or any scenario where a user wants to distribute funds across several assets simultaneously.
How It Works
- The user selects one input token and up to 10 output tokens.
- For each output token, the user specifies a percentage allocation in basis points (bps). All allocations must sum to exactly 10,000 bps (100%).
- The contract splits the input amount according to the specified percentages and executes each sub-swap through the router.
- All output tokens are sent to the user's wallet.
Input Types
- ERC-20 tokens -- Standard token inputs require prior approval to the
QsnBatchSwapcontract. - Native ETH -- ETH can be sent directly as
msg.value. The contract handles wrapping internally.
Dust Refund
If any rounding remainder is left after splitting the input amount, the contract automatically refunds the dust to the user rather than leaving it locked in the contract.
Gas Efficiency
Batch swaps provide meaningful gas savings compared to executing each swap as a separate transaction.
| Scenario | Estimated Gas |
|---|---|
| Individual swap | ~150,000 per transaction |
| Batch swap base cost | ~100,000 |
| Batch swap per sub-swap | ~200,000 |
Example: 5-Way Split
- Individual transactions: 5 x 150,000 = 750,000 gas (plus 5 base transaction fees of 21,000 each = 855,000 total)
- Batch swap: 100,000 + (5 x 200,000) = 1,100,000 gas (plus 1 base transaction fee of 21,000 = 1,121,000 total)
However, the single-transaction approach eliminates four base transaction fees (84,000 gas) and reduces overhead from repeated calldata encoding and state access. In practice, a 5-way batch swap saves approximately 35% in total gas costs compared to five individual swaps.
Use Cases
- Portfolio diversification -- Allocate a lump sum across multiple tokens in a single action.
- Multi-token purchases -- Buy several tokens at once without submitting separate transactions for each.
- Rebalancing -- Convert one concentrated holding into a diversified set of positions.
Limits
- Maximum of 10 output tokens per batch swap.
- Allocation percentages must sum to exactly 10,000 bps.