-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
executable file
·436 lines (364 loc) · 13 KB
/
listener.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#! /usr/bin/env python3
import json
import requests
import time
from datetime import datetime
import psycopg2
import telebot
import sqlite3
import os
bot_token = open("bot_token.txt", "r").read()
bot = telebot.TeleBot(bot_token)
db_file = "database.db"
second_to_nanoseconds = 1_000_000_000
minute_to_nanoseconds = 60 * second_to_nanoseconds
hour_to_nanoseconds = 60 * minute_to_nanoseconds
def create_db():
if not os.path.exists(db_file):
with sqlite3.connect(db_file) as conn:
conn.executescript(
"""
CREATE TABLE users(
user_id text primary key
);
"""
)
conn.executescript(
"""
CREATE TABLE test_users(
user_id text primary key
);
"""
)
conn.executescript(
"""
CREATE TABLE variables(
name text primary key,
value text
);
"""
)
conn.executescript(
f"""
insert into variables(name, value)
values
('time', '{int(int(time.time()) * 1_000_000_000)}'),
('test_time', '{int(int(time.time()) * 1_000_000_000)}'),
('offset', '{0}');
"""
)
def get_users_id(users):
with sqlite3.connect(db_file) as conn:
cursor = conn.cursor()
cursor.execute(
f"""
select * from {users}
"""
)
return cursor.fetchall()
def get_users():
return get_users_id("users")
def get_test_users():
return get_users_id("test_users")
def get_variable(variable):
with sqlite3.connect(db_file) as conn:
cursor = conn.cursor()
cursor.execute(
f"""
select name, value from variables where name = '{variable}'
"""
)
(text, value) = (cursor.fetchall())[0]
return value
def get_max_time():
return get_variable("time")
def get_max_test_time():
return get_variable("test_time")
def get_offset():
return get_variable("offset")
def update_variable(variable, value):
with sqlite3.connect(db_file) as conn:
conn.executescript(
f"""
update variables
set value='{value}'
where name='{variable}'
"""
)
def update_max_time(new_max_time):
update_variable("time", new_max_time)
def update_max_test_time(new_max_test_time):
update_variable("test_time", new_max_test_time)
def update_offset(new_offset):
update_variable("offset", new_offset)
def send_message_to_users(message):
users = get_users()
for user in users:
bot.send_message(int(user[0]), message, parse_mode='HTML')
def send_message_to_test_users(message):
test_users = get_test_users()
for user in test_users:
bot.send_message(int(user[0]), message, parse_mode='HTML')
def get_network(net):
if net == "testnet":
return psycopg2.connect(
host="testnet.db.explorer.indexer.near.dev",
database="testnet_explorer",
user="public_readonly",
password="nearprotocol",
)
elif net == "mainnet":
return psycopg2.connect(
host="mainnet.db.explorer.indexer.near.dev",
database="mainnet_explorer",
user="public_readonly",
password="nearprotocol",
)
else:
raise "Wrong network type!"
def get_period(period):
if period == "1m":
return int(time.time() * second_to_nanoseconds - minute_to_nanoseconds)
elif period == "5m":
return int(time.time() * second_to_nanoseconds - minute_to_nanoseconds * 5)
elif period == "15m":
return int(time.time() * second_to_nanoseconds - minute_to_nanoseconds * 15)
elif period == "30m":
return int(time.time() * second_to_nanoseconds - minute_to_nanoseconds * 30)
elif period == "1h":
return int(time.time() * second_to_nanoseconds - hour_to_nanoseconds)
elif period == "2h":
return int(time.time() * second_to_nanoseconds - hour_to_nanoseconds * 2)
elif period == "3h":
return int(time.time() * second_to_nanoseconds - hour_to_nanoseconds * 3)
elif period == "6h":
return int(time.time() * second_to_nanoseconds - hour_to_nanoseconds * 6)
elif period == "12h":
return int(time.time() * second_to_nanoseconds - hour_to_nanoseconds * 12)
elif period == "24h" or period == "1d":
return int(time.time() * second_to_nanoseconds - hour_to_nanoseconds * 24)
else:
raise "Wrong period type!"
def check_args(args):
return (
"args_json" in args.keys()
and "method_name" in args.keys()
and args["method_name"] == "nft_mint"
)
def get_collection(contract, net):
pload = {"jsonrpc": "2.0", "id": "dontcare", "method": "query", "params": {"request_type": "call_function", "finality": "final", "account_id": contract, "method_name": "nft_metadata", "args_base64": "e30="}}
if net == "testnet":
r = requests.post("https://rpc.testnet.near.org", json=pload)
else:
r = requests.post("https://rpc.mainnet.near.org", json=pload)
if "result" not in r.json() or "result" not in r.json()["result"]:
return contract
result = json.loads(bytearray(r.json()["result"]["result"]).decode("utf-8"))
return result["name"]
def cmp(value):
return value[1]
def start(message):
args = message.text.split()
if len(args) != 1:
bot.send_message(message.chat.id, "Try again!")
else:
bot.send_message(message.chat.id, "Hey!")
def help(message):
args = message.text.split()
if len(args) != 1:
bot.send_message(message.chat.id, "Try again!")
else:
bot.send_message(
message.chat.id,
"<u><b>All available commands:</b></u>\n"
"/get_feed <em>network</em> <em>period</em>: Display feed for a given period \n"
# "network = mainnet | testnet\nperiod = 1m | 5m | 15m | 30m | 1h | 6h | 12h | 1d\n"
"/notify_on <em>network</em>: Turn on notifications for newcomer mints\n"
"/notify_off <em>network</em>: Turn off notifications for newcomer mints\n"
"/help: Display help",
parse_mode="HTML",
)
def notify_on(message):
args = message.text.split()
if len(args) == 1 or (len(args) == 2 and args[1] == "mainnet"):
with sqlite3.connect(db_file) as conn:
conn.executescript(
f"""
insert or ignore into users(user_id)
values
('{message.chat.id}')
"""
)
bot.send_message(
message.chat.id,
"Notifications(mainnet) turned <b>on</b>",
parse_mode="HTML",
)
elif len(args) == 2 and args[1] == "testnet":
with sqlite3.connect(db_file) as conn:
conn.executescript(
f"""
insert or ignore into test_users(user_id)
values
('{message.chat.id}')
"""
)
bot.send_message(
message.chat.id,
"Notifications(testnet) turned <b>on</b>",
parse_mode="HTML",
)
else:
bot.send_message(message.chat.id, "Try again!")
def notify_off(message):
args = message.text.split()
if len(args) == 1 or (len(args) == 2 and args[1] == "mainnet"):
with sqlite3.connect(db_file) as conn:
conn.executescript(
f"""
delete from users
where user_id = '{message.chat.id}'
"""
)
bot.send_message(
message.chat.id, "Notifications(mainnet) turned <b>off</b>", parse_mode="HTML"
)
elif len(args) == 2 and args[1] == "testnet":
with sqlite3.connect(db_file) as conn:
conn.executescript(
f"""
delete from test_users
where user_id = '{message.chat.id}'
"""
)
bot.send_message(
message.chat.id, "Notifications(testnet) turned <b>off</b>", parse_mode="HTML"
)
else:
bot.send_message(message.chat.id, "Try again!")
def get_feed(message):
message_args = message.text.split()[1:]
if len(message_args) != 2:
bot.send_message(message.chat.id, "Try again!(Wrong number of arguments)")
return
try:
conn = get_network(message_args[0])
except:
bot.send_message(message.chat.id, "Try again!(Wrong network type)")
return
try:
timestamp = get_period(message_args[1])
except:
bot.send_message(message.chat.id, "Try again!(Wrong period type)")
return
cur = conn.cursor()
cur.execute(
f"""
select
t.emitted_for_receipt_id as id,
t.emitted_at_block_timestamp as time,
t.emitted_by_contract_account_id,
t.event_kind as event,
w.args as args
from
assets__non_fungible_token_events t,
action_receipt_actions w
where
t.emitted_at_block_timestamp > {timestamp} and
t.event_kind = 'MINT' and
t.emitted_for_receipt_id = w.receipt_id
"""
)
mints = cur.fetchall()
number_of_mints = dict()
for (receipt_id, _, contract, _, args) in mints:
if not check_args(args):
continue
number_of_mints[contract] = number_of_mints.setdefault(contract, 0) + 1
feed = list(number_of_mints.items())
feed.sort(key=cmp, reverse=True)
feed = feed[:20]
msg = ""
for (contract, value) in feed:
collection = get_collection(contract, message_args[0])
msg += f"Collection: {collection}\nMints: {value}\n================\n"
if len(feed) == 0:
msg = "No NFT's were minted."
bot.send_message(message.chat.id, msg)
def process_new_update(update):
if update.message == None or update.message.text == None:
return
args = update.message.text.split()
if args[0] == "/start":
start(update.message)
elif args[0] == "/help":
help(update.message)
elif args[0] == "/notify_on":
notify_on(update.message)
elif args[0] == "/notify_off":
notify_off(update.message)
elif args[0] == "/get_feed":
get_feed(update.message)
def process_new_updates(updates):
for update in updates:
process_new_update(update)
def get_updates():
offset = int(get_offset())
updates = bot.get_updates(offset)
for update in updates:
offset = max(offset, update.update_id + 1)
update_offset(offset)
process_new_updates(updates)
def get_new_transactions(net):
if net == "mainnet":
cur_max_time = get_max_time()
else:
cur_max_time = get_max_test_time()
conn = get_network(net)
cur = conn.cursor()
cur.execute(
f"""
select
t.emitted_for_receipt_id as id,
t.emitted_at_block_timestamp as time,
t.emitted_by_contract_account_id,
t.token_new_owner_account_id as owner,
t.event_kind as event,
w.args as args
from
assets__non_fungible_token_events t,
action_receipt_actions w
where
t.emitted_at_block_timestamp > {cur_max_time} and
t.event_kind = 'MINT' and
t.emitted_for_receipt_id = w.receipt_id
"""
)
new_max_time = 0
mints = cur.fetchall()
nm = dict()
for (id, time, contract, owner_id, _, args) in mints:
new_max_time = max(new_max_time, time)
if not check_args(args):
continue
collection = get_collection(contract, net)
nm[(owner_id, collection)] = nm.setdefault((owner_id, collection), 0) + 1
for ((owner_id, collection), num) in nm.items():
if num != 1:
msg = f"NFT MINTED!\n<b>{owner_id}</b> just minted {num} NFT's from <b>{collection}</b> collection on {net}"
else:
msg = f"NFT MINTED!\n<b>{owner_id}</b> just minted NFT from <b>{collection}</b> collection on {net}"
if net == "mainnet":
send_message_to_users(msg)
else:
send_message_to_test_users(msg)
if new_max_time > int(cur_max_time):
if net == "mainnet":
update_max_time(new_max_time)
else:
update_max_test_time(new_max_time)
if __name__ == "__main__":
create_db()
get_updates()
get_new_transactions("mainnet")
get_new_transactions("testnet")