Skip to content

Commit

Permalink
Merge branch 'main' into 640-docs-create-example-that-checks-public-i…
Browse files Browse the repository at this point in the history
…nput
  • Loading branch information
NicolasRampoldi authored Jul 18, 2024
2 parents c46abb4 + 2260ae1 commit 4345c33
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions alerts/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
SLACK_WEBHOOK_URL=<YOUR_SLACK_WEBHOOK_URL>
TELEGRAM_BOT_TOKEN=<YOUR_TELEGRAM_BOT_TOKEN>
TELEGRAM_CHAT_ID=<YOUR_TELEGRAM_CHAT_ID>

# Variables for contract_alerts.sh
RPC_URL=<YOUR_RPC_URL>
Expand All @@ -9,3 +11,9 @@ VERIFIED_BATCH_TOPIC=<YOUR_VERIFIED_BATCH_TOPIC>
# Variables for process_errors_alerts.sh
SERVICE=<YOUR_SERVICE>
EXPRESSION=<GREP_EXPRESSION>

# Variables for balance_alerts.sh
RPC_URL=<YOUR_RPC_URL>
PAYMENT_CONTRACT_ADDRESS=<YOUR_PAYMENT_CONTRACT_ADDRESS>
BALANCE_THRESHOLD=<YOUR_BALANCE_THRESHOLD_IN_ETH>
WALLET_ADDRESS=<YOUR_WALLET_ADDRESS>
51 changes: 51 additions & 0 deletions alerts/balance_alerts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

# Load env file from $1 path
source $1

# Function to send slack message
# @param message
function send_slack_message() {
curl -X POST -H 'Content-type: application/json' \
--data "{\"text\":\"$1\"}" \
$SLACK_WEBHOOK_URL
}

# Function to send telegram message
# @param message
function send_telegram_message() {
curl -s -X POST https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage \
-d chat_id=$TELEGRAM_CHAT_ID \
-d text="$1" \
-d disable_notification=true
}

# Flags to avoid sending multiple alerts
balance_alert=false

while :
do
balance_wei=$(cast call --rpc-url $RPC_URL $PAYMENT_CONTRACT_ADDRESS "UserBalances(address)(uint256)" $WALLET_ADDRESS | cut -d' ' -f1)

balance_eth=$(cast from-wei $balance_wei)

if [ 1 -eq "$(echo "$balance_eth < $BALANCE_THRESHOLD" | bc)" ]; then
message="⚠️ WARNING: Wallet $WALLET_ADDRESS balance ($balance_eth ETH) is below $BALANCE_THRESHOLD ETH"
printf "$message\n"
if [ "$balance_alert" = false ]; then
send_slack_message "$message"
send_telegram_message "$message"
fi
balance_alert=true
else
message="🟩 INFO: Wallet $WALLET_ADDRESS balance ($balance_eth ETH) is above $BALANCE_THRESHOLD ETH"
printf "$message\n"
if [ "$balance_alert" = true ]; then
send_slack_message "$message"
send_telegram_message "$message"
fi
balance_alert=false
fi

sleep 600
done

0 comments on commit 4345c33

Please sign in to comment.