Files
martbost b062939577 Network Weekend (Marty 2026-11-17): Boost views from the live VABShop SKU on every page, quote and chatbot, the network ribbon
chainskus.js reads skus(1) every 60 s and keeps C.boost in step (BOOST_VIEWS stays the normal 500 as normalViews), so a $5
Boost shows 750 once the owner signs setSku and 500 before or after; crediting already follows the Purchase event's views.
common.js rewrites the pages' static '500 guaranteed views' copy to the live number only when the chain differs.
weekend.js: the Fri 11-27 00:00 CT to Tue 12-01 00:00 CT gate (WEEKEND_NOW test clock) for the dated chatbot fact; the ribbon
line (netdeals.setLocal) only exists while the chain really holds the weekend Boost. chatcore render hook, live intro.
qa/weekend.test.mjs 18 pass, chatbot 206, smoke 197. common.js?v=9.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 07:39:47 -06:00

43 lines
3.2 KiB
JavaScript

// Live SKU values from the shop contract (VABShop: skus(sku) = usdCents, views, active) (Marty 2026-11-17, Network Weekend): every page, pack card and the chatbot show what the
// contract will really credit, so a late or early owner setSku never shows a wrong number. skus(sku) is read over CHAIN_RPC every
// 60 s in the background (never on a request); until the first read answers, or if the RPC is down, the env values stand.
// The crediting itself never depends on this: the watcher credits the Purchase event's own credits (shop.js apply).
'use strict';
const { Interface } = require('ethers');
const IFACE = new Interface(['function skus(uint256) view returns (uint64 usdCents, uint64 units, bool active)']);
const TTL = 60 * 1000;
let onRead = null;
let C = null, ids = [], live = new Map(), at = 0, timer = null, inflight = null;
async function read(sku) {
const ac = new AbortController(); const t = setTimeout(() => ac.abort(), 8000); t.unref && t.unref();
try {
const j = await (await fetch(C.chainRpc, { method: 'POST', headers: { 'Content-Type': 'application/json' }, signal: ac.signal,
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'eth_call', params: [{ to: C.contractAddress, data: IFACE.encodeFunctionData('skus', [sku]) }, 'latest'] }) })).json();
if (j.error || !j.result || j.result === '0x') throw new Error('skus(' + sku + ') ' + (j.error ? j.error.message : 'empty'));
const r = IFACE.decodeFunctionResult('skus', j.result);
return { usdCents: Number(r[0]), units: Number(r[1]), active: !!r[2] };
} finally { clearTimeout(t); }
}
function refresh() {
if (inflight || !C || !C.contractAddress || !C.chainRpc) return inflight || Promise.resolve();
inflight = Promise.all(ids.map(async s => { try { live.set(s, await read(s)); } catch (e) {} })).then(() => { at = Date.now(); if (onRead) try { onRead(); } catch (e) {} }).finally(() => { inflight = null; });
return inflight;
}
function init(cfg, skuIds, after) {
C = cfg; onRead = typeof after === 'function' ? after : null; ids = (skuIds || []).filter(n => Number.isSafeInteger(n) && n > 0);
if (timer) clearInterval(timer);
if (C.contractAddress && C.chainRpc && ids.length) { refresh(); timer = setInterval(refresh, TTL); timer.unref && timer.unref(); }
}
// { usdCents, units, active } as the contract holds it, or null (not read yet / RPC down)
function get(sku) { return live.get(Number(sku)) || null; }
// VAB: the Boost as the contract holds it right now. C.boost (the one config object every module shares) is kept in step after each
// read, so the pages, quotes, proof page and chatbot all say the live views; C.boost.normalViews keeps the BOOST_VIEWS env value.
function syncBoost(C) {
if (C.boost.normalViews == null) { C.boost.normalViews = C.boost.views; C.boost.normalUsd = C.boost.usd; }
const l = get(C.boost.sku);
if (l && l.active && l.units > 0 && l.usdCents > 0) { C.boost.views = l.units; C.boost.usd = l.usdCents / 100; }
else { C.boost.views = C.boost.normalViews; C.boost.usd = C.boost.normalUsd; }
return C.boost;
}
module.exports = { init, get, syncBoost, refresh, _set: (sku, v) => live.set(Number(sku), v), _clear: () => live.clear(), _state: () => ({ at, n: live.size }) };