Skip to content

Instantly share code, notes, and snippets.

@gabridome
Last active September 17, 2020 05:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gabridome/c925bdf861a543c2d4bf0d537d54026f to your computer and use it in GitHub Desktop.
Save gabridome/c925bdf861a543c2d4bf0d537d54026f to your computer and use it in GitHub Desktop.
Snippets for c-lightning

Simple commands to extract informations from c-lightning

To see the list of lightning transactions you have to aggregate the result of two commands:

export invoices=$(lightning-cli listinvoices|jq '[.invoices[]|select( .status == "paid" )|{"payment_hash":.payment_hash, "time":.paid_at|gmtime|todate, "amount":.msatoshi,"fees":0, "description":.description}]')
export sendpays=$(lightning-cli listsendpays|jq '[.payments[]|select(.status == "complete")|{"payment_hash":.payment_hash,"time":.created_at|gmtime|todate,"amount":(.msatoshi_sent * -1),"fees":(.msatoshi_sent - .msatoshi), "description":"payment"}]')

and then you can join the two results and sort them by "time":

jq --argjson invoices "$invoices" --argjson sendpays "$sendpays" -n '$invoices + $sendpays | sort_by(.time)'

Payments sent in a particular day:

lightning-cli listpayments |jq '.payments[]|select(.created_at|todate |contains("2019-09-26"))|{destination, fees: (.msatoshi_sent-.msatoshi), date: .created_at|todate}'

How much have I done in fees by routing payments?

lightning-cli listforwards|jq '[.forwards[]|select(.status=="settled")| .fee]| add'

To make it readable:

lightning-cli listforwards|jq '[.forwards[]|select(.status=="settled")| .fee]| add|tostring |  [ while ( length > 0; .[:-3]) | .[-3:] ] | reverse | join(",")'

If you want them in csv format:

jq -r --argjson invoices "$invoices" --argjson sendpays "$sendpays" -n '$invoices + $sendpays | sort_by(.time)|.[]|[.payment_hash,.time,.amount,.fees,.description]|@csv'

At least now you also have an idea on your expense in fees.

To have the total amount of your output and of the money in your channels:

lightning-cli listfunds|jq '(([.outputs[].value]|add) + ([.channels[].channel_sat]|add))/100000000'

Fees in my channel

lightning-cli listchannels |jq -r --arg mynode $(lightning-cli getinfo|jq -r '.id') '.channels[]|select(.source == $mynode)|[.base_fee_millisatoshi, .fee_per_millionth]|@csv'

Fees in the network excluding your node in csv

lightning-cli listchannels |jq -r --arg mynode $(lightning-cli getinfo|jq -r '.id') '.channels[]|select(.source != $mynode)|[.base_fee_millisatoshi, .fee_per_millionth]|@csv'

Median of the fees in the network base_fee first, create the file median.jq as follow:

def median:
  length as $length
  | sort as $s
  | if $length == 0 then null
    else ($length / 2 | floor) as $l2 
      | if ($length % 2) == 0 then
          ($s[$l2 - 1] + $s[$l2]) / 2
        else $s[$l2]
        end
  end ;
median

Then:

lightning-cli listchannels |jq -r --arg mynode $(lightning-cli getinfo|jq -r '.id') '[.channels[]|select(.source != $mynode)|.base_fee_millisatoshi]'|jq -f median.jq

Median of the fees in the network fee_per_millionth

lightning-cli listchannels |jq -r --arg mynode $(lightning-cli getinfo|jq -r '.id') '[.channels[]|select(.source != $mynode)|.fee_per_millionth]'|jq -f median.jq
@d4amenace
Copy link

For the snippet for seeing all the payments on a particular day, how would one return the payments for a series of days? Say for a week? Or a month?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment