Skip to content

Instantly share code, notes, and snippets.

@gabridome
Last active December 6, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabridome/08fc82e035afe6e0f31a11ebbd4fcb6b to your computer and use it in GitHub Desktop.
Save gabridome/08fc82e035afe6e0f31a11ebbd4fcb6b to your computer and use it in GitHub Desktop.
# starting from `listtransactions` the script collects the inputs and
# the output amounts to calculate the fees.
# the inputs are collected with a SQL statement inside outputs table.
lightning-cli listtransactions| jq -c '.transactions[]' | while read object
do export hash=$(jq '.hash'<<<$object)
export inputs=$(jq '.inputs'<<<$object)
export outputs=$(jq '[.outputs[]]'<<<$object)
export satoshis=$(jq '[.[].satoshis[0:-4] |tonumber / 1000]| add' <<< $outputs)
export block=$(jq '.blockheight' <<< $object)
export new_total=0
export totals=$(jq -c '.[]' <<<$inputs|while read input
do export input_hash=$(jq -r '.txid' <<< $input)
export new_input_hash=$(printf %s "$input_hash" | dd conv=swab 2> /dev/null | rev)
export index=$(jq '.index' <<< $input)
export new_total_query="select sum(value) from outputs where prev_out_tx =$(echo \'\\x$new_input_hash\') and prev_out_index = $(jq '.index' <<< $input)"
export new_total=$(psql -U postgres --host=localhost --port=5432 "dbname=lightningd" -q -t -c "$new_total_query")
echo $new_total
done)
export total=$(echo $totals |jq -s '. | add')
echo $block,$hash, $total, $satoshis
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment