-
Notifications
You must be signed in to change notification settings - Fork 36
/
gas_run.py
81 lines (71 loc) · 2.54 KB
/
gas_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Run a Discord sidebar bot that shows gas price of the Ethereum blockchain
"""
# Example:
# python3 gas_run.py -s etherscan &
from typing import Tuple
def get_gas_from_etherscan(key: str,
verbose: bool = False) -> Tuple[int]:
"""
Fetch gas from Etherscan API
"""
import requests
import time
r = requests.get('https://api.etherscan.io/api',
params={'module':'gastracker',
'action':'gasoracle',
'apikey':key})
if r.status_code == 200:
if verbose:
print('200 OK')
data = r.json()['result']
return int(data['SafeGasPrice']), int(data['ProposeGasPrice']), int(data['FastGasPrice'])
else:
if verbose:
print(r.status_code)
time.sleep(10)
def main(source, verbose=False):
import yaml
import discord
import asyncio
# 1. Load config
filename = 'gas_config.yaml'
with open(filename) as f:
config = yaml.load(f, Loader=yaml.Loader)
# 2. Connect w the bot
client = discord.Client()
async def send_update(slow, medium, fast):
nickname = f'🚶{medium} gwei'
status = f'⚡{fast} 🐌{slow}'
await client.get_guild(config['guildId']).me.edit(nick=nickname)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching,
name=status))
await asyncio.sleep(config['updateFreq']) # in seconds
@client.event
async def on_ready():
"""
When discord client is ready
"""
while True:
# 3. Fetch gas
if source == 'etherscan':
gweiList = get_gas_from_etherscan(config['etherscanKey'],
verbose=verbose)
else:
raise NotImplemented('Unsupported source')
# 4. Feed it to the bot
await send_update(*gweiList)
client.run(config['discordBotKey'])
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source',
choices=['etherscan'],
default='etherscan',
help='select API')
parser.add_argument('-v', '--verbose',
action='store_true', # equiv. default is False
help='toggle verbose')
args = parser.parse_args()
main(source=args.source,
verbose=args.verbose)