-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_handlers.py
541 lines (461 loc) · 22.8 KB
/
event_handlers.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import asyncio
import time
from datetime import datetime, timedelta, timezone
from aiogram import Bot
import eth_abi
from web3 import Web3
import contracts_reader
import helpers
from constants import w3
import constants
tzdata = timezone(timedelta(hours=3))
def handle_task_created(event):
print("task created")
# event args: uint256 taskId, address comToken, uint256 creator, string name, string description, uint deadline, uint reward
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'address', 'uint256', 'string', 'string', 'uint256', 'uint256'], data)
print("\ndecoded args: ", decoded)
task_id = decoded[0]
com_tok = w3.toChecksumAddress(decoded[1])
creator_tg_id = decoded[2]
name = decoded[3]
description = decoded[4]
deadline_ts = decoded[5]
reward = decoded[6]
dt_deadline = time.strftime("%a, %d %b %Y %H:%M:%S MSK", time.localtime(deadline_ts))
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_task_created(task_id, com_tok, creator_tg_id, name, description, dt_deadline, reward)
)
loop.close()
async def send_task_created(task_id, com_tok, creator_tg_id, name, description, date_time_deadline, reward):
# contracts_reader.get_task(ctr_com_id, chat_id, task_id)
print("\ncom_tok from event: ", com_tok)
chat_id = await helpers.get_chat_id_by_token(com_tok)
print("get_chat_id_by_token: ", chat_id)
#chat_id = chat_id[0]
disclaimer = "The description of the task and acceptance criteria must be presented in the description of " \
"the task by its creator.\n" \
f"To propose your solution use <code>/propose_solution {task_id}</code> command." \
f"To get solution description use <code>/get_solution {task_id} solution_id</code> command\n" \
f"IDs of solutions - from 0 to the number of solutions."
_bot = Bot(token=constants.API_TOKEN)
try:
creator = await _bot.get_chat_member(chat_id, creator_tg_id)
creator_username = creator.user.mention
except Exception as e:
creator_username = f"Creator left the chat. Creator Telegram ID: {creator_tg_id}"
res = f"Task created!\n" \
f"Task id: <code>{task_id}</code>\n" \
f"Name: {name}\n" \
f"Creator Telegram username: {creator_username}\n" \
f"Task description: {description}\n" \
f"Deadline: {date_time_deadline}\n" \
f"Reward: {reward/1e18} ETH\n\n" \
+ disclaimer + "\n#task"
await _bot.send_message(chat_id, res, parse_mode="HTML")
await _bot.session.close()
def handle_solution_proposed(event):
# event attr: address comToken, uint256 taskId, uint256 solId, uint256 solver, string solutionLink
print("solution_proposed")
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['address', 'uint256', 'uint256', 'uint256', 'string'], data)
print("\ndecoded args: ", decoded)
com_tok = w3.toChecksumAddress(decoded[0])
task_id = decoded[1]
sol_id = decoded[2]
solver = decoded[3]
sol_descr = decoded[4]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_solution_created(com_tok, task_id, sol_id, solver, sol_descr)
)
#loop.close()
async def send_solution_created(com_tok, task_id, sol_id, solver, sol_descr):
chat_id = await helpers.get_chat_id_by_token(com_tok)
print("get_chat_id_by_token: ", chat_id)
_bot = Bot(token=constants.API_TOKEN)
try:
solver = await _bot.get_chat_member(chat_id, solver)
solver_username = solver.user.mention
except Exception as e:
solver_username = f"Creator left the chat. Creator Telegram ID: {solver}"
res = f"Solution proposed on Task with id = <code>{task_id}</code>!\n" \
f"Solution id: <code>{sol_id}</code>\n" \
f"Solver username: {solver_username}\n" \
f"Solution description: {sol_descr}\n" \
f"#solution"
await _bot.send_message(chat_id, res, parse_mode="HTML")
await _bot.session.close()
def handle_task_voting_started(event):
# event args: uint256 comId, address token, uint256 taskId, uint256 votingId,
# string description, uint256 numberOfChoices, uint256 reward, uint256 deadline)
# _comId, token, _taskId, votingId, description, numberOfChoices, reward, deadline
print("task_voting_started")
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'address', 'uint256', 'uint256', 'string', 'uint256', 'uint256', 'uint256'], data)
print("\ndecoded args: ", decoded)
ctr_com_id = decoded[0]
com_tok = w3.toChecksumAddress(decoded[1])
task_id = decoded[2]
voting_id = decoded[3]
# description = decoded[4]
# numberOfChoices = decoded[5]
voting_deadline_ts = decoded[7]
voting_deadline_dt = time.strftime("%a, %d %b %Y %H:%M:%S MSK", time.localtime(voting_deadline_ts))
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_task_voting_started(ctr_com_id, com_tok, task_id, voting_id, voting_deadline_dt)
)
loop.close()
async def send_task_voting_started(ctr_com_id, com_tok, task_id, voting_id, voting_deadline):
_bot = Bot(token=constants.API_TOKEN)
chat_id = await helpers.get_chat_id_by_token(com_tok)
await _bot.send_message(chat_id, f"Voting on Task with ID = <code>{task_id}</code> started!\n"
f"Voting deadline: {voting_deadline}\n"
f"Voting ID: <code>{voting_id}</code>\n"
f"#task_voting", parse_mode="HTML")
await contracts_reader.get_task(_bot, ctr_com_id, chat_id, task_id)
await _bot.session.close()
def handle_voting_started(event):
# ProposalCreated (address comToken, uint256 proposalId, uint256 creator,
# string proposal, uint256 numberOfOptions, uint256 deadline
print(Web3.toJSON(event))
print("voting_started")
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['address', 'uint256', 'uint256', 'string', 'uint256', 'uint256'], data)
print("\ndecoded args: ", decoded)
com_tok = w3.toChecksumAddress(decoded[0])
voting_id = decoded[1]
creator_tg_id = decoded[2]
description = decoded[3]
number_of_options = decoded[4]
deadline_ts = decoded[5]
date_time_deadline = time.strftime("%a, %d %b %Y %H:%M:%S MSK", time.localtime(deadline_ts))
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_voting_started(com_tok, voting_id, number_of_options, creator_tg_id, description, date_time_deadline)
)
loop.close()
async def send_voting_started(com_tok, voting_id, number_of_options, creator_tg_id, description, date_time_deadline):
chat_id = await helpers.get_chat_id_by_token(com_tok)
disclaimer = "The description of the voting proposal and it's options must be presented in the description of " \
"the task by its creator.\n" \
f"To vote for an option, use \n<code>/vote {com_tok} {voting_id} option_id</code>\ncommand in direct " \
f"messages with bot.\n" \
f"IDs of options - from 1 to {number_of_options}."
_bot = Bot(token=constants.API_TOKEN)
try:
creator = await _bot.get_chat_member(chat_id, creator_tg_id)
creator_username = creator.user.mention
except Exception as e:
creator_username = f"Creator left the chat. Creator Telegram ID: <code>{creator_tg_id}</code>"
time.sleep(2)
await _bot.send_message(chat_id, f"Voting with ID = <code>{voting_id}</code> created!\n"
f"Creator: {creator_username}\n"
f"Description: {description}\n"
f"Number of options: {number_of_options}\n"
f"Voting deadline: {date_time_deadline}\n" + disclaimer +
f"\n#voting", parse_mode="HTML")
await _bot.session.close()
def handle_mem_voted(event):
# event args: MemberVoted(_comId, _propId, member_id)
print("mem_voted")
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'uint256', 'uint256'], data)
print("\ndecoded args: ", decoded)
#ctr_com_id = decoded[0]
voting_id = decoded[1]
mem_id = decoded[2]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_mem_voted(voting_id, mem_id)
)
async def send_mem_voted(voting_id, mem_id):
_bot = Bot(token=constants.API_TOKEN)
await _bot.send_message(mem_id, f"Your vote is accepted! \n(Voting with ID = {voting_id})\n#voting")
await _bot.session.close()
def handle_voting_results_counted(event):
# get_voting
# winning option id, it's description should be in description
# event args: ProposalExecuted(address comToken, uint256 proposalId, uint256[] winningOptions)
print("voting_results_counted")
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['address', 'uint256', 'uint256[]'], data)
print("\ndecoded args: ", decoded)
com_tok = w3.toChecksumAddress(decoded[0])
voting_id = decoded[1]
winning_options = decoded[2]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_voting_results_counted(com_tok, voting_id, winning_options)
)
loop.close()
async def send_voting_results_counted(com_tok, voting_id, winning_options):
_bot = Bot(token=constants.API_TOKEN)
chat_id = await helpers.get_chat_id_by_token(com_tok)
#l = len(winning_options)
#options = ""
#for i in range(l):
# options += f"[{winning_options[i]}] "
ctr_com_id = await contracts_reader.get_ctr_com_id_by_token(com_tok)
voting = await contracts_reader.get_voting(_bot, ctr_com_id, chat_id, voting_id)
await _bot.send_message(chat_id, f"The voting results have been summed up!\n" +
f"{voting}\n#voting", parse_mode="HTML")
await _bot.session.close()
def handle_task_voting_counted(event):
# winning solution id
# get its description and creator
# event args: ResultsCounted(_comId, token, _taskId)
print("task_voting_counted")
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'address', 'uint256'], data)
print("\ndecoded args: ", decoded)
ctr_com_id = decoded[0] # todo test
com_tok = w3.toChecksumAddress(decoded[1])
task_id = decoded[2]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_task_voting_results_counted(ctr_com_id, com_tok, task_id)
)
#loop.close()
async def send_task_voting_results_counted(ctr_com_id, com_tok, task_id):
_bot = Bot(token=constants.API_TOKEN)
chat_id = await helpers.get_chat_id_by_token(com_tok)
voting_id = await contracts_reader.get_voting_by_task_id(ctr_com_id, task_id)
winning_solutions = await contracts_reader.get_voting_winning_options(ctr_com_id, voting_id)
l = len(winning_solutions)
solutions = ""
for i in range(l):
solution = await contracts_reader.get_solution(ctr_com_id, task_id, winning_solutions[i])
solutions += f"\n{solution} \n\n"
if l == 0:
await _bot.send_message(chat_id, f"The results of the voting on the task with id = <code>{task_id}</code> have been "
f"calculated.\nBut none of the options received enough votes (threshold)",
parse_mode="HTML")
else:
notification = f"\nTo reward solvers, use <code>/reward_solvers {task_id}</code> command."
await _bot.send_message(chat_id, f"The results of the voting on the task with id = <code>{task_id}</code> have been "
f"calculated!\n\nWinning solutions: {solutions}\n"
f"To get task description, use <code>/get_task {task_id}</code>\n" + notification +
f"\n#task_voting", parse_mode="HTML")
await _bot.session.close()
def handle_task_rewarded(event):
# rewards sent to winners
# event: TaskClosed(_comToken, _taskId)
print("task_rewarded")
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['address', 'uint256'], data)
print("\ndecoded args: ", decoded)
com_tok = w3.toChecksumAddress(decoded[0])
task_id = decoded[1]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_rewarded(com_tok, task_id)
)
#loop.close()
async def send_rewarded(com_tok, task_id):
_bot = Bot(token=constants.API_TOKEN)
chat_id = await helpers.get_chat_id_by_token(com_tok)
await _bot.send_message(chat_id, f"The solver(s) of the task with id = <code>{task_id}</code> were rewarded!",
parse_mode="HTML")
await _bot.session.close()
def handle_mem_verified(event):
# event: TaskClosed(_comToken, _taskId)
print("mem_verified")
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'uint256'], data)
print("\ndecoded args: ", decoded)
#ctr_com_id = decoded[0]
member_id = decoded[1]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_mem_verified(member_id)
)
async def send_mem_verified(member_id):
_bot = Bot(token=constants.API_TOKEN)
await _bot.send_message(member_id, "Congratulations, you have been verified by a sufficient number of "
"community members. Now you are a verified member and can participate in "
"voting, as well as create and solve tasks.")
await _bot.session.close()
def handle_verification_accepted(event):
print("verification_accepted")
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'uint256', 'uint256'], data)
print("\ndecoded args: ", decoded)
# ctr_com_id = decoded[0]
sender_id = decoded[1]
new_mem_id = decoded[2]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_verification_accepted(sender_id, new_mem_id)
)
async def send_verification_accepted(sender_id, new_mem_id):
_bot = Bot(token=constants.API_TOKEN)
await _bot.send_message(sender_id, f"Your verification of {new_mem_id} member accepted!")
await _bot.session.close()
def handle_member_created(event):
# event args: address _member, _tgID, comToken, ctr_com_id
# MemberCreated(address _member, uint256 _tgID, address comToken, uint256 _comId)
print("handle_member_created")
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['address', 'uint256', 'address', 'uint256'], data)
print("\ndecoded args: ", decoded)
user_tg_id = decoded[1]
com_tok = w3.toChecksumAddress(decoded[2])
user_addr = w3.toChecksumAddress(decoded[0])
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_join_link(user_tg_id, com_tok, user_addr)
)
#loop.close()
async def send_join_link(user_id, com_tok, user_addr):
_bot = Bot(token=constants.API_TOKEN)
chat_id = await helpers.get_chat_id_by_token(com_tok)
expire_date = datetime.now() + timedelta(minutes=2)
link = await _bot.create_chat_invite_link(chat_id, expire_date, 1)
await _bot.send_message(user_id,
f"The check has been successfully passed!\nYou're registered with \n"
f"Telegram ID: <code>{user_id}</code>\n"
f"Address: <code>{user_addr}</code>\n"
f"This is your link to join the chat:"
f"{link.invite_link} \nBe careful: the link will expire in two minutes.\n\n"
f"Note: in order to participate in votng, create and solve tasks, you need to get "
f"verification from other, already verified community members. In addition, if your "
f"community token balance becomes less than threshold, you will be removed from the chat.",
parse_mode="HTML")
await _bot.session.close()
def handle_member_deleted(event):
# kick from group
# event args: tg_id, member_address, com_tok, ctr_com_id
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'address', 'address', 'uint256'], data)
print("\ndecoded args: ", decoded)
member_tg_id = decoded[0]
member_address = w3.toChecksumAddress(decoded[1])
com_tok = w3.toChecksumAddress(decoded[2])
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
kick(com_tok, member_tg_id, member_address)
)
#loop.close()
async def kick(com_tok, member_tg_id, member_address):
_bot = Bot(token=constants.API_TOKEN)
chat_id = await helpers.get_chat_id_by_token(com_tok)
try:
member = await _bot.get_chat_member(chat_id, member_tg_id)
member_username = "@" + member.user.username
except Exception as e:
member_username = f"with TgID = {member_tg_id}"
try:
await _bot.kick_chat_member(chat_id, member_tg_id)
except Exception as e:
await _bot.send_message(chat_id,
f"The user {member_username} lost membership because the balance of community "
f"tokens on his address (<code>{member_address}</code>) is less than the threshold.")
return
await _bot.send_message(chat_id, f"The user {member_username} was kicked out of the chat because the balance of "
f"community tokens on his address (<code>{member_address}</code>) is less than the"
f" threshold.", parse_mode="HTML")
await _bot.send_message(member_tg_id, "You were kicked from the community chat because the balance of "
f"community tokens on address (<code>{member_address}</code>) "
f"became less than the threshold.", parse_mode="HTML")
await _bot.session.close()
def handle_com_created(event):
# write to com owner
# event args: CommunityCreated(comId, comWallet, mainToken, creatorTgID, msg.sender)
print("com_created")
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['uint256', 'address', 'address', 'uint256', 'address'], data)
print("\ndecoded args: ", decoded)
ctr_com_id = decoded[0]
community_wallet = decoded[1]
com_tok = w3.toChecksumAddress(decoded[2])
creator_tg_id = decoded[3]
creator_address = decoded[4]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(
send_com_created(ctr_com_id, community_wallet, com_tok, creator_tg_id, creator_address)
)
#loop.close()
async def send_com_created(ctr_com_id, community_wallet, com_tok, creator_tg_id, creator_address):
_bot = Bot(token=constants.API_TOKEN)
await _bot.send_message(creator_tg_id,
f"The community created successfully\!\nYou're registered as it's creator and a "
f"verified member\. Now you can create a chat for your community, add bot to the chat "
f"and use \n`/add\_community\_token {com_tok}`\n to enable the verification of new participants"
f" for the presence of a token and use the rest of the bot's functionality\. \n\n"
f"You're registered as member with the following parameters:\n"
f"Telegram ID: {creator_tg_id}\n"
f"Address: `{creator_address}`\n\n"
f"Community parameters:\n"
f"Main community token: `{com_tok}`\n"
f"Community wallet: `{community_wallet}`\n"
f"Community ID \(in contracts\): `{ctr_com_id}`\n\n"
f"\nNotice: you need to verify new members after they're getting some community tokens"
f" and joining community chat\. In addition, if their community token balance becomes "
f"less than threshold, they will be removed from the chat\.", parse_mode="MarkdownV2")
await _bot.session.close()
def handle_event2(event):
print("Event: ", event)
data = Web3.toBytes(hexstr=event['data'])
decoded = eth_abi.decode_abi(['string', 'uint256'], data)
print("\ndecoded args: ", decoded)
def switch_events(marker, event):
match marker:
case 0:
handle_task_created(event)
case 1:
handle_solution_proposed(event)
case 2:
handle_task_voting_started(event)
case 3:
handle_voting_started(event)
case 4:
handle_voting_results_counted(event)
case 5:
handle_task_voting_counted(event)
case 6:
handle_task_rewarded(event)
case 7:
handle_member_created(event)
case 8:
handle_member_deleted(event)
case 9:
handle_com_created(event)
case 10:
handle_mem_voted(event)
case 11:
handle_mem_verified(event)
case 12:
handle_verification_accepted(event)
case _:
print(f'Something\'s wrong, I can feel it\n'
f'Event: {event}')
def log_loop(event_filter, poll_interval, marker):
while True:
for event in event_filter.get_new_entries():
switch_events(marker, event)
print(marker)
print('\n')
time.sleep(poll_interval)