diff --git a/lib/faucet.js b/lib/faucet.js index 41778c8..a2b5b06 100644 --- a/lib/faucet.js +++ b/lib/faucet.js @@ -19,10 +19,18 @@ let provider = null, wallet = null, ticking = false; function enabled() { return !!(process.env.HUNT_WALLET_KEY && process.env.HUNT_RPC); } function init() { if (!enabled()) return false; - provider = new ethers.JsonRpcProvider(process.env.HUNT_RPC, Number(process.env.HUNT_CHAIN_ID) || undefined); - wallet = new ethers.Wallet(process.env.HUNT_WALLET_KEY.trim(), provider); + rpcs = String(process.env.HUNT_RPC).split(',').map(s => s.trim()).filter(Boolean); rpcAt = 0; + connect(); return true; } +let rpcs = [], rpcAt = 0; +function connect() { + provider = new ethers.JsonRpcProvider(rpcs[rpcAt % rpcs.length], Number(process.env.HUNT_CHAIN_ID) || undefined); + 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 = /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; +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)); } } 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)); } @@ -66,6 +74,7 @@ async function tick(notify) { // 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; + if (TRANSIENT.test(msg)) { rewards.mark(p.id, { status: 'due', error: 'node: ' + msg.slice(0, 120) }); console.warn('faucet: transient, left due:', p.id, msg.slice(0, 120)); rotate(msg); break; } // next tick, next node rewards.mark(p.id, { status: 'failed', error: msg.slice(0, 200) }); if (notify) await notify('failed', Object.assign({}, p, { error: String(e.message || e).slice(0, 200) })); } diff --git a/server.js b/server.js index ebfea9f..1c24c3a 100644 --- a/server.js +++ b/server.js @@ -58,14 +58,23 @@ async function telegramPhoto(file, caption, general) { } catch (e) { return false; } } const fmt = n => Number(n).toLocaleString('en-US', { maximumFractionDigits: 4 }); +// keys being posted right now, so the claim path and the sweep never post the same thing twice +const postingNow = new Set(); async function notify(kind, p) { - if (kind === 'paid') { const ok = await telegram('\u{1F3AF} PolHunter · ' + (p.username ? '@' + p.username : '#' + p.memberId) + ' found it on ' + p.site + ' and got ' + fmt(p.pol) + ' POL · verify\nHunt yours'); if (ok && p.id) rewards.mark(p.id, { posted: Date.now() }); return ok; } + if (kind === 'paid' && p && p.id) { const k = 'find:' + p.id; if (postingNow.has(k)) return false; postingNow.add(k); try { return await notifyPaid(p); } finally { postingNow.delete(k); } } + if (kind === 'badge' && p && p.me) { const k = 'badge:' + p.me.memberId + ':' + p.id; if (postingNow.has(k)) return false; postingNow.add(k); try { return await notifyBadge(p); } finally { postingNow.delete(k); } } + return notifyOther(kind, p); +} +async function notifyPaid(p) { + { const ok = await telegram('\u{1F3AF} PolHunter · ' + (p.username ? '@' + p.username : '#' + p.memberId) + ' found it on ' + p.site + ' and got ' + fmt(p.pol) + ' POL · verify\nHunt yours'); if (ok && p.id) rewards.mark(p.id, { posted: Date.now() }); return ok; } +} +async function notifyOther(kind, p) { if (kind === 'low') return telegram('⚠️ PolHunter faucet is low: ' + fmt(p.balance) + ' POL left in ' + p.address + ' (alert threshold ' + fmt(p.threshold) + '). Top up from Receiver B.'); if (kind === 'failed') return telegram('❌ PolHunter · drip to #' + p.memberId + ' failed: ' + p.error); - if (kind === 'badge') { const ok = await postBadge(p); if (ok) store.update('badges-posted', {}, all => { const k = String(p.me.memberId); all[k] = Array.from(new Set([...(all[k] || []), p.id])); return all; }); return ok; } if (kind === 'prize') { const medal = ['\u{1F947}', '\u{1F948}', '\u{1F949}']; return telegram('\u{1F3C6} PolHunter weekly prizes for the week of ' + p.week + '\n' + p.winners.map(w => (medal[w.rank - 1] || '#' + w.rank) + ' ' + w.who + ' \u00b7 ' + w.finds + ' finds \u00b7 ' + fmt(w.prizePol) + ' POL').join('\n') + '\nLeaderboard'); } return false; } +async function notifyBadge(p) { const ok = await postBadge(p); if (ok) store.update('badges-posted', {}, all => { const k = String(p.me.memberId); all[k] = Array.from(new Set([...(all[k] || []), p.id])); return all; }); return ok; } async function postBadge(p) { { const b = social.BADGES.find(x => x.id === p.id); if (!b) return false; const who = social.nameOf(p.me); const file = await badge.render(p.id, p.me.username || '#' + p.me.memberId); const cap = '\u{1F3C6} PolHunter \u00b7 ' + (p.me.username ? '@' + p.me.username : '#' + p.me.memberId) + ' unlocked ' + b.name + ': ' + b.why + '\n' + SITE + '/b/' + who + '/' + p.id; if (!file) return telegram(cap); const ok = await telegramPhoto(file, cap); if (String(process.env.HUNT_TG_BADGE_GENERAL || 'on') === 'on') await telegramPhoto(file, cap, true); return ok; } return false; @@ -288,7 +297,7 @@ const POST_SINCE = process.env.HUNT_POST_SINCE ? Date.parse(process.env.HUNT_POS let sweeping = false; async function postMissedFinds() { if (sweeping || !outbound()) return 0; sweeping = true; let n = 0; - try { for (const p of rewards.unposted(POST_SINCE, 15)) { if (await notify('paid', p)) n++; else break; } } + try { for (const p of rewards.unposted(POST_SINCE, 15)) { if (postingNow.has('find:' + p.id)) continue; if (await notify('paid', p)) n++; else break; } } catch (e) { console.error('find sweep', e.message); } finally { sweeping = false; } if (n) console.log('posted', n, 'missed find(s) to Telegram'); await postMissedBadges().catch(e => console.error('badge sweep', e.message)); @@ -302,7 +311,7 @@ async function postMissedBadges() { const posted = store.read('badges-posted', {}); let n = 0; for (const me of seen.values()) { const have = new Set(posted[String(me.memberId)] || []); - for (const b of social.badgesFor(me.memberId)) { if (have.has(b.id)) continue; if (n >= 10) return n; if (await notify('badge', { me, id: b.id })) n++; else return n; } + for (const b of social.badgesFor(me.memberId)) { if (have.has(b.id) || postingNow.has('badge:' + me.memberId + ':' + b.id)) continue; if (n >= 10) return n; if (await notify('badge', { me, id: b.id })) n++; else return n; } } if (n) console.log('posted', n, 'missed badge(s) to Telegram'); return n;