// LinkSpin Wallet: paid-to-this-wallet, credits, positions, payout history (engine APIs). Connect/buy stay on /my#wallet for now.
(function () {
const $ = id => document.getElementById(id); const esc = CR.esc, fmt = CR.fmt;
CR.mount({ active: 'wallet', title: 'Wallet', sub: 'loading' });
const pol = wei => { try { return (Number(BigInt(wei || '0') / 10000000000000000n) / 100).toFixed(2); } catch (e) { return '0.00'; } };
async function load() {
let D, ME, POS = [], E = null;
try { [D, ME] = await Promise.all([CR.get('/api/my/dashboard'), CR.me()]); } catch (e) { CR.sub('could not load'); return; }
try { POS = await CR.get('/api/my/positions'); } catch (e) {}
try { E = await CR.get('/api/my/activity'); } catch (e) {}
const addr = ME.address || ''; const on = !!ME.memberId;
CR.sub(addr ? addr.slice(0, 6) + '…' + addr.slice(-4) + (on ? ' · payouts on' : ' · payouts off') : 'no wallet linked');
$('tiles').innerHTML = [['Paid to this wallet', pol(D.earnedWei || '0') + ' POL', (D.earnCount || 0) + ' transactions'], ['Credits', fmt((D.credits || 0) + (D.earnedCredits || 0)), fmt(D.credits || 0) + ' purchased · ' + fmt(D.earnedCredits || 0) + ' earned'], ['In campaigns', fmt(D.inCampaigns || 0), 'reserved by live campaigns'], ['Qualifying buyers', fmt(D.buyerCount || 0), (D.buyerCount || 0) >= 5 ? 'level 3 open' : (D.buyerCount || 0) >= 2 ? 'level 2 open' : 'level 2 at 2 buyers']]
.map(([k, v, n], i) => '
' + k + '' + v + '' + n + '
').join('');
$('walletBox').innerHTML = addr ? '' + esc(addr) + '
' + (on ? '
PAYOUTS ON · member #' + ME.memberId + '' : '
WALLET LINKED · PAYOUTS OFFSwitch payouts on so the people you bring pay you from their first purchase.
') + '
' : 'No wallet linked yet.Link one to be paid. Your email account keeps working either way.
';
const pays = E && Array.isArray(E.earnings) ? E.earnings : [];
$('payRows').innerHTML = pays.length ? pays.slice(0, 25).map(x => '' + new Date((x.ts || 0) * (x.ts > 1e12 ? 1 : 1000)).toLocaleString() + '' + esc(x.buyerName || x.fromName || (x.buyerId ? 'member #' + x.buyerId : x.type === 'AwardPaid' ? 'award' : 'member')) + '' + (x.tier ? 'L' + x.tier + ' · ' + (x.tier === 1 ? '50%' : x.tier === 2 ? '20%' : '10%') : (x.type === 'AwardPaid' ? 'award' : '')) + '' + (x.priceCents ? '$' + (x.priceCents / 100) : '') + '+' + pol(x.amountWei || '0') + ' POL' + (x.tx ? '
' + esc(String(x.tx).slice(0, 10)) + '…' : '
') + '
').join('')
: 'No payouts yet. They appear here the moment someone in your line buys.
';
$('payFoot').textContent = 'every line is a Polygon transaction · nothing is held by the site';
const rows = Array.isArray(POS) ? POS : (POS && (POS.positions || POS.list)) || [];
$('posRows').innerHTML = rows.length ? rows.map(p => '' + (p.main ? 'Main position' : 'Position') + ' ' + (p.memberId ? '#' + p.memberId : 'not on chain yet') + '
' + esc(String(p.address || '').slice(0, 10)) + '…' + fmt(p.avail || 0) + '' + (p.memberId ? 'ACTIVE' : 'PENDING') + '
').join('')
: '' + (addr ? 'Main wallet only.' : 'No positions yet.') + '
';
}
CR.requireSession(load);
})();