Skip to content

Instantly share code, notes, and snippets.

@d10r
Created January 11, 2022 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d10r/7de46132578f453f28909281497c0012 to your computer and use it in GitHub Desktop.
Save d10r/7de46132578f453f28909281497c0012 to your computer and use it in GitHub Desktop.
checks the balance of an account on an EVM chain and reports it via a Webhook (tested with Discord)
# checks the balance of the given account and reports it via webhook
# usage:
# python3 balancecheck.py <account> <min_balance>
# ENV vars: RPC, WEBHOOK
import requests
import sys
import os
account = sys.argv[1]
min_balance = float(sys.argv[2])
rpc = os.environ['RPC']
webhook = os.environ['WEBHOOK']
chainid_res = requests.post(rpc, json = {"method": "eth_chainId", "params": [], "id":1, "jsonrpc":"2.0"})
chainid = int(chainid_res.json()['result'], 16)
#print(f'chainId: {chainid}')
balance_query = {
"method": "eth_getBalance",
"params": [ account ],
"id":2,
"jsonrpc":"2.0"
}
balance_res = requests.post(rpc, json = balance_query)
balance = int(balance_res.json()['result'], 16)
balance_human = balance / 1e18
#print(f'balance: {balance}, human: {balance_human}')
raise_alert = True if balance_human < min_balance else False
report = {'content': f"{'@here ⚠️ ' if raise_alert else ''}Balance of {account} on chain {chainid} is {balance_human}"}
report_response = requests.post(webhook, data = report)
assert report_response.ok == True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment