/** * Test bucket queries for specific account */ import { IoTeXSDK, toIoAddress } from './src'; async function main() { console.log('🪣 IoTeX Bucket Query Test'); console.log('================================================================================\n'); const ethAddress = '0x909a6e33d95912c87977f49c48060e1e89ad3c52'; const ioAddress = toIoAddress(ethAddress); console.log(`šŸ“ Test Account:`); console.log(` Ethereum Address: ${ethAddress}`); console.log(` IoTeX Address: ${ioAddress}\n`); const sdk = IoTeXSDK.mainnet(); try { console.log('šŸ“” Connecting to IoTeX mainnet...'); await sdk.connect(); console.log('āœ… Connected!\n'); console.log('šŸ” Fetching voting buckets...\n'); const buckets = await sdk.blockchain.getBucketList({ voterAddress: ioAddress, offset: 0, limit: 100 // Get up to 100 buckets }); console.log(`šŸ“Š Found ${buckets.length} voting bucket(s)\n`); console.log('================================================================================\n'); if (buckets.length === 0) { console.log('No voting buckets found for this account.\n'); } else { let totalStaked = 0; buckets.forEach((bucket, idx) => { console.log(`Bucket ${idx + 1}:`); console.log(` Index: ${bucket.index}`); console.log(` Candidate Address: ${bucket.candidateAddress}`); console.log(` Staked Amount: ${bucket.stakedAmount} IOTX`); console.log(` Staked Duration: ${bucket.stakedDuration} days`); console.log(` Create Time: ${bucket.createTime}`); console.log(` Stake Start Time: ${bucket.stakeStartTime}`); if (bucket.unstakeStartTime) { console.log(` Unstake Start Time: ${bucket.unstakeStartTime}`); } console.log(` Auto Stake: ${bucket.autoStake ? 'Yes' : 'No'}`); console.log(` Owner: ${bucket.owner}`); if (bucket.contractAddress) { console.log(` Contract Address: ${bucket.contractAddress}`); } console.log(''); totalStaked += parseFloat(bucket.stakedAmount); }); console.log('================================================================================'); console.log(`šŸ“ˆ Summary:`); console.log(` Total Buckets: ${buckets.length}`); console.log(` Total Staked: ${totalStaked.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} IOTX`); console.log(` Average per Bucket: ${(totalStaked / buckets.length).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} IOTX`); // Group by delegate const byDelegate = buckets.reduce((acc: any, bucket) => { if (!acc[bucket.candidateAddress]) { acc[bucket.candidateAddress] = { count: 0, total: 0, buckets: [] }; } acc[bucket.candidateAddress].count++; acc[bucket.candidateAddress].total += parseFloat(bucket.stakedAmount); acc[bucket.candidateAddress].buckets.push(bucket.index); return acc; }, {}); console.log('\nšŸ“Š Buckets by Delegate:'); Object.entries(byDelegate).forEach(([delegate, info]: [string, any]) => { console.log(` ${delegate}:`); console.log(` Buckets: ${info.count}`); console.log(` Total Staked: ${info.total.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })} IOTX`); console.log(` Bucket Indices: ${info.buckets.join(', ')}`); }); } } catch (error) { console.error('āŒ Error:', error); } finally { console.log('\nšŸ‘‹ Disconnecting...'); sdk.disconnect(); } } main();