-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathanchor_check.sh
157 lines (136 loc) · 4.92 KB
/
anchor_check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# Help text
help_text="
NAME:
bash anchor_check.sh - Get information on your peer's fee-commitments
USAGE:
bash anchor_check.sh [command options]
DESCRIPTION:
shows all your channel peers anchor commit fee-rate in ascending order.
OPTIONS:
--peer value"$'\t\t'"limit to only one specific pubkey
--limit value"$'\t'"Show commitment-fee below specific limit
--type string"$'\t'"filter by 'anchors' or 'static' output only
--umbrel"$'\t\t'"adjust lncli since ☂ needs a different command call
--btcpay"$'\t\t'"adjust lncli for BTCPay Server Docker environment
--inactive"$'\t\t'"Show results for inactive channels only
-h, --help"$'\t\t'"Show this help message
"
# Check for --help option
if [[ $1 == "--help" || $1 == "-h" ]]; then
echo "$help_text"
exit 0
fi
# Set the anchor_list variable based on --umbrel or --btcpay option
if [[ $# -gt 0 && $1 == "--umbrel" ]]; then
anchor_list="/home/umbrel/umbrel/scripts/app compose lightning exec -T lnd lncli"
elif [[ $# -gt 0 && $1 == "--btcpay" ]]; then
anchor_list="docker exec btcpayserver_lnd_bitcoin lncli --macaroonpath /root/.lnd/admin.macaroon"
else
anchor_list="/usr/local/bin/lncli"
fi
# Parse command line argument for inactive filtering
INACTIVE_ONLY=false
for arg in "$@"; do
if [[ "$arg" == "--inactive" ]]; then
INACTIVE_ONLY=true
fi
done
get_channel_info() {
local CHANNEL_INFO=$1
local line=$2
ALIAS=$(echo "$CHANNEL_INFO" | jq -r '.peer_alias')
FEE_PER_KW=$(echo "$CHANNEL_INFO" | jq -r '.fee_per_kw')
ANCHOR=$(echo "$CHANNEL_INFO" | jq -r '.commitment_type')
INITIATOR=$(echo "$CHANNEL_INFO" | jq -r '.initiator')
if [[ "$ANCHOR" == "ANCHORS" ]]; then
ANCHOR="⚓Anchors"
elif [[ "$ANCHOR" == "STATIC_REMOTE_KEY" ]]; then
ANCHOR="⚠ Static"
else
ANCHOR="🥪??"
fi
}
# Check if --peer option is provided
if [[ $1 == "--peer" ]]; then
PEER=$2
LIMIT=$3
# Fetch channel information for the specified pubkey
CHANNEL_INFO=$($anchor_list listchannels --peer "$PEER" | jq -r '.channels[0]')
# Check if channel information is available
if [ -z "$CHANNEL_INFO" ]; then
echo "Invalid node pubkey or no channels found: $PEER"
exit 1
fi
PUBKEY=$PEER
get_channel_info "$CHANNEL_INFO" "$line"
# Calculate the sat_vbyte value
SAT_VBYTE=$((FEE_PER_KW * 4 / 1000))
# Print SAT_VBYTE value
echo "$INITIATOR $ANCHOR $SAT_VBYTE sat/vb "$'\t'" $ALIAS ($PUBKEY)"
else
# Check if --type option is provided
if [[ $1 == "--type" ]]; then
if [[ $2 == "anchors" ]]; then
TYPE="ANCHORS"
elif [[ $2 == "static" ]]; then
TYPE="STATIC_REMOTE_KEY"
else
echo "Invalid type: $2. Valid types are 'anchors' or 'static'."
exit 1
fi
# Check if the next argument is a number (limit) or not
if [[ $3 =~ ^[0-9]+$ ]]; then
LIMIT=$3
else
LIMIT=""
fi
else
# If --limit is provided, set the limit
if [[ $1 == "--limit" && $2 =~ ^[0-9]+$ ]]; then
LIMIT=$2
else
LIMIT=""
fi
fi
# Fetch and parse channel information
if [ "$INACTIVE_ONLY" = true ]; then
CHANNELS=$($anchor_list listchannels --inactive_only | jq -c '.channels | sort_by(.fee_per_kw | tonumber)[]')
else
CHANNELS=$($anchor_list listchannels | jq -c '.channels | sort_by(.fee_per_kw | tonumber)[]')
fi
# Loop through each channel and extract the pubkey and fee_per_kw values
echo "Opener | Type | Commit "$'\t\t'" Alias "$'\t\t'" (Pubkey)"
echo "$CHANNELS" | while read -r line; do
# Extract the fee_per_kw, remote_pubkey, and peer_alias values.
# Test to show initiator, too
FEE_PER_KW=$(echo "$line" | jq -r '.fee_per_kw')
PUBKEY=$(echo "$line" | jq -r '.remote_pubkey')
ALIAS=$(echo "$line" | jq -r '.peer_alias')
ANCHOR=$(echo "$line" | jq -r '.commitment_type')
INITIATOR=$(echo "$line" | jq -r '.initiator')
if [[ -n "$TYPE" && "$ANCHOR" != "$TYPE" ]]; then
continue
fi
# Emoji setting for $line
if [[ "$ANCHOR" == "ANCHORS" ]]; then
ANCHOR_EMOJI="⚓Anchors"
elif [[ "$ANCHOR" == "STATIC_REMOTE_KEY" ]]; then
ANCHOR_EMOJI="⚠ Static"
else
ANCHOR_EMOJI="🥪"
fi
# Calculate the sat_vbyte value
SAT_VBYTE=$((FEE_PER_KW * 4 / 1000))
if [[ -z "$LIMIT" ]]; then
# LIMIT is empty, print everything
echo "$INITIATOR $ANCHOR_EMOJI $SAT_VBYTE sat/vb "$'\t'" $ALIAS ($PUBKEY)"
else
# LIMIT is not empty, proceed with filtering
if [ "$SAT_VBYTE" -lt "$LIMIT" ]; then
# Print everything below filter limit
echo "$INITIATOR $ANCHOR_EMOJI $SAT_VBYTE sat/vb "$'\t'" $ALIAS ($PUBKEY)"
fi
fi
done
fi