|
| 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