The following is my code. I previously was able to use this code to get 3 high roller bets (bets that are more than 5000 USD) to show up in my dashboard. Now, when I check my console in Chrome, it says "Retrieved 0 bets from API". This error still occurs even if I have taken a break for a couple hours from calling the API.
// In stakeApi.ts
const API_URL = "https://stake.com/_api/graphql";
// 1. Fetch bets from Stake API
export const fetchHighRollerBets = async (limit: number = 100) => {
console.log("Fetching bets from Stake API...");
const response = await axios.post(
API_URL,
{
operationName: "AllSportBets",
variables: { limit },
query: `
query AllSportBets($limit: Int!) {
allSportBets(limit: $limit) {
iid
bet {
... on SportBet {
active
amount
currency
status
outcomes {
odds
fixture {
name
tournament {
name
}
}
}
}
}
}
}
`
},
{
headers: STAKE_API_HEADERS
}
);
const bets = response.data.data.allSportBets;
console.log(`Retrieved ${bets.length} bets from API`);
return bets;
};
// 2. Save high roller bets to database
export const saveHighRollerBet = async (bet: Bet) => {
const usdAmount = convertToUSD(bet.bet?.amount || 0, bet.bet?.currency || 'usd');
console.log(`Attempting to save bet ${bet.iid} to database...`);
try {
const { error } = await supabase
.from('high_roller_bets')
.upsert({
bet_id: bet.iid,
amount: usdAmount,
currency: bet.bet?.currency,
status: bet.bet?.status,
bet_data: bet as unknown as Json
}, {
onConflict: 'bet_id'
});
if (error) throw error;
console.log(`Successfully saved bet ${bet.iid} to database`);
} catch (error) {
console.error('Failed to save bet to database:', error);
throw error;
}
};
// 3. Fetch stored bets from database
export const fetchStoredHighRollerBets = async () => {
console.log("Fetching stored bets from database...");
const { data, error } = await supabase
.from('high_roller_bets')
.select('bet_data')
.order('created_at', { ascending: false });
if (error) throw error;
const bets = data?.map(row => row.bet_data as unknown as Bet) || [];
console.log(`Retrieved ${bets.length} bets from database`);
return bets;
};
// In Index.tsx
const MIN_USD_AMOUNT = 5000;
const POLLING_INTERVAL = 5000; // 5 seconds in milliseconds
const fetchAndProcessBets = async () => {
try {
// Fetch both new and stored bets
const [newBets, storedBets] = await Promise.all([
fetchHighRollerBets(),
fetchStoredHighRollerBets()
]);
// Process and save high roller bets
for (const bet of newBets) {
const usdAmount = convertToUSD(bet.bet?.amount || 0, bet.bet?.currency || 'usd');
if (usdAmount >= MIN_USD_AMOUNT) {
await saveHighRollerBet(bet);
}
}
// Combine and filter bets
const allBets = [...newBets, ...storedBets];
const uniqueBets = Array.from(new Map(allBets.map(bet => [bet.iid, bet])).values());
const filteredBets = uniqueBets.filter(bet => {
const usdAmount = convertToUSD(bet.bet?.amount || 0, bet.bet?.currency || 'usd');
const status = bet.bet?.status?.toLowerCase();
return (status === 'confirmed' || status === 'confirmedpending') && usdAmount >= MIN_USD_AMOUNT;
});
setBets(filteredBets);
} catch (error) {
console.error("Error fetching bets:", error);
toast({
title: "Error",
description: "Failed to fetch bets",
variant: "destructive",
});
}
};
useEffect(() => {
fetchAndProcessBets();
const interval = setInterval(() => {
console.log("Fetching new bets...");
fetchAndProcessBets();
}, POLLING_INTERVAL);
return () => clearInterval(interval);
}, []);