608ec6547b
- config: 5 sponsor levels x 10% (company 20%), approved numbers labelled as such, POT_DAILY_CENTS display (2500). - Eligibility: a network member with a linked wallet earns a level or the resharer cut, Boost or not. /api/my/order drops line members with no wallet (compression); empty levels and the no-resharer cut go to the company. - shop.js: VASShop ABI after the contract fix (Purchase.boardDays, skus().boardDays, potDailyMaxCents, potCentsToday); purchase days from the chain. wire.js keeps the on-chain days. - Copy: removed "sponsor levels pay people who have bought a Boost / passed over / none qualified" and the proposal labels; Boost page, chatbot, contract page (split fixed forever, pot daily cap) say a Boost buys placement only. Assets ?v=2. - qa/e2e.mjs: deploys the new VASShop (5 x 10%, pot cap 2500, SKU 7 days); a no-wallet hub member between buyer and sponsor is skipped and both sponsors get exactly 10% without a Boost. 61/61. Chatbot QA 108/108. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
9.5 KiB
JavaScript
142 lines
9.5 KiB
JavaScript
// The engine: verified visitors, verified ad views, verified clicks, ad credits (L3) and what each page shows.
|
|
// - A page load gets a ticket listing the ads it showed (decided by the server, never by the browser), each with the share link it
|
|
// was shown through and its slot. After visitSeconds visible the browser redeems it: the visitor is verified (bots, early sends,
|
|
// the per-IP cap filtered: the VBB / ViralAdBlitz rules) and each ad shown gets one verified view, once per visitor per ad per 24 h.
|
|
// The ad owner's own views never count, nor a resharer's views of their own share link.
|
|
// - L3 credits: a verified view shown through a RESHARE link (step 1 or more) counts for that link's resharer; every CREDITS_VIEWS
|
|
// (100) such views earn CREDITS_PER (10) ad credits. A credit is spent on the member's own ad: their ad fills a "Featured with
|
|
// credits" place and each verified view of it there costs 1 credit. Ad credits buy advertising, not income; they are never paid out.
|
|
// - Boost (the chain's Purchase event, shop.js): the ad sits at the top of the reshare board for boost.days per unit (stacked).
|
|
// - Verified click: /go/<ad>?s=<share>&v=<token>, once per visitor per ad per 24 h, never the owner's own, booked to the share.
|
|
'use strict';
|
|
const crypto = require('crypto');
|
|
const cfgMod = require('./config');
|
|
const store = require('./store');
|
|
|
|
const C = () => cfgMod.get();
|
|
const H24 = 24 * 3600000;
|
|
function init() { rebuild(); }
|
|
|
|
const lastVisitByVk = new Map(), ipVks = new Map(), viewSeen = new Map(), clickSeen = new Map();
|
|
function rebuild() {
|
|
lastVisitByVk.clear(); ipVks.clear(); clickSeen.clear();
|
|
const cut = Date.now() - H24;
|
|
for (const v of store.visits()) if (v.at >= cut) { lastVisitByVk.set(v.vk, v.at); ipAdd(v.iph, v.vk, v.at); }
|
|
for (const c of store.clicks()) if (c.at >= cut && c.verified) clickSeen.set(c.vk + '|' + c.adId, c.at);
|
|
}
|
|
function ipAdd(iph, vk, at) { let m = ipVks.get(iph); if (!m) ipVks.set(iph, m = new Map()); m.set(vk, at); }
|
|
const fresh = (m, k) => { const t = m.get(k); return t && Date.now() - t < H24; };
|
|
|
|
// ---- Boosts ----
|
|
function purchases() { return Object.entries(store.state().purchases || {}).map(([key, x]) => Object.assign({ key }, x)); }
|
|
// when an ad's Boosts end: each unit adds boost.days, back to back from its purchase (or from the end of the one before)
|
|
function boostedUntil(adId) {
|
|
let until = 0;
|
|
for (const b of purchases().filter(x => x.adId === Number(adId)).sort((a, b) => (a.at || 0) - (b.at || 0))) {
|
|
const at = (b.blockAt ? b.blockAt * 1000 : b.at) || 0; until = Math.max(until, at) + (Number(b.qty) || 1) * (b.days || C().boost.days) * 86400000;
|
|
}
|
|
return until;
|
|
}
|
|
const isBoosted = adId => boostedUntil(adId) > Date.now();
|
|
function attach(rec) {
|
|
const a = store.ad(Number(rec.ref)); const own = a && a.owner === rec.buyer && a.status !== 'removed' ? a : store.adOf(rec.buyer);
|
|
rec.adId = own ? own.id : null; rec.days = rec.days || C().boost.days; return rec.adId;
|
|
}
|
|
function attachPending(owner, adId) { let n = 0; for (const x of Object.values(store.state().purchases || {})) if (x.buyer === owner && !x.adId) { x.adId = adId; n++; } if (n) store.save(); return n; }
|
|
function boostView(b) {
|
|
const until = b.adId ? boostedUntil(b.adId) : 0;
|
|
return { key: b.key, tx: String(b.key).split(':')[0], orderId: b.orderId, at: (b.blockAt ? b.blockAt * 1000 : b.at) || null, adId: b.adId || null, qty: b.qty, days: (Number(b.qty) || 1) * (b.days || C().boost.days),
|
|
boostedUntil: until || null, status: !b.adId ? 'waiting-for-ad' : until > Date.now() ? 'on-top' : 'finished', resharer: b.resharer || null, resharerWei: b.resharerPaid || '0' };
|
|
}
|
|
|
|
// ---- what pages show ----
|
|
const short = a => a ? a.slice(0, 6) + '...' + a.slice(-4) : '';
|
|
const hostOf = l => { try { return new URL(l).hostname.replace(/^www\./, ''); } catch (e) { return ''; } };
|
|
function adCard(a, extra) {
|
|
const m = store.member(a.owner);
|
|
return Object.assign({ id: a.id, headline: a.headline, line1: a.line1, line2: a.line2 || '', host: hostOf(a.link), owner: m && m.username ? '@' + m.username : short(a.owner),
|
|
boosted: isBoosted(a.id), reshares: Math.max(0, store.sharesOf(a.id).length - 1), views: a.views || 0 }, extra || {});
|
|
}
|
|
// the reshare board: Boosted ads first (soonest-ending last), then this week's movers (new reshares x 3 + verified clicks), then newest
|
|
function board(n) {
|
|
const wk = store.ctWeek(); const score = new Map();
|
|
for (const s of store.state().shares) if (s.step > 0 && store.ctWeek(s.createdAt) === wk) score.set(s.adId, (score.get(s.adId) || 0) + 3);
|
|
for (const c of store.clicks()) if (c.verified && c.wk === wk) score.set(c.adId, (score.get(c.adId) || 0) + 1);
|
|
const live = store.liveAds();
|
|
const boosted = live.filter(a => isBoosted(a.id)).sort((a, b) => boostedUntil(b.id) - boostedUntil(a.id));
|
|
const rest = live.filter(a => !isBoosted(a.id)).sort((a, b) => (score.get(b.id) || 0) - (score.get(a.id) || 0) || b.createdAt - a.createdAt);
|
|
return boosted.concat(rest).slice(0, n || C().boardSize).map((a, i) => adCard(a, { rank: i + 1, week: score.get(a.id) || 0 }));
|
|
}
|
|
// "Featured with credits": up to k ads whose owners hold credits, the biggest balance first with a little shuffle
|
|
function creditPicks(k, exclude) {
|
|
if (k <= 0) return [];
|
|
const ex = exclude || new Set();
|
|
const list = store.liveAds().filter(a => !ex.has(a.id) && store.creditBalance(a.owner) > 0).sort((a, b) => store.creditBalance(b.owner) - store.creditBalance(a.owner)).slice(0, k * 3);
|
|
for (let i = list.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [list[i], list[j]] = [list[j], list[i]]; }
|
|
return list.slice(0, k);
|
|
}
|
|
function weekStats() {
|
|
const wk = store.ctWeek(); let vclicks = 0, reshares = 0, boosts = 0;
|
|
for (const c of store.clicks()) if (c.verified && c.wk === wk) vclicks++;
|
|
for (const s of store.state().shares) if (s.step > 0 && store.ctWeek(s.createdAt) === wk) reshares++;
|
|
for (const b of purchases()) if (store.ctWeek(b.blockAt ? b.blockAt * 1000 : b.at) === wk) boosts += Number(b.qty) || 1;
|
|
return { week: wk, boosts, vclicks, reshares, adsLive: store.liveAds().length };
|
|
}
|
|
|
|
// ---- verified visitors ----
|
|
function canVisit(vk, iph) {
|
|
if (fresh(lastVisitByVk, vk)) return { ok: true, again: true };
|
|
const m = ipVks.get(iph); let n = 0; if (m) for (const [k, t] of m) { if (Date.now() - t < H24) n++; else m.delete(k); }
|
|
return n >= C().perIpDay ? { ok: false, why: 'ip' } : { ok: true };
|
|
}
|
|
// credit a resharer for one verified view through their share link
|
|
function earnView(owner) {
|
|
const c = store.credits(owner); c.views++;
|
|
if (c.views % C().credits.views === 0) { c.earned += C().credits.per; store.bump('creditsEarned', C().credits.per); }
|
|
}
|
|
// t = { vk, iph, page, shown: [{ id, share, slot }], viewer }. -> { verified, why?, views, credited, spent }
|
|
function verify(t) {
|
|
const chk = canVisit(t.vk, t.iph); if (!chk.ok) return { verified: false, why: chk.why };
|
|
const now = Date.now(); const day = store.ctDay(now), wk = store.ctWeek(now);
|
|
if (!chk.again) { lastVisitByVk.set(t.vk, now); ipAdd(t.iph, t.vk, now); store.addVisit({ at: now, vk: t.vk, iph: t.iph, page: t.page, src: t.src || '', share: t.via || null, wk, day }); store.state().totals.visits++; store.bump('visits'); }
|
|
let views = 0, credited = 0, spent = 0; const done = new Set();
|
|
for (const x of t.shown || []) {
|
|
if (done.has(x.id)) continue; done.add(x.id);
|
|
const a = store.ad(x.id); if (!a || a.status !== 'live' || (t.viewer && a.owner === t.viewer)) continue;
|
|
const k = t.vk + '|' + a.id; if (fresh(viewSeen, k)) continue;
|
|
viewSeen.set(k, now); if (viewSeen.size > 300000) viewSeen.clear();
|
|
a.views = (a.views || 0) + 1; views++; store.saveAd(a);
|
|
const s = x.share ? store.share(x.share) : null;
|
|
if (s && s.adId === a.id && !(t.viewer && s.owner === t.viewer)) {
|
|
s.views = (s.views || 0) + 1; store.saveShare(s);
|
|
if (s.step > 0) { earnView(s.owner); credited++; }
|
|
}
|
|
if (x.slot === 'credit' && store.creditBalance(a.owner) > 0) { store.credits(a.owner).spent++; spent++; store.state().totals.creditViews++; }
|
|
}
|
|
if (views) { store.state().totals.views += views; store.bump('views', views); }
|
|
store.save();
|
|
return { verified: true, views, credited, spent };
|
|
}
|
|
function click(a, vk, verifiedVisitor, viewer, s, placement) {
|
|
a.clicks = (a.clicks || 0) + 1; store.state().totals.clicks++; store.bump('clicks');
|
|
if (s) { s.clicks = (s.clicks || 0) + 1; }
|
|
let verified = false;
|
|
if (verifiedVisitor && fresh(lastVisitByVk, vk) && !(viewer && (viewer === a.owner || (s && viewer === s.owner)))) {
|
|
const k = vk + '|' + a.id;
|
|
if (!fresh(clickSeen, k)) {
|
|
clickSeen.set(k, Date.now()); verified = true; a.vclicks = (a.vclicks || 0) + 1; store.state().totals.vclicks++; store.bump('vclicks');
|
|
if (s) s.vclicks = (s.vclicks || 0) + 1;
|
|
store.addClick({ vk, adId: a.id, share: s ? s.id : null, verified: true, p: String(placement || '').slice(0, 20), wk: store.ctWeek(), day: store.ctDay() });
|
|
}
|
|
}
|
|
if (s) store.saveShare(s);
|
|
store.saveAd(a); return { verified };
|
|
}
|
|
let SECRET = crypto.randomBytes(16).toString('hex');
|
|
function setSecret(s) { SECRET = String(s); }
|
|
const vtoken = vk => crypto.createHmac('sha256', SECRET).update('vt|' + vk + '|' + store.ctDay()).digest('hex').slice(0, 24);
|
|
const vtokenOk = (vk, t) => typeof t === 'string' && t.length === 24 && crypto.timingSafeEqual(Buffer.from(t), Buffer.from(vtoken(vk)));
|
|
|
|
module.exports = { init, rebuild, purchases, boostedUntil, isBoosted, attach, attachPending, boostView, adCard, board, creditPicks, weekStats,
|
|
canVisit, verify, click, vtoken, vtokenOk, setSecret, hostOf };
|