batchSwap function to allow multi-hop swaps.
Functions
batchSwap
| Parameter | Type | Description |
|---|---|---|
| kind | SwapKind | The type of swap (GIVEN_IN or GIVEN_OUT) |
| swaps | BatchSwapStep[] | Array of steps in the batch swap |
| assets | IAsset[] | Array of tokens used in the batch swap |
| funds | FundManagement | Specifies token sources and destinations |
| limits | int256[] | Maximum amounts of each asset to be transferred |
| deadline | uint256 | UNIX timestamp by which the swap must complete |
| Returns | Type | Description |
|---|---|---|
| assetDeltas | int256[] | Net token balances resulting from the swap |
queryBatchSwap
| Returns | Type | Description |
|---|---|---|
| assetDeltas | int256[] | Simulated net token balances resulting from the swap |
Structs
BatchSwapStep
| Field | Type | Description |
|---|---|---|
| poolId | bytes32 | The id of the pool to swap with |
| assetInIndex | uint256 | The index of the input token within the assets array |
| assetOutIndex | uint256 | The index of the output token within the assets array |
| amount | uint256 | The amount to swap, interpretation depends on kind |
| userData | bytes | Additional data required by the pool for the swap |
amount is set to 0 in a multi-hop swap, BEX will use the full output of the previous step as input.
FundManagement
| Field | Type | Description |
|---|---|---|
| sender | address | The address providing tokens for the swap |
| fromInternalBalance | bool | Whether to use tokens from internal balance |
| recipient | address payable | The address receiving tokens after the swap |
| toInternalBalance | bool | Whether to send tokens to internal balance |
The
SwapKind parameter in the batchSwap function determines how swap amounts are interpreted:- GIVEN_IN: Specify the exact amount of tokens to swap in. The function calculates and returns the amount of tokens you’ll receive. Example: “Swap exactly 100 USDC for as much ETH as possible.”
- GIVEN_OUT: Specify the exact amount of tokens to receive. The function calculates and returns the amount of tokens you need to swap in. Example: “Receive exactly 1 ETH, how much USDC do I need to swap?”
amount field in the BatchSwapStep struct is interpreted and how swap calculations are performed.Examples
The following examples demonstrate different ways to use batch swaps in BEX. These patterns can help you optimize trading strategies and reduce gas costs by combining multiple operations into single transactions.Multi-hop examples
Multi-hop swaps allow trading between tokens that don’t have direct liquidity pools by routing through intermediate tokens. This can often result in better pricing than direct swaps.Example 1: GIVEN_IN (HONEY -> $DAI)
In this example, you want to swap 1000 HONEY to get $DAI. The swap is executed in two steps:| Step | Amount | Token In | Token Out | Description |
|---|---|---|---|---|
| 0 | 1000 | $USDC | $HONEY | Swap exact 1000 HONEY |
| 1 | 0 | $HONEY | $DAI | Swap all received DAI |
Example 2: GIVEN_OUT (HONEY -> $DAI)
Here you want exactly 500 USDC you need:| Step | Amount | Token Out | Token In | Description |
|---|---|---|---|---|
| 0 | 500 | $DAI | $HONEY | Request exact 500 $DAI output |
| 1 | 0 | $HONEY | $USDC | Calculate required $USDC input |
Parallel swap examples
Parallel swaps are batch swaps where multiple unrelated swaps are executed in parallel within a single transaction. This allows for efficient execution of multiple trades by batching them together, reducing overall gas costs compared to executing them separately.Example 3: Parallel GIVEN_IN swaps
Execute multiple independent swaps in a single transaction:| Step | Amount | Token In | Token Out | Description |
|---|---|---|---|---|
| 0 | 1000 | $USDC | $HONEY | Swap 1000 HONEY |
| 1 | 0.5 | $WETH | $DAI | Swap 0.5 DAI |
| 2 | 0.01 | $WBTC | $USDC | Swap 0.01 USDC |
Example 4: Combined GIVEN_OUT swaps
This example demonstrates how to combine a multi-hop swap with a single-hop swap in parallel, all using GIVEN_OUT mode. It performs two independent swaps:- A multi-hop swap A->B->C->D to get exactly 100 D
- A single swap E->F to get exactly 50 F
| Step | Amount | Token Out | Token In | Description |
|---|---|---|---|---|
| 0 | 100 | D | C | First step: Request exactly 100 D |
| 1 | 0 | C | B | Second step: Use all C from previous step |
| 2 | 0 | B | A | Third step: Calculate required A input |
| 3 | 50 | F | E | Independent parallel swap: Get exactly 50 F |
- Steps 0-2 form one multi-hop path (A->B->C->D) where you want exactly 100 D
- Step 3 is a completely independent swap (E->F) where you want exactly 50 F
- BEX will calculate the required amounts of tokens A and E needed as inputs
- The two swaps execute in parallel in a single transaction