cln-ledger

CLN ledger accounting
git clone git://jb55.com/cln-ledger
Log | Files | Refs

commit 0d57df2adef9bccaf2c02858ba0b444de9681213
parent eb6089fd397571037e5bfd15c50429557ba1d298
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 31 May 2023 20:17:10 -0700

add routing information and btc denomination conversions

Diffstat:
M.gitignore | 2+-
Mindex.ts | 48++++++++++++++++++++++++++++++++++++++----------
2 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -9,4 +9,4 @@ index.js invoice_events.txt node_modules/ prices.db -test.ledger +*.ledger diff --git a/index.ts b/index.ts @@ -42,7 +42,11 @@ interface ZapDesc { zap: Zap } -type Description = LNDesc | GenericDesc | ZapDesc +interface Routed { + type: "routed" +} + +type Description = LNDesc | GenericDesc | ZapDesc | Routed interface Posting { account: string; @@ -101,7 +105,12 @@ function create_generic_desc(value: string): GenericDesc { return { type: "generic", value } } -function determine_description(str: string): Description { +function determine_description(str: string, tag: string): Description { + if (tag === "routed") { + let desc: Routed = { type: "routed" } + return desc + } + try { let json if (is_json(str) && (json = JSON.parse(str))) { @@ -126,25 +135,36 @@ function msat(val: number): string { return `${val} msat` } -function classify_account(str: string):string { +function classify_account(str: string, debit: number):string { if (str.includes("Damus Merch")) return "merch:tshirt" if (str.includes("Damus Hat")) return "merch:hat" if (str.includes("@tipjar")) return "lnurl:jb55@sendsats.lol:tipjar" + + if (debit === 1971000) + return "zap:1971" + else if (debit === 420000) + return "zap:420" + return "unknown" } function determine_postings(credit: number, debit: number, desc: Description): Posting[] { let postings: Posting[] = [] + const is_credit = credit > 0 + const acct = is_credit? "income" : "expenses" + const amount = is_credit? credit : debit + switch (desc.type) { + case "routed": + postings.push({ account: `${acct}:routed`, amount: msat(is_credit? -amount : amount) }) + postings.push({ account: `assets:cln`, amount: msat(is_credit? amount : -amount) }) + break case "generic": // todo: categorize - const is_credit = credit > 0 - const acct = is_credit? "income" : "expenses" - const amount = is_credit? credit : debit - const subacct = classify_account(desc.value) + const subacct = classify_account(desc.value, debit) postings.push({ account: `${acct}:${subacct}`, amount: msat(is_credit? -amount : amount) }) postings.push({ account: `assets:cln`, amount: msat(is_credit? amount : -amount) }) break @@ -180,7 +200,10 @@ async function process(filePath: string) { crlfDelay: Infinity }); - console.log("P 2022-07-22 msat 0.00000036 CAD") +console.log("P 2022-07-22 msat 0.00000036 CAD") +console.log("P 2022-07-22 msat 0.001 sat") +console.log("P 2022-07-22 msat 0.00001 bit") +console.log("P 2022-07-22 msat 0.00000000001 btc") for await (const line of rl) { let parts = line.split('\t'); @@ -196,9 +219,14 @@ async function process(filePath: string) { if (credit === 0 && debit === 0) continue + const tag = parts[1] + + if (!(tag === "invoice" || tag === "routed")) + continue + const timestamp = Number(parts[4]) const date = new Date(timestamp * 1000) - const description = determine_description(parts[5]) + const description = determine_description(parts[5], tag) const postings = determine_postings(credit, debit, description) // type,.tag,.credit_msat,.debit_msat,.timestamp,.description @@ -221,7 +249,7 @@ function transactionToLedger(transaction: Transaction): string { } async function main() { - await process('invoice_events.txt'); + await process('events.txt'); } main().catch(console.error);