-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
1091 lines (950 loc) · 40.5 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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'Marco'
# Import flask class
from http.client import responses
from flask import Flask
from flask import request
from flask import jsonify
import json
import logging
from indexer_provider import get_proposal_id_hash
from redis_provider import list_farms, list_top_pools, list_pools, list_token_price, list_whitelist, get_token_price, list_base_token_price
from redis_provider import list_pools_by_id_list, list_token_metadata, list_pools_by_tokens, get_pool, list_token_metadata_v2
from redis_provider import list_token_price_by_id_list, get_proposal_hash_by_id, get_24h_pool_volume, get_account_pool_assets
from redis_provider import get_dcl_pools_volume_list, get_24h_pool_volume_list, get_dcl_pools_tvl_list, get_token_price_ratio_report, get_history_token_price_report, get_market_token_price
from utils import combine_pools_info, compress_response_content, get_ip_address, pools_filter, is_base64, combine_dcl_pool_log, handle_dcl_point_bin, handle_point_data, handle_top_bin_fee, handle_dcl_point_bin_by_account, get_circulating_supply, get_lp_lock_info
from config import Cfg
from db_provider import get_history_token_price, query_limit_order_log, query_limit_order_swap, get_liquidity_pools, get_actions, query_dcl_pool_log, query_burrow_liquidate_log, update_burrow_liquidate_log
from db_provider import query_recent_transaction_swap, query_recent_transaction_dcl_swap, \
query_recent_transaction_liquidity, query_recent_transaction_dcl_liquidity, query_recent_transaction_limit_order, query_dcl_points, query_dcl_points_by_account, \
query_dcl_user_unclaimed_fee, query_dcl_user_claimed_fee, query_dcl_user_unclaimed_fee_24h, query_dcl_user_claimed_fee_24h, \
query_dcl_user_tvl, query_dcl_user_change_log, query_burrow_log, get_history_token_price_by_token, add_orderly_trading_data, \
add_liquidation_result, get_liquidation_result, update_liquidation_result, add_user_wallet_info, get_pools_volume_24h
import re
# from flask_limiter import Limiter
from loguru import logger
from analysis_v2_pool_data_s3 import analysis_v2_pool_data_to_s3, analysis_v2_pool_account_data_to_s3
import datetime
from auth.crypto_utl import decrypt
import time
import bleach
import requests
service_version = "20241129.01"
Welcome = 'Welcome to ref datacenter API server, version ' + service_version + ', indexer %s' % \
Cfg.NETWORK[Cfg.NETWORK_ID]["INDEXER_HOST"][-3:]
# Instantiation, which can be regarded as fixed format
app = Flask(__name__)
# limiter = Limiter(
# app,
# key_func=get_ip_address,
# default_limits=["20 per second"],
# # storage_uri="redis://:@127.0.0.1:6379/2"
# storage_uri="redis://:@" + Cfg.REDIS["REDIS_HOST"] + ":6379/2"
# )
@app.before_request
def before_request():
# Processing get requests
path = request.path
if Cfg.NETWORK[Cfg.NETWORK_ID]["AUTH_SWITCH"] and path not in Cfg.NETWORK[Cfg.NETWORK_ID]["NOT_AUTH_LIST"]:
try:
headers_authentication = request.headers.get("Authentication")
if headers_authentication is None or headers_authentication == "":
return 'Authentication error'
decrypted_data = json.loads(decrypt(headers_authentication, Cfg.NETWORK[Cfg.NETWORK_ID]["CRYPTO_AES_KEY"]))
flip_time = int(decrypted_data["time"])
if path != decrypted_data["path"]:
return 'path error'
if int(time.time()) - flip_time > Cfg.NETWORK[Cfg.NETWORK_ID]["SIGN_EXPIRE"] or flip_time - int(
time.time()) > Cfg.NETWORK[Cfg.NETWORK_ID]["SIGN_EXPIRE"]:
return 'time expired'
except Exception as e:
logger.error("decrypt error:", e)
return 'Authentication error'
data = request.args
allowed_tags = []
allowed_attributes = {}
for v in data.values():
v = str(v)
cleaned_value = bleach.clean(v, tags=allowed_tags, attributes=allowed_attributes)
if v != cleaned_value:
return 'Please enter the parameters of the specification!'
@app.route('/authentication', methods=['GET'])
def handle_authentication():
decrypted_data = "null"
path = request.path
if Cfg.NETWORK[Cfg.NETWORK_ID]["AUTH_SWITCH"]:
try:
headers_authentication = request.headers.get("Authentication")
if headers_authentication is None or headers_authentication == "":
return 'Authentication error'
decrypted_data = json.loads(decrypt(headers_authentication, Cfg.NETWORK[Cfg.NETWORK_ID]["CRYPTO_AES_KEY"]))
flip_time = int(decrypted_data["time"])
if path != decrypted_data["path"]:
return 'path error'
if int(time.time()) - flip_time > Cfg.NETWORK[Cfg.NETWORK_ID]["SIGN_EXPIRE"] or flip_time - int(
time.time()) > Cfg.NETWORK[Cfg.NETWORK_ID]["SIGN_EXPIRE"]:
return 'time expired'
except Exception as e:
logger.error("decrypt error:", e)
return 'Authentication error'
return decrypted_data
# route()Method is used to set the route; Similar to spring routing configuration
@app.route('/')
def hello_world():
return Welcome
@app.route('/timestamp', methods=['GET'])
def handle_timestamp():
import time
return jsonify({"ts": int(time.time())})
@app.route('/latest-actions/<account_id>', methods=['GET'])
def handle_latest_actions(account_id):
"""
get user's latest actions
"""
json_obj = []
try:
ret = get_actions(Cfg.NETWORK_ID, account_id)
json_obj = json.loads(ret)
# for obj in json_obj:
# if obj[1] == "":
# obj[1] = get_tx_id(obj[7], Cfg.NETWORK_ID)
except Exception as e:
print("Exception when get_actions: ", e)
return compress_response_content(json_obj)
@app.route('/liquidity-pools/<account_id>', methods=['GET'])
def handle_liquidity_pools(account_id):
"""
get user's liqudity pools
"""
if account_id == "v2.ref-finance.near":
ip_address = get_ip_address()
logger.info("request ip:{}", ip_address)
return ""
ret = []
try:
id_list = get_liquidity_pools(Cfg.NETWORK_ID, account_id)
if len(id_list) > 0:
ret = list_pools_by_id_list(Cfg.NETWORK_ID, [int(x) for x in id_list])
except Exception as e:
print("Exception when get_liquidity_pools: ", e)
return compress_response_content(ret)
@app.route('/list-farms', methods=['GET'])
def handle_list_farms():
"""
list_farms
"""
ret = list_farms(Cfg.NETWORK_ID)
return compress_response_content(ret)
@app.route('/get-token-price', methods=['GET'])
def handle_get_token_price():
"""
list_token_price
"""
token_contract_id = request.args.get("token_id", "N/A")
ret = {"token_contract_id": token_contract_id}
# if token_contract_id == 'usn' or token_contract_id == 'usdt.tether-token.near':
# token_contract_id = "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near"
ret["price"] = get_token_price(Cfg.NETWORK_ID, token_contract_id)
if ret["price"] is None:
ret["price"] = "N/A"
return compress_response_content(ret)
@app.route('/list-token-price', methods=['GET'])
def handle_list_token_price():
"""
list_token_price
"""
ret = {}
prices = list_token_price(Cfg.NETWORK_ID)
for token in Cfg.TOKENS[Cfg.NETWORK_ID]:
if token["NEAR_ID"] in prices:
ret[token["NEAR_ID"]] = {
"price": prices[token["NEAR_ID"]],
"decimal": token["DECIMAL"],
"symbol": token["SYMBOL"],
}
# if usdt exists, mirror its price to USN
# if "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near" in ret:
# ret["usn"] = {
# "price": prices["dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near"],
# "decimal": 18,
# "symbol": "USN",
# }
# ret["usdt.tether-token.near"] = {
# "price": prices["dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near"],
# "decimal": 6,
# "symbol": "USDt",
# }
# # if token.v2.ref-finance.near exists, mirror its info to rftt.tkn.near
# if "token.v2.ref-finance.near" in ret:
# ret["rftt.tkn.near"] = {
# "price": prices["token.v2.ref-finance.near"],
# "decimal": 8,
# "symbol": "RFTT",
# }
return compress_response_content(ret)
@app.route('/get-token-price-by-dapdap', methods=['GET'])
def handle_list_base_token_price():
prices = list_base_token_price(Cfg.NETWORK_ID)
return compress_response_content(prices)
@app.route('/list-token-price-by-ids', methods=['GET'])
def handle_list_token_price_by_ids():
"""
list_token_price_by_ids
"""
ids = request.args.get("ids", "")
# ids = ("|" + ids.lstrip("|").rstrip("|") + "|").replace("|usn|",
# "|dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near|")
# ids = ("|" + ids.lstrip("|").rstrip("|") + "|").replace("|usdt.tether-token.near|",
# "|dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near|")
id_str_list = ids.lstrip("|").rstrip("|").split("|")
prices = list_token_price_by_id_list(Cfg.NETWORK_ID, [str(x) for x in id_str_list])
ret = ["N/A" if i is None else i for i in prices]
return compress_response_content(ret)
@app.route('/list-token', methods=['GET'])
def handle_list_token():
"""
list_token
"""
ret = list_token_metadata(Cfg.NETWORK_ID)
return compress_response_content(ret)
@app.route('/list-token-v2', methods=['GET'])
def handle_list_token_v2():
"""
list_token
"""
ret = list_token_metadata_v2(Cfg.NETWORK_ID)
return compress_response_content(ret)
@app.route('/get-pool', methods=['GET'])
def handle_get_pool():
"""
get_pool
"""
pool_id = request.args.get("pool_id", "N/A")
pool = get_pool(Cfg.NETWORK_ID, pool_id)
if pool:
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
combine_pools_info([pool, ], prices, metadata)
return compress_response_content(pool)
@app.route('/list-top-pools', methods=['GET'])
def handle_list_top_pools():
"""
list_top_pools
"""
pools = list_top_pools(Cfg.NETWORK_ID)
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
combine_pools_info(pools, prices, metadata)
return compress_response_content(pools)
@app.route('/list-pools', methods=['GET'])
def handle_list_pools():
"""
list_pools
"""
tvl = request.args.get("tvl")
amounts = request.args.get("amounts")
pools = list_pools(Cfg.NETWORK_ID)
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
combine_pools_info(pools, prices, metadata)
pools = pools_filter(pools, tvl, amounts)
return compress_response_content(pools)
@app.route('/list-pools-by-tokens', methods=['GET'])
def handle_list_pools_by_tokens():
"""
list_pools_by_tokens
"""
token0 = request.args.get("token0", "N/A")
token1 = request.args.get("token1", "N/A")
pools = list_pools_by_tokens(Cfg.NETWORK_ID, token0, token1)
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
combine_pools_info(pools, prices, metadata)
return compress_response_content(pools)
@app.route('/list-pools-by-ids', methods=['GET'])
def handle_list_pools_by_ids():
"""
list_pools_by_ids
"""
ids = request.args.get("ids", "")
id_str_list = ids.split("|")
pools = list_pools_by_id_list(Cfg.NETWORK_ID, [int(x) for x in id_str_list])
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
combine_pools_info(pools, prices, metadata)
return compress_response_content(pools)
@app.route('/whitelisted-active-pools', methods=['GET'])
def handle_whitelisted_active_pools():
"""
handle_whitelisted_active_pools
"""
ret = []
pools = list_top_pools(Cfg.NETWORK_ID)
whitelist = list_whitelist(Cfg.NETWORK_ID)
for pool in pools:
if pool["pool_kind"] != "SIMPLE_POOL":
continue
token0, token1 = pool['token_account_ids'][0], pool['token_account_ids'][1]
if pool["amounts"][0] == "0":
continue
if token0 in whitelist and token1 in whitelist:
ret.append({
"pool_id": pool["id"],
"token_symbols": pool["token_symbols"],
"token_decimals": [whitelist[token0]["decimals"], whitelist[token1]["decimals"]],
"token_names": [whitelist[token0]["name"], whitelist[token1]["name"]],
"liquidity_amounts": pool["amounts"],
"vol_token0_token1": pool["vol01"],
"vol_token1_token0": pool["vol10"],
})
return compress_response_content(ret)
@app.route('/to-coingecko', methods=['GET'])
def handle_to_coingecko():
"""
handle_price_to_coingecko
"""
ret = {}
# pools = list_pools_by_id_list(Cfg.NETWORK_ID, ['1346', '1429'])
pools = list_top_pools(Cfg.NETWORK_ID)
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
whitelist = list_whitelist(Cfg.NETWORK_ID)
for pool in pools:
if pool["pool_kind"] != "SIMPLE_POOL":
continue
token0, token1 = pool['token_account_ids'][0], pool['token_account_ids'][1]
if pool["amounts"][0] == "0":
continue
if token0 in whitelist and token1 in whitelist:
(balance0, balance1) = (
float(pool['amounts'][0]) / (10 ** metadata[token0]["decimals"]),
float(pool['amounts'][1]) / (10 ** metadata[token1]["decimals"])
)
key = "%s-%s" % (pool["token_symbols"][0], pool["token_symbols"][1])
# add token0_ref_price = token1_price * token1_balance / token0_balance
if balance0 > 0 and balance1 > 0:
vol_to_other_token = {"input": "0", "output": "0"}
vol_from_other_token = {"input": "0", "output": "0"}
if "vol01" in pool:
vol_to_other_token = pool["vol01"]
if "vol10" in pool:
vol_from_other_token = pool["vol10"]
ret[key] = {
"pool_id": pool["id"],
"token_symbol": pool["token_symbols"][0],
"other_token": pool["token_symbols"][1],
"token_decimals": [whitelist[token0]["decimals"], whitelist[token1]["decimals"]],
"liquidity_amounts": pool["amounts"],
"price_in_usd": str(float(prices[token1]) * balance1 / balance0) if token1 in prices else "N/A",
"price_in_other_token": str(balance1 / balance0),
"vol_to_other_token": vol_to_other_token,
"vol_from_other_token": vol_from_other_token,
}
return compress_response_content(ret)
@app.route('/list-history-token-price-by-ids', methods=['GET'])
def handle_history_token_price_by_ids():
ids = request.args.get("ids", "")
ids = ("|" + ids.lstrip("|").rstrip("|") + "|")
id_str_list = ids.lstrip("|").rstrip("|").split("|")
ret = get_history_token_price([str(x) for x in id_str_list])
return compress_response_content(ret)
@app.route('/get-service-version', methods=['GET'])
def get_service_version():
return jsonify(service_version)
@app.route('/get-proposal-hash-by-id', methods=['GET'])
def handle_proposal_hash():
ret = []
proposal_id = request.args.get("proposal_id")
if proposal_id is None:
return ret
proposal_id_list = []
ids = ("|" + proposal_id.lstrip("|").rstrip("|") + "|")
id_str_list = ids.lstrip("|").rstrip("|").split("|")
res = get_proposal_hash_by_id(Cfg.NETWORK_ID, id_str_list)
for proposal in res:
if not proposal is None:
proposal_id_list.append(proposal["proposal_id"])
ret.append(proposal)
difference_set = list(set(id_str_list).difference(set(proposal_id_list)))
if len(difference_set) > 0:
ret += get_proposal_id_hash(Cfg.NETWORK_ID, difference_set)
return compress_response_content(ret)
@app.route('/get-24h-volume-by-id', methods=['GET'])
def handle_24h_pool_volume():
pool_id = request.args.get("pool_id")
if pool_id is None:
return ''
res = get_24h_pool_volume(Cfg.NETWORK_ID, pool_id)
return compress_response_content(res)
@app.route('/get-dcl-pools-volume', methods=['GET'])
def handle_dcl_pools_volume():
pool_id = request.args.get("pool_id")
if pool_id is None:
return ''
res = get_dcl_pools_volume_list(Cfg.NETWORK_ID, pool_id)
return compress_response_content(res)
@app.route('/get-24h-volume-list', methods=['GET'])
def handle_24h_pool_volume_list():
res = get_24h_pool_volume_list(Cfg.NETWORK_ID)
return compress_response_content(res)
@app.route('/get-dcl-pools-tvl-list', methods=['GET'])
def handle_dcl_pools_tvl_list():
pool_id = request.args.get("pool_id")
if pool_id is None:
return ''
res = get_dcl_pools_tvl_list(Cfg.NETWORK_ID, pool_id)
return compress_response_content(res)
@app.route('/get-limit-order-log-by-account/<account_id>', methods=['GET'])
def get_limit_order_log_by_account(account_id):
res = query_limit_order_log(Cfg.NETWORK_ID, account_id)
return compress_response_content(res)
@app.route('/get-limit-order-swap-by-account/<account_id>', methods=['GET'])
def get_limit_order_swap_by_account(account_id):
res = query_limit_order_swap(Cfg.NETWORK_ID, account_id)
return compress_response_content(res)
@app.route('/get-assets-by-account', methods=['GET'])
def handle_assets_by_account():
account_id = request.args.get("account_id")
dimension = request.args.get("dimension")
if account_id is None or dimension is None:
return ""
redis_key = account_id + "_" + dimension.lower()
ret = get_account_pool_assets(Cfg.NETWORK_ID, redis_key)
if ret is None:
return ""
return compress_response_content(json.loads(ret))
@app.route('/token-price-report', methods=['GET'])
def token_price_ratio_report():
token = request.args.get("token")
base_token = request.args.get("base_token")
dimension = request.args.get("dimension")
if token is None or base_token is None or dimension is None:
return "null"
redis_key = token + "->" + base_token + "_" + dimension.lower()
ret = get_token_price_ratio_report(Cfg.NETWORK_ID, redis_key)
if ret is None:
return "null"
return compress_response_content(json.loads(ret))
@app.route('/get-burrow-records', methods=['GET'])
def handle_burrow_records():
account_id = request.args.get("account_id")
page_number = request.args.get("page_number", type=int, default=1)
page_size = request.args.get("page_size", type=int, default=10)
if account_id is None or account_id == '' or page_size == 0:
return ""
burrow_log_list, count_number = query_burrow_log(Cfg.NETWORK_ID, account_id, page_number, page_size)
if count_number % page_size == 0:
total_page = int(count_number / page_size)
else:
total_page = int(count_number / page_size) + 1
for burrow_log in burrow_log_list:
# if burrow_log["tx_id"] is None or burrow_log["tx_id"] == "":
# burrow_log["tx_id"] = get_tx_id(burrow_log["receipt_id"], Cfg.NETWORK_ID)
burrow_log["change"] = ""
if burrow_log["event"] == "borrow":
burrow_log["event"] = "Borrow"
if burrow_log["event"] == "decrease_collateral":
burrow_log["event"] = "Adjust Collateral"
burrow_log["change"] = "decrease"
if burrow_log["event"] == "increase_collateral":
burrow_log["event"] = "Adjust Collateral"
burrow_log["change"] = "increase"
if burrow_log["event"] == "deposit":
burrow_log["event"] = "Supply"
if burrow_log["event"] == "withdraw_succeeded":
burrow_log["event"] = "Withdraw"
res = {
"record_list": burrow_log_list,
"page_number": page_number,
"page_size": page_size,
"total_page": total_page,
"total_size": count_number,
}
return compress_response_content(res)
@app.route('/get-history-token-price-by-token', methods=['GET'])
def token_history_token_price_by_token():
ids = request.args.get("ids", "")
id_str_list = ids.split("|")
data_time = request.args.get("data_time")
ret = get_history_token_price_by_token(id_str_list, data_time)
return compress_response_content(ret)
@app.route('/get-dcl-pool-log', methods=['GET'])
def handle_dcl_pool_log():
start_block_id = request.args.get("start_block_id")
end_block_id = request.args.get("end_block_id")
logger.info("start_block_id:{}", start_block_id)
logger.info("end_block_id:{}", end_block_id)
if start_block_id is None or end_block_id is None:
return "[]"
ret = query_dcl_pool_log(Cfg.NETWORK_ID, start_block_id, end_block_id)
if ret is None:
return "[]"
dcl_pool_log_data = combine_dcl_pool_log(ret)
return compress_response_content(dcl_pool_log_data)
@app.route('/analysis-v2-pool-data', methods=['GET'])
def analysis_v2_pool_data():
file_name = request.args.get("file_name")
logger.info("pool file_name:{}", file_name)
analysis_v2_pool_data_to_s3(file_name, Cfg.NETWORK_ID)
return file_name
@app.route('/analysis-v2-pool-account-data', methods=['GET'])
def analysis_v2_pool_account_data():
file_name = request.args.get("file_name")
logger.info("account file_name:{}", file_name)
analysis_v2_pool_account_data_to_s3(file_name, Cfg.NETWORK_ID)
return file_name
@app.route('/get-recent-transaction-swap', methods=['GET'])
def handle_recent_transaction_swap():
pool_id = request.args.get("pool_id")
ret_data = []
try:
ret_data = query_recent_transaction_swap(Cfg.NETWORK_ID, pool_id)
# for ret in ret_data:
# if ret["tx_id"] is None:
# ret["tx_id"] = get_tx_id(ret["block_hash"], Cfg.NETWORK_ID)
except Exception as e:
print("Exception when swap: ", e)
return compress_response_content(ret_data)
@app.route('/get-recent-transaction-dcl-swap', methods=['GET'])
def handle_recent_transaction_dcl_swap():
pool_id = request.args.get("pool_id")
ret_data = []
try:
ret_data = query_recent_transaction_dcl_swap(Cfg.NETWORK_ID, pool_id)
# for ret in ret_data:
# if ret["tx_id"] is None:
# ret["tx_id"] = get_tx_id(ret["receipt_id"], Cfg.NETWORK_ID)
except Exception as e:
print("Exception when dcl-swap: ", e)
return compress_response_content(ret_data)
@app.route('/get-recent-transaction-liquidity', methods=['GET'])
def handle_recent_transaction_liquidity():
pool_id = request.args.get("pool_id")
ret = []
liquidity_data_list = query_recent_transaction_liquidity(Cfg.NETWORK_ID, pool_id)
if liquidity_data_list is None:
return ret
try:
for liquidity_data in liquidity_data_list:
amounts = str(liquidity_data["amounts"])
if "['0', '0']" == amounts:
amounts = str([liquidity_data["amount_in"], liquidity_data["amount_out"]])
ret_data = {
"method_name": liquidity_data["method_name"],
"pool_id": liquidity_data["pool_id"],
"shares": liquidity_data["shares"],
"timestamp": liquidity_data["timestamp"],
"tx_id": liquidity_data["tx_id"],
"amounts": amounts,
"receipt_id": liquidity_data["receipt_id"]
}
# if ret_data["tx_id"] is None:
# ret_data["tx_id"] = get_tx_id(liquidity_data["block_hash"], Cfg.NETWORK_ID)
ret.append(ret_data)
except Exception as e:
print("Exception when liquidity: ", e)
return compress_response_content(ret)
@app.route('/get-recent-transaction-dcl-liquidity', methods=['GET'])
def handle_recent_transaction_dcl_liquidity():
pool_id = request.args.get("pool_id")
ret_data = []
try:
ret_data = query_recent_transaction_dcl_liquidity(Cfg.NETWORK_ID, pool_id)
for ret_d in ret_data:
ret_d["amount_x"] = str(int(ret_d["amount_x"]))
ret_d["amount_y"] = str(int(ret_d["amount_y"]))
# if ret_d["tx_id"] is None:
# ret_d["tx_id"] = get_tx_id(ret_d["receipt_id"], Cfg.NETWORK_ID)
except Exception as e:
print("Exception when dcl-liquidity: ", e)
return compress_response_content(ret_data)
@app.route('/get-recent-transaction-limit-order', methods=['GET'])
def handle_recent_transaction_limit_order():
pool_id = request.args.get("pool_id")
ret_data = []
try:
ret_data = query_recent_transaction_limit_order(Cfg.NETWORK_ID, pool_id)
# for ret in ret_data:
# if ret["tx_id"] is None:
# ret["tx_id"] = get_tx_id(ret["receipt_id"], Cfg.NETWORK_ID)
except Exception as e:
print("Exception when limit-order: ", e)
return compress_response_content(ret_data)
@app.route('/get-dcl-points', methods=['GET'])
def handle_dcl_points():
pool_id = request.args.get("pool_id")
slot_number = request.args.get("slot_number", type=int, default=50)
start_point = request.args.get("start_point", type=int, default=-800000)
end_point = request.args.get("end_point", type=int, default=800000)
if pool_id is None:
return "null"
pool_id_s = pool_id.split("|")
token_x = pool_id_s[0]
token_y = pool_id_s[1]
token_list = [token_x, token_y]
token_price = list_token_price_by_id_list(Cfg.NETWORK_ID, token_list)
all_point_data, all_point_data_24h = query_dcl_points(Cfg.NETWORK_ID, pool_id)
point_data = handle_point_data(all_point_data, int(start_point), int(end_point))
point_data_24h = handle_point_data(all_point_data_24h, int(start_point), int(end_point))
ret_point_data = handle_dcl_point_bin(pool_id, point_data, int(slot_number), int(start_point), int(end_point),
point_data_24h, token_price)
ret_data = {}
top_bin_fee_data = handle_top_bin_fee(ret_point_data)
ret_data["point_data"] = ret_point_data
ret_data["top_bin_fee_data"] = top_bin_fee_data
return compress_response_content(ret_data)
@app.route('/get-fee-by-account', methods=['GET'])
def handle_fee_by_account():
pool_id = request.args.get("pool_id")
account_id = request.args.get("account_id")
ret_data = handel_account_fee(pool_id, account_id)
return compress_response_content(ret_data)
@app.route('/batch-get-fee-by-account', methods=['GET'])
def handle_batch_fee_by_account():
ret_data = {}
pool_ids = request.args.get("pool_ids")
account_id = request.args.get("account_id")
pool_id_list = pool_ids.split(",")
for pool_id in pool_id_list:
pool_fee = handel_account_fee(pool_id, account_id)
ret_data[pool_id] = pool_fee
return compress_response_content(ret_data)
def handel_account_fee(pool_id, account_id):
ret_data = {}
if pool_id is None or account_id is None:
return "null"
# unclaimed_fee_data = query_dcl_user_unclaimed_fee(Cfg.NETWORK_ID, pool_id, account_id)
claimed_fee_data = query_dcl_user_claimed_fee(Cfg.NETWORK_ID, pool_id, account_id)
fee_x = 0
fee_y = 0
# for unclaimed_fee in unclaimed_fee_data:
# if not unclaimed_fee["unclaimed_fee_x"] is None:
# fee_x = fee_x + int(unclaimed_fee["unclaimed_fee_x"])
# if not unclaimed_fee["unclaimed_fee_y"] is None:
# fee_y = fee_y + int(unclaimed_fee["unclaimed_fee_y"])
for claimed_fee in claimed_fee_data:
if not claimed_fee["claimed_fee_x"] is None:
fee_x = fee_x + int(claimed_fee["claimed_fee_x"])
if not claimed_fee["claimed_fee_y"] is None:
fee_y = fee_y + int(claimed_fee["claimed_fee_y"])
unclaimed_fee_data_24h = query_dcl_user_unclaimed_fee_24h(Cfg.NETWORK_ID, pool_id, account_id)
claimed_fee_data_24h = query_dcl_user_claimed_fee_24h(Cfg.NETWORK_ID, pool_id, account_id)
fee_x_24h = 0
fee_y_24h = 0
for unclaimed_fee_24h in unclaimed_fee_data_24h:
if not unclaimed_fee_24h["unclaimed_fee_x"] is None:
fee_x_24h = fee_x_24h + int(unclaimed_fee_24h["unclaimed_fee_x"])
if not unclaimed_fee_24h["unclaimed_fee_y"] is None:
fee_y_24h = fee_y_24h + int(unclaimed_fee_24h["unclaimed_fee_y"])
for claimed_fee_24h in claimed_fee_data_24h:
if not claimed_fee_24h["claimed_fee_x"] is None:
fee_x_24h = fee_x_24h + int(claimed_fee_24h["claimed_fee_x"])
if not claimed_fee_24h["claimed_fee_y"] is None:
fee_y_24h = fee_y_24h + int(claimed_fee_24h["claimed_fee_y"])
total_earned_fee = {
"total_fee_x": fee_x,
"total_fee_y": fee_y
}
total_fee_24h = {
"fee_x": fee_x - fee_x_24h,
"fee_y": fee_y - fee_y_24h,
}
# if total_fee_24h["fee_x"] < 0:
# total_fee_24h["fee_x"] = 0
# if total_fee_24h["fee_y"] < 0:
# total_fee_24h["fee_y"] = 0
user_tvl_data = query_dcl_user_tvl(Cfg.NETWORK_ID, pool_id, account_id)
token_x = 0
token_y = 0
user_token_timestamp = 0
for user_tvl in user_tvl_data:
if not user_tvl["timestamp"] is None:
user_token_timestamp = user_tvl["timestamp"]
if not user_tvl["tvl_x_l"] is None:
token_x = token_x + float(user_tvl["tvl_x_l"])
if not user_tvl["tvl_y_l"] is None:
token_y = token_y + float(user_tvl["tvl_y_l"])
user_token = {
"token_x": token_x,
"token_y": token_y,
"timestamp": user_token_timestamp,
}
change_log_data = query_dcl_user_change_log(Cfg.NETWORK_ID, pool_id, account_id, user_token_timestamp)
ret_change_log_data = []
for change_log in change_log_data:
change_log_timestamp = int(int(change_log["timestamp"]) / 1000000000)
if change_log_timestamp > user_token_timestamp:
continue
if change_log["token_x"] == "" or change_log["token_y"] == "":
continue
change_token_x = int(change_log["token_x"])
change_token_y = int(change_log["token_y"])
if change_log["event_method"] == "liquidity_removed":
change_token_x = 0 - int(change_log["token_x"])
change_token_y = 0 - int(change_log["token_y"])
if change_log["event_method"] == "liquidity_merge":
if change_log["remove_token_x"] == "" or change_log["merge_token_x"] == "" or change_log["remove_token_y"] == "" or change_log["merge_token_y"] == "":
continue
change_token_x = 0 - (int(change_log["remove_token_x"]) - int(change_log["merge_token_x"]))
change_token_y = 0 - (int(change_log["remove_token_y"]) - int(change_log["merge_token_y"]))
change_log = {
"event_method": change_log["event_method"],
"token_x": change_token_x,
"token_y": change_token_y,
"timestamp": change_log["timestamp"],
}
ret_change_log_data.append(change_log)
ret_data["total_earned_fee"] = total_earned_fee
ret_data["apr"] = {
"fee_data": total_fee_24h,
"user_token": user_token,
"change_log_data": ret_change_log_data
}
return ret_data
@app.route('/get-dcl-points-by-account', methods=['GET'])
def handle_dcl_points_by_account():
pool_id = request.args.get("pool_id")
slot_number = request.args.get("slot_number", type=int, default=50)
start_point = request.args.get("start_point", type=int, default=-800000)
end_point = request.args.get("end_point", type=int, default=800000)
account_id = request.args.get("account_id")
if pool_id is None or account_id is None:
return "null"
point_data = query_dcl_points_by_account(Cfg.NETWORK_ID, pool_id, account_id, int(start_point), int(end_point))
ret_point_data = handle_dcl_point_bin_by_account(pool_id, point_data, int(slot_number), account_id, int(start_point), int(end_point))
return compress_response_content(ret_point_data)
@app.route('/total_supply', methods=['GET'])
def handle_total_supple():
ret = "99990506.142591673655212239"
return ret
@app.route('/circulating_supply', methods=['GET'])
def handle_circulating_supply():
ret = get_circulating_supply()
return ret
@app.route('/crm/orderly/trading-data', methods=['GET', 'POST', 'PUT'])
def handle_crm_orderly_data():
try:
ret = {
"code": 0,
"msg": "success"
}
json_data = request.get_json()
broker_id = json_data["data"]["broker_id"]
if "health_check" == broker_id:
return ret
logger.info(f"orderly trading data:{json_data}")
signature = request.headers.get("signature")
logger.info("signature:{}", signature)
trading_data_info = json_data["data"]
trading_data_info["timestamp"] = json_data["timestamp"]
symbol_data = trading_data_info["symbol"].split("_")
trading_data_info["data_source"] = "orderly"
trading_data_info["trading_type"] = symbol_data[0]
trading_data_info["token_in"] = symbol_data[1]
trading_data_info["token_out"] = symbol_data[2]
add_orderly_trading_data(trading_data_info)
return ret
except Exception as e:
logger.error("handle orderly trading data error:{}", e)
@app.route('/history-token-price-report', methods=['GET'])
def history_token_price_report():
token = request.args.get("token")
base_token = request.args.get("base_token")
redis_key = token + "->" + base_token
ret = get_history_token_price_report(Cfg.NETWORK_ID, redis_key)
if ret is None:
return "null"
return compress_response_content(json.loads(ret))
@app.route('/add-liquidation-result', methods=['POST'])
def handel_add_liquidation_result():
ret = {
"code": 0,
"msg": "success",
"data": None
}
try:
liquidation_result_data_list = request.json
key = liquidation_result_data_list["key"]
values = json.dumps(liquidation_result_data_list["values"])
ret_data = get_liquidation_result(Cfg.NETWORK_ID, key)
if ret_data is None:
add_liquidation_result(Cfg.NETWORK_ID, key, values)
else:
update_liquidation_result(Cfg.NETWORK_ID, key, values)
except Exception as e:
print("handel_add_liquidation_result error:", e)
return ret
@app.route('/get-liquidation-result', methods=['GET'])
def handel_get_liquidation_result():
key = request.args.get("key")
ret_data = get_liquidation_result(Cfg.NETWORK_ID, key)
ret = {
"code": 0,
"msg": "success",
"data": ret_data
}
return ret
@app.route('/get_market_token_price', methods=['GET'])
def handle_market_token_price():
return get_market_token_price()
@app.route('/list-top-pools-v2', methods=['GET'])
def handle_list_top_pools_v2():
"""
list_top_pools
"""
ret_data = {"update_flag": False, "update_time": None}
pools = list_top_pools(Cfg.NETWORK_ID)
prices = list_token_price(Cfg.NETWORK_ID)
metadata = list_token_metadata(Cfg.NETWORK_ID)
combine_pools_info(pools, prices, metadata)
now_time = int(time.time())
min_time = now_time
ret_list_top_pools = []
for pool in pools:
amount_flag = False
price_flag = False
tokens = pool['token_account_ids']
amounts = pool['amounts']
for i in range(len(amounts)):
if int(amounts[i]) > 0:
amount_flag = True
if not amount_flag:
continue
for x in range(len(tokens)):
if tokens[x] in prices:
price_flag = True
if price_flag:
if float(pool["tvl"]) > 10:
ret_list_top_pools.append(pool)
if "update_time" in pool:
update_time = int(pool["update_time"])
if update_time < min_time:
min_time = update_time
else:
ret_list_top_pools.append(pool)
if "update_time" in pool:
update_time = int(pool["update_time"])
if update_time < min_time:
min_time = update_time
ret_data["pool_list"] = ret_list_top_pools
ret_data["timestamp"] = now_time
ret_data["update_time"] = min_time
ret_data["update_flag"] = now_time - min_time < 120
return compress_response_content(ret_data)
@app.route('/get-lp-lock-info', methods=['GET'])
def handel_lp_lock_info():
try:
ret_data_list = []
account_paged, pool_ids = get_lp_lock_info(Cfg.NETWORK_ID)
pools = list_pools_by_id_list(Cfg.NETWORK_ID, pool_ids)
pool_info = {}
for pool in pools:
pool_info[pool["id"]] = pool["shares_total_supply"]
for key, values in account_paged.items():
if key in pool_info:
locked_details = values["locked_details"]
for locked_detail in locked_details:
locked_detail["percent"] = '{:.12f}'.format((int(locked_detail["locked_balance"]) / int(pool_info[key])) * 100)
ret_data = {
"percent": '{:.12f}'.format((values["locked_balance"] / int(pool_info[key])) * 100),
"lock_amount": str(values["locked_balance"]),
"shares_total_supply": pool_info[key],
"pool_id": key,
"locked_details": locked_details
}
ret_data_list.append(ret_data)
ret = {
"code": 0,
"msg": "success",
"data": ret_data_list
}
except Exception as e:
logger.error("handel_lp_lock_info error:{}", e)
ret = {
"code": -1,
"msg": "error",
"data": e.args