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

const balancerApi = new BalancerApi(apiUrl: string, chainId: ChainId);
NameTypeDescriptionMainnet Value
apiUrlstringUrl of APIhttps://chgbtcc9ffu7rbdw2kmu4urwy.stellate.sh/
chainIdnumberChain that will be queried80094

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.
pools.fetchPoolState(id: string): Promise<PoolState>
Parameters
NameTypeDescription
idstringID of pool, v2=poolId, v3=address
Returns
Promise<PoolState>;
PoolState - State of given pool.

pools.fetchPoolStateWithBalances

Finds state of given pool including token balances and pool shares.
fetchPoolStateWithBalances(
  id: string,
): Promise<PoolStateWithBalances>
Parameters
NameTypeDescription
idstringpoolId
Returns
Promise<PoolStateWithBalances>;
PoolStateWithBalances - State of given pool including token balances and pool shares.

Nested pools

nestedPools.fetchPoolState

Finds state of a set of nested pools.
fetchNestedPoolState(id: string): Promise<NestedPoolState>
Parameters
NameTypeDescription
idstringID of pool, v2=poolId, v3=address
Returns
Promise<NestedPoolState>;
NestedPoolState - state of a set of nested pools.

SorSwapPaths

sorSwapPaths.fetchSorSwapPaths

Finds optimized swap paths for a given swap config.
fetchSorSwapPaths(sorInput: SorInput): Promise<Path[]>
Parameters
NameTypeDescription
sorInputSorInputSwap configs
Returns
Promise<Path[]>;
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

const addLiquidity = new AddLiquidity();

Methods

query

Simulate addLiquidity operation by using an onchain call.
query(
  input: AddLiquidityInput,
  poolState: PoolState
): Promise<AddLiquidityQueryOutput>
Parameters
NameTypeDescription
inputAddLiquidityInputUser defined inputs
poolStatePoolStateCurrent state of pool that liquidity is being added to
Returns
Promise<AddLiquidityQueryOutput>;
AddLiquidityQueryOutput - Data that can be passed to buildCall. Includes updated bptOut amount.

buildCall

Builds the addLiquidity transaction using user defined slippage.
buildCall(input: AddLiquidityBuildCallInput): AddLiquidityBuildCallOutput
Parameters
NameTypeDescription
inputAddLiquidityBuildCallInputParameters required to build the call including user defined slippage
Returns
AddLiquidityBuildCallOutput;
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.
buildCallWithPermit2(input: AddLiquidityBuildCallInput, permit2: Permit2): AddLiquidityBuildCallOutput
Parameters
NameTypeDescription
inputAddLiquidityBuildCallInputParameters required to build the call including user defined slippage
permit2Permit2Permit2 object with metadata and encoded signature
Returns
AddLiquidityBuildCallOutput;
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

const removeLiquidity = new RemoveLiquidity();

Methods

query

Simulate removeLiquidity operation by using an onchain call.
query(
  input: RemoveLiquidityInput,
  poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput>
Parameters
NameTypeDescription
inputRemoveLiquidityInputUser defined inputs
poolStatePoolStateCurrent state of pool that liquidity is being removed from
Returns
Promise<RemoveLiquidityQueryOutput>;
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.
queryRemoveLiquidityRecovery(
  input: RemoveLiquidityRecoveryInput,
  poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput>
Parameters
NameTypeDescription
inputRemoveLiquidityRecoveryInputUser defined inputs
poolStatePoolStateCurrent state of pool that liquidity is being removed from
Returns
Promise<RemoveLiquidityQueryOutput>;
RemoveLiquidityQueryOutput - Data that can be passed to buildCall. Includes updated amountsOut amount.

buildCall

Builds the removeLiquidity transaction using user defined slippage.
buildCall(
  input: RemoveLiquidityBuildCallInput,
): RemoveLiquidityBuildCallOutput
Parameters
NameTypeDescription
inputRemoveLiquidityBuildCallInputInput with user defined slippage
Returns
RemoveLiquidityBuildCallOutput;
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.
buildCallWithPermit(
  input: RemoveLiquidityBuildCallInput,
  permit: Permit,
): RemoveLiquidityBuildCallOutput
Parameters
NameTypeDescription
inputRemoveLiquidityBuildCallInputInput with user defined slippage
permitPermitPermit object with metadata and encoded signature
Returns
RemoveLiquidityBuildCallOutput;
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

const swap = new Swap(swapInput: SwapInput);
NameTypeDescription
swapInputSwapInputSwap input including path information.
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.
query(
  rpcUrl?: string,
  block?: bigint,
): Promise<ExactInQueryOutput | ExactOutQueryOutput>
Parameters
NameTypeDescription
rpcUrl (optional)stringRPC URL, e.g. Infura/Alchemy
block (optional)bigintBlock no to perform the query
Returns
Promise<ExactInQueryOutput | ExactOutQueryOutput>;
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.
buildCall(
  input: SwapBuildCallInput,
): SwapBuildOutputExactIn | SwapBuildOutputExactOut
Parameters
NameTypeDescription
inputSwapBuildCallInputInput with user defined slippage
Returns
SwapBuildOutputExactIn | SwapBuildOutputExactOut;
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.
buildCallWithPermit2(
  input: SwapBuildCallInput,
  permit2: Permit2,
): SwapBuildOutputExactIn | SwapBuildOutputExactOut
Parameters
NameTypeDescription
inputSwapBuildCallInputInput with user defined slippage
permit2Permit2Permit2 object with metadata and encoded signature
Returns
SwapBuildOutputExactIn | SwapBuildOutputExactOut;
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.
public get quote(): TokenAmount
Returns
TokenAmount;
TokenAmount - Gives the combined return amount for all paths (output amount for givenIn, input amount for givenOut).

inputAmount

public get inputAmount(): TokenAmount
Returns
TokenAmount;
TokenAmount - Gives the combined input amount for all paths.

outputAmount

public get outputAmount(): TokenAmount
Returns
TokenAmount;
TokenAmount - Gives the combined output amount for all paths.

queryCallData

public queryCallData(): string
Returns
string;
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.
addLiquiditySingleToken(
  input: AddLiquiditySingleTokenInput,
  poolState: PoolState,
): Promise<PriceImpactAmount>
Parameters
NameTypeDescription
inputAddLiquiditySingleTokenInputSame input used in the corresponding add liquidity operation
poolStatePoolStateCurrent state of pool that liquidity is being added to
Returns
Promise<PriceImpactAmount>;
PriceImpactAmount - Price impact for operation.

addLiquidityUnbalanced

Calculate price impact on add liquidity unbalanced operations.
addLiquidityUnbalanced = async (
    input: AddLiquidityUnbalancedInput,
    poolState: PoolState,
): Promise<PriceImpactAmount>
Parameters
NameTypeDescription
inputAddLiquidityUnbalancedInputSame input used in the corresponding add liquidity operation
poolStatePoolStateCurrent state of pool that liquidity is being added to
Returns
Promise<PriceImpactAmount>;
PriceImpactAmount - Price impact for operation.

addLiquidityNested

Calculate price impact on add liquidity nested token operations.
addLiquidityNested = async (
  input: AddLiquidityNestedInput,
  nestedPoolState: NestedPoolState,
): Promise<PriceImpactAmount>
Parameters
NameTypeDescription
inputAddLiquidityNestedInputSame input used in the corresponding add liquidity operation
nestedPoolStateNestedPoolStateCurrent state of nested pools
Returns
Promise<PriceImpactAmount>;
PriceImpactAmount - Price impact for operation.

removeLiquidity

Calculate price impact on remove liquidity operations.
removeLiquidity = async (
  input:
      | RemoveLiquiditySingleTokenExactInInput
      | RemoveLiquidityUnbalancedInput,
  poolState: PoolState,
): Promise<PriceImpactAmount>
Parameters
NameTypeDescription
inputRemoveLiquiditySingleTokenExactInInputSame input used in the corresponding remove liquidity operation
inputRemoveLiquidityUnbalancedInputSame input used in the corresponding remove liquidity operation
poolStatePoolStateCurrent state of pool that liquidity is being removed from
Returns
Promise<PriceImpactAmount>;
PriceImpactAmount - Price impact for operation.

removeLiquidityNested

Calculate price impact on remove liquidity single token nested operations.
removeLiquidityNested = async (
  input: RemoveLiquidityNestedSingleTokenInput,
  nestedPoolState: NestedPoolState,
): Promise<PriceImpactAmount>
Parameters
NameTypeDescription
inputRemoveLiquidityNestedSingleTokenInputSame input used in the corresponding remove liquidity operation
nestedPoolStateNestedPoolStateCurrent state of nested pools
Returns
Promise<PriceImpactAmount>;
PriceImpactAmount - Price impact for operation.

swap

Calculate price impact on swap operations.
swap = async (
  swapInput: SwapInput,
  rpcUrl?: string,
  block?: bigint,
): Promise<PriceImpactAmount>
Parameters
NameTypeDescription
swapInputSwapInputSwap input including path information.
rpcUrl (optional)stringRPC URL, e.g. Infura/Alchemy
block (optional)bigintBlock no to perform the query
Note: SwapInput data is normally returned from an API SOR query but may be constructed manually. Returns
Promise<PriceImpactAmount>;
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.
calculateProportionalAmounts(
  pool: {
    address: Address;
    totalShares: HumanAmount;
    tokens: {
      address: Address;
      balance: HumanAmount;
      decimals: number
    }[];
  },
  referenceAmount: InputAmount,
): {
  tokenAmounts: InputAmount[];
  bptAmount: InputAmount;
}
Parameters
NameTypeDescription
poolSee abovePool state
referenceAmountInputAmountRef token amount
Returns
{
  tokenAmounts: InputAmount[];
  bptAmount: InputAmount;
}
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
static async signAddLiquidityApproval(
    input: AddLiquidityBaseBuildCallInput & {
        client: PublicWalletClient;
        owner: Address;
        nonces?: number[];
        expirations?: number[];
    },
): Promise<Permit2>
Parameters
NameTypeDescription
inputAddLiquidityBaseBuildCallInputAdd Liquidity Input
clientPublicWalletClientViem’s wallet client with public actions
ownerAddressUser address
nonces (optional)number[]Nonces for each token
expirations (optional)number[]Expirations for each token
Returns
Promise<Permit2>;
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
static signRemoveLiquidityApproval = async (
    input: RemoveLiquidityBaseBuildCallInput & {
        client: PublicWalletClient;
        owner: Hex;
        nonce?: bigint;
        deadline?: bigint;
    },
): Promise<Permit>
Parameters
NameTypeDescription
inputRemoveLiquidityBaseBuildCallInputRemove Liquidity Input
clientPublicWalletClientViem’s wallet client with public actions
ownerAddressUser address
nonces (optional)number[]Nonces for each token
expirations (optional)number[]Expirations for each token
Returns
Promise<Permit>;
Permit - Permit object with metadata and encoded signature