Skip to main content

Adding Liquidity

This method allows users to add liquidity to the system by providing a specified amount of tokens (e.g., USDT). Before proceeding with the liquidity addition, users must ensure that the amount is approved for use within the system and that the decimal precision of the tokens is properly handled.

To add liquidity, users need to follow these steps:

The Adding Liquidity method enables users to provide tokens (e.g., USDT) as liquidity to the system. Before proceeding, users must follow these steps:

  1. Check Current Approval: Use the getApprovalAmount method to check if the current approved amount is sufficient. If it is, there's no need to call approveAmount again.
  2. Approve Amount: If the approved amount is insufficient, use the approveAmount method to approve the necessary token amount for liquidity addition.
  3. Get Decimal Precision: Use the getDecimalPrecise method to retrieve the token's decimal precision to ensure that the liquidity amount is formatted correctly according to the token's requirements.
  4. Check Deposit Limits: Use the getMaxDeposit and getMinDeposit methods to ensure the liquidity amount falls within the permissible deposit range.
  5. Check Pool Capacity: Use the getMaxPoolCap method to verify that the pool has enough capacity to accommodate the liquidity addition.
  6. Check Reserve: Use the getReserve method to ensure the current reserve allows for the liquidity addition.
  7. Check token balance: Additionaly you can aslo check users wallet token balance using getTokenBalance.
  8. Pass WalletClient instance: Refer to the Pass WalletClient Instance for selecting the appropriate walletClient instance 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 addLiquidityParams = {
amount: 1000,
walletClient: walletClient, // Refer to Pass WalletClient Instance section for appropriate usage
}; // for 1000 USDT

const addLiquidityResponse = await kohin.addLiquidity(addLiquidityParams);
console.log("Add Liquidity Response:", addLiquidityResponse);

Return Value

Returns a Promise resolving to an object with the following structure:

{
success: boolean; // Indicates whether the liquidity addition was successful
error?: string; // Error message (if the call fails)
transactionData?: any; // Data related to the liquidity addition transaction, if applicable
transactionHash?: string; // Transaction hash for the addition, if successful
}

Error Handling

Handle potential errors gracefully by validating the response or using a try-catch block:

try {
const res = await kohin.addLiquidity({
amount: 60,
walletClient: walletClient,
});
if (res.success) {
if (res.transactionData.status === "success") {
if (res.transactionHash) {
onLiquidityTrigger(res.transactionHash);
}
toastify("Adding Liquidity transaction Successfully!", "autoSuccess");
setIsOpenLoader(false);
} else {
if (res.transactionHash) {
onLiquidityTrigger(res.transactionHash, true);
toastLink(
"Adding 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, "Adding 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 funds to add liquidity."
}