From 3b70f6e52ffa94ce309ba0e8b93c0f63e6acc00a Mon Sep 17 00:00:00 2001 From: Bernat Date: Thu, 30 Jan 2025 11:33:22 +0100 Subject: [PATCH 1/2] Add watchtower script --- watchtower/README.md | 69 ++++++++++++++++++++++ watchtower/hyperliquid_watchtower.sh | 85 ++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 watchtower/README.md create mode 100755 watchtower/hyperliquid_watchtower.sh diff --git a/watchtower/README.md b/watchtower/README.md new file mode 100644 index 0000000..216fc07 --- /dev/null +++ b/watchtower/README.md @@ -0,0 +1,69 @@ +Here's a simple README for your script: + +--- + +# Validator Monitoring Script + +This script monitors a specified validator on the Hyperliquid network (testnet or mainnet) and sends alerts to a Telegram channel if the validator is not active or is jailed. + +## Prerequisites + +- **Bash**: Ensure you have a Unix-like environment with Bash installed. +- **Curl**: The script uses `curl` to make HTTP requests. +- **JQ**: A lightweight and flexible command-line JSON processor. Install it using your package manager (e.g., `apt-get install jq` on Debian-based systems). + +## Setup + +1. **Make the Script Executable**: + + ```bash + chmod +x /path/to/your/script.sh + ``` + +2. **Crontab Setup**: + To run the script every 15 minutes, add it to your crontab: + + ```bash + crontab -e + ``` + + Add the following line, replacing placeholders with your actual values: + + ```bash + */15 * * * * /path/to/your/script.sh -t your_telegram_api_token -c your_telegram_chat_id -v your_target_validator_address -n testnet >> /path/to/your/logfile.log 2>&1 + ``` + +## Usage + +Run the script with the following options: + +```bash +./script.sh -t TELEGRAM_API_TOKEN -c TELEGRAM_CHAT_ID -v TARGET_VALIDATOR -n NETWORK +``` + +- `-t TELEGRAM_API_TOKEN`: Your Telegram bot API token. +- `-c TELEGRAM_CHAT_ID`: The chat ID of the Telegram channel or user to send alerts to. +- `-v TARGET_VALIDATOR`: The address of the validator to monitor. +- `-n NETWORK`: Specify the network to use: `testnet` or `mainnet`. + +## Example + +```bash +./script.sh -t 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 -c -987654321 -v 0xd5f63fd075529c9fea3bd051c9926e4e080bd4aa -n testnet +``` + +## Logging + +The script logs its activities with timestamps to the console. If you have set up the cron job, it will also log to the specified file, helping you monitor its execution and troubleshoot any issues. + +## License + +This script is provided "as-is" without any warranties. Use it at your own risk. + +--- + +### Notes: +- Ensure all paths and values are correctly set in the crontab and when running the script manually. +- Modify the script as needed to fit your specific requirements or environment settings. + + diff --git a/watchtower/hyperliquid_watchtower.sh b/watchtower/hyperliquid_watchtower.sh new file mode 100755 index 0000000..2b268d3 --- /dev/null +++ b/watchtower/hyperliquid_watchtower.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +# Function to log messages +log() { + local message="$1" + echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" +} + +# Function to display usage +usage() { + echo "Usage: $0 -t TELEGRAM_API_TOKEN -c TELEGRAM_CHAT_ID -v TARGET_VALIDATOR -n NETWORK" + echo " -n NETWORK Specify the network to use: 'testnet' or 'mainnet'" + exit 1 +} + +# Parse command-line arguments +while getopts ":t:c:v:n:" opt; do + case $opt in + t) TELEGRAM_API_TOKEN="$OPTARG" + ;; + c) TELEGRAM_CHAT_ID="$OPTARG" + ;; + v) TARGET_VALIDATOR="$OPTARG" + ;; + n) NETWORK="$OPTARG" + ;; + *) usage + ;; + esac +done + +# Check if all required arguments are provided +if [ -z "$TELEGRAM_API_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ] || [ -z "$TARGET_VALIDATOR" ] || [ -z "$NETWORK" ]; then + usage +fi + +# Set API URL based on the network +case $NETWORK in + testnet) + API_URL="https://api.hyperliquid-testnet.xyz/info" + ;; + mainnet) + API_URL="https://api.hyperliquid.xyz/info" + ;; + *) + echo "Invalid network specified. Use 'testnet' or 'mainnet'." + exit 1 + ;; +esac + +# Payload for the API request +PAYLOAD='{ "type": "validatorSummaries"}' + +log "Starting script with target validator: $TARGET_VALIDATOR on $NETWORK using API URL: $API_URL" + +# Fetch the JSON data from the API +response=$(curl -s -X POST --header "Content-Type: application/json" --data "$PAYLOAD" "$API_URL") + +# Clean the response to remove any control characters +clean_response=$(echo "$response" | tr -d '\000-\037') + +# Convert the target validator address to lowercase for comparison +target_validator=$(echo "$TARGET_VALIDATOR" | tr '[:upper:]' '[:lower:]') + +# Check if the response contains the desired validator with the specific conditions +validator_status=$(echo "$clean_response" | jq -r --arg target_validator "$target_validator" ' + .[] | select(.validator | ascii_downcase == $target_validator) | {isActive, isJailed} +') + +is_active=$(echo "$validator_status" | jq -r '.isActive') +is_jailed=$(echo "$validator_status" | jq -r '.isJailed') + +log "Validator status - isActive: $is_active, isJailed: $is_jailed" + +# Check the conditions and send an alert if necessary +if [ "$is_active" != "true" ] || [ "$is_jailed" != "false" ]; then + message="Alert: Validator $TARGET_VALIDATOR is not active or is jailed." + log "$message" + curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_API_TOKEN/sendMessage" \ + -d chat_id="$TELEGRAM_CHAT_ID" \ + -d text="$message" +fi + +log "Script execution completed." + From 3507f3a25c20b47e94f51a55972cd2c72c1e3dd4 Mon Sep 17 00:00:00 2001 From: Bernat Date: Thu, 30 Jan 2025 11:36:38 +0100 Subject: [PATCH 2/2] Remove chatgpt text lol --- watchtower/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/watchtower/README.md b/watchtower/README.md index 216fc07..5f0d280 100644 --- a/watchtower/README.md +++ b/watchtower/README.md @@ -1,7 +1,3 @@ -Here's a simple README for your script: - ---- - # Validator Monitoring Script This script monitors a specified validator on the Hyperliquid network (testnet or mainnet) and sends alerts to a Telegram channel if the validator is not active or is jailed.