-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecho.py
executable file
·103 lines (82 loc) · 2.91 KB
/
echo.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
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
from sys import argv
import argparse
import json
import pywaves as pw
import pandas as pd
parseConfig = lambda x: {"owner": x.split("_")[1], "receiver": x.split("_")[3], "share": x.split("_")[2]}
filterConfigs = lambda cfgs: [x for x in cfgs if x["owner"] != x["receiver"]]
def accountsAggregator(payments):
arr = []
for a in set([p["recipient"] for p in payments]):
arr.append({
"recipient": a,
"amount": sum([x["amount"] for x in payments if x["recipient"] == a])
})
return arr
def refferalWizard(payments, configs):
direct = []
ref = []
for p in accountsAggregator(payments):
ppmt = p["amount"]
for c in configs:
if p["recipient"] == c["owner"]:
share = int(c["share"])/100
ref.append({
"recipient": c["receiver"],
"amount": int(ppmt*share)
})
ppmt = int(ppmt*(1 - share))
direct.append({
"recipient": p["recipient"],
"amount": ppmt
})
return {
"direct": direct,
"ref": accountsAggregator(ref)
}
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
parsed_path = urlparse(self.path)
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps({
'method': self.command,
'path': self.path,
'real_path': parsed_path.query,
'query': parsed_path.query,
'request_version': self.request_version,
'protocol_version': self.protocol_version
}).encode())
return
def do_POST(self):
content_len = int(self.headers.get('content-length'))
post_body = self.rfile.read(content_len)
data = json.loads(post_body)
datapayments = data["payments"]
oracle = pw.Oracle(oracleAddress = '3PNikM6yp4NqcSU8guxQtmR5onr2D4e8yTJ')
configs = oracle.getData(regex = 'stakingconfig_current_.*')
fconfigs = filterConfigs(map(lambda x: parseConfig(x["value"]), configs))
result = refferalWizard(datapayments, fconfigs)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(result).encode())
return
class ScriptUtils:
@staticmethod
def get_params():
port = 8000
# parser = argparse.ArgumentParser()
# parser.add_argument('--nodeUrl')
# args = parser.parse_args()
if len(argv) > 0:
port = int(argv[1])
return [port]
if __name__ == '__main__':
[port] = ScriptUtils.get_params()
server = HTTPServer(('0.0.0.0', port), RequestHandler)
print('Starting server at http://localhost:' + str(port))
server.serve_forever()