Skip to main content

QsnBatchSwap

The QsnBatchSwap contract enables users to execute multiple swap operations in a single transaction, splitting an input amount across several output tokens based on configurable percentage allocations.

Overview

BatchSwap is designed for users who want to diversify a single input token (or ETH) into multiple output tokens atomically. Instead of submitting separate swap transactions for each desired output, the user defines all target swaps upfront, and the contract executes them in one transaction.

Key Features

Percentage-Based Allocation

Each swap in the batch is assigned a percentage of the total input, expressed in basis points. The sum of all allocations must equal exactly 10,000 bps (100%). For example:

Output TokenAllocation
Token A5,000 bps (50%)
Token B3,000 bps (30%)
Token C2,000 bps (20%)

Batch Size Limit

A single batch can contain up to 10 swaps. This limit prevents excessive gas consumption and ensures transactions remain executable within block gas limits.

Input Types

The contract accepts both ERC20 tokens and native ETH as input:

  • ERC20 input -- The user approves and transfers tokens to the contract, which then allocates them across the specified swaps.
  • ETH input -- The user sends ETH with the transaction. The contract wraps it to WETH and distributes accordingly.

Dust Refund

After all swaps are executed, any residual input tokens (dust) remaining due to rounding are refunded to the caller. This ensures users do not lose funds to integer division artifacts.

Gas Analysis

Batching swaps into a single transaction provides meaningful gas savings compared to individual swap transactions:

SwapsIndividual TXs (approx.)Batch TX (approx.)Savings
2~300,000 gas~220,000 gas~27%
5~750,000 gas~480,000 gas~36%
10~1,500,000 gas~900,000 gas~40%

Gas savings increase with the number of swaps due to amortization of base transaction costs, calldata overhead, and shared state loading. Actual gas consumption varies depending on pool complexity and path length.

Usage

struct SwapParams {
address[] path;
uint256[] fees;
uint256 bps; // allocation in basis points
uint256 amountOutMin; // slippage protection per swap
}

function batchSwap(
SwapParams[] calldata swaps,
uint256 deadline
) external payable;

Each SwapParams entry defines a routing path, fee tiers per hop, the percentage allocation, and a minimum output amount for slippage protection. The deadline parameter applies to the entire batch.