-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
420 lines (354 loc) · 14 KB
/
main.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import os
import keep_alive
import aiohttp
import discord
import ens
import io
import base64
import matplotlib.pyplot as plt
from web3 import Web3, HTTPProvider
from discord.ext import commands
from etherscan import Etherscan
from decimal import Decimal
bot = commands.Bot(command_prefix='!scan ')
bot.remove_command('help')
NETWORK = "mainnet" # or ropsten, kovan, rinkeby etc.
EtherscanApiKey = os.environ['ETHERSCAN_API_KEY']
InfuraApiKey = os.environ['INFURA_API_KEY']
w3 = Web3(HTTPProvider(f"https://mainnet.infura.io/v3/{InfuraApiKey}"))
api = Etherscan(api_key=EtherscanApiKey)
def from_wei(wei):
return float(wei) / 10**18
def resolve_address(ens_name):
return w3.ens.address(ens_name)
def get_block_reward(blocknumber):
block_reward = api.get_block_reward_by_block_number(blocknumber)
block_reward_eth = from_wei(block_reward['blockReward'])
return block_reward_eth
async def get_token_balances(address):
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://api.ethplorer.io/getAddressInfo/{address}?apiKey=freekey"
) as response:
if response.status != 200:
return "Error: Couldn't access the Ethplorer API."
data = await response.json()
if not data.get("tokens"):
return "No tokens found at this address."
balances = {}
for token in data["tokens"]:
if token["tokenInfo"].get("decimals") and int(
token["tokenInfo"]["decimals"]) != 0:
balances[token["tokenInfo"]["symbol"]] = {
"balance":
float(token["balance"]) /
10**int(token["tokenInfo"]["decimals"]),
"contract_address":
token["tokenInfo"]["address"]
}
else:
balances[
f"{token['tokenInfo']['name']} (NFT)"] = f"{token['balance']} {token['tokenInfo']['symbol']}"
return balances
async def get_token_price(contract_address):
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://api.coingecko.com/api/v3/coins/ethereum/contract/{contract_address}"
) as response:
if response.status != 200:
return None
data = await response.json()
if not data.get("market_data"):
return None
return float(Decimal(data["market_data"]["current_price"]["usd"]))
last_price = api.get_eth_last_price()
eth_usd_price = float(last_price['ethusd'])
eth_supply = from_wei(api.get_total_eth_supply())
@bot.event
async def on_ready():
print(f"Bot logged in as {bot.user.name}")
@bot.command()
async def hello(ctx):
await ctx.send("Hi")
@bot.command()
async def help(ctx):
embed = discord.Embed(
title="What is Block Scanner?",
description=
"Block Scanner is a Discord bot that provides blockchain information to users in real-time (currently only supports Ethereum Mainnet).",
color=0xa7bf64,
)
embed.set_author(
name="Block Scanner",
icon_url="https://i.imgur.com/miJET1v.png",
)
embed.add_field(
name="!scan hello",
value="Sends a greeting message",
inline=False,
)
embed.add_field(
name="!scan balance [address or ens name]",
value=
"Returns the Ethereum balance of the specified Ethereum address or ENS name",
inline=False,
)
embed.add_field(
name="!scan gas",
value="Returns the current gas price",
inline=False,
)
embed.add_field(
name=
"!scan balancemulti [address or ens name] [address or ens name]...",
value=
"Returns the Ethereum balances of multiple Ethereum addresses or ENS names",
inline=False,
)
embed.add_field(
name="!scan eth",
value="Returns the current ETH price in USD",
inline=False,
)
embed.add_field(
name="!scan ethsupply",
value="Returns the current total ETH supply",
inline=False,
)
embed.add_field(
name="!scan blockreward [blocknumber]",
value="Returns the block reward for the specified block number",
inline=False,
)
embed.add_field(
name="!scan blocksmined [address or ens name]",
value=
"Returns the last 20 blocks mined by the specified Ethereum address or ENS name",
inline=False,
)
embed.add_field(
name="!scan ens [ens name]",
value="Resolves the Ethereum address for the specified ENS name",
inline=False,
)
embed.add_field(
name="!scan portfolio [address or ens name]",
value=
"Returns the token balances of the specified Ethereum address. It may take longer to respond on accounts with a large number of tokens.",
inline=False,
)
embed.add_field(
name="!scan portfoliopic [address or ens name]",
value=
"Returns a pie chart of the specified Ethereum address's portfolio. It may take longer to respond on accounts with a large number of tokens.",
inline=False,
)
embed.set_footer(
text=
"Please write the ENS name with the '.eth' extension. e.g. vitalik.eth"
)
await ctx.send(embed=embed)
@bot.command()
async def balance(ctx, address_or_ens):
if ".eth" in address_or_ens:
# Convert the ENS name to an Ethereum address
address = resolve_address(address_or_ens)
if address is None:
await ctx.send("There is no such ENS name.")
else:
# If ENS resolution fails, assume that the input is already an Ethereum address
address = address_or_ens
short_address = address[:6] + "..." + address[-4:]
balance = api.get_eth_balance(address)
balance_in_ether = float(from_wei(balance))
balance_in_usd = balance_in_ether * eth_usd_price
await ctx.send(
f"Balance for {short_address}: {balance_in_ether:.4f} ETH ({balance_in_usd:.2f}$)"
)
@bot.command()
async def gas(ctx):
gas_price = api.get_proxy_gas_price()
gas_price_wei = int(gas_price, 16)
gas_price_gwei = int(gas_price_wei) // 10**9
await ctx.send(f"Current gas price : {gas_price_gwei} gwei")
@bot.command()
async def balancemulti(ctx, *addresses):
responses = []
for address_or_ens in addresses:
if ".eth" in address_or_ens:
# Convert the ENS name to an Ethereum address
address = resolve_address(address_or_ens)
if address is None:
responses.append(
f"{address_or_ens} : There is no such ENS name.")
continue
else:
# If ENS resolution fails, assume that the input is already an Ethereum address
address = address_or_ens
balance = api.get_eth_balance(address)
balance_in_ether = from_wei(balance)
short_address = address[:6] + "..." + address[-4:]
response = f"{short_address} : {balance_in_ether} ETH"
responses.append(response)
await ctx.send('\n'.join(responses))
@bot.command()
async def eth(ctx):
await ctx.send(f"Current ETH Price : {eth_usd_price}$")
@bot.command()
async def ethsupply(ctx):
formatted_supply = "{:,.4f}".format(eth_supply)
await ctx.send(f"Current ETH Supply : {formatted_supply} ETH")
@bot.command()
async def blockreward(ctx, blocknumber):
reward = get_block_reward(blocknumber)
await ctx.send(f" Block Reward : {reward} ETH")
@bot.command()
async def blocksmined(ctx, address_or_ens):
if ".eth" in address_or_ens:
# Convert the ENS name to an Ethereum address
address = resolve_address(address_or_ens)
if address is None:
await ctx.send("There is no such ENS name.")
else:
# If ENS resolution fails, assume that the input is already an Ethereum address
address = address_or_ens
try:
page = 1
page_size = 20
blocks_mined = api.get_mined_blocks_by_address_paginated(
address, page, page_size)
short_address = address[:6] + "..." + address[-4:]
blocks_mined_last_20 = blocks_mined[:20]
response = f"Last 20 blocks mined by {short_address}:\n"
for i, block in enumerate(blocks_mined_last_20):
response += f"{i+1}. Block: {block['blockNumber']} - Reward: {get_block_reward(block['blockNumber'])} ETH\n"
await ctx.send(response)
except:
await ctx.send(f"{address_or_ens} has not mined any blocks yet.")
@bot.command()
async def ens(ctx, address_or_ens):
address = resolve_address(address_or_ens)
await ctx.send(address)
@bot.command()
async def portfolio(ctx, address_or_ens):
try:
if ".eth" in address_or_ens:
# Convert the ENS name to an Ethereum address
address = resolve_address(address_or_ens)
if address is None:
await ctx.send("There is no such ENS name.")
else:
# If ENS resolution fails, assume that the input is already an Ethereum address
address = address_or_ens
balances = await get_token_balances(address)
short_address = address[:6] + "..." + address[-4:]
response = f"Token balances for {short_address} : \n"
token_balances = {}
nft_balances = {}
for symbol, balance in balances.items():
if "(NFT)" in symbol:
nft_balances[symbol] = balance
else:
token_balances[symbol] = balance
if token_balances:
response += "\nERC20 Token Balances:\n"
for symbol, balance in token_balances.items():
contract_address = balance["contract_address"]
price = await get_token_price(contract_address)
if price is None:
usdbalance = ""
else:
usdbalance = price * float(balance['balance'])
usdbalance = f"({usdbalance:.2f})$"
response += f"{symbol}: {balance['balance']} {usdbalance}\n"
if nft_balances:
response += "\nNon-ERC20 Balances (Possibly NFTs):\n"
for symbol, balance in nft_balances.items():
short_symbol = symbol[:-6]
response += f"{short_symbol}: {balance}\n"
if len(response) > 2000:
error_message = f"Portfolio exceeds Discord's message limit. To see all balances : https://etherscan.io/address/{address}"
await ctx.send(error_message)
else:
await ctx.send(response)
except Exception as e:
await ctx.send(f"Error: {e}")
@bot.command()
async def portfoliopic(ctx, address_or_ens):
try:
if ".eth" in address_or_ens:
# Convert the ENS name to an Ethereum address
address = resolve_address(address_or_ens)
if address is None:
await ctx.send("There is no such ENS name.")
else:
# If ENS resolution fails, assume that the input is already an Ethereum address
address = address_or_ens
balances = await get_token_balances(address)
short_address = address[:6] + "..." + address[-4:]
token_balances = {}
for symbol, balance in balances.items():
if "(NFT)" not in symbol:
contract_address = balance["contract_address"]
price = await get_token_price(contract_address)
usd_balance = price * float(
balance["balance"]) if price is not None else None
if usd_balance is not None:
token_balances[symbol] = usd_balance
if not token_balances:
await ctx.send(
"No ERC20 token balances found for the given address.")
return
# Sort the token balances by USD value in descending order
sorted_balances = sorted(token_balances.items(),
key=lambda x: x[1],
reverse=True)
# Calculate the ETH balance and add it to token_balances dictionary
eth_balance = float(from_wei(api.get_eth_balance(address)))
usd_eth_balance = eth_usd_price * eth_balance if eth_usd_price is not None else None
if usd_eth_balance is not None:
token_balances['ETH'] = usd_eth_balance
# Get the top 10 token balances including ETH balance
sorted_balances = sorted(token_balances.items(),
key=lambda x: x[1],
reverse=True)
top_balances = dict(sorted_balances[:10])
# Add ETH balance to labels and sizes
if 'ETH' in token_balances and 'ETH' not in top_balances:
eth_balance = token_balances['ETH']
eth_label = f"ETH (${eth_balance:.2f})"
# Add ETH balance to top_balances and labels
top_balances[eth_label] = eth_balance
labels.append(eth_label)
sizes.append(eth_balance)
# Create the pie chart
labels = [
f"{symbol} (${usd_balance:.2f})"
for symbol, usd_balance in top_balances.items()
]
sizes = list(top_balances.values())
fig, ax = plt.subplots()
ax.pie(sizes,
labels=labels,
autopct='%1.1f%%',
startangle=90,
shadow=False)
ax.axis('equal')
ax.legend()
# Convert the plot to a PNG image
buffer = io.BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
image_data = buffer.getvalue()
# Convert the PNG image to a base64-encoded string
base64_image = base64.b64encode(image_data).decode('utf-8')
# Send the image as a message
await ctx.send(f"Top ERC20 token balances for {short_address}:",
file=discord.File(io.BytesIO(
base64.b64decode(base64_image)),
filename="portfolio.png"))
except Exception as e:
await ctx.send(f"Error: {e}")
DiscordToken = os.environ['TOKEN']
keep_alive.keep_alive()
bot.run(DiscordToken)