Skip to main content
Using the Berancer SDK, you can remove liquidity using two primary methods (see RemoveLiquidityKind):
  1. Proportional - remove liquidity proportionally across all tokens
  2. SingleTokenExactIn - remove liquidity and receive a single token
For more comprehensive remove liquidity examples, see the remove liquidity examples in the BEX SDK repository.

Example: single token exit

In this example, we’ll demonstrate how to remove liquidity and receive a single token (BERA) while calculating the price impact of the transaction.
Below we breakdown the code example above.

Helper classes

The main helper classes we use from the SDK are:
  • BalancerApi - to simplify retrieving pool data from the Pools API
  • RemoveLiquidity - to build removeLiquidity queries and transactions
  • Slippage - to simplify creating limits with user defined slippage
  • PriceImpact - to calculate the price impact of single token exits

Fetching pool data

After initializing the BalancerApi class, we can fetch current pool data using fetchPoolState.

Simulation and slippage setting

The PriceImpact class calculates the price impact of unbalanced liquidity operations. A PriceImpactAmount is returned, with the price impact expressed in a number of different units. Its use in the above example is informational, but can be used to provide detailed slippage information.

Building the transaction

The RemoveLiquidity class has a buildCall method that allows us to build the transaction. This method takes in the queryOutput and address parameters.