From 6599c2f4ec5182934535be3d93450badb23ff8fd Mon Sep 17 00:00:00 2001 From: martbost Date: Mon, 9 Nov 2026 09:59:13 -0600 Subject: [PATCH] Payout sender: local nonce + nonce clash = retry + dropped tx back to due + 403 rotates (Turkey Drop's week prizes collided 2026-11-09, 'replacement fee too low') Co-Authored-By: Claude Opus 5.5 --- lib/faucet.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/faucet.js b/lib/faucet.js index 310662c..d40388c 100644 --- a/lib/faucet.js +++ b/lib/faucet.js @@ -29,8 +29,15 @@ function connect() { wallet = new ethers.Wallet(process.env.HUNT_WALLET_KEY.trim(), provider); } // a node hiccup is not a failed drip: 5xx, timeouts, connection resets, rate limits, missing responses -const TRANSIENT = /socket hang up|ECONNABORTED|EPIPE|EHOSTUNREACH|ENETUNREACH|network error|fetch failed|UND_ERR|other side closed|server response 5\d\d|timeout|ETIMEDOUT|ECONNRESET|ECONNREFUSED|EAI_AGAIN|429|rate limit|missing response|could not detect network|bad response|SERVER_ERROR|NETWORK_ERROR/i; // + connection-level failures (PolHunter #7, 2026-10-09) +const TRANSIENT = /socket hang up|ECONNABORTED|EPIPE|EHOSTUNREACH|ENETUNREACH|network error|fetch failed|UND_ERR|other side closed|server response 5\d\d|timeout|ETIMEDOUT|ECONNRESET|ECONNREFUSED|EAI_AGAIN|429|rate limit|missing response|could not detect network|bad response|SERVER_ERROR|NETWORK_ERROR|403|Forbidden|401|Unauthorized/i; // + connection-level failures (PolHunter #7, 2026-10-09) function rotate(reason) { if (rpcs.length > 1) { rpcAt = (rpcAt + 1) % rpcs.length; connect(); console.warn('faucet: RPC rotated to', rpcs[rpcAt % rpcs.length], 'after', reason.slice(0, 80)); } } +// Nonce (2026-11-09, after Turkey Drop's week prizes collided with "replacement fee too low"): publicnode is a load-balanced pool, so +// asking it for the next nonce right after a confirmed send can return the SAME nonce again. Sends read the nonce once per tick, keep +// the highest one ever used in faucet-state.nextNonce, and count up locally. A nonce clash is a retry (row back to due), never a failure. +const NONCE_ERR = /replacement fee too low|replacement transaction underpriced|nonce too low|nonce has already been used|already known|NONCE_EXPIRED|REPLACEMENT_UNDERPRICED/i; +async function nextNonce() { const n = await provider.getTransactionCount(wallet.address, 'pending'); return Math.max(n, Number(store.read('faucet-state', {}).nextNonce) || 0); } +const keepNonce = n => store.update('faucet-state', {}, x => Object.assign(x, { nextNonce: Math.max(n, Number(x.nextNonce) || 0) })); +const DROP_MS = 10 * 60000; // a "sent" row whose tx no node knows after this long was dropped: back to due (its nonce slot is reused, so it can never pay twice) function address() { return wallet ? wallet.address : null; } async function balance() { if (!provider || !wallet) return null; const b = await provider.getBalance(wallet.address); return Number(ethers.formatEther(b)); } @@ -50,7 +57,7 @@ async function tick(notify) { // drips sent but never settled here (a restart mid-wait, a receipt that lagged past the wait): // ask the chain, never re-send for (const p of store.read('payouts', []).filter(x => x.status === 'sent' && x.tx)) { - try { const rc = await provider.getTransactionReceipt(p.tx); if (rc && (await settle(p, rc, notify))) paid++; } catch (e) {} + try { const rc = await provider.getTransactionReceipt(p.tx); if (rc) { if (await settle(p, rc, notify)) paid++; } else if (Date.now() - (p.sentAt || p.ts || 0) > DROP_MS && !(await provider.getTransaction(p.tx))) rewards.mark(p.id, { status: 'due', tx: null, error: 'dropped ' + String(p.tx).slice(0, 12) + ', resending' }); } catch (e) { if (TRANSIENT.test(String(e.message || e))) { rotate(String(e.message || e)); break; } } // a node refusing us (403) must not freeze the check } const due = rewards.payable(); // a dry faucet is not a failed drip: check the balance first, leave the drips due, alert once @@ -59,18 +66,20 @@ async function tick(notify) { const need = due.reduce((n, p) => n + p.pol, 0) + 0.01; if (bal0 < due[0].pol + 0.005) { if (notify) { const st = store.read('faucet-state', {}); const day = new Date().toLocaleDateString('en-CA', { timeZone: 'America/Chicago' }); if (st.dryAlertDay !== day) { st.dryAlertDay = day; store.write('faucet-state', st); await notify('low', { balance: bal0, threshold: need, address: wallet.address }); } } ticking = false; return { paid: 0, waiting: due.length, balance: bal0 }; } } + let nonce = null; if (due.length) { try { nonce = await nextNonce(); } catch (e) { if (TRANSIENT.test(String(e.message || e))) rotate(String(e.message || e)); return { paid }; } } for (const p of due) { if (bal0 != null && bal0 < p.pol + 0.005) break; // pay what the balance covers, leave the rest due if (!p.wallet || !/^0x[a-f0-9]{40}$/i.test(p.wallet)) { rewards.mark(p.id, { status: 'failed', error: 'no wallet on the account' }); continue; } try { - const tx = await wallet.sendTransaction({ to: p.wallet, value: ethers.parseEther(String(p.pol)) }); - rewards.mark(p.id, { status: 'sent', tx: tx.hash }); + const tx = await wallet.sendTransaction({ to: p.wallet, value: ethers.parseEther(String(p.pol)), nonce }); + rewards.mark(p.id, { status: 'sent', tx: tx.hash, sentAt: Date.now() }); nonce++; keepNonce(nonce); let rc = null; try { rc = await tx.wait(1, 90000); } catch (e) { if (e && e.code === 'TIMEOUT') continue; throw e; } // still pending: stays 'sent', settled next tick if (await settle(Object.assign({}, p, { tx: tx.hash }), rc, notify)) { paid++; if (bal0 != null) bal0 -= p.pol; } } catch (e) { const msg = String(e.message || e); - if (/insufficient funds/i.test(msg)) { rewards.mark(p.id, { status: 'due', error: null }); break; } // dry: leave it due, stop this pass + if (/insufficient funds/i.test(msg)) { rewards.mark(p.id, { status: 'due', error: null }); break; } + if (NONCE_ERR.test(msg) || NONCE_ERR.test(String(e && e.code))) { rewards.mark(p.id, { status: 'due', error: 'nonce clash, retrying' }); try { keepNonce(await provider.getTransactionCount(wallet.address, 'latest')); } catch (x) {} break; } // dry: leave it due, stop this pass // the send itself failed only if no hash was recorded; a hash means the chain has it, leave it 'sent' const cur = store.read('payouts', []).find(x => x.id === p.id); if (cur && cur.status === 'sent' && cur.tx) continue; @@ -99,7 +108,7 @@ async function send(to, pol, onHash) { if (!to || !/^0x[a-f0-9]{40}$/i.test(to)) throw new Error('bad wallet'); if (!(Number(pol) > 0)) throw new Error('bad amount'); try { - const tx = await wallet.sendTransaction({ to, value: ethers.parseEther(String(pol)) }); + const nonce = await nextNonce(); const tx = await wallet.sendTransaction({ to, value: ethers.parseEther(String(pol)), nonce }); keepNonce(nonce + 1); if (onHash) { try { await onHash(tx.hash); } catch (e) {} } let rc = null; try { rc = await tx.wait(1, 90000); } catch (e) { if (e && e.code === 'TIMEOUT') return { tx: tx.hash, pending: true }; throw e; } return { tx: tx.hash, ok: !!(rc && rc.status === 1) };