INNER CODE UNIT · JavaScript
onLedger
pamgarcia1993/robinhood-lp-bot · legacy/bot.js:229
async function onLedger(page = 0, mid = null) {
const all = CH.readLedger().slice().sort((a, b) => (b.closedAt || 0) - (a.closedAt || 0)); // terbaru dulu
const sum = CH.ledgerSummary();
const out = (txt, extra) => (mid ? edit(mid, txt, extra) : send(txt, extra));
// Kosong → rekonstruksi otomatis dari on-chain (event NPM), bukan nyerah.
if (!all.length) {
await out("⏳ <b>Ledger kosong — rebuild dari on-chain…</b>");
try { await CH.backfillLedger(); } catch (e) { return out(`❌ Rebuild gagal: ${String(e.message).slice(0, 90)}`); }
return onLedger(page, mid);
}
const pages = Math.max(1, Math.ceil(all.length / LEDGER_PER_PAGE));
page = Math.min(Math.max(0, page), pages - 1);
const slice = all.slice(page * LEDGER_PER_PAGE, page * LEDGER_PER_PAGE + LEDGER_PER_PAGE);
const sg = (n, d) => (n >= 0 ? "+" : "") + n.toFixed(d);
const money = (v) => (v >= 0 ? "+" : "-") + "$" + Math.abs(v).toFixed(2);
const when = (ts) => ts ? new Date(ts).toLocaleString("id-ID", { day: "2-digit", month: "short", hour: "2-digit", minute: "2-digit" }) : "?";