// Payment proof on Telegram (Marty 2026-11-13: "anytime members are paid in Downline Banners ... put all those back together in one // message", with the verify link). One message per spot sale, in the group's shared payments topic (TG_CHAT / TG_PAY_TOPIC, the // CTB Rewards bot TG_TOKEN, same as the games): who bought, then every member paid (sponsor, then each line level) in POL with the // dollar value at the contract's own price, then one line for the company + momentum fund share, then the Polygonscan link. // Built from the chain events of the transaction (SpotBought first, then Paid / PaymentFailed in contract order). Sent once across // app copies (docstore.claim). Never posts events older than 2 hours (a re-index after downtime must not flood the topic). 'use strict'; const docstore = require('./docstore'); const ROLE = { SPONSOR: 0, LEVEL: 1, FEE_A: 2, FEE_B: 3, FUND: 4 }; let X = null; const buf = new Map(); const on = () => !!(process.env.TG_TOKEN && process.env.TG_CHAT); const esc = s => String(s == null ? '' : s).replace(/[&<>]/g, c => ({ '&': '&', '<': '<', '>': '>' }[c])); const pol = wei => { const v = Number(BigInt(wei) / 10n ** 12n) / 1e6; return v >= 100 ? v.toFixed(2) : v.toFixed(4); }; function init(deps) { X = deps; } // { nameOf(addr) -> username|null, contract, explorer, site } function onEvent(ev) { if (!on() || !ev || !ev.tx || !['SpotBought', 'Paid', 'PaymentFailed'].includes(ev.type)) return; if (ev.ts && Date.now() - ev.ts > 2 * 3600000) return; let b = buf.get(ev.tx); if (!b) { b = { evs: [], t: null }; buf.set(ev.tx, b); } b.evs.push(ev); clearTimeout(b.t); b.t = setTimeout(() => { buf.delete(ev.tx); send(ev.tx, b.evs).catch(e => console.error('tgproof', e.message)); }, 3000); } async function who(addr) { const a = String(addr || '').toLowerCase(); let n = null; try { n = await X.nameOf(a); } catch (e) {} return n ? '@' + esc(n) : esc(a.slice(0, 6) + '...' + a.slice(-4)); } async function send(tx, evs) { const text = await compose(tx, evs); if (!text) return; if (!(await docstore.claim('tgproof:' + tx))) return; // another app copy already posted this sale const body = { chat_id: process.env.TG_CHAT, text, parse_mode: 'HTML', disable_web_page_preview: true }; if (process.env.TG_PAY_TOPIC) body.message_thread_id = Number(process.env.TG_PAY_TOPIC); const r = await fetch('https://api.telegram.org/bot' + process.env.TG_TOKEN + '/sendMessage', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), signal: AbortSignal.timeout(15000) }); const j = await r.json().catch(() => ({})); if (!j.ok) console.error('tgproof send failed', r.status, String(j.description || '').slice(0, 120)); else console.log('tgproof posted', String(tx).slice(0, 12)); } // the message text for one sale (also used to preview it without sending) async function compose(tx, evs) { const sb = evs.find(e => e.type === 'SpotBought'); if (!sb) return null; const price = Number(sb.usdPrice) / 1e8; // the Chainlink POL/USD answer the contract priced this spot with const usd = wei => price ? ' ($' + (Number(BigInt(wei) / 10n ** 12n) / 1e6 * price).toFixed(2) + ')' : ''; const contract = String(X.contract || '').toLowerCase(); const head = sb.kind === 2 ? '\u{1F381} The momentum fund gifted spot #' + sb.id + ' to ' + await who(sb.owner) : sb.kind === 1 ? '\u{1F9FE} Spot #' + sb.id + ' bought for ' + await who(sb.owner) + (sb.payer !== sb.owner ? ' (refuel spot)' : '') : '\u{1F9FE} Spot #' + sb.id + ' bought by ' + await who(sb.owner); const lines = ['\u{1F7E3} Downline Banners', head + ' · $5 (' + pol(sb.valueWei) + ' POL)']; let level = 0, house = 0n; for (const e of evs) { if (e.type !== 'Paid' && e.type !== 'PaymentFailed') continue; if (e.role === ROLE.LEVEL) level++; const toCompany = e.to === contract || e.role === ROLE.FEE_A || e.role === ROLE.FEE_B || e.role === ROLE.FUND || (X.feeWallets || []).includes(e.to); if (e.type === 'PaymentFailed') { lines.push('⚠️ ' + (e.role === ROLE.SPONSOR ? 'Sponsor' : 'Line ' + level) + ' payment to ' + await who(e.to) + ' bounced, ' + pol(e.amount) + ' POL went to the momentum fund'); continue; } if (toCompany) { house += BigInt(e.amount); continue; } if (e.role === ROLE.SPONSOR) lines.push('\u{1F4B8} Sponsor: ' + pol(e.amount) + ' POL' + usd(e.amount) + ' → ' + await who(e.to)); else if (e.role === ROLE.LEVEL) lines.push('\u{1F4B8} Line ' + level + ' (spot #' + (sb.id - level) + '): ' + pol(e.amount) + ' POL' + usd(e.amount) + ' → ' + await who(e.to)); } if (house > 0n) lines.push('\u{1F3E6} Company + momentum fund: ' + pol(house.toString()) + ' POL' + usd(house.toString())); lines.push('· Verify on Polygonscan · Live ledger'); return lines.join('\n'); } module.exports = { init, onEvent, send, compose, on };