Valence BalancerV2Swap Library

The Valence BalancerV2Swap library enables token swaps from an input account to an output account using the Balancer V2 protocol. It is typically used as part of a Valence Program. In that context, a Processor contract will be the main contract interacting with the BalancerV2Swap library.

High-level flow

---
title: BalancerV2Swap Library
---
graph LR
    IA((Input Account))
    OA((Output Account))
    BV((Balancer Vault))
    P[Processor]
    S[BalancerV2Swap Library]

    subgraph EVM[ EVM Domain ]
        P -- 1/swap or multiSwap --> S
        S -- 2/Query balances --> IA
        S -- 3/Do approve --> IA
        IA -- 4/ERC-20 approve --> BV
        S -- 5/Execute swap --> IA
        IA -- 6/Call swap/batchSwap --> BV
        BV -- 7/Transfer output tokens --> OA
    end

Functions

FunctionParametersDescription
swappoolId, tokenIn, tokenOut, userData, amountIn, minAmountOut, timeoutExecute a single token swap through a Balancer V2 pool.
multiSwappoolIds, tokens, userDataArray, amountIn, minAmountOut, timeoutExecute a multi-hop swap through multiple Balancer V2 pools.

Single Swap Parameters

The swap function requires the following parameters:

ParameterTypeDescription
poolIdbytes32The ID of the Balancer pool to use for the swap
tokenInaddressAddress of the token to swap from
tokenOutaddressAddress of the token to swap to
userDatabytesAdditional data for specialized pools (usually empty bytes)
amountInuint256Amount of tokens to swap. If set to 0, all available tokens in the input_account will be swapped
minAmountOutuint256Minimum amount of output tokens to receive (slippage tolerance). If set to 0 it means no slippage protection is applied.
timeoutuint256How long the transaction is valid for (in seconds)

Multi-Hop Swap Parameters

The multiSwap function enables complex trading routes through multiple pools:

ParameterTypeDescription
poolIdsbytes32[]Array of pool IDs to use for each swap step (in sequence)
tokensaddress[]Array of all token addresses involved in the swap path (in sequence), needs to contain exactly 1 more element than the poolIds array
userDataArraybytes[]Additional data for specialized pools (one entry per pool). This data can be empty for all current Balancer pools but is reserved for possible future pool logic. Must be the same length as the poolIds array
amountInuint256Amount of tokens to swap. If set to 0, all available tokens in the input_account will be swapped
minAmountOutuint256Minimum amount of output tokens to receive (slippage tolerance). If set to 0 it means no slippage protection is applied.
timeoutuint256How long the transaction is valid for (in seconds)

For more information on how swaps work on Balancer V2, please refer to the Single Swap and Batch Swap documentation.

Configuration

The library is configured on deployment using the BalancerV2SwapConfig type.

/**
 * @title BalancerV2SwapConfig
 * @notice Configuration for Balancer V2 swaps
 * @param inputAccount The account from which tokens will be taken
 * @param outputAccount The account to which result tokens will be sent
 * @param vaultAddress Address of the Balancer V2 Vault
 */
struct BalancerV2SwapConfig {
    BaseAccount inputAccount;
    BaseAccount outputAccount;
    address vaultAddress;
}