3e0dee517d
weekend.js: the date gate (WEEKEND_NOW test clock), FREE_CREDITS_PER_DAY stays 1. chainskus.js: skus() read every 60 s, so the $10 pack shows what the contract credits (50 once the owner signs setSku, 40 before or after). netdeals.js + /assets/netweekend.js: /api/network-deals and the ribbon; the VBB line is built from live values and drops itself if there is nothing true to say. chatcore render hook, chatbot FACTS/CANNED from live values with a dated weekend fact. qa/weekend.test.mjs 33 pass, chatbot 182, smoke 268. common.js?v=10, my.js?v=6, packages.js?v=2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
38 lines
2.8 KiB
JavaScript
38 lines
2.8 KiB
JavaScript
// Live SKU values from the shop contract (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 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(); }).finally(() => { inflight = null; });
|
|
return inflight;
|
|
}
|
|
function init(cfg, skuIds) {
|
|
C = cfg; 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; }
|
|
// VBB: the credit packs as the contract holds them right now; the env values (CREDIT_PACKS) until the first read answers
|
|
function packs(C) {
|
|
return C.packs.map(p => { const l = get(p.sku); return l && l.active && l.units > 0 && l.usdCents > 0 ? { credits: l.units, usd: l.usdCents / 100, sku: p.sku } : p; });
|
|
}
|
|
function lookUsd(C) { const l = get(C.lookSku); return l && l.active && l.usdCents > 0 ? l.usdCents / 100 : C.lookUsd; }
|
|
module.exports = { init, get, packs, lookUsd, refresh, _set: (sku, v) => live.set(Number(sku), v), _clear: () => live.clear(), _state: () => ({ at, n: live.size }) };
|