Skip to content

Commit ecfef61

Browse files
feat: improve periodic proof sending script (#2156)
Co-authored-by: JuArce <[email protected]>
1 parent 19b8074 commit ecfef61

File tree

4 files changed

+215
-138
lines changed

4 files changed

+215
-138
lines changed

alerts/.env.devnet

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# # Variables for contract_alerts.sh
66
# RPC_URL=<YOUR_RPC_URL>
7+
# RPC_URL_FALLBACK=<YOUR_FALLBACK_RPC_URL>
78
# CONTRACT_ADDRESS=<YOUR_CONTRACT_ADDRESS>
89
# NEW_BATCH_TOPIC=<YOUR_NEW_BATCH_TOPIC>
910
# VERIFIED_BATCH_TOPIC=<YOUR_VERIFIED_BATCH_TOPIC>
@@ -17,6 +18,7 @@
1718

1819
# # Variables for balance_alerts.sh
1920
# RPC_URL=<YOUR_RPC_URL>
21+
# RPC_URL_FALLBACK=<YOUR_FALLBACK_RPC_URL>
2022
# PAYMENT_CONTRACT_ADDRESS=<YOUR_PAYMENT_CONTRACT_ADDRESS>
2123
# BALANCE_THRESHOLD=<YOUR_BALANCE_THRESHOLD_IN_ETH>
2224
# WALLET_ADDRESS=<YOUR_WALLET_ADDRESS>

alerts/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ EXPRESSION=<GREP_EXPRESSION>
1717

1818
# Variables for balance_alerts.sh
1919
RPC_URL=<YOUR_RPC_URL>
20+
RPC_URL_FALLBACK=<YOUR_FALLBACK_RPC_URL>
2021
PAYMENT_CONTRACT_ADDRESS=<YOUR_PAYMENT_CONTRACT_ADDRESS>
2122
BALANCE_THRESHOLD=<YOUR_BALANCE_THRESHOLD_IN_ETH>
2223
WALLET_NAME=<YOUR_WALLET_NAME> # Example: "Task sender"
@@ -37,6 +38,7 @@ LOGS_BLOCK_RANGE=<LOGS_BLOCK_RANGE>
3738
CONTRACT_ADDRESS=<YOUR_CONTRACT_ADDRESS>
3839
AGGREGATED_PROOF_VERIFIED_TOPIC=0xfe3e9e971000ab9c80c7e06aba2933aae5419d0e44693e3046913e9e58053f62
3940
RPC_URL=<YOUR_RPC_URL>
41+
RPC_URL_FALLBACK=<YOUR_FALLBACK_RPC_URL>
4042
LOGS_BLOCK_RANGE=7500
4143
SLEEP_TIME=3600
4244
PAGER_DUTY_KEY=<YOUR_PAGER_DUTY_KEY>

alerts/periodic_sender.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/bin/bash
2+
# This script is responsible for the handling of periodic sending, and the clauses for sending proofs.
3+
# It forwards the path of the .env file as the first argument to the sender_with_alert.sh script when
4+
# the conditions are met.
5+
6+
ENV_FILE="$1"
7+
8+
if [[ -z "$ENV_FILE" ]]; then
9+
echo "Usage: $0 path/to/.env"
10+
exit 1
11+
fi
12+
13+
# Fetches the current ETH gas price
14+
function fetch_gas_price() {
15+
gas_price=$(cast gas-price --rpc-url $RPC_URL)
16+
if [[ -z "$gas_price" || "$gas_price" == "0" ]]; then
17+
echo "Primary RPC_URL failed to fetch gas price, trying fallback..."
18+
gas_price=$(cast gas-price --rpc-url $RPC_URL_FALLBACK)
19+
fi
20+
21+
echo $gas_price
22+
}
23+
24+
source "$ENV_FILE"
25+
26+
# Each elapsed interval lasts for 30 minutes
27+
sleep_time=1800
28+
elapsed_intervals=0
29+
30+
./alerts/sender_with_alert.sh "$ENV_FILE"
31+
32+
while true; do
33+
echo "Starting pass #$elapsed_intervals"
34+
35+
current_gas_price=$(fetch_gas_price)
36+
if [[ -z "$current_gas_price" || "$current_gas_price" == "0" ]]; then
37+
echo "Failed to fetch current gas price from both RPC URLs, skipping this pass."
38+
39+
elapsed_intervals=$((elapsed_intervals + 1))
40+
41+
echo "Sleeping $sleep_time seconds (($((sleep_time / 60)) minutes))"
42+
sleep "$sleep_time"
43+
continue
44+
fi
45+
echo "Current gas price: $current_gas_price wei"
46+
47+
# In case current and gas price meet the criteria, send a proof and reset counter
48+
if { [ $elapsed_intervals -ge 10 ] && [ $elapsed_intervals -lt 14 ] && [ $current_gas_price -lt 2000000000 ]; }; then
49+
# Between 10 and 14 elapsed intervals (5 to 7 hours), if gas price is below 2 gwei, send a proof
50+
message="Sending proof at $elapsed_intervals with gas price $current_gas_price wei"
51+
echo "$message"
52+
./alerts/sender_with_alert.sh "$ENV_FILE"
53+
elapsed_intervals=0
54+
elif { [ $elapsed_intervals -ge 14 ] && [ $elapsed_intervals -lt 16 ] && [ $current_gas_price -lt 5000000000 ]; }; then
55+
# Between 14 and 16 elapsed intervals (7 to 8 hours), if gas price is below 5 gwei, send a proof
56+
message="Sending proof at $elapsed_intervals with gas price $current_gas_price wei"
57+
echo "$message"
58+
./alerts/sender_with_alert.sh "$ENV_FILE"
59+
elapsed_intervals=0
60+
elif { [ $elapsed_intervals -ge 16 ] && [ $elapsed_intervals -lt 24 ] && [ $current_gas_price -lt 15000000000 ]; }; then
61+
# Between 16 and 24 elapsed intervals (8 to 12 hours), if gas price is below 15 gwei, send a proof
62+
message="Sending proof at $elapsed_intervals with gas price $current_gas_price wei"
63+
echo "$message"
64+
./alerts/sender_with_alert.sh "$ENV_FILE"
65+
elapsed_intervals=0
66+
elif { [ $elapsed_intervals -ge 50 ]; }; then
67+
# After 50 elapsed intervals (25 hours) send a proof
68+
message="Sending proof at $elapsed_intervals with gas price $current_gas_price wei"
69+
echo "$message"
70+
./alerts/sender_with_alert.sh "$ENV_FILE"
71+
elapsed_intervals=0
72+
fi
73+
74+
elapsed_intervals=$((elapsed_intervals + 1))
75+
76+
echo "Sleeping $sleep_time seconds (($((sleep_time / 60)) minutes))"
77+
sleep "$sleep_time"
78+
done

0 commit comments

Comments
 (0)