From 323fdc000cb2f661d19ef9d234fbbc9ce311ca47 Mon Sep 17 00:00:00 2001 From: zramsay Date: Tue, 25 Mar 2025 14:58:02 -0400 Subject: [PATCH] if u say so --- src/services/blockchain/seiService.ts | 63 +++++++++++++++++++-------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/services/blockchain/seiService.ts b/src/services/blockchain/seiService.ts index f1f3eb4..98d58db 100644 --- a/src/services/blockchain/seiService.ts +++ b/src/services/blockchain/seiService.ts @@ -409,16 +409,20 @@ export const getTokenBalance = async (): Promise => { // The WILD token contract address (hardcoded for safety) const tokenContractAddress = 'sei1ch4hx7thp8dztx4emwr2x0npx4s4sf9fgx2azhawunssm5xz9ksq35uzlu'; - // The RPC endpoint for Sei testnet - const rpcEndpoint = NETWORKS.testnet.rpcUrl; + // The REST endpoint for Sei testnet + const restEndpoint = NETWORKS.testnet.restUrl; - // Construct the query for a CW20 balance - // This uses a direct REST query to avoid CosmWasm client issues - const queryUrl = `${rpcEndpoint}/cosmwasm/wasm/v1/contract/${tokenContractAddress}/smart/${encodeURIComponent( - JSON.stringify({ - balance: { address: currentAddress } - }) - )}`; + // Create a proper CW20 balance query + const balanceQuery = { + balance: { address: currentAddress } + }; + + // Base64 encode the query - ensure it works in browser (we already checked we're in browser above) + const queryJson = JSON.stringify(balanceQuery); + const base64Query = btoa(queryJson); + + // Construct the REST API URL + const queryUrl = `${restEndpoint}/cosmwasm/wasm/v1/contract/${tokenContractAddress}/smart/${base64Query}`; console.log(`Querying token balance at: ${queryUrl}`); @@ -431,15 +435,36 @@ export const getTokenBalance = async (): Promise => { const data = await response.json(); - // Extract the balance from the response - if (data && data.data && typeof data.data.balance === 'string') { - // Convert from micro units (1e6) to whole tokens - const balanceInMicro = parseInt(data.data.balance); - const balance = balanceInMicro / 1_000_000; - console.log(`Real token balance for ${currentAddress}: ${balance} WILD`); - return balance; - } else { - console.error('Unexpected balance response format:', data); - throw new Error('Unable to retrieve token balance from blockchain'); + console.log('Raw response data:', data); + + // Extract the balance from the response - Sei REST API returns different format + if (data && data.data) { + // Try to extract balance from the response + let balanceStr: string | undefined; + + // Handle different potential response formats + if (typeof data.data.balance === 'string') { + balanceStr = data.data.balance; + } else if (typeof data.data === 'string') { + // Sometimes the data itself is the balance + balanceStr = data.data; + } else if (data.data.result && typeof data.data.result.balance === 'string') { + // Some APIs nest it under result + balanceStr = data.data.result.balance; + } else if (data.result && typeof data.result === 'string') { + // Direct result format + balanceStr = data.result; + } + + if (balanceStr) { + // Convert from micro units (1e6) to whole tokens + const balanceInMicro = parseInt(balanceStr); + const balance = balanceInMicro / 1_000_000; + console.log(`Real token balance for ${currentAddress}: ${balance} WILD`); + return balance; + } } + + console.error('Unexpected balance response format:', data); + throw new Error('Unable to retrieve token balance from blockchain'); };