Files
linkspin/registry.js
T
martbost a5c2c38106 Sponsor follow + one network holding tank + Sponsor check (Marty, 2026-10-22, approved 1-3)
Same design as InstantAdPay (shared sponsorfollow.js). Deliberate sponsor changes (tank adoption, rescue, release,
adoption window ending, admin edit) are recorded and travel on the iapsync rows; InstantAdPay's deliberate changes are
applied here only when the member is not bound or binding on the LinkSpin contract (memberId, wallet on chain, or an
engine activation in flight), newer than our own, and both sides agreed before (or no sponsor here). Bound / diverged /
race cases change nothing and are logged. Ordinary rows keep the fill-empty rule, except a sponsor emptied on purpose
here is not refilled by an ordinary row.

Carry: an InstantAdPay batch holds its members (carry.hold) so no auto-activation starts mid-batch; ensureOnChain waits,
then re-reads the account so the contract binds the settled sponsor; wallets from a batch are linked after every sponsor
decision; the wallet hook skips activation while a follow is pending and activates once it is settled.
Registry keeps the follow record (loop guard) and a deliberate clear. Network tank, Admin > Members > Sponsor check and
the partner endpoints as on InstantAdPay. Rows without the new facts hash as before (no resend on deploy).

Tests: qa/sponsorfollow.test.cjs 27/27, qa/carry-hold.test.cjs ok, two-server e2e (in the InstantAdPay repo) 20/20,
QA member walk 0 bugs. No sponsor was changed on production.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 09:05:20 -05:00

67 lines
4.1 KiB
JavaScript

// Network registry (LinkSpin, 2026-09-15): who is who across the Crypto Team Build Network
// properties, keyed by email (and by main wallet once one exists). InstantAdPay writes it as
// members join, name themselves, link a wallet and bind a sponsor; LinkSpin reads it at sign-in
// and wallet link so a member's sponsor line carries over. Nothing here moves money: each
// contract still binds its own sponsor at first activation. Dual-mode store like the rest of
// the engine (MySQL when NETWORK_DB_URL is set, JSON on the volume otherwise). In the test area
// the JSON file is seeded from an export of the live InstantAdPay accounts.
const fs = require('fs');
const path = require('path');
let DATA_DIR = null;
const norm = e => String(e || '').trim().toLowerCase();
const J = {
db: null,
FILE: () => path.join(DATA_DIR, 'registry.json'),
load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, byEmail: {} }; } if (!this.db.byEmail) this.db.byEmail = {}; },
save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} },
async get(email) { if (!this.db) this.load(); return this.db.byEmail[norm(email)] || null; },
async byWallet(addr) { if (!this.db) this.load(); const a = norm(addr); return Object.values(this.db.byEmail).find(r => norm(r.wallet) === a) || null; },
async byUsername(u) { if (!this.db) this.load(); const x = norm(u); return Object.values(this.db.byEmail).find(r => norm(r.username) === x) || null; },
async upsert(rec) {
if (!this.db) this.load();
const e = norm(rec.email); if (!e) return null;
const cur = this.db.byEmail[e] || { email: e, created: Date.now() };
for (const k of ['username', 'wallet', 'sponsorEmail', 'firstProperty', 'joinedAt', 'profile']) {
if (rec[k] == null || rec[k] === '') continue;
if (k === 'firstProperty' && cur.firstProperty) continue; // first contact is history: an InstantAdPay echo of a LinkSpin-first row must not relabel it (2026-10-15)
cur[k] = k === 'wallet' || k === 'sponsorEmail' ? norm(rec[k]) : rec[k];
}
// a deliberate sponsor change on InstantAdPay (sponsorfollow.js, 2026-10-22): the registry holds that record too, so
// iapsync can tell our echo of it apart from a change of our own; a deliberate clear (back to the tank) clears it here
if (rec.sponsorSetBy && Number(rec.sponsorSetAt) > 0) {
cur.sponsorSetAt = Number(rec.sponsorSetAt); cur.sponsorSetBy = String(rec.sponsorSetBy);
cur.sponsorPrevEmail = rec.sponsorPrevEmail ? norm(rec.sponsorPrevEmail) : null; cur.sponsorSetOrigin = rec.sponsorSetOrigin ? norm(rec.sponsorSetOrigin) : null;
if (!rec.sponsorEmail) cur.sponsorEmail = null;
}
cur.updated = Date.now(); this.db.byEmail[e] = cur; this.save(); return cur;
},
async all() { if (!this.db) this.load(); return Object.values(this.db.byEmail); },
async count() { if (!this.db) this.load(); return Object.keys(this.db.byEmail).length; }
};
function init(opts) { DATA_DIR = opts.dataDir; }
async function get(email) { return J.get(email); }
async function byWallet(addr) { return J.byWallet(addr); }
async function byUsername(u) { return J.byUsername(u); }
async function upsert(rec) { return J.upsert(rec); }
async function all() { return J.all(); }
async function count() { return J.count(); }
// the sponsor chain above an email, nearest first, bounded (a loop in bad data must not hang a request)
async function chainUp(email, max = 25) {
const out = []; let cur = await get(email); const seen = new Set([norm(email)]);
while (cur && cur.sponsorEmail && out.length < max) {
const e = norm(cur.sponsorEmail); if (seen.has(e)) break; seen.add(e);
const sp = await get(e); if (!sp) break;
out.push(sp); cur = sp;
}
return out;
}
// bulk load from an InstantAdPay export: [{email, username, wallet, sponsorEmail, joinedAt}]
async function importRows(rows, property) {
let n = 0; for (const r of rows || []) { if (await upsert(Object.assign({ firstProperty: property || 'instantadpay' }, r))) n++; } return n;
}
module.exports = { init, get, byWallet, byUsername, upsert, all, count, chainUp, importRows };