Skip to content

Commit 0224b1a

Browse files
committed
chore: address PR feedback -- log HTTP errors and check that the config file exists
1 parent 29e31cd commit 0224b1a

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

contrib/side-cars/fee-estimate.sh

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ function exit_error() {
2525
####################################
2626
# Dependencies
2727
####################################
28-
# Dependencies
29-
for cmd in curl jq bc sed date grep; do
28+
for cmd in curl jq bc sed date grep head tail; do
3029
command -v "$cmd" >/dev/null 2>&1 || exit_error "Command not found: '$cmd'"
3130
done
3231

@@ -54,7 +53,7 @@ function fee_per_kb_to_fee_per_vbyte() {
5453

5554
# must be an integer
5655
if ! [[ "$fee_per_kb" =~ ^[0-9]+$ ]]; then
57-
exit_error "Did not receive a fee/kb from $fee_endpoint, but got '$fee_per_kb'"
56+
return 1
5857
fi
5958

6059
# NOTE: round up -- get the fractional part, and if it's anything other than 000, then add 1
@@ -70,6 +69,41 @@ function fee_per_kb_to_fee_per_vbyte() {
7069
return 0
7170
}
7271

72+
# Query the endpoint and log HTTP errors gracefully
73+
# Arguments:
74+
# $1 endpoint to query
75+
# Stdout: the HTTP response body
76+
# Stderr: an error message, if we failed to query
77+
# Return:
78+
# 0 on success
79+
# nonzero on error
80+
function query_fee_endpoint() {
81+
local fee_endpoint="$1"
82+
local response=
83+
local http_status_code=
84+
85+
response="$(curl -sL -w "\n%{http_code}" "$fee_endpoint" || true)";
86+
http_status_code="$(echo "$response" | tail -n 1)";
87+
case $http_status_code in
88+
200)
89+
;;
90+
429)
91+
echo >&2 "WARN[$(date +%s)]: 429 Rate-Limited retreiving ${fee_endpoint}"
92+
return 1
93+
;;
94+
404)
95+
echo >&2 "WARN[$(date +%s)]: 404 Not Found retrieving ${fee_endpoint}"
96+
return 1
97+
;;
98+
**)
99+
echo >&2 "WARN[$(date +%s)]: ${http_status_code} Error retrieving ${fee_endpoint}"
100+
return 1
101+
;;
102+
esac
103+
echo "$response" | head -n -1
104+
return 0
105+
}
106+
73107
# Determine satoshis per vbyte
74108
# Arguments: none
75109
# Stdout: the satoshis per vbyte, as an integer
@@ -81,8 +115,10 @@ function get_sats_per_vbyte() {
81115
local fee_endpoint="https://api.blockcypher.com/v1/btc/main"
82116
local fee_per_kb=
83117

84-
fee_per_kb="$(curl -sL "$fee_endpoint" | jq -r '.high_fee_per_kb')"
85-
fee_per_kb_to_fee_per_vbyte "$fee_per_kb"
118+
fee_per_kb="$(query_fee_endpoint "$fee_endpoint" | jq -r '.high_fee_per_kb')"
119+
if ! fee_per_kb_to_fee_per_vbyte "$fee_per_kb"; then
120+
return 1
121+
fi
86122
return 0
87123
}
88124

@@ -196,6 +232,10 @@ function main() {
196232
config_path="$2"
197233
interval="$3"
198234

235+
if ! [ -f "$config_path" ]; then
236+
exit_error "No such config file: ${config_path}"
237+
fi
238+
199239
watch_fees "$config_path" "$interval"
200240
;;
201241

0 commit comments

Comments
 (0)