af5a53b833
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
59 lines
4.4 KiB
JavaScript
59 lines
4.4 KiB
JavaScript
// Game figures for the daily snapshot (Marty 2026-10-08: one card for the whole ecosystem, games as their own block).
|
|
// Two live sources, no estimates:
|
|
// players sent in — counted here every time InstantAdPay hands a member into a game (/api/my/<game>), per UTC hour,
|
|
// so "last 24 h" is exact and survives restarts (DATA_DIR/game-handoffs.json)
|
|
// payouts + POL — read from Polygon: outgoing transfers from each game's payout wallet in the last 24 h (Etherscan v2,
|
|
// the same query the hourly wallet watcher on core uses). Our own wallets and the fee wallets are skipped.
|
|
// Env: GAME_WALLETS = "slug:0x…,slug:0x…" (set from the /root/.<game>-address files on core), ETHERSCAN_KEY.
|
|
'use strict';
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
let R = null;
|
|
const NAMES = { spookydoors: 'Spooky Doors', gemkingdom: 'Gem Kingdom', clutterhunt: 'ClutterHunt', driprunner: 'Drip Runner', polywarriors: 'PolyWarriors', spellwheel: 'SpellWheel', roadagent: 'Road Agent', polhunter: 'PolHunter', passivedrip: 'PassiveDrip' };
|
|
const wallets = () => Object.fromEntries(String(process.env.GAME_WALLETS || '').split(',').map(x => x.trim()).filter(Boolean).map(x => { const [k, a] = x.split(':'); return [String(k).toLowerCase(), String(a || '').toLowerCase()]; }).filter(([k, a]) => k && /^0x[0-9a-f]{40}$/.test(a)));
|
|
const KEY = () => String(process.env.ETHERSCAN_KEY || '').trim();
|
|
// money never flows to these from a game wallet as a payout: Marty's fee wallets, the other game wallets
|
|
const SKIP = () => new Set(['0x7627fc78876948ac9d95c1c9eb061e7d6d647b70', '0x8b7d33849a2c4d92c985be31e46dc564ba901ad2', '0xcbe203312e0ea3b542da7a2e7f0a8a09db948f75'].concat(Object.values(wallets())));
|
|
const FILE = () => path.join(R.dataDir, 'game-handoffs.json');
|
|
function load() { try { return JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { return {}; } }
|
|
function init(deps) { R = deps; }
|
|
const hourKey = t => new Date(t || Date.now()).toISOString().slice(0, 13);
|
|
// a member was handed into a game: one tick in this hour's bucket (buckets older than 3 days are dropped)
|
|
function handoff(slug) {
|
|
try {
|
|
const h = load(), k = hourKey(); h[k] = h[k] || {}; h[k][slug] = (h[k][slug] || 0) + 1;
|
|
const cut = hourKey(Date.now() - 3 * 86400000); for (const key of Object.keys(h)) if (key < cut) delete h[key];
|
|
fs.writeFileSync(FILE(), JSON.stringify(h));
|
|
} catch (e) { /* counting must never break a hand-off */ }
|
|
}
|
|
function sentIn(since) { const h = load(), out = {}; const k0 = hourKey(since); for (const [k, m] of Object.entries(h)) if (k >= k0) for (const [slug, c] of Object.entries(m)) out[slug] = (out[slug] || 0) + c; return out; }
|
|
// outgoing payouts from one game wallet since `since` (ms): { count, wei }
|
|
async function payoutsFor(addr, since) {
|
|
const out = { count: 0, wei: 0n }; if (!KEY()) return out;
|
|
const url = 'https://api.etherscan.io/v2/api?chainid=137&module=account&action=txlist&address=' + addr + '&startblock=0&endblock=99999999&sort=desc&page=1&offset=500&apikey=' + KEY();
|
|
const r = await fetch(url, { signal: AbortSignal.timeout(20000) }); const j = await r.json(); const rows = Array.isArray(j.result) ? j.result : [];
|
|
const skip = SKIP(), sinceS = Math.floor(since / 1000);
|
|
for (const x of rows) {
|
|
if (Number(x.timeStamp) < sinceS) break; // sorted newest first
|
|
if (x.isError !== '0' || String(x.from).toLowerCase() !== addr || !x.value || x.value === '0') continue;
|
|
if (skip.has(String(x.to).toLowerCase())) continue;
|
|
out.count++; out.wei += BigInt(x.value);
|
|
}
|
|
return out;
|
|
}
|
|
// everything the card needs: per game and totals, last 24 h
|
|
async function gather(since) {
|
|
const W = wallets(), sent = sentIn(since), games = [];
|
|
const slugs = new Set([...Object.keys(W), ...Object.keys(sent)]);
|
|
for (const slug of slugs) {
|
|
const g = { slug, name: NAMES[slug] || slug, sentIn: sent[slug] || 0, payouts: 0, wei: 0n, error: null };
|
|
if (W[slug]) { try { const p = await payoutsFor(W[slug], since); g.payouts = p.count; g.wei = p.wei; } catch (e) { g.error = e.message; } }
|
|
games.push(g);
|
|
}
|
|
games.sort((a, b) => (b.sentIn - a.sentIn) || (Number(b.wei - a.wei)));
|
|
const totals = games.reduce((t, g) => ({ sentIn: t.sentIn + g.sentIn, payouts: t.payouts + g.payouts, wei: t.wei + g.wei }), { sentIn: 0, payouts: 0, wei: 0n });
|
|
return { games, totals, chainChecked: !!KEY() && Object.keys(W).length > 0 };
|
|
}
|
|
module.exports = { init, handoff, gather, NAMES };
|