Skip to content

Instantly share code, notes, and snippets.

@d10r
Created April 25, 2024 09:41
Show Gist options
  • Save d10r/b835eb5a5ebaaff3c9400009e23ac679 to your computer and use it in GitHub Desktop.
Save d10r/b835eb5a5ebaaff3c9400009e23ac679 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Queries the native coin amount deposited for a given account with a paymaster.
# Requires cast and python installed
set -eu
#set -x
# Check if exactly two arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <network> <account_address> <threshold_in_ether>"
exit 1
fi
# Capture the account address and threshold from the command-line arguments
network=$1
account_address=$2
threshold_in_ether=$3
# TODO: complete RPC URL
rpc="https://$network.rpc..."
# it's deployed to this address on some networks
paymaster=${PAYMASTER:-0x00000f79B7FaF42EEBAdbA19aCc07cD08Af44789}
# #notif-ops-balances
slack_webhook="$SLACK_WEBHOOK"
# Check if 'cast' is installed
if ! command -v cast &> /dev/null; then
echo "Error: 'cast' command is not found. Please ensure it is installed and in your PATH."
exit 1
fi
# Convert the threshold from ether to wei using bc, as Bash does not handle floating points
threshold_in_wei=$(echo "$threshold_in_ether * 10^18 / 1" | bc)
# Make the call to the smart contract function and capture the output
hex_value=$(cast call --rpc-url $rpc $paymaster "getBalance(address)" $account_address)
# Convert hex to decimal
decimal_value=$(python3 -c "print(int('$hex_value', 16))")
balance_in_ether=$(python3 -c "print($decimal_value / 10**18)")
# Compare the decimal value
if [[ $(echo "$decimal_value >= $threshold_in_wei" | bc) -eq 1 ]]; then
exit 0
else
curl -X POST -H 'Content-type: application/json' --data '{"text":"Alert: Low Paymaster Balance for '"$account_address"' on '"$network"': '"$balance_in_ether"' ETH (is below threshold of '"$threshold_in_ether"' ETH)."}' $slack_webhook
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment