c-lightning-REST improvements, balance display fix (#4005)

* improve handling aliases when no network given
* clrest install to use absolute paths
* fix cln balance display
* remove misplaced line
* improve jq expressions
* convert msat balances to sat

discussed in: https://github.com/raspiblitz/raspiblitz/issues/3837
This commit is contained in:
openoms 2023-07-29 13:48:11 +02:00 committed by GitHub
parent 237570af44
commit 8c87bee0f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 65 deletions

View File

@ -67,14 +67,15 @@ if [ "$2" = "status" ]; then
clError=$(sudo journalctl -n5 -u ${netprefix}lightningd)
# cases from 'cl.hsmtool.sh unlock'
if \
[ "$(eval echo \$${netprefix}clEncryptedHSM)" = "on" ] && [ ! -f $passwordFile ] || \
[ $(echo "${clError}" | \
grep -c 'encrypted-hsm: Could not read pass from stdin.') -gt 0 ] || \
[ $(echo "${clError}" | \
grep -c 'hsm_secret is encrypted, you need to pass the --encrypted-hsm startup option.') -gt 0 ] || \
[ $(echo "${clError}" | \
grep -c 'Wrong password for encrypted hsm_secret.') -gt 0 ]; then
if
[ "$(eval echo \$${netprefix}clEncryptedHSM)" = "on" ] && [ ! -f $passwordFile ] ||
[ $(echo "${clError}" |
grep -c 'encrypted-hsm: Could not read pass from stdin.') -gt 0 ] ||
[ $(echo "${clError}" |
grep -c 'hsm_secret is encrypted, you need to pass the --encrypted-hsm startup option.') -gt 0 ] ||
[ $(echo "${clError}" |
grep -c 'Wrong password for encrypted hsm_secret.') -gt 0 ]
then
# signal wallet locked
cl_locked="1"
@ -146,7 +147,6 @@ if [ "$2" = "config" ]; then
exit 1
fi
######################################################
# INFO
######################################################
@ -241,7 +241,6 @@ if [ "$2" = "wallet" ]; then
# /usr/local/bin/lightning-cli --lightning-dir=/home/bitcoin/.lightning --conf=/home/bitcoin/.lightning/config listfunds
# get data
sudo -u bitcoin
command="sudo -u bitcoin $lightningcli_alias listfunds"
cl_listfunds=$(${command} 2>/dev/null)
if [ "${cl_listfunds}" == "" ]; then
@ -251,29 +250,34 @@ if [ "$2" = "wallet" ]; then
fi
ln_walletbalance=0
for i in $(echo "$cl_listfunds" | jq .outputs[] | jq 'select(.status=="confirmed")' | grep value | awk '{print $2}' | cut -d, -f1);do
ln_walletbalance=$((ln_walletbalance+i))
for i in $(echo "$cl_listfunds" | jq '.outputs[] | select(.status == "confirmed").amount_msat'); do
sat=$((i / 1000))
ln_walletbalance=$((ln_walletbalance + sat))
done
ln_walletbalance_wait=0
for i in $(echo "$cl_listfunds" | jq .outputs[] | jq 'select(.status=="unconfirmed")' | grep value | awk '{print $2}' | cut -d, -f1);do
ln_walletbalance_wait=$((ln_walletbalance_wait+i))
for i in $(echo "$cl_listfunds" | jq '.outputs[] | select(.status == "unconfirmed").amount_msat'); do
sat=$((i / 1000))
ln_walletbalance_wait=$((ln_walletbalance_wait + sat))
done
ln_closedchannelbalance=0
for i in $(echo "$cl_listfunds" | jq .channels[] | jq 'select(.state=="ONCHAIN")' | grep channel_sat | awk '{print $2}' | cut -d, -f1);do
ln_closedchannelbalance=$((ln_closedchannelbalance+i))
for i in $(echo "$cl_listfunds" | jq -r '.channels[] | select(.state=="ONCHAIN") | .our_amount_msat'); do
sat=$((i / 1000))
ln_closedchannelbalance=$((ln_closedchannelbalance + sat))
done
ln_pendingonchain=$((ln_walletbalance_wait + ln_closedchannelbalance))
if [ ${#ln_pendingonchain} -gt 0 ]; then ln_pendingonchain="(+${ln_pendingonchain})"; fi
ln_channelbalance=0
for i in $(echo "$cl_listfunds" |jq .channels[]|jq 'select(.state=="CHANNELD_NORMAL")'|grep channel_sat|awk '{print $2}'|cut -d, -f1);do
ln_channelbalance=$((ln_channelbalance+i))
for i in $(echo "$cl_listfunds" | jq -r '.channels[] | select(.state=="CHANNELD_NORMAL") | .our_amount_msat'); do
sat=$((i / 1000))
ln_channelbalance=$((ln_channelbalance + sat))
done
if [ ${#ln_channelbalance} -eq 0 ]; then
ln_channelbalance=0
fi
ln_channelbalance_all=0
for i in $(echo "$cl_listfunds" |jq .channels[]|grep channel_sat|awk '{print $2}'|cut -d, -f1);do
ln_channelbalance_all=$((ln_channelbalance_all+i))
for i in $(echo "$cl_listfunds" | jq -r '.channels[] | .our_amount_msat'); do
sat=$((i / 1000))
ln_channelbalance_all=$((ln_channelbalance_all + sat))
done
ln_channelbalance_pending=$((ln_channelbalance_all - ln_channelbalance - ln_closedchannelbalance))
if [ ${#ln_channelbalance_pending} -gt 0 ]; then ln_channelbalance_pending=" (+${ln_channelbalance_pending})"; fi

View File

@ -142,7 +142,7 @@ if [ "$1" = on ]; then
if [ ! -f /home/bitcoin/c-lightning-REST/cl-rest.js ]; then
cd /home/bitcoin || exit 1
sudo -u bitcoin git clone https://github.com/saubyk/c-lightning-REST
cd c-lightning-REST || exit 1
cd /home/bitcoin/c-lightning-REST || exit 1
sudo -u bitcoin git reset --hard $CLRESTVERSION
sudo -u bitcoin /home/admin/config.scripts/blitz.git-verify.sh \
@ -152,8 +152,11 @@ if [ "$1" = on ]; then
sudo -u bitcoin npm install
fi
cd /home/bitcoin/c-lightning-REST || exit 1
sudo -u bitcoin mkdir ${CLNETWORK}
# copy clrest to a CLNETWORK subdir to make parallel networks possible
sudo -u bitcoin mkdir /home/bitcoin/c-lightning-REST/${CLNETWORK}
sudo -u bitcoin cp -r /home/bitcoin/c-lightning-REST/* \
/home/bitcoin/c-lightning-REST/${CLNETWORK}
echo "
{
\"PORT\": ${portprefix}6100,
@ -162,12 +165,7 @@ if [ "$1" = on ]; then
\"EXECMODE\": \"production\",
\"LNRPCPATH\": \"/home/bitcoin/.lightning/${CLNETWORK}/lightning-rpc\",
\"RPCCOMMANDS\": [\"*\"]
}" | sudo -u bitcoin tee ./${CLNETWORK}/cl-rest-config.json
# copy clrest to a CLNETWORK subdir to make parallel networks possible
sudo -u bitcoin mkdir /home/bitcoin/c-lightning-REST/${CLNETWORK}
sudo -u bitcoin cp -r /home/bitcoin/c-lightning-REST/* \
/home/bitcoin/c-lightning-REST/${CLNETWORK}
}" | sudo -u bitcoin tee /home/bitcoin/c-lightning-REST/${CLNETWORK}/cl-rest-config.json || exit 1
echo "
# systemd unit for c-lightning-REST for ${CHAIN}

View File

@ -44,13 +44,13 @@ if [ "$1" = getvars ];then
chain=main
fi
# CHAIN is: signet | testnet | mainnet
if [ $# -gt 2 ]&&[ "$3" != net ]&&[ "$3" != "" ];then
if [ $# -gt 2 ] && { [ "$3" = "signet" ] || [ "$3" = "testnet" ] || [ "$3" = "mainnet" ]; }; then
CHAIN=$3
chain=${CHAIN::-3}
else
CHAIN=${chain}net
fi
echo "CHAIN=${chain}net"
echo "CHAIN=${CHAIN}"
echo "chain=${chain}"
# netprefix is: "" | t | s