Link the wallet that should receive your payouts. One free signature, no transaction. SafePal is the recommended wallet; MetaMask, Phantom and any WalletConnect wallet work too.
Nothing yet. Your first package shows here with its transaction, what you paid in POL, and who in your line was paid.
';
}
async function guard(btn, fn) { if (btn.disabled) return; btn.disabled = true; try { await fn(); } catch (e) { CR.toast((e && e.message) || 'Something went wrong.', true); } finally { btn.disabled = false; } }
// a named sponsor that cannot be paid right now: hold the transaction and say why (never credit the company by default)
function holdText(sp) {
const who = sp.sponsorName ? ('@' + String(sp.sponsorName).replace(/^@/, '')) : 'your sponsor';
if (sp.sponsorBlocked === 'claim' && sp.claim) return (sp.claim.nobody ? 'Nobody above you on LinkSpin has a wallet yet, so there is no one for the contract to pay. ' : who + ' has not linked a wallet on LinkSpin yet. ') + 'Your first purchase waits until ' + (sp.claim.deadline ? new Date(sp.claim.deadline).toLocaleDateString() : 'they do') + ', then it goes ahead with the nearest member above who has one. Nothing was charged.';
if (sp.sponsorBlocked === 'notActivated') return who + ' has not switched on payouts yet, so this purchase would credit the company instead of them. Ask them to switch on payouts first (one free transaction).';
if (sp.sponsorBlocked === 'rpc') return 'Could not confirm your sponsor on the chain just now. Try again in a minute; nothing was charged.';
return 'Your sponsor link could not be matched to a member. Message support before buying so ' + who + ' gets credit.';
}
async function linkWallet() { CR.toast('Check your wallet for the free link signature…'); await IAPWallet.signIn(); CR.toast('Wallet linked. Earnings pay there from now on.'); await load(); }
async function activate() {
const sp = await CR.get('/api/sponsor?intent=buy'); if (sp.sponsorBlocked) { $('holdBox').textContent = holdText(sp); $('holdBox').hidden = false; return; }
CR.toast('Confirm the free activation in your wallet…');
const r = await IAPWallet.activate(sp.sponsorId || 0);
if (!r.receipt || r.receipt.status !== '0x1') throw new Error('Transaction reverted. Check the explorer.');
CR.toast('Payouts are on. Your invite link is live.'); await load();
}
async function buy() {
const p = sel(); if (!p || !p.costWei) return;
$('holdBox').hidden = true;
let me = await CR.me(true);
if (!me.address) { CR.toast('Link your wallet first: one quick signature…'); await IAPWallet.signIn(); me = await CR.me(true); }
const want = String(me.address || '').toLowerCase(); if (!want) throw new Error('No wallet linked.');
await IAPWallet.connect();
let cur = String(await IAPWallet.activeAddress() || '').toLowerCase();
if (cur !== want) { CR.toast('Pick ' + want.slice(0, 6) + '…' + want.slice(-4) + ' in your wallet\'s account picker…'); cur = String(await IAPWallet.pickAccount() || '').toLowerCase(); }
if (cur !== want) throw new Error('Your wallet connected as ' + cur.slice(0, 6) + '…' + cur.slice(-4) + ' but your linked wallet is ' + want.slice(0, 6) + '…' + want.slice(-4) + '. Switch accounts in the wallet app and try again.');
const sp = await CR.get('/api/sponsor?intent=buy');
if (!me.memberId && sp.sponsorBlocked) { $('holdBox').textContent = holdText(sp); $('holdBox').hidden = false; return; }
try { // pre-flight: the POL has to be there; Trust Wallet refuses transactions that spend most of a balance
const need = BigInt(p.costWei) + BigInt(p.costWei) / 50n;
const bal = await IAPWallet.balance(want);
if (bal < need) { CR.toast('That wallet holds ' + pol(bal.toString()) + ' POL, but this package needs about ' + pol(need.toString()) + ' POL plus a little for gas. Top it up and try again.', true); return; }
const pct = Number(need * 100n / bal);
if (/trust/i.test(IAPWallet.walletName() || '') && pct > 55) {
const ok = await CR.dialog({ title: 'Heads up for Trust Wallet', text: 'This purchase uses about ' + pct + '% of the POL in your wallet, and Trust Wallet refuses transactions that spend most of a balance. Add some POL first, or continue and see if it lets it through.', ok: 'Continue' });
if (!ok) return;
}
} catch (e) { /* balance read failed: let the wallet decide */ }
CR.toast('Confirm the purchase in your wallet…');
const r = await IAPWallet.buy(Number(p.id), sp.sponsorId || 0, p.costWei);
if (!r.receipt || r.receipt.status !== '0x1') throw new Error('Transaction reverted. Nothing was charged beyond gas.');
CR.toast('Settled on-chain. Your credits are in Ads now.');
await load();
}
$('buyBtn').addEventListener('click', () => guard($('buyBtn'), buy));
$('cardBtn').addEventListener('click', () => guard($('cardBtn'), async () => {
const p = sel(); const need = p && p.costWei ? Math.max(10, Math.ceil(Number(BigInt(p.costWei) / 1000000000000000000n)) + 3) : 30;
const q = '/api/moonpay-url?pol=' + need + (ME && ME.address ? '&address=' + encodeURIComponent(ME.address) : '');
const r = await CR.get(q); if (!r.url) throw new Error('The card on-ramp is not available right now.');
window.open(r.url, '_blank', 'noopener'); CR.toast('MoonPay opened in a new tab. POL lands straight in your own wallet.');
}));
async function load() {
try { [CFG, ME] = await Promise.all([CR.get('/api/config'), CR.me(true)]); } catch (e) { CR.sub('could not load'); return; }
let cat = null, E = null;
try { [cat, CAMP, DASH, E] = await Promise.all([CR.get('/api/catalog'), CR.get('/api/my/campaigns'), CR.get('/api/my/dashboard'), CR.get('/api/my/activity')]); } catch (e) {}
CAT = (cat && cat.products) || [];
CR.sub((CAMP ? fmt(CAMP.availableCredits) + ' free to spend · ' + fmt(CAMP.inCampaigns) + ' in live ads' : '') + (CAT[0] && CAT[0].costWei ? '' : ' · price feed catching up'));
paintTiles(); paintPkgs(); paintQuote(); paintGoes(); paintWallet(); paintBuys(E);
const spName = ME.sponsorName ? ('@' + String(ME.sponsorName).replace(/^@/, '')) : null;
$('sponsorLine').innerHTML = spName
? 'Your sponsor ' + esc(spName) + ' is placed on the contract. The moment you confirm, half of the package lands in their wallet and the rest splits as shown. Credits appear in Ads right away.'
: 'No sponsor is recorded on your account. Your purchase still works; the sponsor share goes to the platform position. If someone invited you, open their invite link first.';
if (ME.sponsorBlocked) { $('holdBox').textContent = holdText(ME); $('holdBox').hidden = false; }
}
CR.requireSession(load);
})();