Skip to main content

Calculating Premium

The calculatePremium method calculates the premium for an insurance bet based on the provided parameters. This involves fetching configuration values, validating inputs, and making a blockchain call to compute the premium. The method considers various factors such as bet type, bet amount, and slippage percentage to determine the correct premium.

For an "Express" bet, pass betType as "Combo", while for an "Ordinar" bet, pass betType as "Single". These distinctions allow the method to accurately calculate the premium based on the specific bet type and the corresponding rules.

Usage

import kohin from "./kohinInstance";

const calculatePremiumParams = {
betId: 219,
betType: "Combo",
odds: 2.578721433438,
betAmount: 100,
numLegs: 2,
};

const premiumResponse = await kohin.calculatePremium(calculatePremiumParams);

if (premiumResponse.success) {
console.log("Calculated Premium:", premiumResponse.calculatedPremium);
console.log("Amount:", premiumResponse.amount);
console.log("Odds:", premiumResponse.odds);
} else {
console.error("Error calculating premium:", premiumResponse.error);
}

Parameters

The CalculatePremiumParams interface defines the input parameters for the method:

export interface CalculatePremiumParams {
betId: number; // Unique identifier for the bet
betType: BetTypes; // Type of the bet: "Combo" or "Single"
odds: number; // The odds associated with the bet
betAmount: number; // Amount of money placed on the bet
numLegs: number; // Number of legs (events) in the bet
}

Return Value

The CalculatePremiumResponse interface defines the structure of the returned object:

export interface CalculatePremiumResponse {
success: boolean; // Indicates if the operation was successful
error?: string; // Error message if the operation failed
calculatedPremium?: bigint; // Calculated premium value
amount?: bigint; // Amount used in the calculation
odds?: bigint; // Odds used in the calculation
}

Error Handling

The following code demonstrates how to handle errors while calling calculatePremium:

import { formatUnits } from "viem";

try {
const res = await kohin.calculatePremium(calculatePremiumParams);

if (res.success) {
if (res.calculatedPremium !== undefined) {
const formattedPremium = formatUnits(BigInt(res.calculatedPremium), 6); // Convert to readable format
toastify(
`Premium Calculated Successfully: ${formattedPremium} USDT`,
"autoSuccess"
);
}
} else {
if (res.error) {
// Handle known errors returned by the API
toastify(`Error: ${res.error}`, "autoError");
}
}
} catch (err) {
// Handle unexpected errors during API call
toastify(
"Unexpected error calculating premium. Please try again.",
"autoError"
);
console.error(err); // Log error for debugging
}

Example Response

Success Response:

{
"success": "true",
"calculatedPremium": "53976024n",
"amount": "100000000n",
"odds": "3973337751312n"
}

Error Response:

{
"success": false,
"error": "Invalid betting parameters provided."
}