// 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 }; })();