// One-time Boosts through VABShop. The server builds the buy() call for the signed-in member (POST /api/my/order, with a fresh // line voucher when the member's sponsor line is not on the contract yet); the member's own wallet sends it. The wallet that // pays must be the member's ViralAdBlitz account. After the transaction confirms the page waits for the watcher to record it. window.VABBuy = (function () { const fmtPol = (wei, d) => { const k = d || 4; const s = (BigInt(wei) * 10n ** BigInt(k) / 10n ** 18n).toString().padStart(k + 1, '0'); return s.slice(0, -k) + '.' + s.slice(-k); }; async function quote(qty) { return VAB.getJSON('/api/quote?qty=' + (qty || 1)); } // o: { qty, done() -> bool (the Boost shows on /api/my), say(msg, cls) } async function run(o) { const say = o.say || VAB.status; const c = await VAB.getConfig(); if (!c.shop || !c.shop.live) throw new Error('Boosts are not open right now.'); const me = await VAB.getJSON('/api/me'); if (!me.signedIn) throw new Error('Sign in on My page first, so the Boost lands on your ad.'); say('Connecting your wallet...'); const addr = String(await VABWallet.activeAddress(await VABWallet.connect()) || '').toLowerCase(); if (!addr) throw new Error('No wallet connected. Tap Boost again and pick SafePal or any other crypto wallet.'); if (addr !== me.address) throw new Error('Your wallet is connected as ' + VAB.short(addr) + ', but your ViralAdBlitz account is ' + VAB.short(me.address) + '. A Boost goes to the wallet that pays, so switch your wallet app to ' + VAB.short(me.address) + ', or sign out and sign in with ' + VAB.short(addr) + '.'); const od = await VAB.post('/api/my/order', { qty: o.qty || 1 }); if (od.error) throw new Error(od.error); const value = BigInt(od.value); try { const bal = await VABWallet.balance(addr); if (bal < value) throw new Error('This wallet holds ' + fmtPol(bal) + ' POL. The Boost 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 VABWallet.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 VABWallet.waitTx(hash); if (r.status !== '0x1') throw new Error('The transaction did not go through, so nothing was charged except gas. ' + (link ? 'Details: ' + link : '')); VAB.post('/api/my/paid', {}).catch(() => {}); say('Confirmed on Polygon. Your Boost shows on My page within about a minute.', 'ok'); for (let i = 0; i < 72; i++) { await new Promise(res => setTimeout(res, 5000)); if (i % 4 === 3) VAB.post('/api/my/paid', {}).catch(() => {}); try { if (await o.done()) return { hash, link, recorded: true }; } catch (e) {} } return { hash, link, recorded: false }; } return { run, quote, fmtPol }; })();