1c203eba8a
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
2.4 KiB
JavaScript
40 lines
2.4 KiB
JavaScript
// Buy page: gate check, live price from /api/quote, buy(sponsor) or buyFor(owner, sponsor) from the visitor's wallet.
|
|
(async function () {
|
|
await BL.renderNav('/join'); BL.renderFooter();
|
|
const $ = BL.$;
|
|
const c = await BL.getConfig();
|
|
const [g, me] = await Promise.all([BL.getJSON('/api/gate'), BL.getJSON('/api/me')]);
|
|
if (!g.passed) { $('locked').hidden = false; $('tourLink').href = BL.withRef('/', me.sponsor); return; }
|
|
$('buyCard').hidden = false;
|
|
if (!c.ready) { BL.status('The contract address is not configured yet. Buying opens once it is.', 'err'); $('buyBtn').disabled = true; $('buyForBtn').disabled = true; }
|
|
|
|
let sponsor = null;
|
|
const sp = await BL.getJSON('/api/sponsor');
|
|
if (sp.address && sp.member) { sponsor = sp.address; $('sponsor').textContent = BL.short(sp.address); }
|
|
else $('sponsor').textContent = sp.address ? 'not a member yet (none)' : 'none';
|
|
|
|
let q = null;
|
|
async function loadQuote() {
|
|
q = await BL.getJSON('/api/quote');
|
|
$('price').textContent = q.error ? 'unavailable' : BL.fmtPol(q.sendWei, 5);
|
|
if (q.error) BL.status('Price unavailable: ' + q.error, 'err');
|
|
}
|
|
await loadQuote(); setInterval(loadQuote, 30000);
|
|
|
|
async function doBuy(forAddr) {
|
|
BL.status('');
|
|
try {
|
|
await loadQuote(); if (!q || q.error) throw new Error('No price right now. Try again in a minute.');
|
|
BL.status('Confirm the payment in your wallet. The contract refunds anything above the price in the same transaction.');
|
|
const r = forAddr ? await BLWallet.buyFor(forAddr, sponsor, q.sendWei) : await BLWallet.buy(sponsor, q.sendWei);
|
|
if (r.receipt.status !== '0x1') throw new Error('The transaction reverted. Nothing was charged except gas.');
|
|
BL.status('');
|
|
$('doneCard').hidden = false; $('buyCard').hidden = true;
|
|
$('txLink').innerHTML = c.explorer ? '<a target="_blank" rel="noopener" href="' + BL.esc(c.explorer + '/tx/' + r.hash) + '">View the transaction</a>' : BL.esc(r.hash);
|
|
if (forAddr) $('doneCard').querySelector('h3').textContent = 'The spot is in the line for ' + BL.short(forAddr);
|
|
} catch (e) { BL.status(String(e && e.message || e).slice(0, 300), 'err'); }
|
|
}
|
|
$('buyBtn').onclick = () => doBuy(null);
|
|
$('buyForBtn').onclick = () => { const a = $('forAddr').value.trim(); if (!/^0x[0-9a-fA-F]{40}$/.test(a)) return BL.status('Enter a full wallet address (0x followed by 40 characters).', 'err'); doBuy(a); };
|
|
})();
|