SDK API Reference

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
  • Supports Balancer v2 and v3

Example

See the addLiquidity guide and the addLiquidity exampleopen in new window.

Constructor

const addLiquidity = new AddLiquidity();

Methods

query

Simulate addLiquidity operation by using an onchain call.

query(
  input: AddLiquidityInput, 
  poolState: PoolState
): Promise<AddLiquidityQueryOutput>

Parameters

NameTypeDescription
inputAddLiquidityInputopen in new windowUser defined inputs
poolStatePoolStateopen in new windowCurrent state of pool that liquidity is being added to

Returns

Promise<AddLiquidityQueryOutput>

AddLiquidityQueryOutputopen in new window - 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
inputAddLiquidityBuildCallInputopen in new windowParameters required to build the call including user defined slippage

Returns

AddLiquidityBuildCallOutput

AddLiquidityBuildCallOutputopen in new window - Encoded call data for addLiquidity that user can submit.


RemoveLiquidity

This class provides functionality to:

  • Perform on-chain queries to see the result of an removeLiquidity operation
  • Build a removeLiquidity transaction, with slippage, for a consumer to submit
  • Supported remove types: Unbalanced, SingleTokenExactOutInput, SingleTokenExactInInput, Proportional
  • Supports Balancer v2 and v3

Example

See the removeLiquidity guide and the removeLiquidity example.

Constructor

const removeLiquidity = new RemoveLiquidity();

Methods

query

Simulate removeLiquidity operation by using an onchain call.

query(
  input: RemoveLiquidityInput,
  poolState: PoolState,
): Promise<RemoveLiquidityQueryOutput>

Parameters

NameTypeDescription
inputRemoveLiquidityInputopen in new windowUser defined inputs
poolStatePoolStateopen in new windowCurrent state of pool that liquidity is being removed from

Returns

Promise<RemoveLiquidityQueryOutput>

RemoveLiquidityQueryOutputopen in new window - 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
inputRemoveLiquidityRecoveryInputopen in new windowUser defined inputs
poolStatePoolStateopen in new windowCurrent state of pool that liquidity is being removed from

Returns

Promise<RemoveLiquidityQueryOutput>

RemoveLiquidityQueryOutputopen in new window - 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
inputRemoveLiquidityBuildCallInputopen in new windowInput with user defined slippage

Returns

RemoveLiquidityBuildCallOutput

RemoveLiquidityBuildCallOutputopen in new window - Encoded call data for addLiquidity that 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
  • Supports Balancer v2 and v3

Example

See the swap guide swap exampleopen in new window.

Constructor

const swap = new Swap(swapInput: SwapInput);
NameTypeDescription
swapInputSwapInputopen in new windowSwap 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>

ExactInQueryOutputopen in new windowExactOutQueryOutputopen in new window

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
inputSwapBuildCallInputopen in new windowInput with user defined slippage

Returns

SwapBuildOutputExactIn | SwapBuildOutputExactOut

SwapBuildOutputExactInopen in new windowSwapBuildOutputExactOutopen in new window

RemoveLiquidityBuildCallOutputopen in new window - Encoded call data for swap that 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

TokenAmountopen in new window - Gives the combined return amount for all paths (output amount for givenIn, input amount for givenOut).


inputAmount

public get inputAmount(): TokenAmount

Returns

TokenAmount

TokenAmountopen in new window - Gives the combined input amount for all paths.


outputAmount

public get outputAmount(): TokenAmount

Returns

TokenAmount

TokenAmountopen in new window - Gives the combined output amount for all paths.


queryCallData

public queryCallData(): string

Returns

string

Encoded query data for swap that a user can call to get an updated amount.


PriceImpact

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

  • Supports Balancer v2 and v3

Example

See the price impact exampleopen in new window.

Methods

addLiquiditySingleToken

Calculate price impact on add liquidity single token operations.

addLiquiditySingleToken(
  input: AddLiquiditySingleTokenInput,
  poolState: PoolState,
): Promise<PriceImpactAmount> 

Parameters

NameTypeDescription
inputAddLiquiditySingleTokenInputopen in new windowSame input used in the corresponding add liquidity operation
poolStatePoolStateopen in new windowCurrent state of pool that liquidity is being added to

Returns

Promise<PriceImpactAmount>

PriceImpactAmountopen in new window - 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
poolStatePoolStateopen in new windowCurrent state of pool that liquidity is being added to

Returns

Promise<PriceImpactAmount>

PriceImpactAmountopen in new window - 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
nestedPoolStateNestedPoolStateopen in new windowCurrent state of nested pools

Returns

Promise<PriceImpactAmount>

PriceImpactAmountopen in new window - Price impact for operation.


removeLiquidity

Calculate price impact on remove liquidity operations.

removeLiquidity = async (
  input:
      | RemoveLiquiditySingleTokenExactInInput
      | RemoveLiquidityUnbalancedInput,
  poolState: PoolState,
): Promise<PriceImpactAmount>

Parameters

NameTypeDescription
inputRemoveLiquiditySingleTokenExactInInputopen in new windowSame input used in the corresponding remove liquidity operation
inputRemoveLiquidityUnbalancedInputopen in new windowSame input used in the corresponding remove liquidity operation
poolStatePoolStateopen in new windowCurrent state of pool that liquidity is being removed from

Returns

Promise<PriceImpactAmount>

PriceImpactAmountopen in new window - 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
inputRemoveLiquidityNestedSingleTokenInputopen in new windowSame input used in the corresponding remove liquidity operation
nestedPoolStateNestedPoolStateopen in new windowCurrent state of nested pools

Returns

Promise<PriceImpactAmount>

PriceImpactAmountopen in new window - Price impact for operation.


swap

Calculate price impact on swap operations.

swap = async (
  swapInput: SwapInput,
  rpcUrl?: string,
  block?: bigint,
): Promise<PriceImpactAmount>

Parameters

NameTypeDescription
swapInputSwapInputopen in new windowSwap 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>

PriceImpactAmountopen in new window - Price impact for operation.


BalancerApi

This class provides helper functions for interacting with the Balancer API.

Example

See the examples for add/remove/swap linked above as these use BalancerApi to fetch required data.

Constructor

const balancerApi = new BalancerApi(balancerApiUrl: string, chainId: ChainId);
NameTypeDescription
balancerApiUrlstringUrl of Balancer API
chainIdChainIdopen in new windowChain that will be queried

Methods

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>

PoolStateopen in new window - State of given pool.


pools.fetchPoolStateWithBalances

Finds state of given pool including token balances and pool shares.

fetchPoolStateWithBalances(
  id: string,
): Promise<PoolStateWithBalances> 

Parameters

NameTypeDescription
idstringID of pool, v2=poolId, v3=address

Returns

Promise<PoolStateWithBalances>

PoolStateWithBalancesopen in new window - State of given pool including token balances and pool shares.


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>

NestedPoolStateopen in new window - state of a set of nested pools.


sorSwapPaths.fetchSorSwapPaths

Finds optimized swap paths for a given swap config.

fetchSorSwapPaths(sorInput: SorInput): Promise<Path[]>

Parameters

NameTypeDescription
sorInputSorInputopen in new windowSwap configs

Returns

Promise<Path[]>

Path[]open in new window - optimized swap paths for the given swap.


Utils

Helper functions.

calculateProportionalAmounts

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

Example

See the price impact exampleopen in new window.

calculateProportionalAmounts(
  pool: {
    address: Address;
    totalShares: HumanAmount;
    tokens: { 
      address: Address; 
      balance: HumanAmount; 
      decimals: number 
    }[];
  },
  referenceAmount: InputAmount,
): {
  tokenAmounts: InputAmount[];
  bptAmount: InputAmount;
}

Parameters

NameTypeDescription
poolSee abovePool state
referenceAmountInputAmountopen in new windowRef token amount

Returns

{
  tokenAmounts: InputAmount[];
  bptAmount: InputAmount;
}

Amounts proportional to the reference amount.