mirror of
https://github.com/mito-systems/ranger-app.git
synced 2026-04-28 23:43:48 +00:00
polka:
This commit is contained in:
parent
323fdc000c
commit
5429c8ee32
@ -22,7 +22,8 @@ const NETWORKS = {
|
||||
testnet: {
|
||||
chainId: 'atlantic-1',
|
||||
rpcUrl: 'https://rpc.wallet.atlantic-2.sei.io',
|
||||
restUrl: 'https://rest.atlantic-2.sei.io',
|
||||
// Updated REST URL to use a more reliable endpoint
|
||||
restUrl: 'https://sei-testnet-api.polkachu.com',
|
||||
// Smart contract addresses
|
||||
tokenFactoryAddress: process.env.NEXT_PUBLIC_SEI_TOKEN_FACTORY_ADDRESS || '',
|
||||
},
|
||||
@ -412,59 +413,73 @@ export const getTokenBalance = async (): Promise<number> => {
|
||||
// The REST endpoint for Sei testnet
|
||||
const restEndpoint = NETWORKS.testnet.restUrl;
|
||||
|
||||
// Create a proper CW20 balance query
|
||||
// Create a proper CW20 balance query for the token contract
|
||||
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}`;
|
||||
// Construct the REST API URL for the contract query
|
||||
const queryUrl = `${restEndpoint}/cosmwasm/wasm/v1/contract/${tokenContractAddress}/smart/query`;
|
||||
|
||||
console.log(`Querying token balance at: ${queryUrl}`);
|
||||
console.log(`Query data: ${JSON.stringify(balanceQuery)}`);
|
||||
|
||||
// Make the query
|
||||
const response = await fetch(queryUrl);
|
||||
// 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))
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`RPC query failed with status: ${response.status}`);
|
||||
const errorText = await response.text();
|
||||
console.error(`API request failed with status ${response.status}:`, errorText);
|
||||
throw new Error(`Query failed with status: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
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') {
|
||||
// Extract the balance from the response
|
||||
// The actual response structure for CW20 query may vary by 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 (typeof data.data === 'string') {
|
||||
// Sometimes the data itself is the balance
|
||||
} else if (data.data && typeof data.data === 'string') {
|
||||
balanceStr = data.data;
|
||||
} else if (data.data.result && typeof data.data.result.balance === 'string') {
|
||||
// Some APIs nest it under result
|
||||
} 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') {
|
||||
// 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;
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
|
||||
console.error('Unexpected balance response format:', data);
|
||||
throw new Error('Unable to retrieve token balance from blockchain');
|
||||
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');
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user