mirror of
https://github.com/mito-systems/ranger-app.git
synced 2026-03-04 04:44:07 +00:00
if u say so
This commit is contained in:
parent
fc7b4c94d0
commit
323fdc000c
@ -409,16 +409,20 @@ export const getTokenBalance = async (): Promise<number> => {
|
||||
// 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<number> => {
|
||||
|
||||
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');
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user