diff --git a/carry.js b/carry.js index 5ee74fb..7ec83e2 100644 --- a/carry.js +++ b/carry.js @@ -129,7 +129,7 @@ async function nearestWithWallet(email) { // activate `acct` on this contract via the engine, after its own sponsor is there (recursive, bounded) async function ensureOnChain(acct, depth, why) { if (!acct || !acct.address) return 0; - try { const id = await X.chain.memberIdByAccount(acct.address); if (id) return id; } catch (e) {} + try { const id = await X.chain.memberIdByAccount(acct.address); if (id) { if (Number(acct.memberId) !== Number(id)) await X.accounts.setMemberId(acct.email, id).catch(() => {}); return id; } } catch (e) {} if (!engineReady() || depth > 25) return 0; let sponsorId = 0; if (acct.sponsorRef) { @@ -151,6 +151,9 @@ async function ensureOnChain(acct, depth, why) { } if (!id) { logEvent({ type: 'activateFailed', email: acct.email, error: String((lastErr && (lastErr.shortMessage || lastErr.message)) || 'not on-chain after send').slice(0, 160) }); return 0; } logEvent({ type: 'activated', email: acct.email, id, sponsorId, tx: txHash }); + // keep the member number on the account: the ledger and every name lookup go by it (2026-10-18: 340 engine + // activations had none, so the live ledger showed "member #352" instead of the username) + await X.accounts.setMemberId(acct.email, id).catch(() => {}); await noticeActivated(acct, why); return id; } @@ -162,6 +165,20 @@ async function noticeActivated(acct, why) { + '\n\nSee your line: https://' + (X.host || 'linkspin.co') + '/my'); } +// backfill: every account with a wallet that is on the contract but has no member number gets it, read from the chain. +// Runs once at boot (and is safe to repeat); 4 reads a second so the shared RPC pool is not hammered. +async function backfillMemberIds() { + const all = await X.accounts.listAll(5000); let fixed = 0, missing = 0; + for (const a of all) { + if (!a.address || Number(a.memberId) > 0) continue; + let id = 0; try { id = Number(await X.chain.memberIdByAccount(a.address)) || 0; } catch (e) {} + if (id) { await X.accounts.setMemberId(a.email, id).catch(() => {}); fixed++; } else missing++; + await sleep(250); + } + logEvent({ type: 'memberIdBackfill', fixed, notOnChain: missing }); + return { fixed, notOnChain: missing }; +} + // pre-seed: activate every account with a wallet, sponsors before their downline async function seedAll(limit) { const all = await X.accounts.listAll(5000); @@ -188,4 +205,4 @@ async function seedAll(limit) { } function status() { const s = state(); return { engine: engineReady(), claims: Object.values(s.claims || {}), log: (s.log || []).slice(-50).reverse(), claimDays: claimDays() }; } -module.exports = { init, onJoin, resolveForChain, ensureOnChain, seedAll, status, engineReady }; +module.exports = { init, onJoin, resolveForChain, ensureOnChain, seedAll, status, engineReady, backfillMemberIds }; diff --git a/server.js b/server.js index 03f4016..5083a55 100644 --- a/server.js +++ b/server.js @@ -459,6 +459,8 @@ async function boot() { carry.init({ dataDir: DATA_DIR, accounts, registry, chain, mailer, messages, siteConfig, adminEmail: ADMIN_EMAIL, host: SITE_HOST }); // payouts on the moment a wallet is linked (Marty, 2026-10-17); the engine pays the gas, sponsors above are activated first accounts.onWalletLinked(async email => { const a = await accounts.byEmail(email); if (a && a.address) await carry.ensureOnChain(a, 0, 'wallet'); }); + // member numbers for accounts the engine activated before 2026-10-18 (the ledger names people by them); 60 s after boot so a deploy's old copy is gone + setTimeout(() => carry.backfillMemberIds().then(r => console.log('memberId backfill', JSON.stringify(r))).catch(e => console.error('memberId backfill', e.message)), 60000); // reverse registry sync (2026-10-15): account changes reach InstantAdPay within seconds; a 2-minute reconcile catches the rest iapsync.init({ dataDir: DATA_DIR, accounts, registry, host: SITE_HOST }); accounts.onChange(email => iapsync.touch(email)); setTimeout(() => iapsync.tick().catch(e => console.error('iapsync', e.message)), 2 * 60 * 1000);