getActiveBetData
The getActiveBetData
method is a utility for retrieving active bet information associated with a specified bettor. It simplifies the process of fetching data by encapsulating API logic, handling request and response management internally, and providing a structured output.
This method is particularly useful in applications that require real-time or historical insights into a user's active bets. It ensures robust error handling, making it reliable for various use cases, from displaying active bets in dashboards to processing bettor analytics.
Usage
import kohin from "./kohinInstance";
const activeBetParams = {
bettorAddress: "0x123...", // Address of the bettor
pageCount: 1, // Number of pages to fetch
};
const activeBets = await kohin.getActiveBetData(activeBetParams);
console.log("Active Bets:", activeBets);
Parameters
Parameter | Type | Description |
---|---|---|
bettorAddress | string | The wallet address of the bettor for whom the data is to be fetched. |
pageCount | number | The number of pages to fetch. Useful for paginated results. |
Return Value
Returns a Promise
resolving to an object with the following structure:
{
success: boolean; // Indicates whether the call was successful
data: Array<{ // Array of active bet objects
id: string;
amount: string;
bettor: string;
betId: string;
odds: string;
status: BetStatus;
type: BetType;
potentialPayout: string;
_updatedAt: string;
_wonSubBetsCount: number;
_lostSubBetsCount: number;
_subBetsCount: number;
createdBlockTimestamp: string;
_games: Game[];
_conditions: Condition[];
selections: Selection[];
isEligible: boolean;
maxSubBetsCount: number;
isInsured: boolean;
}>;
error?: string; // Error message if the call fails
}
interface Game {
title: string;
league: League;
startsAt: string;
}
interface League {
name: string;
}
interface Condition {
conditionId: string;
coreAddress: string;
game: Game;
}
interface Selection {
_outcomeId: string;
odds: string;
}
Error Handling
The response contains an error
field when the API call fails. Check for this field and display the error appropriately:
try {
const activeBets = await kohin.getActiveBetData({
bettorAddress: "0x123...",
});
if (!activeBets.success) {
console.error("Error:", activeBets.error);
// Display the error message to the user
toastify(activeBets.error, "error");
} else {
console.log("Active Bets:", activeBets.data);
}
} catch (error) {
console.error("Unexpected error:", error.message);
// Display a fallback error message
toastify("Failed to fetch active bets. Please try again.", "error");
}
Example Response
Success Response:
{
"success": true,
"data": {
"bets": [
{
"id": "0x471dac1052248602fdf05377ef99b5b7b3a769a1_216",
"amount": "100",
"bettor": "0x670eebd1efdf8d576e1fe554f3d8e905f28cc5a9",
"betId": "216",
"odds": "1.729414379158",
"status": "PENDING",
"type": "EXPRESS",
"potentialPayout": "172.941437",
"_updatedAt": "1735798947",
"_wonSubBetsCount": 0,
"_lostSubBetsCount": 0,
"_subBetsCount": 2,
"createdBlockTimestamp": "1735798947",
"_games": [
{
"title": "Fulham FC – Ipswich Town",
"league": {
"name": "Premier League"
},
"startsAt": "1736085600"
},
{
"title": "Liverpool FC – Manchester United",
"league": {
"name": "Premier League"
},
"startsAt": "1736094600"
}
],
"_conditions": [
{
"conditionId": "100610060000000000261347430000000000000172987305",
"coreAddress": "0x87ebffe283be8ded47c3c87451d1b89c8a2c441a",
"game": {
"league": {
"name": "Premier League"
},
"title": "Fulham FC – Ipswich Town",
"startsAt": "1736085600"
}
},
{
"conditionId": "100610060000000000261350860000000000000150794711",
"coreAddress": "0x87ebffe283be8ded47c3c87451d1b89c8a2c441a",
"game": {
"league": {
"name": "Premier League"
},
"title": "Liverpool FC – Manchester United",
"startsAt": "1736094600"
}
}
],
"selections": [
{
"_outcomeId": "16750",
"odds": "1.356857681063"
},
{
"_outcomeId": "29",
"odds": "1.274573157742"
}
],
"isEligible": true,
"maxSubBetsCount": 17,
"isInsured": false
}
]
}
}
Error Response:
{
"success": false,
"error": "Invalid bet ID"
}