Buying Insurance Cover
The Buying Insurance Cover
method allows users to purchase an insurance cover for a bet by specifying the bet details, cover premium, and slippage percentage. Before proceeding, users need to follow these steps:
- Check if a cover is already booked: Use the
hasCoverBooked
method to confirm that a cover is not already booked for the bet. - Verify betting limits: Use the
getBetLimits
method to fetch the minimum and maximum bet amounts, as well as the odds range (MinOdds and MaxOdds) for the bet. - Calculate cover premium: Use the
calculatePremium
method to calculate the premium for the cover based on the bet details and slippage percentage. - Check current approval: Use the
getApprovalAmount
method to check if the approved amount is sufficient for the cover. If the approved amount is less than the required cover premium, callapproveAmount
to approve the necessary amount (within a 5% tolerance of the calculated premium). - Pass walletClient instance: Refer to the
Pass WalletClient Instance
for selecting the appropriate walletClient instance based on authentication method. - Check token balance: Additionaly you can aslo check users wallet token balance using
getTokenBalance
.
Once these steps are completed, the user can proceed to following points for the insurance cover for the bet.
Usage
import kohin from "./kohinInstance";
const buyCoverParams = {
betId: 219, // The ID of the bet to insure
betType: "Combo", // Type of bet: "Combo" for express bet, "Single" for ordinar bet
coverPremium: 53982374n, // The premium amount for the insurance cover (bigint format)
slippagePercent: 5, // Slippage percentage (between 1 and 100)
betAmount: 1000, // The total amount placed on the bet
walletClient: walletClient, // Refer to Pass WalletClient Instance section for appropriate usage
};
const buyCoverResponse = await kohin.buyCover(buyCoverParams);
console.log("Insurance Cover Response:", buyCoverResponse);
Return Value
Returns a Promise
resolving to an object with the following structure:
{
success: boolean; // Indicates whether the API call was successful
error?: string; // Error message (if the call fails)
transactionData?: any; // Data related to the transaction, if applicable
transactionHash?: string; // Transaction hash for the purchase, if successful
}
Error Handling
Handle potential errors gracefully by validating the response or using a try-catch block:
try {
const res = await kohin.buyCover(buyCoverParams);
if (res.success) {
if (res.transactionData.status === "success") {
if (res.transactionHash) {
onLiquidityTrigger(res.transactionHash);
}
toastify(
"Buying Insurance Cover transaction Successfully!",
"autoSuccess"
);
setIsOpenLoader(false);
} else {
if (res.transactionHash) {
onLiquidityTrigger(res.transactionHash, true);
toastLink(
"Buying Insurance Cover 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, "Buying Insurance Cover 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": "0x1234567890abcdef"
}
Error Response:
{
"success": false,
"error": "Insufficient funds for cover premium."
}