Batch Swap

Split liquidity across multiple DEXes for optimal rates

Batch Swap intelligently splits your trade across multiple DEX adapters in parallel, optimizing for the best combined rate while managing price impact.

Structure

         ┌──[DEX A: 60%]──┐
TokenA ──┤                 ├──> TokenB
         └──[DEX B: 40%]──┘

Characteristics:

  • Multiple parallel routes

  • Same tokenIn and tokenOut

  • Liquidity splitting by percentage

  • Optimized for large orders

How It Works

Liquidity Distribution

The API calculates optimal splits based on:

  1. Pool Depths - Available liquidity in each pool

  2. Price Curves - Price impact at different volumes

  3. Gas Costs - Additional overhead vs. rate improvement

  4. Slippage - Combined slippage across routes

Example Calculation:

When Batch Swap is Used

The API automatically selects Batch Swap when:

Multiple competitive pools - Several DEXes offer similar rates ✅ Large swap amounts - Price impact reduction justifies extra gas ✅ Rate improvement > Gas cost - Net benefit after considering fees ✅ Sufficient liquidity - Multiple pools can handle split amounts

Example Response

Real-World Example: 5 BNB → USDT

Breakdown:

  • Route 1: 3 BNB (60%) via PancakeSwapV3 → 1,740.74 USDT

  • Route 2: 2 BNB (40%) via UniswapV2 → 1,160.49 USDT

  • Total: 2,901.23 USDT

  • vs Single Route: ~2,895 USDT (saves ~$6)

Smart Contract Encoding

BatchSwapRoute Structure

Execution Flow

Gas Costs

Routes
BSC Gas
Ethereum Gas
Additional Cost

2 DEXes

150k-200k

160k-220k

+30-50k vs Simple

3 DEXes

200k-280k

220k-320k

+60-100k vs Simple

4 DEXes

280k-350k

320k-450k

+100-150k vs Simple

Gas Cost Analysis:

Advantages

✅ Better Rates for Large Orders

Price Impact Comparison:

Swap Amount
Simple Swap Impact
Batch Swap Impact
Savings

$10k

0.2%

0.15%

$5

$50k

1.2%

0.6%

$300

$100k

3.5%

1.5%

$2,000

$500k

15%

6%

$45,000

✅ Reduced Slippage Risk

Multiple pools = diversified price exposure:

  • Less vulnerable to single pool manipulation

  • Better average execution price

  • Lower total price impact

✅ Access More Liquidity

✅ Automatic Optimization

The API handles:

  • Optimal percentage splits

  • Gas cost calculations

  • Route selection

  • Slippage management

Limitations

❌ Higher Gas Costs

❌ Increased Complexity

  • More points of failure

  • Complex slippage calculations

  • Harder to debug

  • Potential atomic revert if one route fails

❌ Percentage Precision

Code Examples

JavaScript Implementation

Python Implementation

Best Practices

✅ Minimum Swap Amount

Only use batch swaps for amounts where improvement justifies gas:

✅ Monitor Individual Routes

✅ Set Conservative Slippage

Batch swaps have compounding slippage:

Troubleshooting

Issue: One Route Fails, Entire TX Reverts

Cause: Atomic execution - all routes must succeed

Solution:

Issue: Worse Rate Than Expected

Check:

  • Market moved between quote and execution

  • MEV sandwich attack

  • One pool had stale price

Solution:

  • Fetch fresh route

  • Use private transaction relay

  • Increase slippage slightly

Issue: High Gas Cost

Optimize:

Comparison Table

Metric
Simple
Batch (2 DEX)
Batch (3 DEX)

Gas Cost

120k

180k (+50%)

250k (+108%)

Rate

Baseline

+0.2-0.5%

+0.3-0.8%

Complexity

Low

Medium

High

Min Amount

Any

$100+

$500+

Slippage Risk

Single pool

Diversified

Most diversified

Summary

Batch Swap is ideal for:

  • ✅ Large trades ($100k+)

  • ✅ When multiple DEXes have competitive rates

  • ✅ Price optimization over gas savings

  • ✅ Reducing price impact

Use Simple Swap for:

  • ❌ Small trades (<$100)

  • ❌ When gas cost matters most

  • ❌ Single pool has dominant liquidity

  • ❌ Quick, straightforward swaps

Last updated