#1762 replacing the lnbalances script (#1763)

This commit is contained in:
Christian Rotzoll 2020-11-11 23:47:29 +01:00 committed by GitHub
parent d7fc0d6792
commit 6307cc2218
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,91 +1,147 @@
#!/bin/bash #!/bin/bash
# RaspiBolt channel balance display, by robclark56
# make executable & copy to # SOURCE: https://github.com/gcomte/ln-gems/blob/master/showTotalLightningBalance.sh
# /usr/local/bin/lnbalance # LATEST COMMIT: a2ea316f1962f26921a95922d5fe95f430b6120f
# current user must be able to execute bitcoin-cli and lncli
# Usage ##############################################################################
# $ lnbalance to display lnd mainnet status # COLORING
# $ lnbalance --testnet to display lnd testnet status ##############################################################################
# $ lnbalance litecoin to display lnd litecoin status
# Set default (mainnet) YELLOW=`tput setaf 3`
lncli='/usr/local/bin/lncli' RESET=`tput sgr0`
lnd_pid=$(systemctl show -p MainPID lnd | awk -F"=" '{print $2}') GREEN=`tput setaf 2`
chain='main' RED=`tput setaf 1`
# read cli args ##############################################################################
for i in "$@" # CALCULATIONS
do ##############################################################################
case $i in
--testnet*)
lncli="${lncli} --network=testnet"
lnd_pid=$(systemctl show -p MainPID lnd | awk -F"=" '{print $2}')
chain='test'
shift # past argument=value
;;
*)
lncli="/usr/local/bin/lncli --chain=$i"
;;
esac
done
# set colors LN_REMOTE_BALANCE=$(lncli listchannels | jq -r '.[][].remote_balance' | awk '{s+=$1} END {print s}')
color_red='\033[0;31m' LN_LOCAL_BALANCE=$(lncli listchannels | jq -r '.[][].local_balance' | awk '{s+=$1} END {print s}')
color_green='\033[0;32m' LN_TOTAL_BALANCE=$((LN_REMOTE_BALANCE + LN_LOCAL_BALANCE))
color_yellow='\033[0;33m' LN_COMMIT_FEES=$(lncli listchannels | jq -r '.[][] | select(.initiator==true) | .commit_fee' | awk '{s+=$1} END {print s}')
color_gray='\033[0;37m' LN_INVOICES=$(lncli listinvoices | jq -r '.invoices[] | select(.settled==true) | .value' | awk '{s+=$1} END {print s}')
LN_PAYMENTS=$(lncli listpayments | jq -r '.payments[] | select(.status=="SUCCEEDED") | .value' | awk '{s+=$1} END {print s}')
LN_PAYMENTS_FEES=$(lncli listpayments | jq -r '.payments[] | select(.status=="SUCCEEDED") | .fee' | awk '{s+=$1} END {print s}')
LN_EARNED_FEES_IN_MSATS=$(lncli fwdinghistory 0 | jq -r '.forwarding_events[] | .fee_msat' | awk '{s+=$1} END {print s}')
LN_EARNED_FEES_IN_SATS=$((LN_EARNED_FEES_IN_MSATS / 1000))
# get LND info ONCHAIN_FUNDS_CONFIRMED=$(lncli walletbalance | jq -r '.confirmed_balance')
wallet_color="${color_yellow}" ONCHAIN_FUNDS_UNCONFIRMED=$(lncli walletbalance | jq -r '.unconfirmed_balance')
if [ "$lnd_pid" -ne "0" ]; then ONCHAIN_FUNDS_TOTAL=$(lncli walletbalance | jq -r '.total_balance')
${lncli} getinfo 2>&1 | grep "Please unlock" >/dev/null
wallet_unlocked=$?
if [ "$wallet_unlocked" -eq 0 ] ; then ONCHAIN_FUNDS_CONFIRMED_BTC=$(printf %.3f\\n "$((ONCHAIN_FUNDS_CONFIRMED))e-8")
wallet_color="${color_red}" ONCHAIN_FUNDS_UNCONFIRMED_BTC=$(printf %.3f\\n "$((ONCHAIN_FUNDS_UNCONFIRMED))e-8")
ln_walletbalance="Locked" ONCHAIN_FUNDS_TOTAL_BTC=$(printf %.3f\\n "$((ONCHAIN_FUNDS_TOTAL))e-8")
else ONCHAIN_TX=$(lncli listchaintxns | jq -r '.transactions[] | .amount' | awk '{s+=$1} END {print s}')
ln_walletbalance="$(${lncli} walletbalance | jq -r '.confirmed_balance')" 2>/dev/null ONCHAIN_TX_FEES=$(lncli listchaintxns | jq -r '.transactions[] | .total_fees' | awk '{s+=$1} END {print s}')
ln_channelbalance="$(${lncli} channelbalance | jq -r '.balance')" 2>/dev/null
ln_channels_active="$(${lncli} listchannels --active_only| jq '.[] | length')" 2>/dev/null LN_LOCAL_BALANCE_PERCENTAGE=$((100 * LN_LOCAL_BALANCE / LN_TOTAL_BALANCE))
ln_channels_inactive="$(${lncli} listchannels --inactive_only| jq '.[] | length')" 2>/dev/null LN_REMOTE_BALANCE_PERCENTAGE=$((100 * LN_REMOTE_BALANCE / LN_TOTAL_BALANCE))
active_remote="$(${lncli} listchannels --active_only | jq -r '.channels |.[] | .remote_balance ' | jq -s 'add')" TOTAL_BALANCE_PERCENTAGE=100
active_local="$(${lncli} listchannels --active_only | jq -r '.channels |.[] | .local_balance ' | jq -s 'add')"
inactive_remote="$(${lncli} listchannels --inactive_only | jq -r '.channels |.[] | .remote_balance ' | jq -s 'add')" ONCHAIN_FUNDS_CONFIRMED_PERCENTAGE=$((100 * ONCHAIN_FUNDS_CONFIRMED / ONCHAIN_FUNDS_TOTAL))
active_fees="$(${lncli} listchannels --active_only | jq -r '.channels |.[] | .commit_fee ' | jq -s 'add')" ONCHAIN_FUNDS_UNCONFIRMED_PERCENTAGE=$((100 * ONCHAIN_FUNDS_UNCONFIRMED / ONCHAIN_FUNDS_TOTAL))
inactive_fees="$(${lncli} listchannels --inactive_only | jq -r '.channels |.[] | .commit_fee ' | jq -s 'add')"
inactive_local="$(${lncli} listchannels --inactive_only | jq -r '.channels |.[] | .local_balance ' | jq -s 'add')" TOTAL_BALANCE=$((ONCHAIN_FUNDS_TOTAL + LN_LOCAL_BALANCE))
if [ "${active_local}" = 'null' ];then active_local=0;fi
if [ "${active_remote}" = 'null' ];then active_remote=0;fi CONTROL_SUM=$((\
if [ "${inactive_local}" = 'null' ];then inactive_local=0;fi ONCHAIN_FUNDS_CONFIRMED\
if [ "${active_fees}" = 'null' ];then active_fees=0;fi + ONCHAIN_FUNDS_UNCONFIRMED\
if [ "${inactive_fees}" = 'null' ];then inactive_fees=0;fi + ONCHAIN_TX_FEES
if [ "${inactive_remote}" = 'null' ];then inactive_remote=0;fi + LN_LOCAL_BALANCE\
if [ "${ln_walletbalance}" = 'null' ];then ln_walletbalance=0;fi + LN_COMMIT_FEES\
if [ "${ln_walletbalance}" = 'Locked' ];then ln_walletbalance=0;fi - LN_INVOICES\
total_local=$(( ${ln_walletbalance} + ${active_local} + ${inactive_local} )) + LN_PAYMENTS\
total_remote=$(( ${active_remote} + ${inactive_remote} )) + LN_PAYMENTS_FEES
total_fees=$(( ${active_fees} + ${inactive_fees} )) - LN_EARNED_FEES_IN_SATS
ln_channels=$(( ${ln_channels_active} + ${ln_channels_inactive} )) ))
PROFIT_AND_LOSS=$((LN_EARNED_FEES_IN_SATS - ONCHAIN_TX_FEES))
LN_SPEND=$((LN_PAYMENTS + LN_PAYMENTS_FEES))
##############################################################################
# Sats to BTC
##############################################################################
TOTAL_BALANCE_BTC=$(printf %.3f\\n "$(($TOTAL_BALANCE))e-8")
LN_LOCAL_BALANCE_BTC=$(printf %.3f\\n "$(($LN_LOCAL_BALANCE))e-8")
LN_REMOTE_BALANCE_BTC=$(printf %.3f\\n "$(($LN_REMOTE_BALANCE))e-8")
LN_TOTAL_BALANCE_BTC=$(printf %.3f\\n "$(($LN_TOTAL_BALANCE))e-8")
LN_INVOICES_BTC=$(printf %.3f\\n "$(($LN_INVOICES))e-8")
LN_COMMIT_FEES_BTC=$(printf %.3f\\n "$(($LN_COMMIT_FEES))e-8")
LN_PAYMENTS_BTC=$(printf %.3f\\n "$(($LN_PAYMENTS))e-8")
LN_PAYMENTS_FEES_BTC=$(printf %.3f\\n "$(($LN_PAYMENTS_FEES))e-8")
##############################################################################
# PRINT
##############################################################################
# turn '0 sats' into '0.000 sats' to keep table nicely formatted
if [ $LN_LOCAL_BALANCE -eq 0 ]; then
LN_LOCAL_BALANCE="0.000"
fi fi
else if [ $LN_REMOTE_BALANCE -eq 0 ]; then
wallet_color="${color_red}" LN_REMOTE_BALANCE="0.000"
ln_walletbalance="Not Running" fi
if [ $LN_COMMIT_FEES -eq 0 ]; then
LN_COMMIT_FEES="0.000"
fi
if [ $LN_INVOICES -eq 0 ]; then
LN_INVOICES="0.000"
fi
if [ $LN_PAYMENTS -eq 0 ]; then
LN_PAYMENTS="0.000"
fi
if [ $LN_PAYMENTS_FEES -eq 0 ]; then
LN_PAYMENTS_FEES="0.000"
fi
if [ $ONCHAIN_FUNDS_CONFIRMED -eq 0 ]; then
ONCHAIN_FUNDS_CONFIRMED="0.000"
fi
if [ $ONCHAIN_FUNDS_UNCONFIRMED -eq 0 ]; then
ONCHAIN_FUNDS_UNCONFIRMED="0.000"
fi fi
margin='' echo -e "\n${YELLOW}LN BALANCE${RESET}"
printf " echo -e "LOCAL\t\tREMOTE\t\tTOTAL"
${margin}${color_yellow}%-21s${color_gray}| ${color_yellow}Local${color_gray}| ${color_yellow}Remote${color_gray}|${color_yellow}Commitment Fees${color_gray}| echo -e "--------------\t---------------\t---------------"
${margin}${color_gray}%-21s|${color_green}%12s${color_gray}|%12s|%15s| echo -e "$LN_LOCAL_BALANCE sats\t$LN_REMOTE_BALANCE sats\t$LN_TOTAL_BALANCE sats"
${margin}${color_gray}%-18s%3s|${color_green}%12s${color_gray}|${color_yellow}%12s${color_gray}|${color_red}%15s${color_gray}| echo -e "$LN_LOCAL_BALANCE_BTC BTC\t$LN_REMOTE_BALANCE_BTC BTC\t$LN_TOTAL_BALANCE_BTC BTC"
${margin}${color_gray}%-18s%3s|${color_red}%12s${color_gray}|${color_red}%12s${color_gray}|${color_red}%15s${color_gray}| echo -e "$LN_LOCAL_BALANCE_PERCENTAGE%\t\t$LN_REMOTE_BALANCE_PERCENTAGE%\t\t$TOTAL_BALANCE_PERCENTAGE%"
${margin}${color_gray}%-18s%3s|%12s|%12s|${color_red}%15s${color_gray}|
" \
"${chain}net (sat)" \
"Wallet" "${ln_walletbalance}" "" "" \
"Active Channels" "${ln_channels_active}" "${active_local}" "${active_remote}" "${active_fees}" \
"Inactive Channels" "${ln_channels_inactive}" "${inactive_local}" "${inactive_remote}" "${inactive_fees}" \
"Total" "${ln_channels}" "${total_local}" "${total_remote}" "${total_fees}"
echo "$(tput -T xterm sgr0)" echo -e "\n${YELLOW}ON-CHAIN BALANCE${RESET}"
echo -e "CONFIRMED\tUNCONFIRMED\tTOTAL"
echo -e "--------------\t---------------\t---------------"
echo -e "$ONCHAIN_FUNDS_CONFIRMED sats\t$ONCHAIN_FUNDS_UNCONFIRMED sats\t$ONCHAIN_FUNDS_TOTAL sats"
echo -e "$ONCHAIN_FUNDS_CONFIRMED_BTC BTC\t$ONCHAIN_FUNDS_UNCONFIRMED_BTC BTC\t$ONCHAIN_FUNDS_TOTAL_BTC BTC"
echo -e "$ONCHAIN_FUNDS_CONFIRMED_PERCENTAGE%\t\t$ONCHAIN_FUNDS_UNCONFIRMED_PERCENTAGE%\t\t$TOTAL_BALANCE_PERCENTAGE%"
echo -e "\n${YELLOW}OWNED BALANCE [LN + ON-CHAIN]${RESET}"
echo -e "$TOTAL_BALANCE sats | $TOTAL_BALANCE_BTC BTC\n"
echo -e "---------------------------------------------"
echo -e "ON-CHAIN CONFIRMED $(printf %10s $ONCHAIN_FUNDS_CONFIRMED) sats"
echo -e "ON-CHAIN UNCONFIRMED $(printf %10s $ONCHAIN_FUNDS_UNCONFIRMED) sats"
echo -e "ON-CHAIN FEES ${RED}$(printf %10s "-$ONCHAIN_TX_FEES")${RESET} sats"
echo -e "---------------------------------------------"
echo -e "LN LOCAL BALANCE $(printf %10s $LN_LOCAL_BALANCE) sats"
echo -e "LN LOCKED IN COMMIT FEES $(printf %10s $LN_COMMIT_FEES) sats"
echo -e "LN INVOICES (RECEIVED) $(printf %10s $LN_INVOICES) sats"
echo -e "LN PAYMENTS (PAID) $(printf %10s "-$LN_PAYMENTS") sats"
echo -e "LN PAYMENTS FEES $(printf %10s "-$LN_PAYMENTS_FEES") sats"
echo -e "LN EARNED (FORWARD) FEES ${GREEN}$(printf %10s $LN_EARNED_FEES_IN_SATS)${RESET} sats"
echo -e "---------------------------------------------"
if [ $PROFIT_AND_LOSS -gt 0 ]; then
COLORED_PNL=${GREEN}$(printf %10s "$PROFIT_AND_LOSS")${RESET}
else
COLORED_PNL=${RED}$(printf %10s "$PROFIT_AND_LOSS")${RESET}
fi
echo -e "${YELLOW}PROFIT AND LOSS${RESET} $COLORED_PNL sats"
echo -e "---------------------------------------------"
echo -e "CONTROL SUM $(printf %10s $CONTROL_SUM) sats"
echo -e "LN SPEND $(printf %10s $LN_SPEND) sats"
echo -e ""