This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.py
More file actions
62 lines (50 loc) · 1.59 KB
/
transfer.py
File metadata and controls
62 lines (50 loc) · 1.59 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
import json
from discord.ext import commands
import asyncio
from threading import Lock
transfer_mutex = Lock()
pool = []
def is_valid_json(json) -> bool:
try:
if len(json['source']) != 70:
return False
if len(json['destination']) != 70:
return False
amount = json['amount']
if amount < pow(10, 6) or amount > pow(10, 15):
return False
if json['tick'] <= 0:
return False
if len(json['signature']) <= 0:
return False
return True
except Exception:
return False
@commands.command()
async def transfer(ctx, *, transfer_data):
json_array = json.loads("[]")
try:
json_obj = json.loads(transfer_data)
if is_valid_json(json_obj):
json_array.append(json_obj)
except Exception as e:
print(e)
await ctx.reply(e)
if len(json_array) > 0:
pool.append(json_array)
if transfer_mutex.locked():
return
transfer_mutex.acquire()
while len(pool) > 0:
print(f"Transfer: pool len -- {len(pool)}")
reader, writer = await asyncio.open_connection("172.19.0.2", 21847)
try:
print("Send transfer")
json_str = json.dumps(pool.pop(0))
writer.write(json_str.encode())
await writer.drain()
writer.close()
await asyncio.gather(writer.wait_closed(), ctx.channel.send("Transfer was sent"))
except Exception as e:
print(e)
transfer_mutex.release()