forked from Solexplorer/discord-gas-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas_bot.py
370 lines (300 loc) · 13.4 KB
/
gas_bot.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
Run a Discord bot that takes the !gas command and shows the status in an embed + shows the prices in the sidebar
"""
# Example:
# python3 gas_bot.py -s etherscan
from typing import Tuple
import logging
import yaml
import discord
import asyncio
from discord.ext.commands import Bot
import argparse
import requests
import time
from tinydb import TinyDB, Query
from tinydb.operations import delete
from tinydb import where
import requests
import json
from types import SimpleNamespace
import re
db = TinyDB('db.json')
table = Query()
def get_NFT_image(url):
##givenURL will look like https://opensea.io/assets/0x10064373e248bc7253653ca05df73cf226202956/11211
##strip after assets/
asset = url.split("assets/")
asset = asset[1]
asset = asset.split("?")
asset = asset[0]
#print(asset)
##add to api url
APIurl = "https://api.opensea.io/api/v1/asset/"+asset
response = requests.request("GET", APIurl)
#print(response.text)
x = json.loads(response.text, object_hook=lambda d: SimpleNamespace(**d))
#print(x.name, x.hometown.name, x.hometown.id)
if x.image_original_url != None:
print(x.image_original_url)
return x.image_original_url
else:
print(x.image_url)
return x.image_url
def get_gas_from_etherscan(key: str,
verbose: bool = False) -> Tuple[int, int, int]:
"""
Fetch gas from Etherscan API
"""
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().get('result')
return int(data['FastGasPrice']), int(data['ProposeGasPrice']), int(data['SafeGasPrice'])
else:
if verbose:
print(r.status_code)
time.sleep(10)
def get_gas_from_gasnow(verbose: bool = False) -> Tuple[int, int, int]:
"""
Fetch gas from Gasnow API
"""
r = requests.get('https://www.gasnow.org/api/v3/gas/price')
if r.status_code == 200:
if verbose:
print('200 OK')
data = r.json()['data']
return int(data['fast'] // 1e9), int(data['standard'] // 1e9), int(data['slow'] // 1e9)
else:
if verbose:
print(r.status_code)
time.sleep(10)
def get_gas_from_ethgasstation(key: str, verbose: bool = False):
"""
Fetch gas from ETHGASSTATION API
"""
r = requests.get('https://ethgasstation.info/api/ethgasAPI.json?', params={'api-key': key})
if r.status_code == 200:
if verbose:
print('200 OK')
data = r.json()
return int(data['fastest'] / 10), int(data['fast'] / 10), int(data['average'] / 10), int(
data['safeLow'] / 10), int(data['fastestWait'] * 60), int(data['fastWait'] * 60), int(
data['avgWait'] * 60), int(data['safeLowWait'] * 60)
else:
if verbose:
print(r.status_code)
time.sleep(10)
def main(source, verbose=False):
# 1. Instantiate the bot
# Allow the command prefix to be either ! or %
bot = Bot(command_prefix=('!', '%'), help_command=None)
@bot.event
async def on_message(message):
if message.content == "!thread":
f = await message.channel.create_thread(name="Thread", minutes=60, message=message)
openseaAssetURL = "https://opensea.io/assets"
if openseaAssetURL in message.content:
#pull URL only from message
apiImageUrl = re.search("(?P<url>https?://[^\s]+)", message.content).group("url")
imgURL = get_NFT_image(apiImageUrl)
print(imgURL)
#print(message.channel)
await message.channel.send(f"{imgURL}")
await bot.process_commands(message)
@bot.command(pass_context=True, brief="Get ETH gas prices")
async def gas(ctx):
embed = discord.Embed(title=":fuelpump: Current gas prices")
if source == 'ethgasstation':
fastest, fast, average, slow, fastestWait, fastWait, avgWait, slowWait = get_gas_from_ethgasstation(
config['ethgasstationKey'],
verbose=verbose)
embed.add_field(name=f"Slow :turtle: | {slowWait} seconds", value=f"{round(float(slow), 1)} Gwei",
inline=False)
embed.add_field(name=f"Average :person_walking: | {avgWait} seconds",
value=f"{round(float(average), 1)} Gwei", inline=False)
embed.add_field(name=f"Fast :race_car: | {fastWait} seconds", value=f"{round(float(fast), 1)} Gwei",
inline=False)
embed.add_field(name=f"Quick :zap: | {fastestWait} seconds", value=f"{round(float(fastest), 1)} Gwei",
inline=False)
else:
if source == 'etherscan':
fast, average, slow = get_gas_from_etherscan(config['etherscanKey'], verbose=verbose)
else:
fast, average, slow = get_gas_from_gasnow(verbose=verbose)
embed.add_field(name=f"Slow :turtle:", value=f"{slow} Gwei", inline=False)
embed.add_field(name=f"Average :person_walking:", value=f"{average} Gwei", inline=False)
embed.add_field(name=f"Fast :zap:", value=f"{fast} Gwei", inline=False)
embed.set_footer(text=f"Fetched from {source}\nUse help to get the list of commands")
embed.set_author(
name='{0.display_name}'.format(ctx.author),
icon_url='{0.avatar_url}'.format(ctx.author)
)
await ctx.send(embed=embed)
@bot.command(pass_context=True, brief="Get the cost for each tx type")
async def fees(ctx):
calculate_eth_tx = lambda gwei, limit: round(gwei * limit * 0.000000001, 4)
fast, standard, slow = get_gas_from_gasnow(verbose=verbose)
eth_price = \
requests.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd').json()[
'ethereum'][
'usd']
simple_tx_fee_eth = calculate_eth_tx(fast, 21000)
simple_tx_fee_usd = round(simple_tx_fee_eth * eth_price, 2)
token_approve_eth = calculate_eth_tx(fast, 51000)
token_approve_usd = round(token_approve_eth * eth_price, 2)
token_transfer_eth = calculate_eth_tx(fast, 48000)
token_transfer_usd = round(token_transfer_eth * eth_price, 2)
range1_uniswap_eth = calculate_eth_tx(fast, 120000)
range1_uniswap_usd = round(range1_uniswap_eth * eth_price, 2)
range2_uniswap_eth = calculate_eth_tx(fast, 200000)
range2_uniswap_usd = round(range2_uniswap_eth * eth_price, 2)
fees_eth = f"**Fast: {fast}** **Standard: {standard}** **Slow: {slow}**\n"\
f"Simple ETH TX: **${simple_tx_fee_usd}** ({simple_tx_fee_eth} Ξ)\n" \
f"Token Approval (ERC20): **${token_approve_usd}** ({token_approve_eth} Ξ)\n" \
f"Token Transfer (ERC20): **${token_transfer_usd}** ({token_transfer_eth} Ξ)\n" \
f"Uniswap Trades: **${range1_uniswap_usd} - ${range2_uniswap_usd}** ({range1_uniswap_eth} Ξ - {range2_uniswap_eth} Ξ)\n"
await ctx.send(fees_eth)
@bot.command(pass_context=True, brief="Get list of commands")
async def help(ctx, args=None):
help_embed = discord.Embed(
title="Gas Tracker",
colour=discord.Colour.from_rgb(206, 17, 38))
help_embed.set_author(
name='{0.display_name}'.format(ctx.author),
icon_url='{0.avatar_url}'.format(ctx.author)
)
command_list = [x for x in bot.commands if not x.hidden]
def sortCommands(value):
return value.name
command_list.sort(key=sortCommands)
if not args:
help_embed.add_field(
name="Command Prefix",
value="`!`",
inline=False)
help_embed.add_field(
name="List of supported commands:",
value="```" + "\n".join(['{:>2}. {:<14}{}'.format(str(i + 1), x.name, x.brief) for i, x in
enumerate(command_list)]) + "```",
inline=False
)
else:
help_embed.add_field(
name="Nope.",
value="Don't think I got that command, boss."
)
help_embed.set_footer(text="For any inquiries, suggestions, or bug reports, get in touch with @Nerte#1804")
await ctx.send(embed=help_embed)
@bot.command(pass_context=True, brief="Get the cost for each tx type")
async def gasping(ctx):
message = ctx.message.content
#trim end off
trimmed = message.rstrip()
#remove command
gasNum = trimmed.split("!gasping ")
gasNum = gasNum[1]
gasUser = ctx.message.author.id
gasChannel = ctx.message.channel.id
#print(ctx.message)
embed = discord.Embed(title=":fuelpump: GasPing Logged")
fast, average, slow = get_gas_from_etherscan(config['etherscanKey'],verbose=verbose)
embed.add_field(name=f"I'll let you know when it hits ", value=f"{gasNum} Gwei", inline=False)
embed.set_footer(text=f"Fetched from {source}\nUse help to get the list of commands")
embed.set_author(
name='{0.display_name}'.format(ctx.author),
icon_url='{0.avatar_url}'.format(ctx.author)
)
print(f"Gas watch: {gasNum}")
print(f"Gas User: {gasUser}")
#search if user already there
search = db.search(where('gasUser') == f'{gasUser}')
#if user not already in db
if not search:
#insert
db.insert({'gasNum': f'{gasNum}', 'gasUser': f'{gasUser}', 'gasChannel': f'{gasChannel}'})
else:
#update
db.remove(where('gasUser') == f"{gasUser}")
db.insert({'gasNum': f'{gasNum}', 'gasUser': f'{gasUser}', 'gasChannel': f'{gasChannel}'})
#add to text file
#file1 = open("gasPingLog.txt", "a")
#file1.write(f"{gasNum} {gasUser} {gasChannel}\n")
#file1.close()
await ctx.send(embed=embed)
# 2. Load config
filename = 'config.yaml'
with open(filename) as f:
config = yaml.load(f, Loader=yaml.Loader)
async def send_update(fastest, average, slow, **kw):
status = f'⚡{fastest} |🐢{slow} | !help'
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing,name=status))
for guild in bot.guilds:
guser = guild.get_member(bot.user.id);
await guser.edit(nick=f'Gas: 🚶{average}');
#print (f"{guild}")
print (f"{average}")
##search db for gas notification
notifyList = db.search(Query()['gasNum'] == f"{average}")
print(f"{notifyList}")
#loop through notifyList post in discord tagging users of that gas number
for results in notifyList:
print(results)
user = results['gasUser']
channelid = results['gasChannel']
gas = results['gasNum']
channel = bot.get_channel(id=int(channelid))
print(f"<@{user}>, Gwei is now {gas}")
#delete from database
#db.update(delete, Query()['gasUser'] == f"{user}")
db.remove(where('gasUser') == f"{user}")
#remove from LIST
#notifyList.pop(idx)
await channel.send(f"<@{user}>, Gwei is now {gas}")
await asyncio.sleep(config['updateFreq']) # in seconds
@bot.event
async def on_ready():
"""
When discord client is ready
"""
while True:
# 3. Fetch gas
try:
if source == 'etherscan':
gweiList = get_gas_from_etherscan(config['etherscanKey'],
verbose=verbose)
elif source == 'gasnow':
gweiList = get_gas_from_gasnow(verbose=verbose)
elif source == 'ethgasstation':
gweiList = get_gas_from_ethgasstation(config['ethgasstationKey'])
await send_update(gweiList[0], gweiList[2], gweiList[3])
continue
else:
raise NotImplemented('Unsupported source')
# 4. Feed it to the bot
await send_update(*gweiList)
except Exception as exc:
logger.error(exc)
continue
bot.run(config['discordBotKey'])
if __name__ == '__main__':
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--source',
choices=['etherscan', 'gasnow', 'ethgasstation'],
default='etherscan',
help='select API')
parser.add_argument('-v', '--verbose',
action='store_true',
help='toggle verbose')
args = parser.parse_args()
main(source=args.source,
verbose=args.verbose)