Skip to main content
The SDK API Reference is a comprehensive guide to the Berancer SDK’s API. It includes all the functions and classes available in the SDK, as well as their parameters and return values.

API initialization

The BalancerApi class provides helper functions for interacting with the Berancer API.

Constructor

Properties

The BalancerApi class initializes the following properties:
  • Pools - Methods for interacting with individual pools
  • NestedPools - Methods for working with nested pools
  • SorSwapPaths - Methods for optimizing swap paths

Pools

pools.fetchPoolState

Finds state of given pool.
Parameters Returns
PoolState - State of given pool.

pools.fetchPoolStateWithBalances

Finds state of given pool including token balances and pool shares.
Parameters Returns
PoolStateWithBalances - State of given pool including token balances and pool shares.

Nested pools

nestedPools.fetchPoolState

Finds state of a set of nested pools.
Parameters Returns
NestedPoolState - state of a set of nested pools.

SorSwapPaths

sorSwapPaths.fetchSorSwapPaths

Finds optimized swap paths for a given swap config.
Parameters Returns
Path[] - optimized swap paths for the given swap.

AddLiquidity

This class provides functionality to:
  • Perform on-chain queries to see the result of an addLiquidity operation
  • Build an addLiquidity transaction, with slippage, for a consumer to submit
  • Supported add types: SingleToken, Unbalanced, Proportional

Constructor

Methods

query

Simulate addLiquidity operation by using an onchain call.
Parameters Returns
AddLiquidityQueryOutput - Data that can be passed to buildCall. Includes updated bptOut amount.

buildCall

Builds the addLiquidity transaction using user defined slippage.
Parameters Returns
AddLiquidityBuildCallOutput - Encoded call data for addLiquidity that the user can submit.

buildCallWithPermit2

Builds the addLiquidity transaction using user defined slippage and Permit2 signature for token approval.
Check out the Permit2 Helper section on how to generate a Permit2 signature.
Parameters Returns
AddLiquidityBuildCallOutput - Encoded call data for addLiquidity that the user can submit.

RemoveLiquidity

This class provides functionality to:
  • Perform on-chain queries to see the result of a removeLiquidity operation
  • Build a removeLiquidity transaction, with slippage, for a consumer to submit
  • Supported remove types: Unbalanced, SingleTokenExactOutInput, SingleTokenExactInInput, Proportional

Constructor

Methods

query

Simulate removeLiquidity operation by using an onchain call.
Parameters Returns
RemoveLiquidityQueryOutput - Data that can be passed to buildCall. Includes updated amountsOut amount.

queryRemoveLiquidityRecovery

Calculates proportional exit using pool state. Note - this does not do an onchain query.
Parameters Returns
RemoveLiquidityQueryOutput - Data that can be passed to buildCall. Includes updated amountsOut amount.

buildCall

Builds the removeLiquidity transaction using user defined slippage.
Parameters Returns
RemoveLiquidityBuildCallOutput - Encoded call data for removeLiquidity that the user can submit.

buildCallWithPermit

Builds the removeLiquidity transaction using user defined slippage and Permit signature for token approval.
Check out the Permit Helper section on how to generate a Permit signature.
Parameters Returns
RemoveLiquidityBuildCallOutput - Encoded call data for removeLiquidity that the user can submit.

Swap

This class provides functionality to:
  • Perform on-chain queries to see the result of a Swap operation
  • Build a Swap transaction, with slippage, for a consumer to submit

Constructor

Note: SwapInput data is normally returned from an API SOR query but may be constructed manually.

Methods

query

Gets up to date swap result by querying onchain.
Parameters Returns
ExactInQueryOutput | ExactOutQueryOutput The updated return for the given swap, either expectedAmountOut or expectedAmountIn depending on swap kind.

buildCall

Builds the swap transaction using user defined slippage.
Parameters Returns
SwapBuildOutputExactIn | SwapBuildOutputExactOut - Encoded call data for swap that the user can submit. Includes minAmountOut or maxAmountIn depending on swap kind.

buildCallWithPermit2

Builds the swap transaction using user defined slippage and Permit2 signature for token approval.
Check out the Permit2 Helper section on how to generate a Permit2 signature.
Parameters Returns
SwapBuildOutputExactIn | SwapBuildOutputExactOut - Encoded call data for swap that the user can submit. Includes minAmountOut or maxAmountIn depending on swap kind.

quote

Gives the combined return amount for all paths. Note - this always uses the original path amounts provided in constructor and does not get updated.
Returns
TokenAmount - Gives the combined return amount for all paths (output amount for givenIn, input amount for givenOut).

inputAmount

Returns
TokenAmount - Gives the combined input amount for all paths.

outputAmount

Returns
TokenAmount - Gives the combined output amount for all paths.

queryCallData

Returns
Encoded query data for swap that you can call to get an updated amount.

PriceImpact

This class provides helper functions to calculate Price Impact for add/remove/swap actions.

Methods

addLiquiditySingleToken

Calculate price impact on add liquidity single token operations.
Parameters Returns
PriceImpactAmount - Price impact for operation.

addLiquidityUnbalanced

Calculate price impact on add liquidity unbalanced operations.
Parameters Returns
PriceImpactAmount - Price impact for operation.

addLiquidityNested

Calculate price impact on add liquidity nested token operations.
Parameters Returns
PriceImpactAmount - Price impact for operation.

removeLiquidity

Calculate price impact on remove liquidity operations.
Parameters Returns
PriceImpactAmount - Price impact for operation.

removeLiquidityNested

Calculate price impact on remove liquidity single token nested operations.
Parameters Returns
PriceImpactAmount - Price impact for operation.

swap

Calculate price impact on swap operations.
Parameters Note: SwapInput data is normally returned from an API SOR query but may be constructed manually. Returns
PriceImpactAmount - Price impact for operation.

Utils

Helper functions.

calculateProportionalAmounts

Given pool balances (including BPT) and a reference token amount, it calculates all other amounts proportional to the reference amount.
Parameters Returns
Amounts proportional to the reference amount.

Permit2 helper

Balancer v3 handles token approval through Permit2 and this helper facilitates Permit2 signature generation. Each operation (i.e. addLiquidity, addLiquidityNested, addLiquidityBoosted and swap) has its own method that leverages the same input type of the operation itself in order to simplify signature generation. Function
Parameters Returns
Permit2 - Permit2 object with metadata and encoded signature

Permit helper

Balancer v3 conforms with EIP-2612 and this helper facilitates Permit signature generation. Each operation (i.e. removeLiquidity, removeLiquidityNested and removeLiquidityBoosted) has its own method that leverages the same input type of the operation itself in order to simplify signature generation. Function
Parameters Returns
Permit - Permit object with metadata and encoded signature