Files
vbb/public/assets/buy.js
T
martbost aee1ccc36a VBB purchases live + weekly prize payouts (Marty 2026-11-14: "why does it say 'coming soon' on all the stuff in it? Shouldn't it all be live already?")
Buying (VBBShop 0x36F6...07D7): Packages page has live Buy buttons for the 10 and 40 credit packs (USD price + live POL from quoteWei, SafePal or any other crypto wallet, one-time, never a subscription, where the payment goes); My Banners has "Add the banner look ($2)" on each waiting or in-loop banner (SKU 3, ref = banner id) and a 40-character teaser editor once it has the look. POST /api/my/order builds the member's buy() call on the server (fresh keeper voucher when the hub knows their sponsor, else no sponsor); the page checks the connected wallet is the member's VBB account (explains how to switch if not), checks the balance, sends with Polygon's fee floor (/api/gas), waits on /api/tx, nudges the watcher (/api/my/paid) and polls until the credits or the look arrive. Public: /api/quote, /api/gas, /api/tx/<hash>; /api/config has shop {live, contract} + explorer. The banner look on the loop: double border + star, hover teaser (always shown on touch screens; default = the link's site). Only Tokens still says coming soon. wallet.js gains sendTx/waitTx/balance (DLB flow).

Weekly prize payouts (prize.js): each Central-time week's pot = 10% of its VBBShop sales (usdCents of Purchase events by block time), $20 floor (company top-up shown to admin), $50 cap internal, half per board, 50/30/20. Every Monday the app prepares last week's plan (winners, wallets, USD; POL quoted live from the Chainlink feed the contract uses). /admin "Approve and pay" (two clicks): refuses unless PRIZE_KEY is the contract's prizeWallet(), the feed is fresh and the wallet holds the POL + gas (says how much more to send); then once per week (docstore.claim in MySQL + plan status), consecutive nonces, each hash saved as sent, stops at the first unclear send (never auto-retried), receipts watched. Ties: earlier to reach the count wins (leaderboards use the same rule). A winner that cannot be paid (the prize or company wallet) is skipped and the rest move up; an unfilled place stays in the prize wallet. /leaders shows past winners with transaction links. Posts nothing anywhere. Compose passes PRIZE_KEY.

Tests: smoke 192/192 (stand-in chain answers quoteWei + receipts: quote, order calldata + voucher, look purchase, teaser, prizes API, UI Buy buttons). New qa/chain.mjs on a local anvil with the real VBBShop + mock feed: buy -> Purchase -> credits, voucher binds sponsor, 30/10/60 split, overpay refunded, banner look; prize prepare -> short refusal -> pay 6 transfers exact POL -> paid once: 29/29. common.js?v=3, vbb.css?v=4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 09:47:58 -06:00

50 lines
3.4 KiB
JavaScript

// One-time purchases through VBBShop (Packages page + My Banners). The server builds the buy() call for the signed-in member
// (POST /api/my/order, with a fresh sponsor voucher when the network knows their sponsor); the member's own wallet sends it.
// The wallet that pays must be the member's VBB account: credits go to the paying wallet. After the transaction confirms the
// page waits for the purchase watcher to credit it (it reads only confirmed blocks).
window.VBBBuy = (function () {
const fmtPol = (wei, d) => { const s = (BigInt(wei) * 10n ** BigInt(d || 4) / 10n ** 18n).toString().padStart((d || 4) + 1, '0'); return s.slice(0, -(d || 4)) + '.' + s.slice(-(d || 4)); };
const short = a => VBB.short(a);
async function quote(sku, qty) { return VBB.getJSON('/api/quote?sku=' + sku + '&qty=' + (qty || 1)); }
// o: { sku, qty, ref, done(check) -> bool (credits arrived?), say(msg, cls) }
async function run(o) {
const say = o.say || VBB.status;
const c = await VBB.getConfig();
if (!c.shop || !c.shop.live) throw new Error('Purchases are not open right now.');
const me = await VBB.getJSON('/api/me');
if (!me.signedIn) throw new Error('Sign in first (My Banners), so the credits land on your account.');
say('Connecting your wallet...');
const addr = String(await VBBWallet.activeAddress(await VBBWallet.connect()) || '').toLowerCase();
if (!addr) throw new Error('No wallet connected. Tap Buy again and pick SafePal or any other crypto wallet.');
if (addr !== me.address) {
throw new Error('Your wallet is connected as ' + short(addr) + ', but your Viral Banner Blitz account is ' + short(me.address)
+ '. Purchases are credited to the wallet that pays, so switch your wallet app to ' + short(me.address)
+ ', or sign out and sign in with ' + short(addr) + '.');
}
const od = await VBB.post('/api/my/order', { sku: o.sku, qty: o.qty || 1, ref: o.ref || 0 });
if (od.error) throw new Error(od.error);
const value = BigInt(od.value);
try {
const bal = await VBBWallet.balance(addr);
if (bal < value) throw new Error('This wallet holds ' + fmtPol(bal) + ' POL. The purchase needs about ' + fmtPol(value) + ' POL plus a little gas. Add POL on Polygon and try again.');
} catch (e) { if (/holds/.test(e.message)) throw e; }
say('Confirm the payment in your wallet: about ' + fmtPol(value) + ' POL ($' + od.usd + '). Anything above the exact price comes straight back in the same transaction.');
const hash = await VBBWallet.sendTx(od.to, od.data, od.value);
const link = c.explorer ? c.explorer + '/tx/' + hash : '';
say('Payment sent. Waiting for the Polygon network to confirm it...');
const r = await VBBWallet.waitTx(hash);
if (r.status !== '0x1') throw new Error('The transaction did not go through, so nothing was charged except gas. ' + (link ? 'Details: ' + link : ''));
VBB.post('/api/my/paid', {}).catch(() => {});
say('Confirmed on Polygon. Your purchase shows up here within about a minute, once the network has a few more blocks on top of it.', 'ok');
// the watcher credits after its confirmations: poll the account
for (let i = 0; i < 72; i++) {
await new Promise(res => setTimeout(res, 5000));
if (i % 4 === 3) VBB.post('/api/my/paid', {}).catch(() => {});
try { if (await o.done()) return { hash, link, credited: true }; } catch (e) {}
}
return { hash, link, credited: false };
}
return { run, quote, fmtPol };
})();