-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAlphaProof.py
More file actions
206 lines (152 loc) · 6.78 KB
/
AlphaProof.py
File metadata and controls
206 lines (152 loc) · 6.78 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
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
import requests
import hashlib
import random
import string
import json
class Client():
'''
This module allows human and algorithmic traders to create immutables proofs of their trading performance.\n
The Proof-of-ROI protocol uses a commit-reveal scheme to ensure that the valuable signals are only publicly verifiable once they lost their value.
Dependencies:
* requests
* hashlib
\n
Usage:
At the moment the signal is created call the `commit` or the `commit_encrypted` method and provide the required data.
After enougth time is passed and the signal has lost its value call the `reveal` or `reveal_encrypted`method.
Examples:
Example of how the Module is used can be found at:\n
`test.py`
'''
def __init__(self, api_key, base_url):
self.api_key = api_key
self.base_url = base_url
def open_commits(self) -> dict:
''' Gets a list of all commits that have not yet been revealed
Returns:
a JSON object in format {
'list': the list of dicts with commited data,\n
'message': 'Success' | error_message
}
'''
url_open_commits = self.base_url + "open_commits"
header = {
"X-API-KEY": self.api_key
}
r = requests.get(url_open_commits, headers=header)
return r.json()
def commit(self, signal: str, price_open: str, exchange: str, time_open: str) -> dict:
''' Sends the data to be hashed and commited, data is stored on server for later reveal.
Args:
signal (str): the signal either BUY, SELL or STOP
price_open (str): the price at which the trade was opend
exchange (str): the exchange on which the trade was executed
time_open (str): the time at which the trade was opend, compatible with https://pypi.org/project/dateparser/
Returns:
a JSON object in format {
'tx_hash': the transaction identifier from the Ethereum Blockchain,\n
'message': 'Success' | error_message
}
'''
if signal != "BUY" and signal != "SELL" and signal != "STOP":
return {'message': 'Needs to be one of BUY, SELL, STOP'}
url_commit_normal = self.base_url + "commit"
header = {
"X-API-KEY": self.api_key
}
data = {
"signal": signal,
"price": str(price_open),
"exchange": exchange,
"datetime": time_open
}
r = requests.post(url_commit_normal, headers=header, data=data)
return r.json()
def reveal(self, index=0) -> dict:
''' Reveals a previous commit by publishing its data, default is index 0 (oldes commit).
Args:
index (int, default=0): the index of where the commit is in the list returned by `open_commits`
Returns:
a JSON object in format {
'tx_hash': the transaction identifier from the Ethereum Blockchain,\n
'message': 'Success' | error_message
}
'''
url_reveal = self.base_url + "reveal"
header = {
"X-API-KEY": self.api_key
}
data = {
"api_key": self.api_key,
"index": index
}
r = requests.post(url_reveal, headers=header, data=data)
return r.json()
def commit_encrypted(self, signal: str, price_open: str, exchange: str, time_open: str) -> (str, dict):
''' Encrypts and sends the data to be commited. Encrypted message is stored on server for later reveal.
Args:
signal (str): the signal either BUY, SELL or STOP
price_open (str): the price at which the trade was opend
exchange (str): the exchange on which the trade was executed
time_open (str): the time at which the trade was opend, compatible with https://pypi.org/project/dateparser/
Returns:
the encryption key used to generated the encrypted hash
a JSON object in format {
'tx_hash': the transaction identifier from the Ethereum Blockchain,\n
'index': position of where in the commit list it was stored IMPORTANT: this might change when reveals are made, use `open_commits` instead\n
'message': 'Success' | error_message
}
'''
if signal != "BUY" and signal != "SELL" and signal != "STOP":
return {'message': 'Needs to be one of BUY, SELL, STOP'}
url_commit = self.base_url + "commit_encrypted"
header = {
"X-API-KEY": self.api_key
}
encryption_key = "".join(random.choice(string.ascii_uppercase + string.digits) for i in range(20))
obj = {
"encryption_key": encryption_key,
"signal": signal,
"price": str(price_open),
"exchange": exchange
}
dataBytes = json.dumps(obj)
message = hashlib.sha256(dataBytes.encode()).hexdigest()
data = {
"datetime": time_open,
"message": message
}
r = requests.post(url_commit, headers=header, data=data)
response = r.json()
return encryption_key, response
def reveal_encrypted(self, encryption_key: str, signal: str, price_open: str, exchange: str, time_open: str, index=0):
''' Reveals and encrypted commit by publishing the cleartext data, default is index 0 (oldes commit).
Args:
encryption_key (str): the key returend by `commit_encrypted`
signal (str): the signal either BUY, SELL or STOP
price_open (str): the price at which the trade was opend
exchange (str): the exchange on which the trade was executed
time_open (str): the time at which the trade was opend, compatible with https://pypi.org/project/dateparser/
index (int, default=0): the index of where the commit is in the list returned by `open_commits`
Returns:
a JSON object in format {
'tx_hash': the transaction identifier from the Ethereum Blockchain,\n
'message': 'Success' | error_message
}
'''
if signal != "BUY" and signal != "SELL" and signal != "STOP":
return {'message': 'Needs to be one of BUY, SELL, STOP'}
url_reveal_encr = self.base_url + "reveal_encrypted"
header = {
"X-API-KEY": self.api_key
}
data = {
"index": index,
"encryption_key": encryption_key,
"signal": signal,
"price": str(price_open),
"exchange": exchange,
"datetime": time_open
}
r = requests.post(url_reveal_encr, headers=header, data=data)
return r.json()