|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Check Pending Transactions for IPC Faucet |
| 5 | + * |
| 6 | + * Helps diagnose stuck transactions |
| 7 | + */ |
| 8 | + |
| 9 | +import { ethers } from 'ethers' |
| 10 | +import dotenv from 'dotenv' |
| 11 | +import { fileURLToPath } from 'url' |
| 12 | +import { dirname, join } from 'path' |
| 13 | + |
| 14 | +const __filename = fileURLToPath(import.meta.url) |
| 15 | +const __dirname = dirname(__filename) |
| 16 | + |
| 17 | +// Load environment variables from parent directory |
| 18 | +dotenv.config({ path: join(__dirname, '..', '.env') }) |
| 19 | + |
| 20 | +const RPC_URL = process.env.RPC_URL || 'http://node-1.test.ipc.space:8545' |
| 21 | +const PRIVATE_KEY = process.env.PRIVATE_KEY |
| 22 | + |
| 23 | +async function checkPendingTransactions() { |
| 24 | + try { |
| 25 | + if (!PRIVATE_KEY) { |
| 26 | + console.error('❌ Error: PRIVATE_KEY not found in .env file') |
| 27 | + process.exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + console.log('\n🔍 Checking for pending transactions...\n') |
| 31 | + console.log(`RPC: ${RPC_URL}\n`) |
| 32 | + |
| 33 | + const provider = new ethers.JsonRpcProvider(RPC_URL) |
| 34 | + const wallet = new ethers.Wallet(PRIVATE_KEY, provider) |
| 35 | + |
| 36 | + console.log(`Wallet Address: ${wallet.address}\n`) |
| 37 | + |
| 38 | + // Get current nonce from network (includes pending) |
| 39 | + const pendingNonce = await provider.getTransactionCount(wallet.address, 'pending') |
| 40 | + |
| 41 | + // Get confirmed nonce |
| 42 | + const confirmedNonce = await provider.getTransactionCount(wallet.address, 'latest') |
| 43 | + |
| 44 | + // Get balance |
| 45 | + const balance = await provider.getBalance(wallet.address) |
| 46 | + const balanceFIL = ethers.formatEther(balance) |
| 47 | + |
| 48 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 49 | + console.log('📊 Wallet Status') |
| 50 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 51 | + console.log(`Balance: ${balanceFIL} tFIL`) |
| 52 | + console.log(`Confirmed Nonce: ${confirmedNonce}`) |
| 53 | + console.log(`Pending Nonce: ${pendingNonce}`) |
| 54 | + console.log(`Stuck Transactions: ${pendingNonce - confirmedNonce}`) |
| 55 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n') |
| 56 | + |
| 57 | + if (pendingNonce === confirmedNonce) { |
| 58 | + console.log('✅ No pending transactions!\n') |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + console.log('⚠️ Pending transactions detected!\n') |
| 63 | + console.log('Checking transaction details...\n') |
| 64 | + |
| 65 | + // Try to get pending transactions |
| 66 | + try { |
| 67 | + // Note: Not all RPC endpoints support this method |
| 68 | + const pendingBlock = await provider.send('eth_getBlockByNumber', ['pending', true]) |
| 69 | + |
| 70 | + if (pendingBlock && pendingBlock.transactions) { |
| 71 | + const myPendingTxs = pendingBlock.transactions.filter( |
| 72 | + tx => tx.from && tx.from.toLowerCase() === wallet.address.toLowerCase() |
| 73 | + ) |
| 74 | + |
| 75 | + if (myPendingTxs.length > 0) { |
| 76 | + console.log(`Found ${myPendingTxs.length} pending transaction(s):\n`) |
| 77 | + |
| 78 | + myPendingTxs.forEach((tx, index) => { |
| 79 | + console.log(`Transaction ${index + 1}:`) |
| 80 | + console.log(` Hash: ${tx.hash}`) |
| 81 | + console.log(` To: ${tx.to}`) |
| 82 | + console.log(` Value: ${ethers.formatEther(tx.value)} tFIL`) |
| 83 | + console.log(` Nonce: ${parseInt(tx.nonce)}`) |
| 84 | + console.log(` Gas Price: ${tx.gasPrice ? ethers.formatUnits(tx.gasPrice, 'gwei') : 'N/A'} Gwei`) |
| 85 | + console.log('') |
| 86 | + }) |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + console.log('ℹ️ Could not fetch pending transaction details (RPC may not support this)\n') |
| 91 | + } |
| 92 | + |
| 93 | + // Check recent confirmed transactions |
| 94 | + console.log('📜 Recent confirmed transactions:\n') |
| 95 | + |
| 96 | + try { |
| 97 | + const latestBlock = await provider.getBlockNumber() |
| 98 | + const fromBlock = Math.max(0, latestBlock - 20) // Check last 20 blocks |
| 99 | + |
| 100 | + let foundTxs = 0 |
| 101 | + for (let i = latestBlock; i >= fromBlock && foundTxs < 5; i--) { |
| 102 | + const block = await provider.getBlock(i, true) |
| 103 | + if (block && block.transactions) { |
| 104 | + for (const tx of block.transactions) { |
| 105 | + if (tx.from && tx.from.toLowerCase() === wallet.address.toLowerCase()) { |
| 106 | + const receipt = await provider.getTransactionReceipt(tx.hash) |
| 107 | + console.log(`Block ${i}:`) |
| 108 | + console.log(` Hash: ${tx.hash}`) |
| 109 | + console.log(` To: ${tx.to}`) |
| 110 | + console.log(` Value: ${ethers.formatEther(tx.value || 0)} tFIL`) |
| 111 | + console.log(` Nonce: ${parseInt(tx.nonce)}`) |
| 112 | + console.log(` Status: ${receipt.status === 1 ? '✅ Success' : '❌ Failed'}`) |
| 113 | + console.log('') |
| 114 | + foundTxs++ |
| 115 | + if (foundTxs >= 5) break |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (foundTxs === 0) { |
| 122 | + console.log(' No recent transactions found\n') |
| 123 | + } |
| 124 | + } catch (error) { |
| 125 | + console.log(' Could not fetch recent transactions\n') |
| 126 | + } |
| 127 | + |
| 128 | + // Provide solutions |
| 129 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━') |
| 130 | + console.log('💡 Solutions to Clear Stuck Transactions') |
| 131 | + console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n') |
| 132 | + |
| 133 | + console.log('Option 1: Wait for transactions to be mined') |
| 134 | + console.log(' - Transactions may just need more time\n') |
| 135 | + |
| 136 | + console.log('Option 2: Speed up with higher gas (if RPC supports)') |
| 137 | + console.log(' - Use node scripts/speed-up-tx.js\n') |
| 138 | + |
| 139 | + console.log('Option 3: Cancel stuck transactions') |
| 140 | + console.log(' - Send 0 value tx to yourself with same nonce') |
| 141 | + console.log(' - Use node scripts/cancel-tx.js <nonce>\n') |
| 142 | + |
| 143 | + console.log('Option 4: Check gas price settings') |
| 144 | + console.log(' - Ensure faucet is using adequate gas price') |
| 145 | + console.log(' - Check network congestion\n') |
| 146 | + |
| 147 | + // Get network gas info |
| 148 | + try { |
| 149 | + const feeData = await provider.getFeeData() |
| 150 | + console.log('Current Network Gas Prices:') |
| 151 | + if (feeData.gasPrice) { |
| 152 | + console.log(` Gas Price: ${ethers.formatUnits(feeData.gasPrice, 'gwei')} Gwei`) |
| 153 | + } |
| 154 | + if (feeData.maxFeePerGas) { |
| 155 | + console.log(` Max Fee: ${ethers.formatUnits(feeData.maxFeePerGas, 'gwei')} Gwei`) |
| 156 | + } |
| 157 | + if (feeData.maxPriorityFeePerGas) { |
| 158 | + console.log(` Max Priority Fee: ${ethers.formatUnits(feeData.maxPriorityFeePerGas, 'gwei')} Gwei`) |
| 159 | + } |
| 160 | + console.log('') |
| 161 | + } catch (error) { |
| 162 | + console.log(' Could not fetch gas prices\n') |
| 163 | + } |
| 164 | + |
| 165 | + } catch (error) { |
| 166 | + console.error('❌ Error:', error.message) |
| 167 | + process.exit(1) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +checkPendingTransactions() |
| 172 | + |
| 173 | + |
| 174 | + |
0 commit comments