add scripts

Signed-off-by: Martin Matous <m@matous.dev>
This commit is contained in:
Martin Matous 2022-03-28 00:46:25 +02:00
parent c6c66648ea
commit 5a4419bb4e
Signed by: mmatous
GPG key ID: 8BED4CD352953224
12 changed files with 802 additions and 1 deletions

36
gasquery.py Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python
import json
import requests
import subprocess
import sys
API_KEY = sys.argv[1]
BASEFEE_THRESHOLD = int(sys.argv[2])
def to_gwei(wei: int) -> int:
return int(wei/(10**9))
response = requests.post(
url=f'https://eth-mainnet.alchemyapi.io/v2/{API_KEY}',
data='{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":0}'
)
if not response.ok:
subprocess.run(['notify-send', 'Gasquery', f'Query returned {response.status_code}: {response.reason}'])
print('{"text": "NaN"}')
sys.exit()
basefee = response.json()['result']
basefee = to_gwei(int(basefee, base=16))
if basefee <= BASEFEE_THRESHOLD:
subprocess.run(['notify-send', 'Gasquery', f'Current basefee is {basefee}'])
state = 'Idle'
if basefee < BASEFEE_THRESHOLD:
state = 'Good'
elif basefee > 150:
state = 'Warning'
elif basefee > 200:
state = 'Critical'
text = '{' + f'"state": "{state}", "text": "{basefee}"' + '}'
print(text)