Files
vbb/public/assets/admin.js
T
martbost aee1ccc36a VBB purchases live + weekly prize payouts (Marty 2026-11-14: "why does it say 'coming soon' on all the stuff in it? Shouldn't it all be live already?")
Buying (VBBShop 0x36F6...07D7): Packages page has live Buy buttons for the 10 and 40 credit packs (USD price + live POL from quoteWei, SafePal or any other crypto wallet, one-time, never a subscription, where the payment goes); My Banners has "Add the banner look ($2)" on each waiting or in-loop banner (SKU 3, ref = banner id) and a 40-character teaser editor once it has the look. POST /api/my/order builds the member's buy() call on the server (fresh keeper voucher when the hub knows their sponsor, else no sponsor); the page checks the connected wallet is the member's VBB account (explains how to switch if not), checks the balance, sends with Polygon's fee floor (/api/gas), waits on /api/tx, nudges the watcher (/api/my/paid) and polls until the credits or the look arrive. Public: /api/quote, /api/gas, /api/tx/<hash>; /api/config has shop {live, contract} + explorer. The banner look on the loop: double border + star, hover teaser (always shown on touch screens; default = the link's site). Only Tokens still says coming soon. wallet.js gains sendTx/waitTx/balance (DLB flow).

Weekly prize payouts (prize.js): each Central-time week's pot = 10% of its VBBShop sales (usdCents of Purchase events by block time), $20 floor (company top-up shown to admin), $50 cap internal, half per board, 50/30/20. Every Monday the app prepares last week's plan (winners, wallets, USD; POL quoted live from the Chainlink feed the contract uses). /admin "Approve and pay" (two clicks): refuses unless PRIZE_KEY is the contract's prizeWallet(), the feed is fresh and the wallet holds the POL + gas (says how much more to send); then once per week (docstore.claim in MySQL + plan status), consecutive nonces, each hash saved as sent, stops at the first unclear send (never auto-retried), receipts watched. Ties: earlier to reach the count wins (leaderboards use the same rule). A winner that cannot be paid (the prize or company wallet) is skipped and the rest move up; an unfilled place stays in the prize wallet. /leaders shows past winners with transaction links. Posts nothing anywhere. Compose passes PRIZE_KEY.

Tests: smoke 192/192 (stand-in chain answers quoteWei + receipts: quote, order calldata + voucher, look purchase, teaser, prizes API, UI Buy buttons). New qa/chain.mjs on a local anvil with the real VBBShop + mock feed: buy -> Purchase -> credits, voucher binds sponsor, 30/10/60 split, overpay refunded, banner look; prize prepare -> short refusal -> pay 6 transfers exact POL -> paid once: 29/29. common.js?v=3, vbb.css?v=4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 09:47:58 -06:00

64 lines
6.6 KiB
JavaScript

// Admin: status + the cycle reward ledger, read with the ADMIN_TOKEN (kept in this tab only).
(function () {
VBB.renderNav('');
const $ = VBB.$, esc = VBB.esc;
let tok = ''; try { tok = sessionStorage.getItem('vbb.admin') || ''; } catch (e) {}
const get = u => VBB.getJSON(u, { headers: { 'x-admin-token': tok } });
async function open() {
const s = await get('/api/admin/status');
if (s.error) { VBB.status('That token was not accepted.', 'err'); return; }
try { sessionStorage.setItem('vbb.admin', tok); } catch (e) {}
$('tokCard').hidden = true; $('adm').hidden = false; VBB.status('');
$('aStats').innerHTML = [['Loop filled', s.loop.filled + ' / ' + s.loop.size], ['Waiting', s.queue.length], ['Next shift', s.progress + ' / ' + s.config.perShift + (s.held ? ' (held)' : '')],
['Shifts today', s.today.shifts], ['Cycles today', s.today.cycles], ['Visitors today', s.today.visits], ['Cycles total', s.totals.cycles], ['Views recorded', VBB.n(s.ledger.views)], ['Views credited', VBB.n(s.ledger.credited)], ['Members', s.members]]
.map(x => '<div class="stat"><b>' + esc(x[1]) + '</b><span>' + esc(x[0]) + '</span></div>').join('');
$('aRaw').textContent = JSON.stringify(s, null, 2);
prizes(); setInterval(prizes, 30000);
const l = await get('/api/admin/ledger?n=300');
const who = r => r.username ? '@' + r.username : VBB.short(r.address || r.owner);
$('aMembers').innerHTML = l.members && l.members.length ? '<table class="t"><tr><th>Member</th><th class="n">Cycles</th><th class="n">Views</th></tr>' + l.members.map(m => '<tr><td>' + esc(who(m)) + '</td><td class="n">' + m.cycles + '</td><td class="n">' + VBB.n(m.views) + '</td></tr>').join('') + '</table>' : '<p class="muted">No cycles yet.</p>';
$('aLedger').innerHTML = l.entries && l.entries.length ? '<table class="t"><tr><th>When (CT)</th><th>Entry</th><th>Banner</th><th>Member</th><th class="n">Views</th><th>Status</th></tr>'
+ l.entries.map(e => '<tr><td>' + esc(new Date(e.at).toLocaleString('en-US', { timeZone: 'America/Chicago' })) + '</td><td class="mono">' + esc(e.id) + '</td><td>#' + e.bannerId + '</td><td>' + esc(who(e)) + '</td><td class="n">' + VBB.n(e.views) + '</td><td>' + esc(e.status) + '</td></tr>').join('') + '</table>' : '<p class="muted">The ledger is empty.</p>';
}
// ---- weekly prize payouts: review, then "Approve and pay" (two clicks), once per week ----
const pst = { prepared: 'Waiting for approval', nothing: 'No winners', paying: 'Paying', sent: 'Sent, confirming', paid: 'Paid', partial: 'Partly paid: check below', 'claimed-elsewhere': 'Claimed elsewhere: check the wallet' };
async function prizes() {
const z = await get('/api/admin/prizes'); if (z.error) return;
const cur = z.current || {};
$('pzNow').innerHTML = '<p class="small">This week (' + esc(z.week) + '): sales $' + VBB.n(cur.salesUsd) + ' in ' + VBB.n(cur.purchases) + ' purchases, 10% = $' + VBB.n(cur.shareUsd) + ', pot $' + VBB.n(cur.potUsd)
+ (cur.topUpUsd > 0 ? ' (company top-up $' + VBB.n(cur.topUpUsd) + ')' : '') + (cur.atCap ? ' (at the cap)' : '') + '. Prize key: ' + (z.prize.keyOn ? esc(VBB.short(z.prize.keyAddress)) : 'not set') + '.</p>';
const tx = h => z.explorer ? '<a target="_blank" rel="noopener" href="' + esc(z.explorer + '/tx/' + h) + '">' + esc(h.slice(0, 10)) + '...</a>' : esc(h);
$('pzWeeks').innerHTML = (z.weeks || []).map(w => {
const q = w.quote || {};
const lines = w.transfers.map((t, i) => '<tr><td>' + esc(t.board) + '</td><td>' + t.place + '</td><td>' + esc(t.name) + '<br><span class="mono small">' + esc(t.address) + '</span></td><td class="n">' + VBB.n(t.count) + '</td><td class="n">$' + VBB.n(t.usd) + '</td><td class="n">'
+ esc(t.pol || (q.lines && q.lines[i] ? q.lines[i].pol : '')) + '</td><td>' + esc(t.status || '') + (t.tx ? ' ' + tx(t.tx) : '') + (t.error ? '<br><span class="small">' + esc(t.error) + '</span>' : '') + '</td></tr>').join('');
let foot = '';
if (w.status === 'prepared') {
foot = q.error ? '<p class="note err">' + esc(q.error) + '</p>'
: '<p class="small">POL at $' + Number(q.priceUsd).toFixed(4) + '. Needs ' + esc(q.totalPol) + ' POL + about ' + esc(q.gasPol) + ' POL gas. Prize wallet ' + esc(VBB.short(q.contractPrizeWallet || '')) + ' holds ' + esc(q.balancePol) + ' POL.'
+ (BigInt(q.shortWei || 0) > 0n ? ' <b>Short by ' + esc(q.shortPol) + ' POL.</b>' : '') + (q.keyMatches ? '' : ' <b>PRIZE_KEY is missing or is not the prize wallet.</b>') + '</p>'
+ '<button class="btn small" data-pay="' + esc(w.week) + '">Approve and pay</button>';
}
return '<div class="card" style="margin:12px 0"><h3>Week of ' + esc(w.week) + ': ' + esc(pst[w.status] || w.status) + '</h3><p class="small">Sales $' + VBB.n(w.salesUsd) + ', pot $' + VBB.n(w.potUsd) + ', paying $' + VBB.n(w.payUsd)
+ (w.unfilledUsd > 0 ? ', $' + VBB.n(w.unfilledUsd) + ' unfilled (stays in the prize wallet)' : '') + (w.payment && w.payment.priceUsd ? ', paid at $' + Number(w.payment.priceUsd).toFixed(4) + '/POL' : '') + '</p>'
+ (w.transfers.length ? '<div class="scroll"><table class="t"><tr><th>Board</th><th>#</th><th>Winner</th><th class="n">Count</th><th class="n">USD</th><th class="n">POL</th><th>Status</th></tr>' + lines + '</table></div>' : '<p class="muted">Nobody on the leaderboards that week.</p>') + foot + '</div>';
}).join('') || '<p class="muted">No weeks prepared yet.</p>';
document.querySelectorAll('[data-pay]').forEach(b => {
b.onclick = async () => {
const wk = b.dataset.pay;
if (!b.dataset.armed) { b.dataset.armed = '1'; b.textContent = 'Click again to send the POL for ' + wk; setTimeout(() => { if (b.isConnected) { delete b.dataset.armed; b.textContent = 'Approve and pay'; } }, 8000); return; }
b.disabled = true; b.textContent = 'Sending...';
const r = await VBB.getJSON('/api/admin/prizes/pay', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-admin-token': tok }, body: JSON.stringify({ week: wk, confirm: wk }) });
VBB.status(r.error || 'Payout sent for the week of ' + wk + '. Receipts confirm below.', r.error ? 'err' : 'ok'); prizes();
};
});
}
$('pzPrep').onclick = async () => {
const r = await VBB.getJSON('/api/admin/prizes/prepare', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-admin-token': tok }, body: '{}' });
VBB.status(r.error || 'Prepared the week of ' + r.week + '.', r.error ? 'err' : 'ok'); prizes();
};
$('tokBtn').onclick = () => { tok = $('tok').value.trim(); open(); };
$('tok').addEventListener('keydown', e => { if (e.key === 'Enter') $('tokBtn').click(); });
if (tok) open();
})();