Skip to main content

Withdraw Liquidity

Overview

This documentation will guide you through the process of withdrawing liquidity on the Chromatic Protocol.

Prerequisites

Before removing liquidity, ensure that you have completed the following steps:

  • Set up the Chromatic Protocol SDK by following the installation instructions.
  • Provide liquidity in the market using the SDK, as it is a prerequisite for removing liquidity. Refer to the "Provide Liquidity" section for detailed information.

Remove Liquidity

Removing liquidity in the Chromatic Protocol involves two stages: "Remove Liquidity" and "Withdraw Liquidity".

In the "Remove Liquidity" stage, you initiate the process of removing liquidity from the market.

To remove liquidity from a market, use the removeLiquidity method of the ChromaticRouter instance:

  • feeRate: The fee rate of the Bin from which liquidity is being removed.
  • clbTokenAmount: The amount of CLB tokens to remove.
import { ethers } from 'ethers'
import { Client } from '@chromatic-protocol/sdk-ethers-v5'

const marketAddress = '0x...'
await client
.router()
.removeLiquidity(marketAddress, { feeRate: 100, clbTokenAmount: ethers.utils.parseEther('10') })

To remove liquidity from multiple Bins at once, you can use the removeLiquidities method of the ChromaticRouter instance:

const marketAddress = '0x...'
await client.router().removeLiquidities(marketAddress, [
{ feeRate: 100, clbTokenAmount: ethers.utils.parseEther('5') },
{ feeRate: 200, clbTokenAmount: ethers.utils.parseEther('5') }
])

Both the removeLiquidity method and removeLiquidities method are asynchronous and return Promises, so you need to use await to wait for the removing liquidity process to complete.

Waiting for Oracle Update

After removing liquidity, you need to wait for the next oracle version to update the price feed before you can proceed with the withdrawal. This ensures that the liquidity values are correctly updated based on the latest market conditions.

Retrieving Burnable Liquidity

Once the transaction is completed and when the oracle price has been updated, you can retrieve the amount of burnable liquidity using the claimableLiquidities method of ChromaticLens:

const lpReceipts = await client.lens().lpReceipts(marketAddress);
const targetReceiptIndex = ...; // removeLiquidity receipt index
const tradingFeeRate = lpReceipts[targetReceiptIndex].tradingFeeRate;
const oracleVersion = lpReceipts[targetReceiptIndex].oracleVersion;
const claimableLiquidities = await client.lens().claimableLiquidities(marketAddress, [{ tradingFeeRate, oracleVersion }]);

The claimableLiquidities method is asynchronous and returns a Promise, so you need to use await to retrieve the amount of burnable liquidity.

Withdraw Liquidity

Once you have confirmed that the amount of burnable liquidity is sufficient, you can proceed to withdraw the liquidity by utilizing the withdrawLiquidity method provided by the ChromaticRouter instance.

const marketAddress = '0x...'
const lpReceiptId = 1 // Receipt ID of the removeLiquidity transaction
await client.router().withdrawLiquidity(marketAddress, lpReceiptId)

To withdraw liquidity from multiple receipts at once, you can use the withdrawLiquidities method of the ChromaticRouter instance:

const marketAddress = '0x...'
const lpReceiptIds = [1, 2]
await client.router().withdrawLiquidities(marketAddress, lpReceiptIds)

The withdrawLiquidity method is asynchronous and returns a Promise, so you need to use await to wait for the withdraw liquidity process to complete.

Retrieving Liquidity

Once the transaction is completed, you can retrieve your own liquidity using the ownedLiquidityBins method of ChromaticLens:

const marketAddress = '0x...'
const signerAddress = '0x...'
const myBins = await client.lens().ownedLiquidityBins(marketAddress, signerAddress)

The ownedLiquidityBins method is also asynchronous and returns a Promise that resolves to your own liquidity.

Additionally, you can utilize the balanceOf method of the ERC20 contract to check the balance changes of the settlement token. This allows you to verify the changes in the balance of the settlement token before and after the liquidity withdrawal.