-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
653 lines (560 loc) · 21.3 KB
/
app.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask
from flask import request
from burrow.burrow_handler import storage_balance_of, get_assets_paged_detailed, get_asset_farms_paged, get_account, \
get_price_data, list_token_data, deposit, burrow, withdraw, repay_from_wallet, repay_from_supplied, \
storage_deposit, near_withdraw, increase_collateral, decrease_collateral, send_message, account_stake_booster, \
account_unstake_booster, account_farm_claim_all, health_factor, max_supply_balance, max_burrow_balance, \
max_withdraw_balance, max_adjust_balance, max_repay_from_wallet, max_repay_from_account, account_apy, \
supply_health_factor, burrow_health_factor, repay_from_account_health_factor, withdraw_health_factor, \
check_claim_rewards, supply_not_collateral_health_factor, collateral_health_factor, get_account_all_positions, \
get_config, get_unit_lpt_assets, get_pool_shares, get_shadow_records, deposit_lp, withdraw_lp
import logging
from burrow.tool_util import error, is_number
from burrow.circulating_supply import update_marketcap, get_circulating_supply
from loguru import logger
service_version = "20240221.01"
Welcome = 'Welcome to burrow SDK API server, ' + service_version
app = Flask(__name__)
@app.route('/')
def hello_world():
return Welcome
@app.route('/storage_balance_of', methods=['POST'])
def handle_storage_balance_of():
try:
request_data = request.get_json()
account_id = request_data["account_id"]
token_id = request_data["token_id"]
except Exception as e:
return error("The required field is empty", "1002")
try:
return storage_balance_of(account_id, token_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/storage_deposit', methods=['POST'])
def handle_storage_deposit():
try:
request_data = request.get_json()
account_id = request_data["account_id"]
token_id = request_data["token_id"]
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
try:
return storage_deposit(account_id, token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_assets_paged_detailed', methods=['GET'])
def handle_get_assets_paged():
try:
return get_assets_paged_detailed()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_asset_farms_paged', methods=['GET'])
def handle_get_asset_farms_paged():
try:
return get_asset_farms_paged()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_account/<account_id>', methods=['GET'])
def handle_get_account(account_id):
try:
return get_account(account_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_price_data', methods=['GET'])
def handle_get_price_data():
try:
return get_price_data()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/list_token_data', methods=['GET'])
def handle_list_token_dta():
try:
return list_token_data()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/supply', methods=['POST'])
def handle_supply():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
is_collateral = request_data["is_collateral"]
pool_id = ""
if "pool_id" in request_data:
pool_id = request_data["pool_id"]
amount = ""
if "amount" in request_data:
amount = request_data["amount"]
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "" or is_collateral is None or is_collateral == "":
return error("The required field is empty", "1002")
try:
if token_id.startswith("shadow_ref_v1-"):
if not is_number(pool_id):
return error("The pool_id incorrect", "1008")
return deposit_lp(token_id, amount, is_collateral, pool_id)
else:
if not is_number(amount):
return error("Amount Non numeric", "1003")
return deposit(token_id, amount, is_collateral)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/burrow', methods=['POST'])
def handle_burrow():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return burrow(token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/withdraw', methods=['POST'])
def handle_withdraw():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
pool_id = ""
if "pool_id" in request_data:
pool_id = request_data["pool_id"]
amount = ""
if "amount" in request_data:
amount = request_data["amount"]
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
if token_id.startswith("shadow_ref_v1-"):
if not is_number(pool_id):
return error("The pool_id incorrect", "1008")
return withdraw_lp(token_id, amount, pool_id)
else:
if not is_number(amount):
return error("Amount Non numeric", "1003")
return withdraw(token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/repay_from_wallet', methods=['POST'])
def handle_repay_from_wallet():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return repay_from_wallet(token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/repay_from_supplied', methods=['POST'])
def handle_repay_from_supplied():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return repay_from_supplied(token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/near_withdraw', methods=['POST'])
def handle_near_withdraw():
try:
request_data = request.get_json()
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if amount is None or amount == "":
return error("The required field is empty", "1002")
try:
return near_withdraw(amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/increase_collateral', methods=['POST'])
def handle_increase_collateral():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return increase_collateral(token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/decrease_collateral', methods=['POST'])
def handle_decrease_collateral():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return decrease_collateral(token_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/send_message', methods=['POST'])
def handle_send_message():
try:
request_data = request.get_json()
message = request_data["message"]
return send_message(message)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/v1/circulating-supply', methods=['GET'])
def handle_circulating_supply():
try:
return update_marketcap()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/v2/circulating_supply', methods=['GET'])
def handle_get_circulating_supply():
try:
return str(get_circulating_supply())
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/total_supply', methods=['GET'])
def handle_total_supply():
try:
return str(1000000000)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/account_stake_booster', methods=['POST'])
def handle_account_stake_booster():
try:
request_data = request.get_json()
amount = request_data["amount"]
duration = request_data["duration"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
try:
return account_stake_booster(amount, duration)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/account_unstake_booster', methods=['POST'])
def handle_account_unstake_booster():
try:
return account_unstake_booster()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/account_farm_claim_all', methods=['POST'])
def handle_account_farm_claim_all():
try:
return account_farm_claim_all()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/health_factor/<account_id>', methods=['GET'])
def handle_health_factor(account_id):
try:
return health_factor(account_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/max_supply_balance/<account_id>/<token>', methods=['GET'])
def handle_max_supply_balance(account_id, token):
try:
return max_supply_balance(account_id, token, True)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/max_burrow_balance/<account_id>/<token>', methods=['GET'])
def handle_max_burrow_balance(account_id, token):
try:
return max_burrow_balance(account_id, token)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/max_withdraw_balance/<account_id>/<token>', methods=['GET'])
def handle_max_withdraw_balance(account_id, token):
try:
return max_withdraw_balance(account_id, token, True)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/max_adjust_balance/<account_id>/<token>', methods=['GET'])
def handle_max_adjust_balance(account_id, token):
try:
return max_adjust_balance(account_id, token)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/max_repay_from_wallet/<account_id>/<token>', methods=['GET'])
def handle_max_repay_from_wallet(account_id, token):
try:
return max_repay_from_wallet(account_id, token)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/max_repay_from_account/<account_id>/<token>', methods=['GET'])
def handle_max_repay_from_account(account_id, token):
try:
return max_repay_from_account(account_id, token)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/account_apy/<account_id>/<token>', methods=['GET'])
def handle_account_apy(account_id, token):
try:
return account_apy(account_id, token)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/supply_health_factor', methods=['POST'])
def handle_supply_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
is_collateral = request_data["is_collateral"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "" or is_collateral is None or is_collateral == "":
return error("The required field is empty", "1002")
try:
if is_collateral:
ret = supply_health_factor(token_id, account_id, amount, True)
else:
ret = supply_not_collateral_health_factor(account_id, token_id)
return ret
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/burrow_health_factor', methods=['POST'])
def handle_burrow_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return burrow_health_factor(token_id, account_id, amount, True)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/increase_collateral_health_factor', methods=['POST'])
def handle_increase_collateral_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return collateral_health_factor(token_id, account_id, amount, True)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/decrease_collateral_health_factor', methods=['POST'])
def handle_decrease_collateral_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return collateral_health_factor(token_id, account_id, amount, False)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/withdraw_health_factor', methods=['POST'])
def handle_withdraw_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return withdraw_health_factor(token_id, account_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/repay_from_wallet_health_factor', methods=['POST'])
def handle_repay_from_wallet_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return burrow_health_factor(token_id, account_id, amount, False)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/repay_from_account_health_factor', methods=['POST'])
def handle_repay_from_account_health_factor():
try:
request_data = request.get_json()
token_id = request_data["token_id"]
amount = request_data["amount"]
account_id = request_data["account_id"]
if not is_number(amount):
return error("Amount Non numeric", "1003")
except Exception as e:
return error("The required field is empty", "1002")
if token_id is None or token_id == "":
return error("The required field is empty", "1002")
try:
return repay_from_account_health_factor(token_id, account_id, amount)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/check_claim_rewards/<account_id>', methods=['GET'])
def handle_check_claim_rewards(account_id):
try:
return check_claim_rewards(account_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_account_all_positions/<account_id>', methods=['GET'])
def handle_get_account_all_positions(account_id):
try:
return get_account_all_positions(account_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_config', methods=['GET'])
def handle_get_config():
try:
return get_config()
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_unit_lpt_assets', methods=['POST'])
def handle_get_unit_lpt_assets():
try:
request_data = request.get_json()
contract_id = request_data["contract_id"]
pool_ids = request_data["pool_ids"]
except Exception as e:
return error("The required field is empty", "1002")
if contract_id is None or contract_id == "" or pool_ids is None or pool_ids == "":
return error("The required field is empty", "1002")
try:
return get_unit_lpt_assets(contract_id, pool_ids)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_pool_shares', methods=['POST'])
def handle_get_pool_shares():
try:
request_data = request.get_json()
contract_id = request_data["contract_id"]
account_id = request_data["account_id"]
pool_id = request_data["pool_id"]
except Exception as e:
return error("The required field is empty", "1002")
if contract_id is None or contract_id == "" or pool_id is None or pool_id == "" or account_id is None or account_id == "":
return error("The required field is empty", "1002")
try:
return get_pool_shares(contract_id, account_id, pool_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
@app.route('/get_shadow_records', methods=['POST'])
def handle_get_shadow_records():
try:
request_data = request.get_json()
contract_id = request_data["contract_id"]
account_id = request_data["account_id"]
except Exception as e:
return error("The required field is empty", "1002")
if contract_id is None or contract_id == "" or account_id is None or account_id == "":
return error("The required field is empty", "1002")
try:
return get_shadow_records(contract_id, account_id)
except Exception as e:
msg = str(e.args)
return error(msg, "1001")
logger.add("burrow.log")
if __name__ == '__main__':
app.logger.setLevel(logging.INFO)
app.logger.info(Welcome)
app.run(host='0.0.0.0', port=8100, debug=False)