-
-
Save takinbo/5134ed7a29d7afa5ba798ccd4f295fcb to your computer and use it in GitHub Desktop.
c-lightning cli script with custom functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
declare -A NODEMAP | |
for node_id in $(lightning-cli listpeers | jq -r '.peers | .[].id'); do | |
node_alias=$(lightning-cli listnodes $node_id | jq -r '.nodes | .[] | .alias'); | |
if [ ${#node_alias} != 0 ]; then | |
NODEMAP[$node_id]=$node_alias; | |
NODES+="{\"$node_id\":\"$node_alias\"}"; | |
fi | |
done | |
for entry in $(lightning-cli listpeers | jq -r '.peers | map(select(.channels | length > 0)) | map({peer: .id, channel: .channels[]}) | map({scid: .channel.short_channel_id, nodeid: .peer}) | (map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $rows[] | @csv'); do | |
IFS="," read -r node_id scid <<< "$(echo $entry | tr -d '\"')"; | |
if [ ${#NODEMAP[$node_id]} != 0 ]; then | |
CHANNELS+="{\"$scid\":\"${NODEMAP[$node_id]}\"}"; | |
fi | |
done | |
channels () { | |
ARGSJSON=$(echo $NODES | jq --indent 0 -r -n '[inputs] | add') | |
lightning-cli listpeers | jq -r --argjson NODES "$ARGSJSON" ' | |
.peers | |
| map(select(.channels | length > 0)) | |
| map({ | |
peer: (if $NODES[.id] != "null" and $NODES[.id] != null then $NODES[.id] else .id[:20] end), | |
peer_id: .id, | |
online: (if .connected then "π" else "π" end), | |
channel: .channels[] | |
}) | |
| sort_by(.channel.total_msat) | reverse | |
| map({ | |
scid: .channel.short_channel_id, | |
opener: .channel.opener, | |
fee_base: (.channel.fee_base_msat | tonumber / 1000), | |
fee_ppm: .channel.fee_proportional_millionths, | |
capacity: (.channel.total_msat / 1000), | |
max_htlc: (.channel.maximum_htlc_out_msat | tonumber / 1000), | |
max_routable: ((.channel.to_us_msat - .channel.total_msat * 0.01) / 1000), | |
local: (.channel.to_us_msat / 1000), | |
remote: ((.channel.total_msat - .channel.to_us_msat) / 1000), | |
balance: ((.channel.total_msat - 2 * .channel.to_us_msat) / .channel.total_msat), | |
in_payments: .channel.in_payments_fulfilled, | |
out_payments: .channel.out_payments_fulfilled, | |
io_rate: (if .channel.out_payments_fulfilled == 0 then 1 else .channel.in_payments_fulfilled / .channel.out_payments_fulfilled end), | |
public: (if .channel.private == false then "π" else "π" end), | |
online, | |
peer, | |
peer_id, | |
})' | mlr -S --ijson --opprint put '$balance = float($balance) * 100' then sort -nr balance then put '$balance = fmtnum($balance, "%d") . "%"; $max_routable = fmtnum(float($max_routable), "%d"); $local = fmtnum(float($local), "%d"); $remote = fmtnum(float($remote), "%d"); $io_rate = fmtnum(float($io_rate) * 100, "%d") . "%"' | |
} | |
forwards () { | |
ARGSJSON=$(echo $CHANNELS | jq --indent 0 -r -n '[inputs] | add') | |
lightning-cli listforwards | jq -r --argjson CHANNELS "$ARGSJSON" ' | |
.forwards | |
| sort_by(.received_time) | reverse | |
| map(select(.status != "local_failed")) | |
| map({ | |
in_node: (if $CHANNELS[.in_channel] != "null" and $CHANNELS[.in_channel] != null then $CHANNELS[.in_channel] else "-" end), | |
out_node: (if $CHANNELS[.out_channel] != "null" and $CHANNELS[.out_channel] != null then $CHANNELS[.out_channel] else "-" end), | |
in: .in_channel, | |
out: .out_channel, | |
amount: (if .in_msat then (.in_msat / 1000) else 0 end), | |
fee: (if .fee_msat then (.fee_msat / 1000) else 0 end), | |
received: .received_time, | |
duration: (if .resolved_time then (.resolved_time - .received_time) else 0 end), | |
status: .status | |
})' | mlr -S --ijson --opprint sort -nr received then put '$duration = fmtnum(float($duration), "%.3f"); $received = strftime(float($received), "%Y-%m-%d %H:%M:%S")' then head -n 25 | |
} | |
success_forwards () { | |
ARGSJSON=$(echo $CHANNELS | jq --indent 0 -r -n '[inputs] | add') | |
lightning-cli listforwards | jq -r --argjson CHANNELS "$ARGSJSON" ' | |
.forwards | |
| sort_by(.received_time) | reverse | |
| map(select(.status != "local_failed")) | |
| map(select(.status != "failed")) | |
| map({ | |
in_node: (if .in_channel != null and $CHANNELS[.in_channel] != "null" and $CHANNELS[.in_channel] != null then $CHANNELS[.in_channel] else "-" end), | |
out_node: (if .out_channel != null and $CHANNELS[.out_channel] != "null" and $CHANNELS[.out_channel] != null then $CHANNELS[.out_channel] else "-" end), | |
in: .in_channel, | |
out: .out_channel, | |
amount: (if .in_msat then (.in_msat / 1000) else 0 end), | |
fee: (if .fee_msat then (.fee_msat / 1000) else 0 end), | |
received: .received_time, | |
duration: (if .resolved_time then (.resolved_time - .received_time) else 0 end), | |
status: .status | |
})' | mlr -S --ijson --opprint sort -nr received then put '$duration = fmtnum(float($duration), "%.3f"); $received = strftime(float($received), "%Y-%m-%d %H:%M:%S")' then head -n 25 | |
} | |
all_forwards () { | |
ARGSJSON=$(echo $CHANNELS | jq --indent 0 -r -n '[inputs] | add') | |
lightning-cli listforwards | jq -r --argjson CHANNELS "$ARGSJSON" ' | |
.forwards | |
| sort_by(.received_time) | reverse | |
| map({ | |
in_node: (if .in_channel != null and $CHANNELS[.in_channel] != "null" and $CHANNELS[.in_channel] != null then $CHANNELS[.in_channel] else "-" end), | |
out_node: (if .out_channel != null and $CHANNELS[.out_channel] != "null" and $CHANNELS[.out_channel] != null then $CHANNELS[.out_channel] else "-" end), | |
in: .in_channel, | |
out: .out_channel, | |
amount: (if .in_msat then (.in_msat / 1000) else 0 end), | |
fee: (if .fee_msat then (.fee_msat / 1000) else 0 end), | |
received: .received_time, | |
duration: (if .resolved_time then (.resolved_time - .received_time) else 0 end), | |
status: .status | |
})' | mlr -S --ijson --opprint sort -nr received then put '$duration = fmtnum(float($duration), "%.3f"); $received = strftime(float($received), "%Y-%m-%d %H:%M:%S")' then head -n 25 | |
} | |
leases () { | |
lightning-cli listnodes | jq -r ' | |
.nodes | |
| map(select(.option_will_fund | length > 0)) | |
| map({ | |
peer: .nodeid, | |
lease_fee_base: (.option_will_fund.lease_fee_base_msat | sub("msat$"; "") | tonumber / 1000), | |
lease_fee_pct: (.option_will_fund.lease_fee_basis | tonumber * 0.01), | |
funding_weight: .option_will_fund.funding_weight, | |
channel_fee_max_base: (.option_will_fund.channel_fee_max_base_msat | sub("msat$"; "") | tonumber / 1000), | |
channel_fee_max_ppm: (.option_will_fund.channel_fee_max_proportional_thousandths | tonumber * 1000), | |
compact_lease: .option_will_fund.compact_lease, | |
name: .alias | |
})' | mlr -S --ijson --opprint sort -n lease_fee_pct | |
} | |
funds () { | |
lightning-cli listfunds | jq '{"on-chain": [.outputs[].value] | add, "off-chain": [.channels[].channel_sat] | add}' | mlr -S --ijson --opprint cat | |
} | |
case $1 in | |
channels) | |
channels | |
;; | |
forwards) | |
case $2 in | |
-s) | |
success_forwards | |
;; | |
-a) | |
all_forwards | |
;; | |
*) | |
forwards | |
;; | |
esac | |
;; | |
leases) | |
leases | |
;; | |
funds) | |
funds | |
;; | |
*) | |
lightning-cli $@ | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment