The Draft on the real holding tank (2026-11-15, LOCAL ONLY): tankpool.js 'iap' mode = signed client for InstantAdPay's draft partner routes
TANK_MODE=iap (or hub) + IAP_URL + DRAFT_PARTNER_SECRET: pool / adopt / release / status as HMAC-signed POSTs ('draft.v1.' + ts +
newline + body, X-Draft-Ts / X-Draft-Sig). The stub stays the default and keeps every existing test. adopt carries the coach claim
(wallet, hubId, username, positions = store.positionsOf) and a request id; one retry with the same rid on a network failure, so a
retry never makes two adoptions. release sends why 'coach' | 'return' + the pick's iap ref and takes InstantAdPay's outcome
(returned / kept / done). league.sweep follows a pick InstantAdPay closed first (its own 3-day sweep, a release there, a LinkSpin
follow). Surfing arrives as ad credits (surfCredits, shown "N ad credits" to coaches, +10 scouting like surf POL); games stay
"not tracked yet" (InstantAdPay has no per-member game record).
Fix found by the cross test: a tank refusal about the COACH (no InstantAdPay account, full, not vested, hub off) used to mark the
person unavailable and let the same coach choose again, which would burn the whole pool; now the slot passes (skip 'tank: <code>')
and a silent tank pauses the draft (nothing recorded, the next tick retries the same slot). Manual picks record nothing on either.
Tests: unit 62/62 (2 new: skip + wait), smoke 156/156 (ports 3561-3564), qa/iap-tank.test.cjs 23/23 (this client + league against
InstantAdPay's REAL draftpartner.js + tank.js from the sibling checkout behind a signed server on 3565, then a dead port 3566).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+62
-13
@@ -13,16 +13,21 @@
|
||||
// 'stub' (phase 1, TANK_MODE unset): a local fixture (STUB_POOL_FILE, else qa/fixtures/pool.json) with relative times, adoption
|
||||
// state in the document stub-tank.json. It adopts nobody for real and sends no mail. Admin test routes can move a person's
|
||||
// milestones (POST /api/admin/stub/status) so the coaching tools, cards and boards can be exercised.
|
||||
// 'hub' (phase 2): NOT WIRED. Every call answers { error } / [] so nothing is drafted. What it needs (open for Marty + the hub):
|
||||
// - a pool read: the network tank's waiting list (InstantAdPay tank.waiting() is the source today, the hub has no tank route)
|
||||
// with rule 3 applied (hub /v1/locked detail), username, door (hub joinedVia), last seen, wallet linked
|
||||
// - scouting stats where they exist: game plays + POL earned (game wallet watchers), surf earnings (IAP ads.earnedTotals)
|
||||
// - adopt: the tank's own adopt (so the 2-adoption count, the hold, the adoption email and the Telegram line stay in one place),
|
||||
// e.g. IAP POST /api/admin/tank/adopt with the coach as adopter, then hub /v1/sponsor-change { by: 'adopt' }
|
||||
// - status: payouts on / first purchase per person (IAP memberId + chain Purchase), hub /v1/activity/person for last seen
|
||||
// 'iap' (phase 2, TANK_MODE=iap; 'hub' is read the same): the REAL network holding tank on InstantAdPay, through its signed partner
|
||||
// routes (InstantAdPay site/draftpartner.js, 2026-11-15). One secret both sides, DRAFT_PARTNER_SECRET (32+ characters), sent as
|
||||
// X-Draft-Ts (ms) + X-Draft-Sig = hex HMAC-SHA256('draft.v1.' + ts + newline + raw body); IAP_URL is the base.
|
||||
// POST /api/partner/draft/pool {} -> { ok, rules, pool: [card] }
|
||||
// POST /api/partner/draft/adopt { rid, coach: { wallet, hubId, username, positions }, person, kind, week, slot } -> { ok, ref } | { error, code }
|
||||
// POST /api/partner/draft/release { rid, coach: { wallet }, ref, person, why: 'coach' | 'return' } -> { ok, outcome } | { error, code }
|
||||
// POST /api/partner/draft/status { ids } -> { ok, people: { id: milestones + pick | null } }
|
||||
// InstantAdPay runs tank.adopt() for every pick (the 2-adoption count, the 24 h grace, rule 3, the normal adoption email, the hub's
|
||||
// sponsor change) and checks "holds a 5x position" from the coach claim this app signs (positions = store.positionsOf). It owns the
|
||||
// 3-day window too (its hourly sweep returns unstarted picks); this app's own sweep calls release(why 'return') and takes
|
||||
// InstantAdPay's outcome (returned / kept / done). A call that fails answers { error } / [] / null: nothing is drafted or moved.
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const H = 3600000, D = 86400000;
|
||||
let C = null, mode = 'stub', fixture = null, nowFn = () => Date.now();
|
||||
@@ -37,9 +42,34 @@ function init(cfg, opts) {
|
||||
try { fixture = JSON.parse(fs.readFileSync(f, 'utf8')); } catch (e) { fixture = { people: [] }; console.error('tankpool: no stub pool at', f); }
|
||||
fixture.base = nowFn(); // relative times are measured from boot
|
||||
console.log('tankpool: STUB mode (' + fixture.people.length + ' fixture people, nobody is adopted for real, no mail)');
|
||||
} else console.log('tankpool: hub mode is not wired yet (phase 2): the pool is empty and no pick can be made');
|
||||
} else console.log('tankpool: InstantAdPay mode (' + C.iapBase + '/api/partner/draft/*' + (secret() ? '' : ', DRAFT_PARTNER_SECRET MISSING: every call refuses') + ')');
|
||||
}
|
||||
const isStub = () => mode === 'stub';
|
||||
// ---- the InstantAdPay client ----
|
||||
const secret = () => { const s = String(process.env.DRAFT_PARTNER_SECRET || '').trim(); return s.length >= 32 ? s : ''; };
|
||||
const remote = { calls: 0, failed: 0, lastError: null, lastPool: 0 };
|
||||
async function call(op, body, timeoutMs) {
|
||||
const sec = secret(); if (!sec) return { error: 'The holding tank is not connected (no DRAFT_PARTNER_SECRET).', code: 'no_secret' };
|
||||
const raw = JSON.stringify(body || {}); const ts = String(Date.now());
|
||||
const sig = crypto.createHmac('sha256', sec).update('draft.v1.' + ts + '\n' + raw).digest('hex');
|
||||
remote.calls++;
|
||||
try {
|
||||
const r = await fetch(C.iapBase + '/api/partner/draft/' + op, { method: 'POST', body: raw, signal: AbortSignal.timeout(timeoutMs || 15000), headers: { 'Content-Type': 'application/json', 'X-Draft-Ts': ts, 'X-Draft-Sig': sig } });
|
||||
const j = await r.json().catch(() => null);
|
||||
if (!j || typeof j !== 'object') throw new Error('InstantAdPay answered ' + r.status + ' with no JSON');
|
||||
if (r.status >= 500 || r.status === 401 || r.status === 404) throw new Error('InstantAdPay answered ' + r.status + (j.error ? ': ' + j.error : ''));
|
||||
return j; // 200 { ok ... } or 400 { error, code }
|
||||
} catch (e) {
|
||||
remote.failed++; remote.lastError = { at: Date.now(), op, error: String(e.name === 'TimeoutError' ? 'timeout' : e.message).slice(0, 160) };
|
||||
console.error('tankpool:', op, 'failed:', remote.lastError.error);
|
||||
return { error: 'The holding tank did not answer.', code: 'unreachable', transient: true };
|
||||
}
|
||||
}
|
||||
const rid = () => 'fx-' + Date.now().toString(36) + '-' + crypto.randomBytes(6).toString('hex');
|
||||
// a card from InstantAdPay -> the candidate shape draft.js reads (InstantAdPay already left out locked / sponsored / spam / twice)
|
||||
const candOf = c => ({ id: String(c.id), username: c.username || null, joinedAt: Number(c.joinedAt) || 0, lastSeen: Number(c.lastSeen) || 0, door: c.door || null, wallet: !!c.wallet,
|
||||
locked: false, sponsored: false, spam: false, throwaway: false, adoptions: Number(c.adoptions) || 0, games: c.games || null, surfWei: c.surfWei || null,
|
||||
surfCredits: c.surfCredits == null ? null : Number(c.surfCredits) || 0, opensAt: Number(c.opensAt) || 0, ready: !!c.ready, holdUntil: Number(c.holdUntil) || 0 });
|
||||
const ago = (h, base) => (h == null ? 0 : base - h * H);
|
||||
// a fixture person at time `now` (relative hours), with any admin override applied
|
||||
function person(p, s, now) {
|
||||
@@ -52,20 +82,35 @@ function person(p, s, now) {
|
||||
}
|
||||
|
||||
async function candidates(now) {
|
||||
if (!isStub()) return [];
|
||||
if (!isStub()) { const r = await call('pool', {}); if (!r.ok || !Array.isArray(r.pool)) return []; remote.lastPool = r.pool.length; return r.pool.filter(c => c && c.id).map(candOf); }
|
||||
const s = st(); now = now || nowFn();
|
||||
return fixture.people.filter(p => !s.adopted[p.id]).map(p => { const x = person(p, s, now); return Object.assign(x, { wallet: !!x.wallet }); })
|
||||
.filter(x => !x.payouts && !x.purchase); // payouts on = out of the tank (the IAP rule)
|
||||
}
|
||||
async function status(ids, now) {
|
||||
const out = {}; if (!isStub()) { for (const id of ids) out[id] = null; return out; }
|
||||
const out = {};
|
||||
if (!isStub()) {
|
||||
for (const id of ids) out[id] = null;
|
||||
for (let i = 0; i < ids.length; i += 500) {
|
||||
const r = await call('status', { ids: ids.slice(i, i + 500) }); if (!r.ok || !r.people) continue; // the network cannot say: nothing changes
|
||||
for (const [id, s] of Object.entries(r.people)) if (s) out[id] = { lastSeen: Number(s.lastSeen) || 0, username: s.username || null, wallet: Number(s.wallet) || 0, payouts: Number(s.payouts) || 0,
|
||||
purchase: Number(s.purchase) || 0, games: s.games || null, surfWei: s.surfWei || null, surfCredits: s.surfCredits == null ? null : Number(s.surfCredits) || 0, adoptions: Number(s.adoptions) || 0, door: s.door || null, pick: s.pick || null };
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const s = st(); now = now || nowFn();
|
||||
for (const id of ids) { const p = fixture.people.find(x => x.id === id); if (!p) { out[id] = null; continue; } const x = person(p, s, now);
|
||||
out[id] = { lastSeen: x.lastSeen, username: x.username, wallet: x.wallet, payouts: x.payouts, purchase: x.purchase, games: x.games, surfWei: x.surfWei, adoptions: x.adoptions, door: x.door }; }
|
||||
return out;
|
||||
}
|
||||
async function adopt(o) {
|
||||
if (!isStub()) return { error: 'The holding tank is not connected yet.' };
|
||||
if (!isStub()) {
|
||||
const c = o.coach || {}; const body = { rid: rid(), coach: { wallet: c.address, hubId: c.hubId || null, username: c.username || null, positions: Number(c.positions) || 0 },
|
||||
person: o.person, kind: o.kind || 'draft', week: o.week || null, slot: o.slot || null, note: o.note || '' };
|
||||
let r = await call('adopt', body);
|
||||
if (r.transient) r = await call('adopt', body); // one retry with the SAME rid: InstantAdPay answers the first result, never two adoptions
|
||||
return r.ok ? { ok: true, ref: r.ref || null, expires: r.expires || null } : { error: r.error || 'The holding tank refused.', code: r.code || null };
|
||||
}
|
||||
const s = st(); const p = fixture.people.find(x => x.id === o.person);
|
||||
if (!p) return { error: 'Not in the holding tank.' };
|
||||
if (p.locked) return { error: 'Locked on a network contract.' };
|
||||
@@ -76,7 +121,11 @@ async function adopt(o) {
|
||||
return { ok: true, ref: 'stub:' + p.id };
|
||||
}
|
||||
async function release(o) {
|
||||
if (!isStub()) return { error: 'The holding tank is not connected yet.' };
|
||||
if (!isStub()) {
|
||||
const body = { rid: rid(), coach: { wallet: o.coach }, ref: o.ref || null, person: o.person, why: o.kind === 'return' || /window/.test(o.why || '') ? 'return' : 'coach' };
|
||||
let r = await call('release', body); if (r.transient) r = await call('release', body);
|
||||
return r.ok ? { ok: true, outcome: r.outcome || 'returned', already: !!r.already } : { error: r.error || 'The holding tank refused.', code: r.code || null };
|
||||
}
|
||||
const s = st(); if (!s.adopted[o.person]) return { ok: true };
|
||||
delete s.adopted[o.person]; s.log.push({ at: nowFn(), release: o.person, why: o.why || '' }); put(s); return { ok: true };
|
||||
}
|
||||
@@ -87,5 +136,5 @@ function setStatus(id, patch) {
|
||||
for (const k of ['wallet', 'payouts', 'purchase', 'seen']) if (patch[k] !== undefined) o[k] = patch[k] ? (typeof patch[k] === 'number' ? patch[k] : t) : 0;
|
||||
put(s); return { ok: true, over: o };
|
||||
}
|
||||
function view() { if (!C) return {}; const s = isStub() ? st() : null; return { mode, fixture: fixture ? fixture.people.length : 0, adopted: s ? Object.keys(s.adopted).length : 0, adoptionMailsThatWouldSend: s ? s.mails : 0, log: s ? s.log.slice(-20) : [] }; }
|
||||
function view() { if (!C) return {}; const s = isStub() ? st() : null; return { mode, remote: isStub() ? null : Object.assign({ url: C.iapBase, secret: !!secret() }, remote), fixture: fixture ? fixture.people.length : 0, adopted: s ? Object.keys(s.adopted).length : 0, adoptionMailsThatWouldSend: s ? s.mails : 0, log: s ? s.log.slice(-20) : [] }; }
|
||||
module.exports = { init, candidates, status, adopt, release, setStatus, view, isStub, H, D };
|
||||
|
||||
Reference in New Issue
Block a user