getBetsStatusData
The getBetsStatusData
method allows developers to fetch the status of a bettor's bet. It includes detailed information like bet ID, status, potential payouts, and cover status. This method is ideal for tracking and claiming bet covers, checking bet statuses, or monitoring active/resolved bets.
Usage
import kohin from "./kohinInstance";
const myBetData = {
betId: [123, 456], // List of bet IDs to check status
betType: "Combo", // Type of bet: "Combo" for express bet, "Single" for ordinar bet
};
const betStatus = await kohin.getBetsStatusData(myBetData);
console.log("Bet Status:", betStatus);
Parameters
Parameter | Type | Description |
---|---|---|
betId | number[] | List of bet IDs to check the status for. |
betType | string | Bet type to filter (either "Single" for ordinar bet or "Combo" for express bet). |
Return Value
Returns a Promise
resolving to an object with the following structure:
{
success: boolean; // Indicates whether the API call was successful
data?: [ // Bet status data (present if successful)
{
betId: string; // Unique bet identifier.
CoverStatus: string | null; // Current cover status (e.g., "Active", "Claimed") or null if not insured.
coverId: string | null; // ID of the cover associated with the bet or null if not insured.
betType: string | null; // Type of bet (e.g., "Express", "Ordinar", or null if not insured.
transactionHash: string | null; // buy cover transaction hash linked to the bet or null if not insured
isInsured: boolean; // Indicates whether the bet is insured (true or false)
}
];
error?: string; // Error message if the API call fails
}
Error Handling
Handle errors gracefully by checking the success value or using try-catch:
try {
const betStatus = await kohin.getBetsStatusData({
betId: [123, 456],
betType: "Combo",
});
if (!betStatus.success) {
console.error("Error:", betStatus.error);
toastify(betStatus.error, "error");
} else {
console.log("Bet Status:", betStatus.data);
}
} catch (error) {
console.error("Unexpected error:", error.message);
toastify("Failed to fetch bet status. Please try again.", "error");
}
Example Response
Success Response:
{
"success": true,
"data": [
{
"betId": 655,
"CoverStatus": "Active",
"coverId": "68",
"betType": "Ordinar",
"transactionHash": "0xdb043d5a7f49d8058aa8d4977d3dfb8d77fa9d74b2f98702e65804a79f4a0eef",
"isInsured": true
},
{
"betId": 654,
"CoverStatus": null,
"coverId": null,
"betType": false,
"transactionHash": null,
"isInsured": false
},
{
"betId": 642,
"CoverStatus": "Active",
"coverId": "65",
"betType": "Ordinar",
"transactionHash": "0x864a1c682ee61c7b3d499f7280b7f0b69e24e1af4409b21b71e0b6c12216b5e8",
"isInsured": true
},
{
"betId": 641,
"CoverStatus": "Active",
"coverId": "63",
"betType": "Ordinar",
"transactionHash": "0x48040843d47c12f15dcecaa049e1f763c063dfc74122f498c0daf75be5a700b1",
"isInsured": true
}
],
"message": "OK",
"code": 200
}
Error Response:
{
"success": false,
"error": "Bet IDs not found"
}
Example Integration of Claim Functionality in Bet Cards Component
This function fetches the bet statuses for both "Combo" and "Single" bet types and updates the list of bets with claim-related data. The updated data is stored in the state and can be used to enable claim functionality directly within the bet cards component.
Step 1: Fetch Bet Status when Bets Update
Ensures getBetsStatus
runs whenever the bets array (the list of bet objects) changes:
useEffect(() => {
bets?.length > 0 && getBetsStatus();
}, [bets]);
Step 2: Implementing GetBetsStatus Function
Categorizes bets into "Combo" and "Single" based on the number of outcomes, fetches bet statuses using kohin.getBetsStatusData(), and merges the fetched data into the existing bets list:
const getBetsStatus = async () => {
setMyBetLoading(true);
const comboBetIds: number[] = [];
const singleBetIds: number[] = [];
// Categorizing bets into Combo and Single
bets.forEach((bet) => {
if (bet?.outcomes?.length > 1) {
comboBetIds.push(+bet.tokenId);
} else if (bet?.outcomes?.length === 1) {
singleBetIds.push(+bet.tokenId);
}
});
let comboBetDataRes;
let singleBetDataRes;
// Fetching Combo bet status
if (comboBetIds?.length > 0) {
const res = await kohin.getBetsStatusData({
betId: comboBetIds,
betType: "Combo",
});
if (res?.success) {
comboBetDataRes = res.data; // Assuming res.data is of type BetStatusData[]
} else {
setMyBetLoading(false);
console.log("error", res?.error);
}
}
// Fetching Single bet status
if (singleBetIds?.length > 0) {
const res = await kohin.getBetsStatusData({
betId: singleBetIds,
betType: "Single",
});
console.log("res singleBetIds --->", res);
if (res?.success) {
singleBetDataRes = res.data; // Assuming res.data is of type BetStatusData[]
} else {
setMyBetLoading(false);
console.log("error", res?.error);
}
}
// Add bet status data to a_dict, using betId as the key
const a_dict = {};
[comboBetDataRes, singleBetDataRes].forEach((arr) => {
arr?.forEach((item) => {
a_dict[item.betId] = item; // Store the bet status data using betId as the key
});
});
// Merging `bets` array with `a_dict`
const merged_data = bets.map((item) => {
const token_id = item.tokenId;
if (a_dict[token_id]) {
// Create a new merged item
const merged_item: any = { ...item, betId: token_id }; // Use 'any' to bypass TypeScript errors
// Add claimRes from a_dict only if it doesn't already exist in the item
if (!merged_item.hasOwnProperty("claimRes")) {
merged_item["claimRes"] = { ...a_dict[token_id] }; // Add claimRes from a_dict
}
// Clean up unnecessary `betId` from claimRes
delete merged_item.claimRes.betId;
return merged_item;
} else {
return item;
}
});
// Output the merged data
console.log("merged_data", merged_data);
setMyBets(merged_data);
setMyBetLoading(false);
};
Step 3: Claim Bet Functionality in UI
Allows users to claim a bet cover directly from the UI when eligible:
const coverStatus = bet?.claimRes?.CoverStatus;
const coverId = Number(bet?.claimRes?.coverId);
const onHandleSettleClaim = async () => {
setClaimLoading(true);
const res = await kohin.settleClaim({
coverId: coverId,
walletClient: walletClient,
});
setClaimLoading(false);
if (res.success) {
setTab(BetType.Accepted);
openModal("SuccessModal", {
title: `Your bet cover has been claimed successfully`,
text: `Claim details: ${res.data?.claimInfo}`, // Include response data if necessary
});
} else {
openModal("ErrorModal", {
text: res.error || "An error occurred while claiming.",
});
}
};
return (
<>
{(coverStatus === "Claimable" ||
coverStatus === "Active" ||
coverStatus === "Manual") && (
<Button
className="w-full mt-2"
title={"Claim Cover"}
style="secondary"
size={32}
onClick={onHandleSettleClaim}
disabled={coverStatus === "Active" || coverStatus === "Manual"}
loading={claimLoading}
/>
)}
</>
);
Conclusion
This implementation ensures that bet status data is fetched and integrated into the UI dynamically, allowing claim functionality to be displayed within each bet card. It also provides proper handling of success and error responses when claiming a bet cover.