-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
336 lines (275 loc) · 14.7 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
import datetime
import requests
import json
import time
import os.path
import re
from telegram.ext import Updater, CommandHandler
# Update the following variables with your own Etherscan and BscScan API keys and Telegram bot token
ETHERSCAN_API_KEY = ''
BSCSCAN_API_KEY = ''
TELEGRAM_BOT_TOKEN = ''
TELEGRAM_CHAT_IDs = ['']
TELEGRAM_CHAT_ID_ADMIN = ''
# Define some helper functions
def get_wallet_transactions(wallet_address, blockchain):
if blockchain == 'eth':
url = f'https://api.etherscan.io/api?module=account&action=txlist&address={wallet_address}&sort=desc&apikey={ETHERSCAN_API_KEY}'
elif blockchain == 'bnb':
url = f'https://api.bscscan.com/api?module=account&action=txlist&address={wallet_address}&sort=desc&apikey={BSCSCAN_API_KEY}'
else:
raise ValueError('Invalid blockchain specified')
response = requests.get(url)
data = json.loads(response.text)
result = data.get('result', [])
if not isinstance(result, list):
print(
f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Error fetching transactions for {wallet_address} on {blockchain.upper()} blockchain: {data}")
return []
return result
def send_telegram_notification(message, value, p_or_m, usd_value, tx_hash, blockchain):
if blockchain == 'eth':
etherscan_link = f'<a href="https://etherscan.io/tx/{tx_hash}">Etherscan</a>'
elif blockchain == 'bnb':
etherscan_link = f'<a href="https://bscscan.com/tx/{tx_hash}">BscScan</a>'
else:
raise ValueError('Invalid blockchain specified')
for i in range(len(TELEGRAM_CHAT_IDs)):
url = f'https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage'
payload = {'chat_id': f'{TELEGRAM_CHAT_IDs[i]}',
'text': f'{message}: {etherscan_link}\nValue: {p_or_m}{value:.6f} {blockchain.upper()} (${usd_value:.2f})',
'parse_mode': 'HTML'}
response = requests.post(url, data=payload)
print(
f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Telegram notification sent with message: {message}, value: {p_or_m}{value} {blockchain.upper()} (${usd_value:.2f})")
return response
def monitor_wallets():
watched_wallets = set()
file_path = "watched_wallets.txt"
if not os.path.exists(file_path):
open(file_path, 'w').close()
latest_tx_hashes = {}
latest_tx_hashes_path = "latest_tx_hashes.json"
if os.path.exists(latest_tx_hashes_path):
with open(latest_tx_hashes_path, "r") as f:
latest_tx_hashes = json.load(f)
last_run_time = 0
last_run_time_path = "last_run_time.txt"
if os.path.exists(last_run_time_path):
with open(last_run_time_path, "r") as f:
last_run_time = int(f.read())
while True:
try:
# Fetch current ETH and BNB prices in USD from CoinGecko API
eth_usd_price_url = 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum%2Cbinancecoin&vs_currencies=usd'
response = requests.get(eth_usd_price_url)
data = json.loads(response.text)
eth_usd_price = data['ethereum']['usd']
bnb_usd_price = data['binancecoin']['usd']
# Read from file
with open(file_path, 'r') as f:
watched_wallets = set(f.read().splitlines())
for wallet in watched_wallets:
blockchain, wallet_address, name_of_wallet = wallet.split(':')
transactions = get_wallet_transactions(wallet_address, blockchain)
for tx in transactions:
tx_hash = tx['hash']
tx_time = int(tx['timeStamp'])
if tx_hash not in latest_tx_hashes and tx_time > last_run_time:
tm = tx['timeStamp']
value = float(tx['value']) / 10 ** 18 # Convert from wei to ETH or BNB
date_time = datetime.datetime.fromtimestamp(int(tm)+7200)
date_time_str = date_time.strftime('%Y-%m-%d %H:%M:%S')
if tx['to'].lower() == wallet_address.lower() and value > 0.001:
usd_value = value * (
eth_usd_price if blockchain == 'eth' else bnb_usd_price) # Calculate value in USD
message = f'🚨 <i>BUY transaction detected on:</i>\n'
message += f' - <b>Name of the wallet:</b> {name_of_wallet}\n'
message += f' - <b>Time:</b> {date_time_str}\n'
try:
contract_address = tx['from']
r = requests.get((f'https://api.etherscan.io/api?module=contract&action=getsourcecode&address={contract_address}&apikey={ETHERSCAN_API_KEY}'))
contract_name = r.json()['result'][0]['ContractName']
if contract_name != '':
message += f' - from: <b>{contract_name}</b>\n'
except:
pass
p_or_m = "-"
send_telegram_notification(message, value, p_or_m, usd_value, tx['hash'], blockchain)
elif tx['from'].lower() == wallet_address.lower() and value > 0.001:
usd_value = value * (
eth_usd_price if blockchain == 'eth' else bnb_usd_price) # Calculate value in USD
message = f'🚨 <i>SELL transaction detected on:</i>\n'
message += f' - Name of the wallet: <b>{name_of_wallet}</b>\n'
message += f' - <b>Time:</b> {date_time_str}\n'
try:
contract_address = tx['to']
r = requests.get((f'https://api.etherscan.io/api?module=contract&action=getsourcecode&address={contract_address}&apikey={ETHERSCAN_API_KEY}'))
contract_name = r.json()['result'][0]['ContractName']
if contract_name != '':
message += f' - from: <b>{contract_name}</b>\n'
except:
print('ni')
p_or_m = "+"
send_telegram_notification(message, value, p_or_m, usd_value, tx['hash'], blockchain)
latest_tx_hashes[tx_hash] = int(tx['blockNumber'])
# Save latest_tx_hashes to file
with open(latest_tx_hashes_path, "w") as f:
json.dump(latest_tx_hashes, f)
# Update last_run_time
last_run_time = int(time.time())
with open(last_run_time_path, "w") as f:
f.write(str(last_run_time))
# Sleep for 15 minute
time.sleep(900)
except Exception as e:
print(f'An error occurred: {e}')
# Sleep for 10 seconds before trying again
time.sleep(10)
def add_wallet(wallet_address, blockchain, name_of_wallet):
file_path = "watched_wallets.txt"
with open(file_path, 'a') as f:
f.write(f'{blockchain}:{wallet_address}:{name_of_wallet}\n')
def remove_wallet(wallet_address, blockchain, name_of_wallet):
file_path = "watched_wallets.txt"
temp_file_path = "temp.txt"
with open(file_path, 'r') as f, open(temp_file_path, 'w') as temp_f:
for line in f:
if line.strip() != f'{blockchain}:{wallet_address}:{name_of_wallet}':
temp_f.write(line)
os.replace(temp_file_path, file_path)
# Define the command handlers for the Telegram bot
def start(update, context):
message = """
👋 *Welcome to the Ethereum and Binance Wallet Monitoring Bot!*
To add a new wallet to monitor, use the command:
/add <blockchain> <wallet_address> <name_of_wallet>
Example: /add ETH 0x123456789abcdef MyETHWallet
To stop monitoring a wallet, use the command:
/remove <blockchain> <wallet_address> <name_of_wallet>
Example: /remove ETH 0x123456789abcdef MyETHWallet
To list all wallets being monitored for a specific blockchain, use the command:
/list <blockchain>
Example: /list ETH
To list all wallets being monitored for all blockchains, use the command:
/list
"""
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
def add(update, context):
user_id = update.effective_user.id
if user_id != int(TELEGRAM_CHAT_ID_ADMIN):
message = "You do not have permission to run this command."
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
return
if len(context.args) < 2:
context.bot.send_message(chat_id=update.message.chat_id,
text="Please provide a blockchain and wallet address to add and name of the wallet Example: /add ETH 0x123456789abcdef MyETHWallet")
return
blockchain = context.args[0].lower()
wallet_address = context.args[1]
name_of_wallet = context.args[2]
print(context)
print(blockchain, wallet_address, wallet_address)
# Check if the wallet address is in the correct format for the specified blockchain
if blockchain == 'eth':
if not re.match(r'^0x[a-fA-F0-9]{40}$', wallet_address):
context.bot.send_message(chat_id=update.message.chat_id,
text=f"{wallet_address} is not a valid Ethereum wallet address.")
return
elif blockchain == 'bnb':
if not re.match(r'^0x[a-fA-F0-9]{40}$', wallet_address):
context.bot.send_message(chat_id=update.message.chat_id,
text=f"{wallet_address} is not a valid Binance Smart Chain wallet address.")
return
else:
context.bot.send_message(chat_id=update.message.chat_id, text=f"Invalid blockchain specified: {blockchain}")
return
add_wallet(wallet_address, blockchain, name_of_wallet)
message = f'Added {wallet_address} to the list of watched {blockchain.upper()} wallets and name of wallet {name_of_wallet}.'
context.bot.send_message(chat_id=update.message.chat_id, text=message)
def remove(update, context):
user_id = update.effective_user.id
if user_id != int(TELEGRAM_CHAT_ID_ADMIN):
message = "You do not have permission to run this command."
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
return
if len(context.args) < 2:
context.bot.send_message(chat_id=update.message.chat_id,
text="Please provide a blockchain and wallet address to remove.\nExample: /remove ETH 0x123456789abcdef MyETHWallet")
return
blockchain = context.args[0].lower()
wallet_address = context.args[1]
name_of_wallet = context.args[2]
remove_wallet(wallet_address, blockchain, name_of_wallet)
message = f'Removed {wallet_address} from the list of watched {blockchain.upper()} wallets.'
context.bot.send_message(chat_id=update.message.chat_id, text=message)
def list_wallets(update, context):
with open("watched_wallets.txt", "r") as f:
wallets = [line.strip() for line in f.readlines()]
if wallets:
eth_wallets = []
bnb_wallets = []
print(eth_wallets)
for wallet in wallets:
blockchain, wallet_address, name_of_wallet = wallet.split(':')
if blockchain == 'eth':
eth_wallets.append(wallet_address + " " + name_of_wallet)
elif blockchain == 'bnb':
bnb_wallets.append(wallet_address + " " + name_of_wallet)
message = "The following wallets are currently being monitored\n"
message += "\n"
if eth_wallets:
message += "*Ethereum Wallets*:\n"
for i, wallet in enumerate(eth_wallets):
message += f"{i + 1}. {wallet}.{name_of_wallet}\n"
message += "\n"
if bnb_wallets:
message += "*Binance Coin Wallets*:\n"
for i, wallet in enumerate(bnb_wallets):
message += f"{i + 1}. {wallet}.{name_of_wallet}\n"
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
else:
message = "There are no wallets currently being monitored."
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
def addchat(update, context):
user_id = update.effective_user.id
if user_id != int(TELEGRAM_CHAT_ID_ADMIN):
message = "You do not have permission to run this command."
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
return
message = f'chat added to the list {update.message.chat_id}'
TELEGRAM_CHAT_IDs.append(update.message.chat_id)
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
def removechat(update, context):
user_id = update.effective_user.id
if user_id != int(TELEGRAM_CHAT_ID_ADMIN):
message = "You do not have permission to run this command."
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
return
chat_id = update.message.chat_id
if chat_id in TELEGRAM_CHAT_IDs:
TELEGRAM_CHAT_IDs.remove(chat_id)
message = f'Chat {chat_id} removed from the list.'
else:
message = f'Chat {chat_id} not found in the list.'
context.bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode='Markdown')
updater = Updater(token=TELEGRAM_BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher
# Define the command handlers
start_handler = CommandHandler('start', start)
add_handler = CommandHandler('add', add)
remove_handler = CommandHandler('remove', remove)
list_handler = CommandHandler('list', list_wallets)
addchat_handler = CommandHandler('addchat', addchat)
removechat_handler = CommandHandler('removechat', removechat)
# Add the command handlers to the dispatcher
dispatcher.add_handler(start_handler)
dispatcher.add_handler(add_handler)
dispatcher.add_handler(remove_handler)
dispatcher.add_handler(list_handler)
dispatcher.add_handler(addchat_handler)
dispatcher.add_handler(removechat_handler)
updater.start_polling()
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Telegram bot started.")
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Monitoring wallets...")
monitor_wallets()