Never hold a purchase: the 3-day claim window is gone. A sponsor with a wallet is activated by the engine (one retry); no wallet, unknown sponsor or a failed activation walks up to the nearest ancestor with a wallet, else the company. Only the buy (intent=buy) moves the member and sends notices; /api/me and /api/sponsor block only on an RPC hiccup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-15 07:55:31 -05:00
parent f47c4ecd54
commit 4a432975b0
6 changed files with 7282 additions and 7279 deletions
+29 -28
View File
@@ -9,9 +9,8 @@
// position on this contract, the engine activates them on their behalf (activateFor,
// engine-signed, gas paid by the engine), walking up until it reaches someone already
// on-chain. The sponsor is told, and their first payout email follows on its own.
// - Sponsor with no wallet at all: the claim window. The member can do everything except
// buy; the sponsor has CLAIM_DAYS to link a wallet. After that the seat walks up to the
// nearest ancestor with a wallet and both sides are told.
// - Sponsor with no wallet at all: NO hold (the claim window was removed 2026-10-15). At the
// buy, the seat walks up to the nearest ancestor with a wallet and both sides are told.
let ethers = null; try { ethers = require('ethers'); } catch (e) {}
const fs = require('fs');
const path = require('path');
@@ -82,36 +81,38 @@ async function onJoin(acct, usedRef) {
}
// ---- before activation / purchase: make sure the sponsor exists on this contract ----
// returns { sponsorId, hold, claim } — hold is set when the buy must wait (claim window)
async function resolveForChain(acct, spd) {
// A purchase is NEVER held (Marty, 2026-10-15: "We should never, ever hold up a purchase"; the 3-day claim
// window is gone). Returns { sponsorId, routed } or null when the normal resolver already has the answer.
// - Sponsor with a wallet: the engine activates them (gas on us) and they are paid, wallet off or not.
// - Sponsor with no wallet, unknown, or an activation that failed: the sale goes to the nearest ancestor
// with a wallet (activated on the spot), else sponsorId 0 = the caller's company fallback.
// Only `commit` (the buyer is about to sign) moves the member and sends the notices; a display check just answers.
async function resolveForChain(acct, spd, opts) {
if (!acct || !acct.sponsorRef) return null;
if (spd.id) return null; // already on-chain: nothing to do
if (spd.reason !== 'notActivated') return null; // unknown token or rpc: the normal guard handles it
if (spd.reason === 'rpc' || spd.reason === 'none') return null; // a chain hiccup is retried by the client; nothing is charged
const commit = !!(opts && opts.commit);
let sp = await X.accounts.byCode(acct.sponsorRef); if (!sp) sp = await X.accounts.byUsername(acct.sponsorRef);
if (!sp) return null;
if (sp.address) {
const id = await ensureOnChain(sp, 0);
return id ? { sponsorId: id } : null;
if (sp && sp.address) {
let id = await ensureOnChain(sp, 0);
if (!id) id = await ensureOnChain(sp, 0); // one retry before passing a wallet holder over for our own failure
if (id) return { sponsorId: id };
logEvent({ type: 'activateGaveUp', member: acct.email, sponsor: sp.email });
}
// no wallet: the claim window
const st = state(); const k = norm(acct.email);
let c = st.claims[k];
if (!c) {
c = { member: k, sponsor: sp.email, opened: Date.now(), deadline: Date.now() + claimDays() * DAY, notified: true }; st.claims[k] = c; setState(st);
await tell(sp.email, label(acct) + ' is ready to buy on LinkSpin. Link a wallet to be their sponsor',
label(acct) + ' joined LinkSpin under you and wants to buy a package. The contract can only pay a wallet, and there is none on your LinkSpin account yet.\n\nLink one within ' + claimDays() + ' days (Wallet tab, one free signature) and their purchases pay you. If the window passes, the seat goes to the next person above you who has a wallet.\n\nhttps://' + (X.host || 'linkspin.co') + '/my#wallet');
// walk up to the first ancestor with a wallet the engine can place on the contract
let up = sp ? await nearestWithWallet(sp.email) : null; let id = 0; let hops = 0;
while (up && hops < 25) { id = await ensureOnChain(up, 0); if (id) break; up = await nearestWithWallet(up.email); hops++; }
if (!id) up = null;
const routed = { from: sp ? label(sp) : null, to: up ? label(up) : 'the company', toEmail: up ? up.email : null };
if (commit) {
if (up) await X.accounts.setSponsorRef(acct.email, up.username || up.code);
logEvent({ type: 'walkedUp', member: acct.email, from: sp ? sp.email : acct.sponsorRef, to: up ? up.email : 'company' });
if (sp) await tell(sp.email, 'You missed ' + label(acct) + ' on LinkSpin: no wallet linked',
label(acct) + ' joined LinkSpin under you and bought a package, but there is no wallet on your LinkSpin account, so the contract could not pay you. The sale went to ' + routed.to + ', and ' + label(acct) + ' is now in their line.\n\nLink a wallet now (Wallet tab, one free signature) and every purchase from the people who join under you from here on pays you.\n\nhttps://' + (X.host || 'linkspin.co') + '/my#wallet');
if (up) await tell(up.email, label(acct) + ' was placed under you on LinkSpin',
label(acct) + ' joined through ' + (sp ? label(sp) : 'a link') + ', who has no wallet on LinkSpin, so the network placed them under you. Their purchases pay you from now on.');
}
if (Date.now() < c.deadline) return { sponsorId: 0, hold: true, claim: { sponsor: label(sp), deadline: c.deadline } };
// window passed: walk up to the nearest ancestor with a wallet, activate them, tell both sides
const up = await nearestWithWallet(sp.email);
if (!up) return { sponsorId: 0, hold: true, claim: { sponsor: label(sp), deadline: c.deadline, nobody: true } };
const id = await ensureOnChain(up, 0);
if (!id) return null;
await X.accounts.setSponsorRef(acct.email, up.username || up.code);
delete st.claims[k]; setState(st); logEvent({ type: 'walkedUp', member: acct.email, from: sp.email, to: up.email });
await tell(sp.email, 'The seat for ' + label(acct) + ' passed to ' + label(up), 'No wallet was linked within ' + claimDays() + ' days, so ' + label(acct) + ' was placed under ' + label(up) + ', the nearest person above you with a wallet. Link a wallet now and everyone who joins under you from here on is yours.');
await tell(up.email, label(acct) + ' was placed under you on LinkSpin', label(acct) + ' joined through ' + label(sp) + ', who has no wallet on LinkSpin, so the network placed them under you. Their purchases pay you from now on.');
return { sponsorId: id, movedTo: up.email };
return { sponsorId: id, routed };
}
async function nearestWithWallet(email) {