Remove Liquidity with Typescript SDK

This guide demonstrates how to remove liquidity from a pool. We will use the preferred function for removing liquidity, removeLiquidityProportional. Tokens are removed from the pool in proportional amounts, causing zero price impact and avoiding the swap fee charged when exiting non-proportional. Specifying an exactBptAmountIn ensures that the user will not be left with any dust. See the Router API for other supported remove methods.

This guide is for removing liquidity from Balancer v3 with the b-sdkopen in new window. This sdk supports removing liquidity from Balancer v3, Balancer v2 as well as Cow-AMMs.

Install the Balancer SDK

The Balancer SDKopen in new window is a Typescript/Javascript library for interfacing with the Balancer protocol and can be installed with:

Example Script

Run this example script on a local fork of Ethereum mainnet using our v3 pool operation examples repoopen in new window

Loading...

The four 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
  • PermitHelper - to simplify creating a permit signature

Fetch Pool Data

In this example we use the BalancerApi fetchPoolState function to fetch the pool data required for the removeLiquidityProportional poolState parameter.

const balancerApi = new BalancerApi('https://api-v3.balancer.fi/', chainId);
const poolState = await balancerApi.pools.fetchPoolState(pool);

To see the full query used to fetch pool state refer to the code hereopen in new window.

Query remove liquidity

Router queries allow for simulation of operations without execution. In this example, when the query function is called:

const queryOutput = await removeLiquidity.query(
  removeLiquidityInput,
  poolState
);
// queryOutput.amountsOut

The Routers queryRemoveLiquidityUnbalanced function is used to find the amount of pool tokens that would be received, amountsOut.

Build the call with permit and slippage

The PermitHelper abstracts away the complexity involved with creating a permit signature

const permit = await PermitHelper.signRemoveLiquidityApproval({
  ...queryOutput,
  slippage,
  client: walletClient.extend(publicActions),
  owner: walletClient.account,
});

Then buildCallWithPermit uses the amountsOut and the user defined slippage to calculate the minAmountsOut:

const call = removeLiquidity.buildCallWithPermit(
  { ...queryOutput, slippage },
  permit2
);

In the full example above, we defined our slippage as Slippage.fromPercentage('1'), meaning that we if we do not receive at least 99% of our expected amountsOut, the transaction should revert. Internally, the SDK subtracts 1% from the query output, as shown in Slippage.applyTo below:

/**
 * Applies slippage to an amount in a given direction
 *
 * @param amount amount to apply slippage to
 * @param direction +1 adds the slippage to the amount, and -1 will remove the slippage from the amount
 * @returns
 */
public applyTo(amount: bigint, direction: 1 | -1 = 1): bigint {
    return MathSol.mulDownFixed(
        amount,
        BigInt(direction) * this.amount + WAD,
    );
}

Send the call

The output of the buildCallWithPermit function provides all that is needed to submit the removeLiquidity transaction:

  • to - the address of the Router
  • callData - the encoded call data
  • value - the native asset value to be sent

It also returns the minAmountsOut amounts which can be useful to display/validation purposes before the transaction is sent.

const hash = await walletClient.sendTransaction({
  account: walletClient.account,
  data: call.callData,
  to: call.to,
  value: call.value,
});