-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcryptoinfo.py
More file actions
299 lines (245 loc) · 12.5 KB
/
cryptoinfo.py
File metadata and controls
299 lines (245 loc) · 12.5 KB
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
@nightyScript(
name="Crypto Address Info v1.10",
author="simnJS",
description="Fetches information about cryptocurrency addresses.",
usage="<p>cryptoinfo <currency> <address>"
)
def CryptoScript():
"""
CRYPTO ADDRESS INFO SCRIPT
--------------------------
This script allows users to search for cryptocurrency address information using blockchain APIs.
COMMANDS:
<p>cryptoinfo <currency> <address> - Search for a crypto address (BTC, LTC, ETH, etc.)
SUPPORTED CURRENCIES:
- BTC (Bitcoin)
- LTC (Litecoin)
- ETH (Ethereum)
- BCH (Bitcoin Cash)
- DOGE (Dogecoin)
- DASH (Dash)
- ZEC (Zcash)
- BTS (BitShares)
EXAMPLES:
<p>cryptoinfo btc 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa - Shows BTC address info
<p>cryptoinfo eth 0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6 - Shows ETH address info
NOTES:
- Supports multiple major cryptocurrencies
- Clean and fast address information lookup
API ENDPOINTS USED:
- https://blockchain.info/rawaddr/{address} - For Bitcoin addresses
- https://api.blockcypher.com/v1/{currency}/main/addrs/{address} - For other currencies
CHANGELOG:
v1.10 - Added EURO conversion ( Thanks to 1gz )
v1.0 - Initial release
- Support for BTC, LTC, ETH, BCH, DOGE
- Balance and transaction information
- Clean error handling and logging
"""
import aiohttp
import asyncio
import json
from datetime import datetime
SUPPORTED_CURRENCIES = {
"btc": {"name": "Bitcoin", "api": "blockchain", "divisor": 100000000},
"ltc": {"name": "Litecoin", "api": "blockcypher", "divisor": 100000000},
"eth": {"name": "Ethereum", "api": "blockcypher", "divisor": 1000000000000000000},
"bch": {"name": "Bitcoin Cash", "api": "blockcypher", "divisor": 100000000},
"doge": {"name": "Dogecoin", "api": "blockcypher", "divisor": 100000000},
"dash": {"name": "Dash", "api": "blockcypher", "divisor": 100000000},
"zec": {"name": "Zcash", "api": "blockcypher", "divisor": 100000000},
"bts": {"name": "BitShares", "api": "blockcypher", "divisor": 100000000}
}
async def get_bitcoin_info(session, address):
url = f"https://blockchain.info/rawaddr/{address}"
headers = {
"Accept": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
try:
async with session.get(url, headers=headers) as response:
if response.status == 200:
data = await response.json()
return {
"address": data.get("address", address),
"balance": data.get("final_balance", 0) / 100000000,
"total_received": data.get("total_received", 0) / 100000000,
"total_sent": data.get("total_sent", 0) / 100000000,
"n_tx": data.get("n_tx", 0),
"currency": "BTC"
}
else:
print(f"Error fetching Bitcoin info: Status {response.status}", type_="ERROR")
return None
except Exception as e:
print(f"Error fetching Bitcoin info: {str(e)}", type_="ERROR")
return None
async def get_blockcypher_info(session, currency, address):
url = f"https://api.blockcypher.com/v1/{currency}/main/addrs/{address}"
headers = {
"Accept": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
try:
async with session.get(url, headers=headers) as response:
if response.status == 200:
data = await response.json()
divisor = SUPPORTED_CURRENCIES[currency]["divisor"]
return {
"address": data.get("address", address),
"balance": data.get("final_balance", 0) / divisor,
"total_received": data.get("total_received", 0) / divisor,
"total_sent": data.get("total_sent", 0) / divisor,
"n_tx": data.get("n_tx", 0),
"currency": currency.upper()
}
else:
print(f"Error fetching {currency} info: Status {response.status}", type_="ERROR")
return None
except Exception as e:
print(f"Error fetching {currency} info: {str(e)}", type_="ERROR")
return None
async def get_eur_conversion_rate(session, currency_symbol):
"""Get EUR conversion rate for a cryptocurrency"""
url = f"https://api.coingecko.com/api/v3/simple/price"
coingecko_ids = {
"BTC": "bitcoin",
"LTC": "litecoin",
"ETH": "ethereum",
"BCH": "bitcoin-cash",
"DOGE": "dogecoin",
"DASH": "dash",
"ZEC": "zcash",
"BTS": "bitshares"
}
currency_id = coingecko_ids.get(currency_symbol.upper())
if not currency_id:
return None
params = {
"ids": currency_id,
"vs_currencies": "eur"
}
headers = {
"Accept": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}
try:
async with session.get(url, params=params, headers=headers) as response:
if response.status == 200:
data = await response.json()
return data.get(currency_id, {}).get("eur")
else:
print(f"Error fetching EUR rate for {currency_symbol}: Status {response.status}", type_="WARNING")
return None
except Exception as e:
print(f"Error fetching EUR rate for {currency_symbol}: {str(e)}", type_="WARNING")
return None
@bot.command(name="cryptoinfo", usage="<currency> <address>", description="Fetches crypto address info")
async def crypto_info(ctx, *, args: str):
await ctx.message.delete()
parts = args.strip().split()
if len(parts) < 2:
supported_list = ", ".join(SUPPORTED_CURRENCIES.keys()).upper()
await forwardEmbedMethod(
channel_id=ctx.channel.id,
content=f"❌ **Usage:** `<p>cryptoinfo <currency> <address>`\n\n**Supported currencies:** {supported_list}",
title="Crypto Address Info"
)
return
currency = parts[0].lower()
address = " ".join(parts[1:])
if currency not in SUPPORTED_CURRENCIES:
supported_list = ", ".join(SUPPORTED_CURRENCIES.keys()).upper()
await forwardEmbedMethod(
channel_id=ctx.channel.id,
content=f"❌ **Unsupported currency:** {currency.upper()}\n\n**Supported currencies:** {supported_list}\n\n**Want more currencies?** Contact **simnJS_** on Discord!",
title="Crypto Address Info"
)
return
min_length = 20
if len(address) < min_length:
await forwardEmbedMethod(
channel_id=ctx.channel.id,
content="❌ **Invalid address format.** Please provide a valid cryptocurrency address.",
title="Crypto Address Info"
)
return
print(f"Looking up {currency.upper()} address: '{address}'", type_="INFO")
msg = await ctx.send(f"Getting {SUPPORTED_CURRENCIES[currency]['name']} information for address '{address[:10]}...', please wait...")
current_private = getConfigData().get("private")
updateConfigData("private", False)
try:
async with aiohttp.ClientSession() as session:
currency_config = SUPPORTED_CURRENCIES[currency]
if currency == "btc":
address_data = await get_bitcoin_info(session, address)
else:
address_data = await get_blockcypher_info(session, currency, address)
if not address_data:
await forwardEmbedMethod(
channel_id=ctx.channel.id,
content=f"❌ **Failed to fetch information for {currency.upper()} address.**\n\nThis could mean:\n• The address doesn't exist\n• The address format is invalid\n• The API is temporarily unavailable.",
title="Crypto Address Info"
)
await msg.delete()
updateConfigData("private", current_private)
return
currency_name = SUPPORTED_CURRENCIES[currency]['name']
if currency in ["btc", "ltc", "bch", "doge", "dash", "zec", "bts"]:
precision = 8
elif currency == "eth":
precision = 6
else:
precision = 8
balance = f"{address_data['balance']:.{precision}f}"
total_received = f"{address_data['total_received']:.{precision}f}"
total_sent = f"{address_data['total_sent']:.{precision}f}"
eur_rate = await get_eur_conversion_rate(session, address_data['currency'])
balance_eur = ""
total_received_eur = ""
total_sent_eur = ""
if eur_rate:
if address_data['balance'] > 0:
balance_eur_value = address_data['balance'] * eur_rate
balance_eur = f" (≈ €{balance_eur_value:,.2f})"
if address_data['total_received'] > 0:
received_eur_value = address_data['total_received'] * eur_rate
total_received_eur = f" (≈ €{received_eur_value:,.2f})"
if address_data['total_sent'] > 0:
sent_eur_value = address_data['total_sent'] * eur_rate
total_sent_eur = f" (≈ €{sent_eur_value:,.2f})"
content = f"""**Currency:** {currency_name} ({address_data['currency']})
**Address:** `{address_data['address']}`
**Current Balance:** {balance} {address_data['currency']}{balance_eur}
**Total Received:** {total_received} {address_data['currency']}{total_received_eur}
**Total Sent:** {total_sent} {address_data['currency']}{total_sent_eur}
**Number of Transactions:** **{address_data['n_tx']}**"""
explorer_links = {
"btc": f"https://blockstream.info/address/{address_data['address']}",
"ltc": f"https://live.blockcypher.com/ltc/{address_data['address']}/",
"eth": f"https://etherscan.io/address/{address_data['address']}",
"bch": f"https://live.blockcypher.com/bch/{address_data['address']}/",
"doge": f"https://live.blockcypher.com/doge/{address_data['address']}/",
"dash": f"https://live.blockcypher.com/dash/{address_data['address']}/",
"zec": f"https://live.blockcypher.com/zec/{address_data['address']}/",
"bts": f"https://live.blockcypher.com/bts/{address_data['address']}/"
}
if currency in explorer_links:
content += f"\n\n**Blockchain Explorer:**\n• [View Address Details]({explorer_links[currency]})"
await forwardEmbedMethod(
channel_id=ctx.channel.id,
content=content,
title="Crypto Address Info"
)
updateConfigData("private", current_private)
await msg.delete()
except Exception as e:
print(f"Error in cryptoinfo command: {str(e)}", type_="ERROR")
await forwardEmbedMethod(
channel_id=ctx.channel.id,
content=f"❌ **Error occurred: {str(e)}**",
title="Crypto Address Info"
)
updateConfigData("private", current_private)
await msg.delete()
CryptoScript()