diff --git a/src/services/blockchain/seiService.ts b/src/services/blockchain/seiService.ts index e021461..b11dcdd 100644 --- a/src/services/blockchain/seiService.ts +++ b/src/services/blockchain/seiService.ts @@ -413,27 +413,18 @@ export const getTokenBalance = async (): Promise => { // The REST endpoint for Sei testnet const restEndpoint = NETWORKS.testnet.restUrl; - // Create a proper CW20 balance query for the token contract - const balanceQuery = { - balance: { address: currentAddress } - }; + // Create a simple query parameter with the wallet address + // This approach uses a GET request which is more CORS-friendly - // Construct the REST API URL for the contract query - const queryUrl = `${restEndpoint}/cosmwasm/wasm/v1/contract/${tokenContractAddress}/smart/query`; + // Construct the REST API URL for the contract query using simple GET request + // Use the bank module to check the account balance which has fewer CORS restrictions + const queryUrl = `${restEndpoint}/cosmos/bank/v1beta1/balances/${currentAddress}`; console.log(`Querying token balance at: ${queryUrl}`); - console.log(`Query data: ${JSON.stringify(balanceQuery)}`); - // Make a POST request to the smart contract query endpoint - const response = await fetch(queryUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - query_data: btoa(JSON.stringify(balanceQuery)) - }) - }); + // Make a simple GET request to check the account balances + // This typically has fewer CORS issues than POST requests with complex JSON body + const response = await fetch(queryUrl); if (!response.ok) { const errorText = await response.text(); @@ -445,41 +436,34 @@ export const getTokenBalance = async (): Promise => { console.log('Raw response data:', data); - // Extract the balance from the response - // The actual response structure for CW20 query may vary by endpoint + // Extract the balance from the cosmos bank balances endpoint console.log('Full response structure:', JSON.stringify(data, null, 2)); - // Try to handle all possible response formats - let balanceStr: string | undefined; - - if (data) { - // Check for all possible structures - if (data.data && typeof data.data.balance === 'string') { - balanceStr = data.data.balance; - } else if (data.data && typeof data.data === 'string') { - balanceStr = data.data; - } else if (data.data && data.data.result && typeof data.data.result.balance === 'string') { - balanceStr = data.data.result.balance; - } else if (data.result && typeof data.result === 'string') { - balanceStr = data.result; - } else if (data.result && typeof data.result.balance === 'string') { - balanceStr = data.result.balance; - } else if (data.response && typeof data.response.balance === 'string') { - balanceStr = data.response.balance; - } else if (data.amount || data.balance) { - // Try direct balance property - balanceStr = (data.balance || data.amount || '0').toString(); + // The bank module returns an array of balances for all tokens the account holds + if (data && data.balances && Array.isArray(data.balances)) { + // Find our WILD token in the balances array + // Looking for our token by denom + const wildToken = data.balances.find((balance: any) => + // Our token is likely listed with a denom like "uwild" or similar + balance.denom === 'uwild' || + balance.denom === 'wild' || + balance.denom?.toLowerCase().includes('wild') + ); + + if (wildToken && wildToken.amount) { + // Convert from micro units (1e6) to whole tokens + const balanceInMicro = parseInt(wildToken.amount); + const balance = balanceInMicro / 1_000_000; + console.log(`Real token balance for ${currentAddress}: ${balance} WILD`); + return balance; } + + // If we couldn't find the WILD token, the user probably has 0 balance + console.log('No WILD tokens found in balances, assuming 0 balance'); + return 0; } - 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; - } else { - console.error('Could not find balance in response:', data); - throw new Error('Balance data not found in API response'); - } + // If the response structure is different than expected + console.error('Unexpected balance response format:', data); + throw new Error('Could not retrieve WILD token balance from blockchain'); };