Removing Liquidity
This method allows users to remove a specified percentage of liquidity from a particular deposit. Before removing liquidity, several checks are required to ensure that the process is carried out under the right conditions.
To remove liquidity, users must complete the following steps:
- Check if Early Withdrawal is Enabled: Use the
isEarlyWithdrawalEnabledmethod to verify if early withdrawal is allowed for the specific deposit. Early withdrawal may be restricted under certain conditions. - Check Withdrawal Timing: Use the
getWithdrawAftermethod to check when the liquidity can be withdrawn after the deposit was made. This helps to ensure that liquidity is withdrawn at an appropriate time. - Early Withdrawal Fee: If early withdrawal is enabled, use the
earlyWithdrawalFeemethod to calculate any applicable fees that may apply for removing liquidity before the allowed withdrawal time. - Check Balance of Deposit: Use the
getBalanceOfDepositmethod to check the current balance of the deposit to ensure that the liquidity to be removed is available. - Pass WalletClient instance: Refer to the
Pass WalletClient Instancefor selecting the appropriatewalletClientinstance based on authentication.
Once these steps are completed and the conditions are met, the user can proceed to remove the specified percentage of liquidity from the deposit.
Usage
import kohin from "./kohinInstance";
const removeLiquidityParams = {
depositId: 1099511627894, // The ID of the deposit to remove liquidity from
percent: 16, // Percentage of liquidity to remove (value between 0 and 100)
walletClient: walletClient, // Refer to Pass WalletClient Instance section for appropriate usage
};
const removeLiquidityResponse = await kohin.removeLiquidity(
removeLiquidityParams
);
console.log("Remove Liquidity Response:", removeLiquidityResponse);
Parameters
depositId(number): The unique identifier of the deposit from which liquidity will be removed.percent(number): The percentage of liquidity to be removed (must be between0and100).
Return Value
Returns a Promise resolving to an object with the following structure:
{
success: boolean; // Indicates if the operation was successful
error?: string; // Error message if the operation failed
transactionData?: any; // Transaction details if the operation succeeded
transactionHash?: string; // Transaction hash for the removal
}
Error Handling
Below is a robust error-handling implementation for the removeLiquidity method:
try {
const res = await kohin.removeLiquidity(removeLiquidityParams);
if (res.success) {
if (res.transactionData.status === "success") {
if (res.transactionHash) {
onLiquidityTrigger(res.transactionHash);
}
toastify("Removing Liquidity transaction Successfully!", "autoSuccess");
setIsOpenLoader(false);
} else {
if (res.transactionHash) {
onLiquidityTrigger(res.transactionHash, true);
toastLink(
"Removing Liquidity transaction failed!",
res.transactionHash,
"error"
);
}
setIsOpenLoader(false);
}
} else {
if (res.error) {
toastify(res.error, "autoError");
setIsOpenLoader(false);
return;
}
}
} catch (error: any) {
console.error(error);
errorHandle(error, "Removing Liquidity transaction failed!");
setIsOpenLoader(false);
return;
}
Example Response
Success Response:
{
"success": true,
"transactionData": {
"blockHash": "0x8e59f1e5659326b7b05200b171153dd96c16407ce4b29c31b60eac568a0b95f6",
"blockNumber": 16647288,
"contractAddress": null,
"cumulativeGasUsed": 756062,
"effectiveGasPrice": 99680306396,
"from": "0x670eebd1efdf8d576e1fe554f3d8e905f28cc5a9",
"gasUsed": 756062,
"logs": [
/* log data */
],
"logsBloom": "0x00000000000000000080000000020000000000000000000000200800000000000000008000000000000040000000008000048000000000000000000000200000000010000000000080000008000000800000000000000000000100000000020000000000020000000000000000000800000000000000000880000010000000000000000000000000000000000000001000008800000000000000000000000000221008020000000000000000000000000000000000001000000000000000004010000022000040000001000010000000000000004000000000100000000020000011000000000000000000000800000000000000000000000000008020102000",
"status": "success",
"to": "0x34a07094a4e49a1cc745438ba5aa033f2579daae",
"transactionHash": "0xff16dc02da777d04f8a9bc40f1037910b094f92779703b03824d3dc7f51e3b56",
"transactionIndex": 0,
"type": "eip1559"
},
"transactionHash": "0xff16dc02da777d04f8a9bc40f1037910b094f92779703b03824d3dc7f51e3b56"
}
Error Response:
{
"success": false,
"error": "Insufficient liquidity for the requested percentage."
}