cln-ledger

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

zap-accounting.gmi (6019B)


      1 Double-entry accounting is a tried and true method for tracking the flow of money using a principle from physics: the conservation of energy. If we account for all the inflows and outflows of money, then we know that we can build an accurate picture of all of the money we've made and spent.
      2 
      3 Bitcoin is particularly good at accounting in this sense, since transaction inflows and outflows are checked by code, with the latest state of the ledger stored in the UTXO set.
      4 
      5 What about lightning? Every transaction is not stored on the blockchain, so we need same way to account for all the incoming and outgoing lightning transactions. Luckily for us, core-lightning (CLN) comes with a plugin that describes these transactions in detail!
      6 
      7 For every transaction, CLN stores the amount credited and debited from your node: routed payments, invoices, etc. To access this, you just need to run the `lightning-cli bkpr-listaccountevents` command:
      8 
      9 ```
     10 lightning-cli bkpr-listaccountevents | jq -cr '.events[] | [.type,.tag,.credit_msat,.debit_msat,.timestamp,.description] | @tsv' > events.txt
     11 ```
     12 
     13 This will save a tab-separated file with some basic information about each credit and debit event on your node.
     14 
     15 ```
     16 channel  invoice  232000000  0        1662187126  Havana
     17 channel  invoice  2050000    0        1662242391  coinos voucher
     18 channel  invoice  0          1002203  1662463949  lightningpicturebot
     19 channel  invoice  300000     0        1663110636  [["text/plain","jb55's lightning address"],["text/identifier","jb55@sendsats.lol"]]
     20 channel  invoice  0          102626   1663483583  Mile high lightning club 
     21 ```
     22 
     23 Now here's comes the cool part, we can take this data and build a [ledger-cli](https://ledger-cli.org) file. ledger is a very powerful command-line accounting tool built on a plaintext transaction format. Using the tab-separated file we got from CLN, we can build a ledger file with a chart-of-accounts that we can use for detailed reporting. To do this, I wrote a script for converting `bkpt` reports to ledger:
     24 
     25 http://git.jb55.com/cln-ledger
     26 
     27 The ledger file looks like so:
     28 
     29 ```
     30 2023-05-31      f10074c748917a2ecd8c5ffb5c3067114e2677fa6152d5b5fd89c0aec7fd81c5
     31         expenses:zap:1971       1971000 msat
     32         assets:cln      -1971000 msat
     33 
     34 2023-05-31      damus donations
     35         income:lnurl:damus@sendsats.lol -111000 msat
     36         assets:cln      111000 msat
     37 
     38 2023-05-31      Zap
     39         income:zap:event:f8dd1e7eafa18add4aa8ff78c63f17bdb2fab3ade44f8980f094bdf3fb72d512       -10000000 msat
     40         assets:cln      10000000 msat
     41 ```
     42 
     43 Each transaction has multiple postings which track the flow of money from one account to another. Once we have this file we can quickly build reports:
     44 
     45 ## Balance report
     46 
     47 Here's the command for "account balance report since 2023-05 in CAD"
     48 
     49 `$ ledger -b 2023-05-01 -S amount -X CAD -f cln.ledger bal`
     50 
     51 ```
     52  CAD5290  assets:cln
     53  CAD2202  expenses
     54   CAD525    routed
     55  CAD1677    unknown
     56 CAD-7492  income
     57  CAD-587    unknown
     58  CAD-526    routed
     59 CAD-1515    lnurl
     60  CAD-614      jb55@sendsats.lol
     61    CAD-1        tipjar
     62  CAD-537      damus@sendsats.lol
     63  CAD-364      gpt3@sendsats.lol
     64 CAD-4012    merch
     65 CAD-2571      tshirt
     66 CAD-1441      hat
     67  CAD-852    zap
     68  CAD-847      event
     69   CAD-66        30e763a1206774753da01ba4ce95852a37841e1a1777076ba82e068f6730b75d
     70   CAD-60        f9cda1d7b6792e5320a52909dcd98d20e7f95003de7a813fa18aa8c43ea66710
     71   CAD-49        5ae0087aa6245365a6d357befa9a59b587c01cf30bd8580cd4f79dc67fc30aef
     72   CAD-43        a4d44469dd3db920257e0bca0b6ee063dfbf6622514a55e2d222f321744a2a0e
     73          ...
     74 ------------
     75            0
     76 ```
     77 
     78 As we can see it shows a breakdown of all the sats we've earned (in this case converted to fiat). We can have a higher-level summary using the depth argument:
     79 
     80 `$ ledger -M -S amount -X sat -f cln.ledger bal`
     81 
     82 ```
     83  sat14694904  assets:cln
     84   sat6116712  expenses
     85   sat1457926    routed
     86   sat4658786    unknown
     87 sat-20811616  income
     88  sat-1630529    unknown
     89  sat-1461610    routed
     90  sat-4207647    lnurl
     91 sat-11144666    merch
     92  sat-2367164    zap
     93 ------------
     94            0
     95 ```
     96 
     97 As we can see we made 14 million sats this month, not bad! The number at the bottom balances to zero which means we've properly accounted for all income and expenses.
     98 
     99 ## Daily Damus Donation Earnings
    100 
    101 To support damus, some users have turned on a feature that sends zaps to support damus development. This simply sends a payment to the damus@sendsats.lol lightning address. Since we record these we can build a daily report of damus donations:
    102 
    103 `$ ledger -D -V -f cln.ledger reg damus`
    104 
    105 ```
    106 23-May-15 - 23-May-15         ..damus@sendsats.lol      CAD-46      CAD-46
    107 23-May-16 - 23-May-16         ..damus@sendsats.lol      CAD-73     CAD-120
    108 23-May-17 - 23-May-17         ..damus@sendsats.lol      CAD-41     CAD-161
    109 23-May-18 - 23-May-18         ..damus@sendsats.lol      CAD-37     CAD-197
    110 23-May-19 - 23-May-19         ..damus@sendsats.lol      CAD-35     CAD-233
    111 23-May-20 - 23-May-20         ..damus@sendsats.lol      CAD-28     CAD-261
    112 23-May-21 - 23-May-21         ..damus@sendsats.lol      CAD-19     CAD-280
    113 23-May-22 - 23-May-22         ..damus@sendsats.lol      CAD-29     CAD-309
    114 23-May-23 - 23-May-23         ..damus@sendsats.lol      CAD-19     CAD-328
    115 23-May-24 - 23-May-24         ..damus@sendsats.lol      CAD-25     CAD-353
    116 23-May-25 - 23-May-25         ..damus@sendsats.lol      CAD-36     CAD-390
    117 23-May-26 - 23-May-26         ..damus@sendsats.lol      CAD-37     CAD-426
    118 23-May-27 - 23-May-27         ..damus@sendsats.lol      CAD-25     CAD-451
    119 23-May-28 - 23-May-28         ..damus@sendsats.lol      CAD-25     CAD-476
    120 23-May-29 - 23-May-29         ..damus@sendsats.lol      CAD-12     CAD-488
    121 23-May-30 - 23-May-30         ..damus@sendsats.lol      CAD-29     CAD-517
    122 23-May-31 - 23-May-31         ..damus@sendsats.lol      CAD-21     CAD-537
    123 ```
    124 
    125 Not making bank or anything but this covered the relay server costs this month!
    126 
    127 Hopefully ya'll found this useful, feel free to fork the script and try it out!
    128 
    129 1685589307953