-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.py
More file actions
337 lines (275 loc) · 11.2 KB
/
wrapper.py
File metadata and controls
337 lines (275 loc) · 11.2 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
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
from flask import Flask
from flask import jsonify
from flask import request
from datetime import datetime, timedelta
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
from src.registration import register_user, register_airdrop
import ccxt
import urllib
import os
from flasgger import Swagger
es = Elasticsearch([os.environ['ELASTICSEARCH_URL']],timeout=60)
app = Flask(__name__)
from flask_cors import CORS
CORS(app)
app.config['SWAGGER'] = {
'title': 'Bitshares ES API',
'uiversion': 2
}
Swagger(app, template_file='wrapper.yaml')
#
# limiter = Limiter(
# app,
# key_func=get_remote_address,
# default_limits=["5 per sec"]
# )
@app.route('/get_account_history')
def get_account_history():
account_id = request.args.get('account_id', False)
operation_type = request.args.get('operation_type', False)
from_ = request.args.get('from_', 0)
size = request.args.get('size', 10)
from_date = request.args.get('from_date', "2015-10-10")
to_date = request.args.get('to_date', "now")
sort_by = request.args.get('sort_by', "-block_data.block_time")
type = request.args.get('type', "data")
agg_field = request.args.get('agg_field', "operation_type")
filter_field = request.args.get('filter_field', False)
filter_value = request.args.get('filter_value', False)
if type != "data":
s = Search(using=es, index="quanta-*")
else:
s = Search(using=es, index="quanta-*", extra={"size": size, "from": from_})
if type == "agg_by_risk":
s.update_from_dict({
"aggs": {
"by_asset": {
"terms": {
"field": "operation_history.op_object.risk.asset_id.keyword"
},
"aggs": {
"total_risk": {
"sum": {
"field": "operation_history.op_object.risk.amount"
}
}
}
}
}
})
if type == "agg_by_payout" or type == "agg_by_loss":
s.update_from_dict({
"aggs": {
"by_asset": {
"terms": {
"field": "operation_history.op_object.payout.asset_id.keyword"
},
"aggs": {
"total_payout": {
"sum": {
"field": "operation_history.op_object.payout.amount"
}
}
}
}
}
})
q = Q()
if account_id and operation_type:
q = Q("match", account_history__account=account_id) & Q("match", operation_type=operation_type)
elif account_id and not operation_type:
q = Q("match", account_history__account=account_id)
elif not account_id and operation_type:
q = Q("match", operation_type=operation_type)
range_query = Q("range", block_data__block_time={'gte': from_date, 'lte': to_date})
s.query = q & range_query
if filter_field and filter_value:
kv = { filter_field: filter_value.split(",")}
s = s.filter("terms", **kv)
if type == "agg_by_payout":
kv = { "operation_history.op_object.payout.amount" : { 'gte': 0}}
s = s.filter("range", **kv)
elif type == "agg_by_loss":
kv = { "operation_history.op_object.payout.amount" : { 'lte': 0}}
s = s.filter("range", **kv)
s = s.sort(sort_by)
#print (s.to_dict())
response = s.execute()
#print (response)
results = []
#print s.count()
if type == "data":
for hit in response:
results.append(hit.to_dict())
else:
for field in response.aggregations:
results.append(field.to_dict())
return jsonify(results)
@app.route('/get_single_operation')
def get_single_operation():
operation_id = request.args.get('operation_id', "1.11.0")
s = Search(using=es, index="bitshares-*", extra={"size": 1})
q = Q("match", account_history__operation_id=operation_id)
s.query = q
response = s.execute()
results = []
for hit in response:
results.append(hit.to_dict())
return jsonify(results)
@app.route('/is_alive')
def is_alive():
find_string = datetime.utcnow().strftime("%Y-%m")
from_date = (datetime.utcnow() - timedelta(days=1)).strftime("%Y-%m-%d")
s = Search(using=es, index="bitshares-" + find_string)
s.query = Q("range", block_data__block_time={'gte': from_date, 'lte': "now"})
s.aggs.metric("max_block_time", "max", field="block_data.block_time")
json_response = {
"server_time": datetime.utcnow(),
"head_block_timestamp": None,
"head_block_time": None
}
try:
response = s.execute()
if response.aggregations.max_block_time.value is not None:
json_response["head_block_time"] = str(response.aggregations.max_block_time.value_as_string)
json_response["head_block_timestamp"] = response.aggregations.max_block_time.value
json_response["deltatime"] = abs((datetime.utcfromtimestamp(json_response["head_block_timestamp"] / 1000) - json_response["server_time"]).total_seconds())
if json_response["deltatime"] < 30:
json_response["status"] = "ok"
else:
json_response["status"] = "out_of_sync"
json_response["error"] = "last_block_too_old"
else:
json_response["status"] = "out_of_sync"
json_response["deltatime"] = "Infinite"
json_response["query_index"] = find_string
json_response["query_from_date"] = from_date
json_response["error"] = "no_blocks_last_24_hours"
except NotFoundError:
json_response["status"] = "out_of_sync"
json_response["deltatime"] = "Infinite"
json_response["error"] = "index_not_found"
json_response["query_index"] = find_string
return jsonify(json_response)
@app.route('/get_trx')
def get_trx():
trx = request.args.get('trx', "738be2bd22e2da31d587d281ea7ee9bd02b9dbf0")
from_ = request.args.get('from_', 0)
size = request.args.get('size', 10)
s = Search(using=es, index="bitshares-*", extra={"size": size, "from": from_})
q = Q("match", block_data__trx_id=trx)
s.query = q
response = s.execute()
results = []
for hit in response:
results.append(hit.to_dict())
return jsonify(results)
import pals
@app.route('/register_account',methods=['POST'])
# @limiter.limit("10 per day")
def register_account():
registrar = os.environ.get("REGISTRAR")
referrer_default = os.environ.get("REFERRER")
content = request.json
try:
locker = pals.Locker('quanta-api', os.environ.get("DB_URL"))
lock = locker.lock("registration")
lock.acquire(blocking=True)
register_user(content["name"], content["public_key"], registrar, "referrer" in content and content["referrer"] or referrer_default)
lock.release()
return jsonify({"status": "success"})
except Exception as inst:
return jsonify({"error": str(inst)}), 400
import time
from cachetools import cached, LRUCache, TTLCache
@cached(cache=TTLCache(maxsize=1024, ttl=15))
def get_data(exchange_str, symbol, timeframe, since, limit):
exchange_found = exchange_str in ccxt.exchanges
if exchange_found:
exchange = getattr(ccxt, exchange_str)({
'timeout': 20000})
if since is None:
ohlcv = exchange.fetch_ohlcv(symbol, timeframe,limit=limit)
return ohlcv
else:
ohlcv = exchange.fetch_ohlcv
return ohlcv
return None
@app.route('/get_external_price')
def get_external_price():
start = time.time()
exchange_str = request.args.get('exchange')
symbol = urllib.parse.unquote(request.args.get('symbol'))
timeframe = request.args.get('timeframe',"1m").lower()
since = request.args.get('since')
limit = int(request.args.get('limit',"300"))
ohlcv = get_data(exchange_str, symbol, timeframe, since, limit)
end = time.time()
print("Exchange API Response", end - start)
if ohlcv:
return jsonify(ohlcv)
else:
return jsonify({"error": "exchange not found"}), 400
@app.route('/claim_airdrop',methods=['POST'])
def claim_airdrop():
content = request.data
registrar = os.environ.get("REGISTRAR")
referrer = os.environ.get("REFERRER")
sig = request.headers.get('signature')
try:
return jsonify(register_airdrop(content, registrar, referrer, sig))
except Exception as inst:
return jsonify({"error": str(inst)}), 200
from src.email_api import verify_email, check_code, send_walletinfo, checkRecaptcha
from src.mailinglist import subscribe
from src.db import create_user
import hashlib
import hmac
import base64
from binascii import hexlify, unhexlify
@app.route('/account/verify_email',methods=['POST'])
def verify_email_post():
content = request.json
try:
sig = request.headers.get('signature')
if sig is not None:
sigObj = hmac.new(bytes(os.environ.get("MOBILE_SECRET"), 'UTF-8'), request.data, hashlib.sha256)
expectSig = hexlify(sigObj.digest()).decode("ascii")
if sig == expectSig:
verify_email(content["email"])
return jsonify({"success": True})
else:
print(sig, expectSig)
return jsonify({"error": str("unexpected sig")}), 400
if checkRecaptcha(content["recaptcha"], os.environ.get("SITE_SECRET")):
verify_email(content["email"])
return jsonify({"success": True})
else:
return jsonify({"error": "recaptcha failed"})
except Exception as inst:
return jsonify({"error": str(inst)}), 200
@app.route('/account/confirm_email',methods=['POST'])
def confirm_email_post():
content = request.json
try:
if check_code(content["email"], int(content["confirm"])):
subscribe(content["email"],"")
return jsonify({"success": True})
else:
return jsonify({"error": "wrong code"}), 400
except Exception as inst:
return jsonify({"error": str(inst)}), 200
@app.route('/account/send_walletinfo',methods=['POST'])
def sendwallet_post():
content = request.json
try:
referrer_default = os.environ.get("REFERRER")
send_walletinfo(content["email"], int(content["confirm"]), content["public_key"], content["account"], content["json"],"referrer" in content and content["referrer"] or referrer_default)
create_user(content["email"],content["public_key"], content["account"])
subscribe(content["email"], content["account"])
return jsonify({"success": True})
except Exception as inst:
return jsonify({"error": repr(inst)}), 200
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)