Hub rule 2 (Marty 2026-11-14): every paid Purchase on the InstantAdPay contract goes to the hub's /v1/contract-spend (keyed by the position wallet so its bind is sent first, tx hash so a resend counts once, POL-in only), so when two contracts lock different sponsors the one with money spent decides the network sponsor. Still one way, only with HUB_ENABLED; needs network-hub 296aa5d. qa/hubsync.test.cjs 15/15

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-14 07:53:05 -06:00
parent ff568d973c
commit a01d6f2e8c
2 changed files with 44 additions and 1 deletions
+21 -1
View File
@@ -26,6 +26,8 @@
// - a DELIBERATE sponsor change (sponsorfollow: adopt, rescue, release, expiry, admin) goes to /v1/sponsor-change;
// - every MemberActivated on the InstantAdPay contract goes to /v1/contract-bind (contract 'iap', the sponsor by wallet),
// by email when the wallet belongs to an account here, else by wallet alone.
// - every paid Purchase goes to /v1/contract-spend (2026-11-14, rule 2: the contract with money spent decides the network
// sponsor when two contracts hold different ones).
// Where the hub and InstantAdPay name different sponsors, nothing is forced on either side: the case goes to `disagree`
// (Admin > /api/admin/hub) for Marty, since an InstantAdPay member bound on our contract cannot move anyway.
'use strict';
@@ -187,6 +189,7 @@ function touch(email) {
}
// chain.onEvent (once across copies): a payout switch-on on the InstantAdPay contract goes to /v1/contract-bind
function onChain(ev) {
if (ev && ev.type === 'Purchase') return onPurchase(ev);
if (!R || !on() || !ev || ev.type !== 'MemberActivated' || !ev.account) return;
(async () => {
let spW = null; try { const m = ev.sponsorId ? await R.chain.member(ev.sponsorId) : null; spW = m && m.account ? norm(m.account) : null; } catch (e) {}
@@ -194,6 +197,19 @@ function onChain(ev) {
s.outbox.push({ kind: 'bind', email: norm(ev.account), wallet: norm(ev.account), sponsorWallet: spW, at: Date.now(), tries: 0, next: 0 }); save(); kick();
})().catch(err => console.error('hubsync onChain', err.message));
}
// chain.onEvent: money paid into the InstantAdPay contract (Purchase with POL in) goes to /v1/contract-spend (Marty 2026-11-14,
// rule 2: when two contracts hold different sponsors, the one with money spent decides the network sponsor). Keyed by the
// buyer's position wallet, the same key as its bind, so the bind is always sent first; the tx hash makes a resend count once.
// A 404 (bind not there yet, or a linked extra position the hub does not know) is retried like any other miss.
function onPurchase(ev) {
if (!R || !on() || !ev.buyerId || !(Number(ev.priceCents) > 0)) return;
try { if (BigInt(ev.paidWei || '0') <= 0n) return; } catch (e) { return; } // nothing paid in: not a spend
(async () => {
const m = await R.chain.member(ev.buyerId); const w = m && m.account ? norm(m.account) : null; if (!w) return;
const s = load(); if (s.outbox.length >= 20000) { s.stats.sendErrors++; return; }
s.outbox.push({ kind: 'spend', email: w, wallet: w, cents: Number(ev.priceCents), tx: ev.tx || null, paidAt: Number(ev.ts) || Date.now(), at: Date.now(), tries: 0, next: 0 }); save(); kick();
})().catch(err => console.error('hubsync onPurchase', err.message));
}
// accounts.onWalletLinked: a main wallet linked here goes to /v1/wallet
function wallet(email) {
if (!R || !on() || !email) return;
@@ -202,8 +218,12 @@ function wallet(email) {
if (a && a.address && !R.accounts.isUnclaimedShell(a)) enqueue('wallet', norm(email)); // a waiting account's wallet came from a sync
})().catch(err => console.error('hubsync wallet', err.message));
}
const ROUTE = { join: '/v1/join', wallet: '/v1/wallet', sponsor: '/v1/sponsor-change', bind: '/v1/contract-bind' };
const ROUTE = { join: '/v1/join', wallet: '/v1/wallet', sponsor: '/v1/sponsor-change', bind: '/v1/contract-bind', spend: '/v1/contract-spend' };
async function bodyFor(item) {
if (item.kind === 'spend') {
let owner = null; try { owner = (await R.accounts.byAddress(item.wallet)) || (R.accounts.positionOwner ? await R.accounts.positionOwner(item.wallet) : null); } catch (e) {}
return { contract: 'iap', email: owner && owner.email ? owner.email : undefined, wallet: item.wallet, cents: item.cents, tx: item.tx || undefined, at: item.paidAt };
}
if (item.kind === 'bind') {
let owner = null; try { owner = await R.accounts.byAddress(item.wallet); } catch (e) {}
return { contract: 'iap', email: owner && owner.email ? owner.email : undefined, wallet: item.wallet, sponsorRef: item.sponsorWallet || '' };