@@ -25,8 +25,7 @@ function exit_error() {
25
25
# ###################################
26
26
# Dependencies
27
27
# ###################################
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
30
29
command -v " $cmd " > /dev/null 2>&1 || exit_error " Command not found: '$cmd '"
31
30
done
32
31
@@ -54,7 +53,7 @@ function fee_per_kb_to_fee_per_vbyte() {
54
53
55
54
# must be an integer
56
55
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
58
57
fi
59
58
60
59
# 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() {
70
69
return 0
71
70
}
72
71
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
+
73
107
# Determine satoshis per vbyte
74
108
# Arguments: none
75
109
# Stdout: the satoshis per vbyte, as an integer
@@ -81,8 +115,10 @@ function get_sats_per_vbyte() {
81
115
local fee_endpoint=" https://api.blockcypher.com/v1/btc/main"
82
116
local fee_per_kb=
83
117
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
86
122
return 0
87
123
}
88
124
@@ -196,6 +232,10 @@ function main() {
196
232
config_path=" $2 "
197
233
interval=" $3 "
198
234
235
+ if ! [ -f " $config_path " ]; then
236
+ exit_error " No such config file: ${config_path} "
237
+ fi
238
+
199
239
watch_fees " $config_path " " $interval "
200
240
;;
201
241
0 commit comments