#!/usr/bin/env ts-node /** * Test script to get delegates with votes and produced blocks */ import { IoTeXSDK } from './src'; async function testDelegates() { console.log('๐Ÿงช Testing Delegate Queries'); console.log('='.repeat(80)); const sdk = IoTeXSDK.mainnet(); try { console.log('\n๐Ÿ“ก Connecting to IoTeX mainnet...'); await sdk.connect(); console.log('โœ… Connected!\n'); // Get current epoch first console.log('๐Ÿ“Š Getting current epoch...'); const currentEpoch = await sdk.node.getCurrentEpoch(); console.log(`Current epoch: ${currentEpoch.num}`); console.log(`Epoch height: ${currentEpoch.height}\n`); // Get delegates console.log('๐Ÿ‘ฅ Getting delegates...'); const delegates = await sdk.node.getDelegates(); console.log(`โœ… Retrieved ${delegates.length} delegates\n`); // Display top 20 delegates console.log('Top 20 Delegates:'); console.log('='.repeat(80)); console.log('Rank | Name | Votes (IOTX) | Blocks | Address'); console.log('-'.repeat(80)); for (let i = 0; i < Math.min(20, delegates.length); i++) { const d = delegates[i]; const rank = d.rank.toString().padStart(4); const name = d.name.padEnd(20); const votes = d.votes.padStart(20); const blocks = d.production.toString().padStart(6); const addr = d.address.slice(0, 20) + '...'; console.log(`${rank} | ${name} | ${votes} | ${blocks} | ${addr}`); } console.log('\n' + '='.repeat(80)); // Summary statistics console.log('\n๐Ÿ“ˆ Summary Statistics:'); const totalVotes = delegates.reduce((sum, d) => sum + parseFloat(d.votes), 0); const totalBlocks = delegates.reduce((sum, d) => sum + d.production, 0); const avgVotes = totalVotes / delegates.length; console.log(`Total Delegates: ${delegates.length}`); console.log(`Total Votes: ${totalVotes.toLocaleString()} IOTX`); console.log(`Average Votes: ${avgVotes.toLocaleString()} IOTX`); console.log(`Total Blocks Produced: ${totalBlocks.toLocaleString()}`); // Test getting all delegates console.log('\n\n๐Ÿ‘ฅ Testing "all delegates" query...'); const allDelegates = await sdk.node.getDelegates({ all: true }); console.log(`โœ… Retrieved ${allDelegates.length} delegates (with all: true)\n`); // Test specific epoch console.log('๐Ÿ“… Testing specific epoch query (epoch 1000)...'); try { const epoch1000Delegates = await sdk.node.getDelegates({ epochNumber: 1000 }); console.log(`โœ… Retrieved ${epoch1000Delegates.length} delegates for epoch 1000\n`); } catch (error) { console.log(`โš ๏ธ Could not get epoch 1000 delegates: ${error}\n`); } // Detailed info for top 3 console.log('๐Ÿ” Detailed Info for Top 3 Delegates:'); console.log('='.repeat(80)); for (let i = 0; i < Math.min(3, delegates.length); i++) { const d = delegates[i]; console.log(`\n${i + 1}. ${d.name}`); console.log(` Rank: ${d.rank}`); console.log(` Address: ${d.address}`); console.log(` Votes: ${parseFloat(d.votes).toLocaleString()} IOTX`); console.log(` Blocks Produced: ${d.production}`); console.log(` Expected Production: ${d.expectedProduction}`); if (d.production > 0 && d.expectedProduction > 0) { const efficiency = (d.production / d.expectedProduction * 100).toFixed(2); console.log(` Production Efficiency: ${efficiency}%`); } } console.log('\n' + '='.repeat(80)); console.log('โœ… All delegate queries completed successfully!'); } catch (error) { console.error('\nโŒ Error occurred:'); console.error(error); throw error; } finally { sdk.disconnect(); console.log('\n๐Ÿ‘‹ Disconnected from IoTeX\n'); } } // Run test if (require.main === module) { testDelegates().catch(error => { console.error('Fatal error:', error); process.exit(1); }); } export { testDelegates };