Router API

The Router can be used to interact with Balancer onchain via state changing operations or used to query operations in an off-chain context.

The main router provides user-friendly interfaces for basic Vault operations: initialization, adding/removing liquidity, and single-pool swaps.

State-changing functions

Pool Initialization

initialize

function initialize(
    address pool,
    IERC20[] memory tokens,
    uint256[] memory exactAmountsIn,
    uint256 minBptAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256 bptAmountOut);

Initializes a new liquidity pool with exact token amounts. This is the first liquidity operation that must be performed on any pool. V3 pools have a separate initialization step, which changes pool metadata in the Vault to guarantee that it is only done once. This eliminates any attacks based on 're-initialization' of pools.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool to initialize
tokensIERC20[] memoryPool tokens in token registration order
exactAmountsInuint256[] memoryExact amounts of tokens to deposit, sorted in token registration order
minBptAmountOutuint256Minimum BPT tokens to receive (slippage protection)
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data passed to the pool

Returns:

NameTypeDescription
bptAmountOutuint256Actual amount of pool tokens minted

Add Liquidity Operations

addLiquidityProportional

function addLiquidityProportional(
    address pool,
    uint256[] memory maxAmountsIn,
    uint256 exactBptAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256[] memory amountsIn);

Adds liquidity proportionally to receive an exact amount of BPT. The ratio of tokens added matches the current pool composition.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
maxAmountsInuint256[] memoryMaximum amounts of tokens to be added, sorted in token registration order
exactBptAmountOutuint256Exact amount of pool tokens to be received
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to add liquidity

Returns:

NameTypeDescription
amountsInuint256[] memoryActual amounts of tokens added, sorted in token registration order

addLiquidityUnbalanced

function addLiquidityUnbalanced(
    address pool,
    uint256[] memory exactAmountsIn,
    uint256 minBptAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256 bptAmountOut);

Adds liquidity with arbitrary token amounts (not necessarily proportional). The pool calculates the appropriate BPT to mint.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactAmountsInuint256[] memoryExact amounts of tokens to be added, sorted in token registration order
minBptAmountOutuint256Minimum amount of pool tokens to be received
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to add liquidity

Returns:

NameTypeDescription
bptAmountOutuint256Actual amount of pool tokens received

addLiquiditySingleTokenExactOut

function addLiquiditySingleTokenExactOut(
    address pool,
    IERC20 tokenIn,
    uint256 maxAmountIn,
    uint256 exactBptAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256 amountIn);

Adds liquidity using only one token to receive an exact amount of BPT. Useful for adding liquidity when you only have one of the pool's tokens.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenInIERC20Token used to add liquidity
maxAmountInuint256Maximum amount of tokens to be added
exactBptAmountOutuint256Exact amount of pool tokens to be received
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to add liquidity

Returns:

NameTypeDescription
amountInuint256Actual amount of tokens added
function donate(
    address pool,
    uint256[] memory amountsIn,
    bool wethIsEth,
    bytes memory userData
) external payable;

Donates tokens to a pool without receiving BPT. This increases the value of existing BPT tokens. The pool must have the enableDonation flag set to true.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
amountsInuint256[] memoryAmounts of tokens to be donated, sorted in token registration order
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to donate liquidity

addLiquidityCustom

function addLiquidityCustom(
    address pool,
    uint256[] memory maxAmountsIn,
    uint256 minBptAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (
    uint256[] memory amountsIn,
    uint256 bptAmountOut,
    bytes memory returnData
);

Adds liquidity with a custom operation defined by the pool. The interpretation of max/min amounts depends on the pool type and userData.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
maxAmountsInuint256[] memoryMaximum amounts of tokens to be added, sorted in token registration order
minBptAmountOutuint256Minimum amount of pool tokens to be received
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to add liquidity

Returns:

NameTypeDescription
amountsInuint256[] memoryActual amounts of tokens added, sorted in token registration order
bptAmountOutuint256Actual amount of pool tokens received
returnDatabytes memoryArbitrary (optional) data with an encoded response from the pool

Remove Liquidity Operations

removeLiquidityProportional

function removeLiquidityProportional(
    address pool,
    uint256 exactBptAmountIn,
    uint256[] memory minAmountsOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256[] memory amountsOut);

Removes liquidity proportionally by burning an exact amount of BPT. Receives all pool tokens in proportion to current pool composition.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountInuint256Exact amount of pool tokens provided
minAmountsOutuint256[] memoryMinimum amounts of tokens to be received, sorted in token registration order
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to remove liquidity

Returns:

NameTypeDescription
amountsOutuint256[] memoryActual amounts of tokens received, sorted in token registration order

removeLiquiditySingleTokenExactIn

function removeLiquiditySingleTokenExactIn(
    address pool,
    uint256 exactBptAmountIn,
    IERC20 tokenOut,
    uint256 minAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256 amountOut);

Burns an exact amount of BPT to receive a single token. Useful for exiting positions into one specific token.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountInuint256Exact amount of pool tokens provided
tokenOutIERC20Token to be received
minAmountOutuint256Minimum amount of tokens to be received
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to remove liquidity

Returns:

NameTypeDescription
amountOutuint256Actual amount of tokens received

removeLiquiditySingleTokenExactOut

function removeLiquiditySingleTokenExactOut(
    address pool,
    uint256 maxBptAmountIn,
    IERC20 tokenOut,
    uint256 exactAmountOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (uint256 bptAmountIn);

Burns BPT to receive an exact amount of a single token. You specify how much of the output token you want.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
maxBptAmountInuint256Maximum amount of pool tokens provided
tokenOutIERC20Token to be received
exactAmountOutuint256Exact amount of tokens to be received
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to remove liquidity

Returns:

NameTypeDescription
bptAmountInuint256Actual amount of pool tokens burned

removeLiquidityCustom

function removeLiquidityCustom(
    address pool,
    uint256 maxBptAmountIn,
    uint256[] memory minAmountsOut,
    bool wethIsEth,
    bytes memory userData
) external payable returns (
    uint256 bptAmountIn,
    uint256[] memory amountsOut,
    bytes memory returnData
);

Removes liquidity with a custom operation defined by the pool.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
maxBptAmountInuint256Maximum amount of pool tokens provided
minAmountsOutuint256[] memoryMinimum amounts of tokens to be received, sorted in token registration order
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes memoryAdditional (optional) data sent with the request to remove liquidity

Returns:

NameTypeDescription
bptAmountInuint256Actual amount of pool tokens burned
amountsOutuint256[] memoryActual amounts of tokens received, sorted in token registration order
returnDatabytes memoryArbitrary (optional) data with an encoded response from the pool

removeLiquidityRecovery

function removeLiquidityRecovery(
    address pool,
    uint256 exactBptAmountIn,
    uint256[] memory minAmountsOut
) external payable returns (uint256[] memory amountsOut);

Emergency exit function available only when a pool is in Recovery Mode. Allows proportional exits even if the pool is in a bad state.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountInuint256Exact amount of pool tokens provided
minAmountsOutuint256[] memoryMinimum amounts of tokens to be received, sorted in token registration order

Returns:

NameTypeDescription
amountsOutuint256[] memoryActual amounts of tokens received, sorted in token registration order

Swap Operations

swapSingleTokenExactIn

function swapSingleTokenExactIn(
    address pool,
    IERC20 tokenIn,
    IERC20 tokenOut,
    uint256 exactAmountIn,
    uint256 minAmountOut,
    uint256 deadline,
    bool wethIsEth,
    bytes calldata userData
) external payable returns (uint256 amountOut);

Swaps an exact amount of one token for another within a single pool. You specify the input amount.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenInIERC20Token to be swapped from
tokenOutIERC20Token to be swapped to
exactAmountInuint256Exact amount of input tokens to send
minAmountOutuint256Minimum amount of tokens to be received
deadlineuint256Deadline for the swap, after which it will revert
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes calldataAdditional (optional) data sent with the swap request

Returns:

NameTypeDescription
amountOutuint256Calculated amount of output tokens to be received in exchange for the given input tokens

swapSingleTokenExactOut

function swapSingleTokenExactOut(
    address pool,
    IERC20 tokenIn,
    IERC20 tokenOut,
    uint256 exactAmountOut,
    uint256 maxAmountIn,
    uint256 deadline,
    bool wethIsEth,
    bytes calldata userData
) external payable returns (uint256 amountIn);

Swaps tokens to receive an exact amount of the output token. You specify the output amount you want.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenInIERC20Token to be swapped from
tokenOutIERC20Token to be swapped to
exactAmountOutuint256Exact amount of output tokens to receive
maxAmountInuint256Maximum amount of tokens to be sent
deadlineuint256Deadline for the swap, after which it will revert
wethIsEthboolIf true, incoming ETH will be wrapped to WETH and outgoing WETH will be unwrapped to ETH
userDatabytes calldataAdditional (optional) data sent with the swap request

Returns:

NameTypeDescription
amountInuint256Calculated amount of input tokens to be sent in exchange for the requested output tokens

Queries

queryAddLiquidityProportional

function queryAddLiquidityProportional(
    address pool,
    uint256 exactBptAmountOut,
    address sender,
    bytes memory userData
) external returns (uint256[] memory amountsIn);

Queries an addLiquidityProportional operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountOutuint256Exact amount of pool tokens to be received
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountsInuint256[] memoryExpected amounts of tokens to add, sorted in token registration order

queryAddLiquidityUnbalanced

function queryAddLiquidityUnbalanced(
    address pool,
    uint256[] memory exactAmountsIn,
    address sender,
    bytes memory userData
) external returns (uint256 bptAmountOut);

Queries an addLiquidityUnbalanced operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactAmountsInuint256[] memoryExact amounts of tokens to be added, sorted in token registration order
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
bptAmountOutuint256Expected amount of pool tokens to receive

queryAddLiquiditySingleTokenExactOut

function queryAddLiquiditySingleTokenExactOut(
    address pool,
    IERC20 tokenIn,
    uint256 exactBptAmountOut,
    address sender,
    bytes memory userData
) external returns (uint256 amountIn);

Queries an addLiquiditySingleTokenExactOut operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenInIERC20Token used to add liquidity
exactBptAmountOutuint256Expected exact amount of pool tokens to receive
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountInuint256Expected amount of tokens to add

queryAddLiquidityCustom

function queryAddLiquidityCustom(
    address pool,
    uint256[] memory maxAmountsIn,
    uint256 minBptAmountOut,
    address sender,
    bytes memory userData
) external returns (
    uint256[] memory amountsIn,
    uint256 bptAmountOut,
    bytes memory returnData
);

Queries an addLiquidityCustom operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
maxAmountsInuint256[] memoryExpected maximum amounts of tokens to add, sorted in token registration order
minBptAmountOutuint256Expected minimum amount of pool tokens to receive
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountsInuint256[] memoryExpected amounts of tokens to add, sorted in token registration order
bptAmountOutuint256Expected amount of pool tokens to receive
returnDatabytes memoryArbitrary (optional) data with an encoded response from the pool

queryRemoveLiquidityProportional

function queryRemoveLiquidityProportional(
    address pool,
    uint256 exactBptAmountIn,
    address sender,
    bytes memory userData
) external returns (uint256[] memory amountsOut);

Queries a removeLiquidityProportional operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountInuint256Exact amount of pool tokens provided
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountsOutuint256[] memoryExpected amounts of tokens to receive, sorted in token registration order

queryRemoveLiquiditySingleTokenExactIn

function queryRemoveLiquiditySingleTokenExactIn(
    address pool,
    uint256 exactBptAmountIn,
    IERC20 tokenOut,
    address sender,
    bytes memory userData
) external returns (uint256 amountOut);

Queries a removeLiquiditySingleTokenExactIn operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountInuint256Exact amount of pool tokens provided
tokenOutIERC20Token to be received
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountOutuint256Expected amount of tokens to receive

queryRemoveLiquiditySingleTokenExactOut

function queryRemoveLiquiditySingleTokenExactOut(
    address pool,
    IERC20 tokenOut,
    uint256 exactAmountOut,
    address sender,
    bytes memory userData
) external returns (uint256 bptAmountIn);

Queries a removeLiquiditySingleTokenExactOut operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenOutIERC20Token to be received
exactAmountOutuint256Exact amount of tokens to be received
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
bptAmountInuint256Expected amount of pool tokens to burn

queryRemoveLiquidityCustom

function queryRemoveLiquidityCustom(
    address pool,
    uint256 maxBptAmountIn,
    uint256[] memory minAmountsOut,
    address sender,
    bytes memory userData
) external returns (
    uint256 bptAmountIn,
    uint256[] memory amountsOut,
    bytes memory returnData
);

Queries a removeLiquidityCustom operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
maxBptAmountInuint256Maximum amount of pool tokens provided
minAmountsOutuint256[] memoryExpected minimum amounts of tokens to receive, sorted in token registration order
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes memoryAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
bptAmountInuint256Expected amount of pool tokens to burn
amountsOutuint256[] memoryExpected amounts of tokens to receive, sorted in token registration order
returnDatabytes memoryArbitrary (optional) data with an encoded response from the pool

queryRemoveLiquidityRecovery

function queryRemoveLiquidityRecovery(
    address pool,
    uint256 exactBptAmountIn
) external returns (uint256[] memory amountsOut);

Queries a removeLiquidityRecovery operation without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
exactBptAmountInuint256Exact amount of pool tokens provided for the query

Returns:

NameTypeDescription
amountsOutuint256[] memoryExpected amounts of tokens to receive, sorted in token registration order

querySwapSingleTokenExactIn

function querySwapSingleTokenExactIn(
    address pool,
    IERC20 tokenIn,
    IERC20 tokenOut,
    uint256 exactAmountIn,
    address sender,
    bytes calldata userData
) external returns (uint256 amountOut);

Queries a swap operation specifying an exact input token amount without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenInIERC20Token to be swapped from
tokenOutIERC20Token to be swapped to
exactAmountInuint256Exact amount of input tokens to send
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes calldataAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountOutuint256Calculated amount of output tokens to be received in exchange for the given input tokens

querySwapSingleTokenExactOut

function querySwapSingleTokenExactOut(
    address pool,
    IERC20 tokenIn,
    IERC20 tokenOut,
    uint256 exactAmountOut,
    address sender,
    bytes calldata userData
) external returns (uint256 amountIn);

Queries a swap operation specifying an exact output token amount without actually executing it.

Parameters:

NameTypeDescription
pooladdressAddress of the liquidity pool
tokenInIERC20Token to be swapped from
tokenOutIERC20Token to be swapped to
exactAmountOutuint256Exact amount of output tokens to receive
senderaddressThe sender passed to the operation. It can influence results (e.g., with user-dependent hooks)
userDatabytes calldataAdditional (optional) data sent with the query request

Returns:

NameTypeDescription
amountInuint256Calculated amount of input tokens to be sent in exchange for the requested output tokens