diff --git a/chain.js b/chain.js index 4c43969..1aaabc9 100644 --- a/chain.js +++ b/chain.js @@ -792,6 +792,60 @@ function memberIdByAccount(address) { return null; } +// --- Public "Find a member" lookup (/lookup). Index first, RPC only on a miss. +// The indexed state already holds every position's account, sponsor, tier, level, +// direct count and join time (daily snapshot + the one-minute log tail), so a normal +// lookup costs ZERO RPC calls. Only a position or wallet the index has not seen yet +// (joined since the last tick, or index still cold) falls back to one eth_call. +// Returns chain facts only: never emails, names or any other off-chain data. +const SEL_MEMBER_ID = '0x39106821'; // memberId(address) -> uint48 (keccak-256 of the signature) +let acctMap = null, acctMapN = -1, acctMapAt = 0; +function idForAccountIndexed(a) { + if (!state || !state.members) return null; + const n = Object.keys(state.members).length; + if (!acctMap || n !== acctMapN || Date.now() - acctMapAt > 300000) { + acctMap = new Map(); + for (const [id, m] of Object.entries(state.members)) if (m && m.account) acctMap.set(String(m.account).toLowerCase(), Number(id)); + acctMapN = n; acctMapAt = Date.now(); + } + return acctMap.get(a) || null; +} +function lookupShape(id, m, source) { + const lvl = m.level || 1, tier = m.tier === 2 ? 2 : (m.tier || 1); + return { + found: true, id: Number(id), account: String(m.account).toLowerCase(), + level: lvl, levelName: levelName(lvl), tier, tierName: tierName(tier), + joinedAt: m.joinedAt || null, + sponsorId: m.referrerId && Number(m.referrerId) !== Number(id) ? Number(m.referrerId) : null, + directCount: m.directCount || 0, + source, indexedAt: state && state.updatedAt ? state.updatedAt : null + }; +} +function parseLookupQuery(raw) { + const s = String(raw == null ? '' : raw).trim().replace(/^#/, ''); + if (/^\d{1,15}$/.test(s)) { const id = Number(s); if (id >= 1 && id <= 281474976710655) return { kind: 'id', id }; return { error: 'That member ID is out of range.' }; } + if (/^0x[0-9a-fA-F]{40}$/.test(s)) return { kind: 'address', address: s.toLowerCase() }; + return { error: 'Enter a member ID (like 21) or a full wallet address (0x followed by 40 characters).' }; +} +async function lookupPublic(raw, opts = {}) { + const q = parseLookupQuery(raw); + if (q.error) return { error: q.error }; + const allowRpc = opts.allowRpc !== false; + let id = q.kind === 'id' ? q.id : idForAccountIndexed(q.address); + if (!id && q.kind === 'address') { + if (!allowRpc) return { found: false, query: q.address, busy: true }; + const r = await ethCall(SEL_MEMBER_ID + q.address.slice(2).padStart(64, '0')); + id = wInt(r, 0) || 0; + if (!id) return { found: false, query: q.address }; + } + const m = state && state.members ? state.members[id] : null; + if (m && m.account && !m.preIndexed) return lookupShape(id, m, 'index'); + if (!allowRpc) return { found: false, query: String(id), busy: true }; + const live = await fetchMember(id); + if (!live) return { found: false, query: String(id) }; + return lookupShape(id, live, 'chain'); +} + // Company-rotation pick: breadth-first (matrix order, left→right) first // position under `rootId` that still needs directs — the "next open team // position" for the public rotation when publicRotationMode==='chain'. @@ -902,4 +956,6 @@ async function paymentPath(id) { return { found: true, buyer, uplines, stopped, costs: costs ? { up: costs.up } : null, at: Date.now() }; } -module.exports = { paymentPath, startIndexer, getPayoutsPublic, verifyMember, memberLookup, memberPublic, getIncome, getOwnerUpgradeNeeds, getOrgRouting, getOrgShare, getCoachingScan, getMatrixTree, isInTeam, nextOpenPosition, depthFrom, memberIdByAccount, liveDirects, balanceOf, CONTRACT }; +// QA only: seed the in-memory index without starting the RPC indexer (qa/lookup-*.mjs, harness). +const __test = { setState(s) { state = s; acctMap = null; } }; +module.exports = { __test, lookupPublic, parseLookupQuery, paymentPath, startIndexer, getPayoutsPublic, verifyMember, memberLookup, memberPublic, getIncome, getOwnerUpgradeNeeds, getOrgRouting, getOrgShare, getCoachingScan, getMatrixTree, isInTeam, nextOpenPosition, depthFrom, memberIdByAccount, liveDirects, balanceOf, CONTRACT }; diff --git a/private/who-gets-paid.html b/private/who-gets-paid.html index e0a4d9f..aa70daf 100644 --- a/private/who-gets-paid.html +++ b/private/who-gets-paid.html @@ -105,7 +105,7 @@ if (u.level > levelIndex && u.directCount >= 2) { Every row also needs 2 directs. The amount is set by the buyer's tier; Standard is exactly half of Premium. The search follows the matrix (where each member was placed), not the sponsor line. Can the contract change? See the security review →
- +