Skip to content

Instantly share code, notes, and snippets.

@tsjk
Created August 22, 2023 09:39
Show Gist options
  • Save tsjk/1a2a81b9021904d09747e34f35f84d14 to your computer and use it in GitHub Desktop.
Save tsjk/1a2a81b9021904d09747e34f35f84d14 to your computer and use it in GitHub Desktop.
Defines an ln-fee-info() function. Needs the uplot ruby package, sqlite3, bc, jq, working bitcoin-cli, working lightning-cli, and clboss's data. To see the response, you need to call the function.
function ln-fee-info() {
pgrep -u lightning -f '^/usr/bin/lightningd\s.*' &> /dev/null && { \
function o() {
if [[ "${1}" =~ ^.*1$ ]]; then echo "st"
elif [[ "${1}" =~ ^.*2$ ]]; then echo "nd"
elif [[ "${1}" =~ ^.*3$ ]]; then echo "rd"
else echo "th"; fi
}
local -i M N f n p x; local percentile j j_c t x_v; local -A T
local b=$(/usr/bin/timeout -k 35s 30s /usr/bin/lightning-cli --rpc-file=/run/lightning/lightningd.rpc getinfo | jq -r .blockheight)
cp -a ~/bitcoin/data.clboss /tmp/data.clboss && {
N=$(sqlite3 /tmp/data.clboss 'SELECT * FROM OnchainFeeMonitor_samples' | cut -d'|' -f2 | wc -l)
M=$(sqlite3 /tmp/data.clboss 'SELECT * FROM OnchainFeeMonitor_samples' | cut -d'|' -f2 | awk '$1 > max { max = $1; } END { print max }')
x=$(sed -E 's@\..*@@' < <(bc -l <<< "l(${M})/l(10)")); x=$(bc -l <<< "10^${x}"); M=$(( (${M:0:1} + 1) * ${x} ))
for p in 17 20 23; do
T[${p}]=$(sqlite3 /tmp/data.clboss 'SELECT * FROM OnchainFeeMonitor_samples' | cut -d'|' -f2 | \
( percentile=${p}; (sort -n; echo) | nl -ba -v0 | tac | (read count; cut=$(((count * percentile + 99) / 100)); tac | sed -n "${cut}s/.*\t//p" ) )); done
f=$(sqlite3 /tmp/data.clboss 'SELECT * FROM OnchainFeeMonitor_samples' | cut -d'|' -f2 | tail -n 1)
n=$(sqlite3 /tmp/data.clboss 'SELECT * FROM OnchainFeeMonitor_samples' | cut -d'|' -f2 | sort -n | \
awk -v f=${f} 'BEGIN { c=0; } { if ($1 < f) c++; } END { printf("%d", c); }')
percentile=$(bc <<< "scale=2; (100 * ${n})/${N}"); percentile=$(LC_ALL=C printf '%.0f' "${percentile}")
if (( percentile < 17 )); then j='low'
elif (( percentile > 23 )); then j='high'
else j='neither high nor low'; fi
j_c=$(/usr/bin/timeout -k 35s 30s /usr/bin/lightning-cli --rpc-file=/run/lightning/lightningd.rpc clboss-status | jq -r ".onchain_feerate.judgment")
if [[ "${j_c}" == "high fees" ]]; then x="${T[17]}"; t="high->low"
else x="${T[23]}"; t="low->high"; fi
sqlite3 /tmp/data.clboss 'SELECT * FROM OnchainFeeMonitor_samples' | cut -d'|' -f2 | nl -ba -s',' -v $((b + 1 - N)) | sed -E 's@^\s+@@; s@$@,'"${x}"'@' | \
~/bin/uplot lines -d, --fmt xyy -w 140 -h 30 -t "Channel opening fee w/ current ${t} threshold" --xlabel block --xlim $((b + 1 - N)),${b} --ylabel "sat/kwu" --ylim 0,${M}
echo -e "\n\tEstimated channel opening fee at block ${b} is ${f} sat/kwu => at ~${percentile}$(o ${percentile}) percentile and fee is judged to be ${j} [using two weeks of clboss data]."
for p in 17 20 23; do
if (( p == 17 )); then t="high->low"
elif (( p == 23 )); then t="low->high"
else t="middle"; fi
echo -e "\t${p}$(o ${p}) percentile (${t} threshold) of channel opening fee [using two weeks of clboss data]: ${T[${p}]} sat/kwu."
done
echo -e -n "\n\tbitcoin-cli estimatesmartfee at block $(bitcoin-cli -datadir='/var/lib/bitcoin/.bitcoin' getblockcount):\n\t\t"
for n in 1 2 3 6 12 24; do
x=$(LC_ALL=C printf '%.0f' $(bc <<< "scale=8; ($(bitcoin-cli -datadir='/var/lib/bitcoin/.bitcoin' estimatesmartfee $((n * 6)) | jq -r .feerate) * 10^11) / 1024"))
x_v=$(bc <<< "scale=3; (250 * ${x})/1000"); x_v=$(LC_ALL=C printf '%.0f' "${x_v}")
echo -n "$((n * 6)) (${n}h) = ${x} msat/vB (${x_v} sat/kwu)"
[[ ${n} -eq 24 ]] && echo "." || echo -n "; "
[[ ${n} -ne 3 ]] || echo -e -n "\n\t\t"
done
}
rm -f /tmp/data.clboss
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment