-
Notifications
You must be signed in to change notification settings - Fork 12
/
db_provider.py
1516 lines (1331 loc) · 67.1 KB
/
db_provider.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
import decimal
import pymysql
import json
from datetime import datetime
from config import Cfg
import time
from redis_provider import RedisProvider, list_history_token_price, list_token_price, get_account_pool_assets, get_pool_point_24h_by_pool_id
from data_utils import add_redis_data
class Encoder(json.JSONEncoder):
"""
Handle special data types, such as decimal and time types
"""
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
if isinstance(o, datetime):
return o.strftime("%Y-%m-%d %H:%M:%S")
super(Encoder, self).default(o)
def get_db_connect(network_id: str):
conn = pymysql.connect(
host=Cfg.NETWORK[network_id]["DB_HOST"],
port=int(Cfg.NETWORK[network_id]["DB_PORT"]),
user=Cfg.NETWORK[network_id]["DB_UID"],
passwd=Cfg.NETWORK[network_id]["DB_PWD"],
db=Cfg.NETWORK[network_id]["DB_DSN"])
return conn
def get_near_lake_connect(network_id: str):
conn = pymysql.connect(
host=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_HOST"],
port=int(Cfg.NETWORK[network_id]["NEAR_LAKE_DB_PORT"]),
user=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_UID"],
passwd=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_PWD"],
db=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_DSN"])
return conn
def get_near_lake_dcl_connect(network_id: str):
conn = pymysql.connect(
host=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_HOST"],
port=int(Cfg.NETWORK[network_id]["NEAR_LAKE_DB_PORT"]),
user=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_UID"],
passwd=Cfg.NETWORK[network_id]["NEAR_LAKE_DB_PWD"],
db=Cfg.NETWORK[network_id]["NEAR_LAKE_DCL_DB_DSN"])
return conn
def get_crm_db_connect(network_id: str):
conn = pymysql.connect(
host=Cfg.NETWORK[network_id]["DB_HOST"],
port=int(Cfg.NETWORK[network_id]["DB_PORT"]),
user=Cfg.NETWORK[network_id]["CRM_DB_UID"],
passwd=Cfg.NETWORK[network_id]["CRM_DB_PWD"],
db="crm")
return conn
def get_burrow_connect(network_id: str):
conn = pymysql.connect(
host=Cfg.NETWORK[network_id]["DB_HOST"],
port=int(Cfg.NETWORK[network_id]["DB_PORT"]),
user=Cfg.NETWORK[network_id]["BURROW_DB_UID"],
passwd=Cfg.NETWORK[network_id]["BURROW_DB_PWD"],
db="burrow")
return conn
def get_liquidity_pools(network_id, account_id):
ret = []
db_conn = get_db_connect(network_id)
sql = "select DISTINCT(pool_id) as pool_id from near_lake_liquidity_pools where account_id = %s"
cursor = db_conn.cursor()
try:
cursor.execute(sql, account_id)
rows = cursor.fetchall()
return [row[0] for row in rows if row[0]]
except Exception as e:
print("query liquidity pools to db error:", e)
finally:
cursor.close()
return ret
def get_actions(network_id, account_id):
json_ret = []
db_conn = get_db_connect(network_id)
sql = "select `timestamp`,tx_id,receiver_account_id,method_name,args,deposit,`status`,receipt_id " \
"from near_lake_latest_actions where predecessor_account_id = %s order by `timestamp` desc limit 10"
cursor = db_conn.cursor()
try:
cursor.execute(sql, account_id)
rows = cursor.fetchall()
json_ret = json.dumps(rows, cls=DecimalEncoder)
except Exception as e:
print("query liquidity pools to db error:", e)
finally:
cursor.close()
return json_ret
def add_tx_receipt(data_list, network_id):
db_conn = get_near_lake_connect(network_id)
sql = "insert into t_tx_receipt(tx_id, receipt_id) values(%s,%s)"
insert_data = []
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
for data in data_list:
insert_data.append((data["tx_id"], data["receipt_id"]))
cursor.executemany(sql, insert_data)
db_conn.commit()
except Exception as e:
# Rollback on error
db_conn.rollback()
print("insert tx receipt log to db error:", e)
print("insert tx receipt log to db insert_data:", insert_data)
finally:
cursor.close()
def query_tx_by_receipt(receipt_id, network_id):
db_conn = get_near_lake_connect(network_id)
sql = "SELECT tx_id FROM t_tx_receipt WHERE receipt_id = %s"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, receipt_id)
tx_data = cursor.fetchone()
if tx_data is None:
return ""
else:
tx_id = tx_data["tx_id"]
return tx_id
except Exception as e:
print("query tx to db error:", e)
finally:
cursor.close()
def get_history_token_price(id_list: list) -> list:
"""
Batch query historical price
"""
"""because 'usn' Special treatment require,'use 'dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near'
Price of, Record whether the input parameter is passed in 'usn',If there is an incoming 'usn', But no incoming
'dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near', Then the return parameter only needs to be
returned 'usn', Do not return 'dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near', If both have
incoming,It is necessary to return the price information of two at the same time,usn_flag 1 means no incoming 'usn'
2 means that it is passed in at the same time 'usn和dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near'
,3 means that only 'usn',No incoming 'dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near' """
# usn_flag = 1
# Special treatment of USN to determine whether USN is included in the input parameter
# if "usn" in id_list:
# if "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near" in id_list:
# usn_flag = 2
# else:
# usn_flag = 3
# id_list = ['dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near' if i == 'usn' else i for i in
# id_list]
# usdt_flag = 1
# # Special treatment of USN to determine whether USN is included in the input parameter
# if "usdt.tether-token.near" in id_list:
# if "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near" in id_list:
# usdt_flag = 2
# else:
# usdt_flag = 3
# id_list = ['dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near' if i == 'usdt.tether-token.near'
# else i for i in id_list]
#
ret = []
history_token_prices = list_history_token_price(Cfg.NETWORK_ID, id_list)
for token_price in history_token_prices:
if not token_price is None:
float_ratio = format_percentage(float(token_price['price']), float(token_price['history_price']))
# if "dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near" in token_price['contract_address']:
# if 2 == usn_flag:
# new_usn = {
# "price": token_price['price'],
# "history_price": token_price['history_price'],
# "decimal": 18,
# "symbol": "USN",
# "float_ratio": float_ratio,
# "timestamp": token_price['datetime'],
# "contract_address": "usn"
# }
# ret.append(new_usn)
# elif 3 == usn_flag:
# token_price['contract_address'] = "usn"
# token_price['symbol'] = "USN"
# token_price['decimal'] = 18
# if 2 == usdt_flag:
# new_usdt = {
# "price": token_price['price'],
# "history_price": token_price['history_price'],
# "decimal": 6,
# "symbol": "USDt",
# "float_ratio": float_ratio,
# "timestamp": token_price['datetime'],
# "contract_address": "usdt.tether-token.near"
# }
# ret.append(new_usdt)
# elif 3 == usdt_flag:
# token_price['contract_address'] = "usdt.tether-token.near"
# token_price['symbol'] = "USDt"
# token_price['decimal'] = 6
token_price['float_ratio'] = float_ratio
ret.append(token_price)
return ret
def add_history_token_price(contract_address, symbol, price, decimals, network_id):
"""
Write the token price to the MySQL database
"""
for token in Cfg.TOKENS[Cfg.NETWORK_ID]:
if token["NEAR_ID"] in contract_address:
symbol = token["SYMBOL"]
# Get current timestamp
now = int(time.time())
before_time = now - (1 * 24 * 60 * 60)
db_conn = get_db_connect(Cfg.NETWORK_ID)
sql = "insert into mk_history_token_price(contract_address, symbol, price, `decimal`, create_time, update_time, " \
"`status`, `timestamp`) values(%s,%s,%s,%s, now(), now(), 1, %s) "
par = (contract_address, symbol, price, decimals, now)
# Query the price records 24 hours ago according to the token
sql2 = "SELECT price FROM mk_history_token_price where contract_address = %s and `timestamp` < " \
"%s order by from_unixtime(`timestamp`, '%%Y-%%m-%%d %%H:%%i:%%s') desc limit 1"
par2 = (contract_address, before_time)
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, par)
# Submit to database for execution
db_conn.commit()
cursor.execute(sql2, par2)
old_rows = cursor.fetchone()
old_price = price
if old_rows is not None:
old_price = old_rows["price"]
history_token = {
"price": price,
"history_price": old_price,
"symbol": symbol,
"datetime": now,
"contract_address": contract_address,
"decimal": decimals
}
redis_conn = RedisProvider()
redis_conn.begin_pipe()
redis_conn.add_history_token_price(network_id, contract_address, json.dumps(history_token, cls=Encoder))
redis_conn.end_pipe()
redis_conn.close()
except Exception as e:
# Rollback on error
db_conn.rollback()
print(e)
finally:
cursor.close()
def batch_add_history_token_price(data_list, network_id):
now = int(time.time())
before_time = now - (1 * 24 * 60 * 60)
new_token_price = {}
old_token_price = {}
db_conn = get_db_connect(network_id)
sql = "insert into mk_history_token_price(contract_address, symbol, price, `decimal`, create_time, update_time, " \
"`status`, `timestamp`) values(%s,%s,%s,%s, now(), now(), 1, %s)"
sql2 = "SELECT contract_address, price FROM mk_history_token_price where contract_address in (%s) " \
"and `timestamp` > '%s' group by contract_address"
insert_data = []
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
for data in data_list:
insert_data.append((data["contract_address"], data["symbol"], data["price"], data["decimal"], now))
new_token_price[data["contract_address"]] = {"symbol": data["symbol"], "price": data["price"], "decimal": data["decimal"]}
cursor.executemany(sql, insert_data)
db_conn.commit()
end_time = int(time.time())
print("insert to db time:", end_time - now)
contract_address_ids = ""
par2 = (contract_address_ids, before_time)
cursor.execute(sql2, par2)
old_rows = cursor.fetchall()
end_time1 = int(time.time())
print("query old price time:", end_time1 - end_time)
for old_row in old_rows:
old_token_price[old_row["contract_address"]] = old_row["price"]
handle_history_token_price_to_redis(now, new_token_price, old_token_price, network_id)
end_time2 = int(time.time())
print("add price to redis time:", end_time2 - end_time1)
except Exception as e:
db_conn.rollback()
print("insert mk_history_token_price to db error:", e)
finally:
cursor.close()
db_conn.close()
def handle_history_token_price_to_redis(now, new_token_price, old_token_price, network_id):
redis_conn = RedisProvider()
redis_conn.begin_pipe()
for token, token_data in new_token_price.items():
price = token_data["price"]
symbol = token_data["symbol"]
decimals = token_data["decimal"]
old_price = price
if token in old_token_price and old_token_price[token] is not None:
old_price = old_token_price[token]
history_token = {
"price": price,
"history_price": old_price,
"symbol": symbol,
"datetime": now,
"contract_address": token,
"decimal": decimals
}
redis_conn.add_history_token_price(network_id, token, json.dumps(history_token, cls=Encoder))
redis_conn.end_pipe()
redis_conn.close()
def format_percentage(new, old):
p = 100 * (new - old) / old
return '%.2f' % p
def clear_token_price():
now = int(time.time())
before_time = now - (7*24*60*60)
print("seven days ago time:", before_time)
conn = get_db_connect(Cfg.NETWORK_ID)
sql = "delete from mk_history_token_price where `timestamp` < %s"
cursor = conn.cursor()
try:
cursor.execute(sql, before_time)
# Submit to database for execution
conn.commit()
except Exception as e:
# Rollback on error
conn.rollback()
print(e)
finally:
cursor.close()
def handle_dcl_pools(data_list, network_id):
old_pools_data = query_dcl_pools(network_id)
for old_pool in old_pools_data:
for pool in data_list:
if old_pool["pool_id"] == pool["pool_id"]:
# print("old_pool:", old_pool["volume_x_in"])
# print("pool:", pool["volume_x_in"])
pool["volume_x_in_grow"] = str(int(pool["volume_x_in"]) - int(old_pool["volume_x_in"]))
pool["volume_y_in_grow"] = str(int(pool["volume_y_in"]) - int(old_pool["volume_y_in"]))
pool["volume_x_out_grow"] = str(int(pool["volume_x_out"]) - int(old_pool["volume_x_out"]))
pool["volume_y_out_grow"] = str(int(pool["volume_y_out"]) - int(old_pool["volume_y_out"]))
pool["total_order_x_grow"] = str(int(pool["total_order_x"]) - int(old_pool["total_order_x"]))
pool["total_order_y_grow"] = str(int(pool["total_order_y"]) - int(old_pool["total_order_y"]))
add_dcl_pools_to_db(data_list, network_id)
def query_dcl_pools(network_id):
db_conn = get_db_connect(network_id)
db_table = "t_dcl_pools_data"
if network_id == "MAINNET":
db_table = "t_dcl_pools_data_mainnet"
sql = "SELECT id, pool_id, volume_x_in, volume_y_in, volume_x_out, volume_y_out, total_order_x, total_order_y " \
"FROM " + db_table + " WHERE id IN ( SELECT max(id) FROM " + db_table + " GROUP BY pool_id )"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql)
old_pools_data = cursor.fetchall()
return old_pools_data
except Exception as e:
# Rollback on error
db_conn.rollback()
print("query dcl_pools to db error:", e)
finally:
cursor.close()
def add_dcl_pools_to_db(data_list, network_id):
now = int(time.time())
zero_point = int(time.time()) - int(time.time() - time.timezone) % 86400
time_array = time.localtime(zero_point)
check_point = time.strftime("%Y-%m-%d", time_array)
db_conn = get_db_connect(network_id)
db_table = "t_dcl_pools_data"
if network_id == "MAINNET":
db_table = "t_dcl_pools_data_mainnet"
sql = "insert into " + db_table + "(pool_id, token_x, token_y, volume_x_in, volume_y_in, volume_x_out, " \
"volume_y_out, total_order_x, total_order_y, total_x, total_y, total_fee_x_charged, total_fee_y_charged, " \
"volume_x_in_grow, volume_y_in_grow, volume_x_out_grow, volume_y_out_grow, total_order_x_grow, " \
"total_order_y_grow, token_x_price, token_y_price, token_x_decimal, token_y_decimal, timestamp, create_time) " \
"values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())"
insert_data = []
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
token_price = get_dcl_token_price(network_id)
for data in data_list:
token_x_price = 0
token_y_price = 0
token_x_decimal = 0
token_y_decimal = 0
if data["token_x"] in token_price:
token_x_price = token_price[data["token_x"]]["price"]
token_x_decimal = token_price[data["token_x"]]["decimal"]
if data["token_y"] in token_price:
token_y_price = token_price[data["token_y"]]["price"]
token_y_decimal = token_price[data["token_y"]]["decimal"]
insert_data.append((data["pool_id"], data["token_x"], data["token_y"], data["volume_x_in"],
data["volume_y_in"], data["volume_x_out"], data["volume_y_out"], data["total_order_x"],
data["total_order_y"], data["total_x"], data["total_y"], data["total_fee_x_charged"],
data["total_fee_y_charged"], data["volume_x_in_grow"],
data["volume_y_in_grow"], data["volume_x_out_grow"], data["volume_y_out_grow"],
data["total_order_x_grow"], data["total_order_y_grow"], token_x_price,
token_y_price, token_x_decimal, token_y_decimal, now))
pool_id = data["pool_id"] + "_" + check_point
order_x_price = (int(data["total_x"]) - int(data["total_fee_x_charged"])) / int("1" + "0" * int(token_x_decimal)) * float(token_x_price)
order_y_price = (int(data["total_y"]) - int(data["total_fee_y_charged"])) / int("1" + "0" * int(token_y_decimal)) * float(token_y_price)
tvl = order_x_price + order_y_price
pool_tvl_data = {
"pool_id": data["pool_id"],
"dateString": check_point,
"tvl": str(tvl)
}
add_dcl_pools_tvl_to_redis(network_id, pool_id, pool_tvl_data)
cursor.executemany(sql, insert_data)
db_conn.commit()
handle_dcl_pools_to_redis_data(network_id, zero_point)
except Exception as e:
# Rollback on error
db_conn.rollback()
print("insert dcl_pools to db error:", e)
print("insert dcl_pools to db insert_data:", insert_data)
finally:
cursor.close()
def handle_dcl_pools_to_redis_data(network_id, zero_point):
now = int(time.time())
before_time = now - (1 * 24 * 60 * 60)
db_conn = get_db_connect(network_id)
db_table = "t_dcl_pools_data"
if network_id == "MAINNET":
db_table = "t_dcl_pools_data_mainnet"
sql = "SELECT pool_id, SUM(volume_x_in_grow) AS volume_x_in_grow, SUM(volume_y_in_grow) AS volume_y_in_grow, " \
"SUM(volume_x_out_grow) AS volume_x_out_grow , SUM(volume_y_out_grow) AS volume_y_out_grow, " \
"SUM(total_order_x_grow) AS total_order_x_grow, SUM(total_order_y_grow) AS total_order_y_grow, " \
"token_x_price, token_y_price, token_x_decimal, token_y_decimal " \
"FROM " + db_table + " WHERE `timestamp` >= %s GROUP BY pool_id"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, before_time)
twenty_four_hour_pools_data = cursor.fetchall()
add_24h_dcl_pools_to_redis(network_id, twenty_four_hour_pools_data)
cursor.execute(sql, zero_point)
zero_point_pools_data = cursor.fetchall()
add_list_dcl_pools_to_redis(network_id, zero_point_pools_data, zero_point)
except Exception as e:
print("query dcl_pools to db error:", e)
finally:
cursor.close()
def add_24h_dcl_pools_to_redis(network_id, dcl_pools_data):
try:
redis_conn = RedisProvider()
for pool_data in dcl_pools_data:
token_x_in_price = int(pool_data["volume_x_in_grow"]) / int("1" + "0" * int(pool_data["token_x_decimal"])) * float(pool_data["token_x_price"])
token_x_out_price = int(pool_data["volume_x_out_grow"]) / int("1" + "0" * int(pool_data["token_x_decimal"])) * float(pool_data["token_x_price"])
token_y_in_price = int(pool_data["volume_y_in_grow"]) / int("1" + "0" * int(pool_data["token_y_decimal"])) * float(pool_data["token_y_price"])
token_y_out_price = int(pool_data["volume_y_out_grow"]) / int("1" + "0" * int(pool_data["token_y_decimal"])) * float(pool_data["token_y_price"])
volume_x = token_x_in_price + token_x_out_price
volume_y = token_y_in_price + token_y_out_price
if volume_x > volume_y:
volume = volume_x
else:
volume = volume_y
redis_conn.begin_pipe()
redis_conn.add_twenty_four_hour_pools_data(network_id, pool_data["pool_id"], str(volume))
add_redis_data(network_id, Cfg.NETWORK[network_id]["REDIS_DCL_POOLS_VOLUME_24H_KEY"], pool_data["pool_id"], str(volume))
redis_conn.end_pipe()
redis_conn.close()
except Exception as e:
print("add dcl_pools to redis error:", e)
finally:
redis_conn.close()
def add_list_dcl_pools_to_redis(network_id, dcl_pools_data, zero_point):
try:
time_array = time.localtime(zero_point)
check_point = time.strftime("%Y-%m-%d", time_array)
redis_conn = RedisProvider()
for pool_data in dcl_pools_data:
pool_id = pool_data["pool_id"] + "_" + check_point
token_x_in_price = int(pool_data["volume_x_in_grow"]) / int("1" + "0" * int(pool_data["token_x_decimal"])) * float(pool_data["token_x_price"])
token_x_out_price = int(pool_data["volume_x_out_grow"]) / int("1" + "0" * int(pool_data["token_x_decimal"])) * float(pool_data["token_x_price"])
token_y_in_price = int(pool_data["volume_y_in_grow"]) / int("1" + "0" * int(pool_data["token_y_decimal"])) * float(pool_data["token_y_price"])
token_y_out_price = int(pool_data["volume_y_out_grow"]) / int("1" + "0" * int(pool_data["token_y_decimal"])) * float(pool_data["token_y_price"])
volume_x = token_x_in_price + token_x_out_price
volume_y = token_y_in_price + token_y_out_price
if volume_x > volume_y:
volume = volume_x
else:
volume = volume_y
pool_volume_data = {
"pool_id": pool_data["pool_id"],
"dateString": check_point,
"volume": str(volume)
}
redis_conn.begin_pipe()
redis_conn.add_dcl_pools_data(network_id, pool_id, json.dumps(pool_volume_data, cls=Encoder), pool_data["pool_id"])
add_redis_data(network_id, Cfg.NETWORK[network_id]["REDIS_DCL_POOLS_VOLUME_LIST_KEY"] + "_" + pool_data["pool_id"], pool_id, json.dumps(pool_volume_data))
redis_conn.end_pipe()
redis_conn.close()
except Exception as e:
print("add dcl_pools to redis error:", e)
finally:
redis_conn.close()
def add_dcl_pools_tvl_to_redis(network_id, pool_id, pool_tvl_data):
redis_conn = RedisProvider()
try:
redis_conn.begin_pipe()
redis_conn.add_dcl_pools_tvl_data(network_id, pool_tvl_data["pool_id"], pool_id, json.dumps(pool_tvl_data, cls=Encoder))
add_redis_data(network_id, Cfg.NETWORK[network_id]["REDIS_DCL_POOLS_TVL_LIST_KEY"] + "_" + pool_tvl_data["pool_id"], pool_id, json.dumps(pool_tvl_data, cls=Encoder))
redis_conn.end_pipe()
redis_conn.close()
except Exception as e:
print("add dcl_pools to redis error:", e)
finally:
redis_conn.close()
def get_dcl_token_price(network_id):
token_price_list = {}
prices = list_token_price(network_id)
for token in Cfg.TOKENS[Cfg.NETWORK_ID]:
if token["NEAR_ID"] in prices:
token_price_list[token["NEAR_ID"]] = {
"price": prices[token["NEAR_ID"]],
"decimal": token["DECIMAL"],
"symbol": token["SYMBOL"],
}
return token_price_list
def query_limit_order_log(network_id, owner_id):
db_conn = get_db_connect(network_id)
sql = "select order_id, tx_id, receipt_id from near_lake_limit_order where type = 'order_added' and owner_id = %s"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, owner_id)
limit_order_data = cursor.fetchall()
return limit_order_data
except Exception as e:
# Rollback on error
db_conn.rollback()
print("query limit_order_log to db error:", e)
finally:
cursor.close()
def query_limit_order_swap(network_id, owner_id):
db_conn = get_db_connect(network_id)
sql = "select tx_id, token_in,token_out,pool_id,point,amount_in,amount_out,timestamp, receipt_id from " \
"near_lake_limit_order where type = 'swap' and owner_id = %s"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, owner_id)
limit_order_data = cursor.fetchall()
return limit_order_data
except Exception as e:
# Rollback on error
db_conn.rollback()
print("query limit_order_log to db error:", e)
finally:
cursor.close()
def add_account_assets_data(data_list):
now_time = int(time.time())
db_conn = get_db_connect(Cfg.NETWORK_ID)
sql = "insert into t_account_assets_data(type, pool_id, farm_id, account_id, tokens, token_amounts, " \
"token_decimals, token_prices, amount, `timestamp`, create_time) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,now())"
insert_data = []
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
for data in data_list:
insert_data.append((data["type"], data["pool_id"], data["farm_id"], data["account_id"], data["tokens"],
data["token_amounts"], data["token_decimals"], data["token_prices"],
data["amount"], now_time))
cursor.executemany(sql, insert_data)
db_conn.commit()
except Exception as e:
print("insert account assets assets log to db error:", e)
print("insert account assets assets log to db insert_data:", insert_data)
finally:
cursor.close()
def get_token_price():
tokens = {}
db_conn = get_db_connect(Cfg.NETWORK_ID)
sql = "select contract_address,price,`decimal` from mk_history_token_price where id in " \
"(select max(id) from mk_history_token_price group by contract_address)"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
contract_address = row["contract_address"]
token_data = {
"contract_address": contract_address,
"price": row["price"],
"decimal": row["decimal"]
}
tokens[contract_address] = token_data
return tokens
except Exception as e:
# Rollback on error
print(e)
finally:
cursor.close()
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
super(DecimalEncoder, self).default(o)
def handle_account_pool_assets_data(network_id):
now_time = int(time.time()) - 60 * 60
db_conn = get_db_connect(Cfg.NETWORK_ID)
sql = "select account_id,sum(amount) as amount from t_account_assets_data where `status` = '1' group by account_id"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
handle_account_pool_assets_h_data(network_id, now_time, row)
handle_account_pool_assets_w_data(network_id, now_time, row)
handle_account_pool_assets_m_data(network_id, now_time, row)
handle_account_pool_assets_all_data(network_id, now_time, row)
update_account_pool_assets_status()
except Exception as e:
print(e)
finally:
cursor.close()
def handle_account_pool_assets_h_data(network_id, now_time, row):
redis_key = row["account_id"] + "_h"
amount = row["amount"]
ret_pool_assets = get_account_pool_assets(network_id, redis_key)
time_array = time.localtime(now_time)
now_date_time_h = time.strftime("%Y-%m-%d %H", time_array)
pool_assets = []
pool_asset_data = {
"date_itme": now_date_time_h,
"assets": amount
}
if ret_pool_assets is not None:
pool_assets = json.loads(ret_pool_assets)
if len(pool_assets) >= 24:
pool_assets.pop(0)
pool_assets.append(pool_asset_data)
add_account_pool_assets_to_redis(network_id, redis_key, json.dumps(pool_assets, cls=DecimalEncoder, ensure_ascii=False))
def handle_account_pool_assets_w_data(network_id, now_time, row):
redis_key = row["account_id"] + "_w"
amount = row["amount"]
ret_pool_assets = get_account_pool_assets(network_id, redis_key)
time_array = time.localtime(now_time)
now_date_time_w = time.strftime("%Y-%m-%d %H", time_array)
pool_assets = []
pool_asset_data = {
"date_itme": now_date_time_w,
"assets": amount
}
if ret_pool_assets is not None:
pool_assets = json.loads(ret_pool_assets)
if len(pool_assets) >= 168:
pool_assets.pop(0)
pool_assets.append(pool_asset_data)
add_account_pool_assets_to_redis(network_id, redis_key, json.dumps(pool_assets, cls=DecimalEncoder, ensure_ascii=False))
def handle_account_pool_assets_m_data(network_id, now_time, row):
redis_key = row["account_id"] + "_m"
amount = row["amount"]
ret_pool_assets = get_account_pool_assets(network_id, redis_key)
time_array = time.localtime(now_time)
now_date_time_m = time.strftime("%Y-%m-%d", time_array)
pool_assets = []
pool_asset_data = {
"date_itme": now_date_time_m,
"assets": amount
}
data_flag = True
if ret_pool_assets is not None:
pool_assets = json.loads(ret_pool_assets)
for asset in pool_assets:
if now_date_time_m == asset["date_itme"]:
data_flag = False
asset["assets"] = amount
if len(pool_assets) >= 30 and data_flag:
pool_assets.pop(0)
if data_flag:
pool_assets.append(pool_asset_data)
set_lst = set()
new_pool_assets = []
for pool in pool_assets:
if pool["date_itme"] not in set_lst:
set_lst.add(pool["date_itme"])
new_pool_assets.append(pool)
add_account_pool_assets_to_redis(network_id, redis_key, json.dumps(new_pool_assets, cls=DecimalEncoder, ensure_ascii=False))
def handle_account_pool_assets_all_data(network_id, now_time, row):
redis_key = row["account_id"] + "_all"
amount = row["amount"]
ret_pool_assets = get_account_pool_assets(network_id, redis_key)
time_array = time.localtime(now_time)
now_date_time_all = time.strftime("%Y-%m-%d", time_array)
pool_assets = []
pool_asset_data = {
"date_itme": now_date_time_all,
"assets": amount
}
data_flag = True
if ret_pool_assets is not None:
pool_assets = json.loads(ret_pool_assets)
for asset in pool_assets:
if now_date_time_all == asset["date_itme"]:
data_flag = False
asset["assets"] = amount
if data_flag:
pool_assets.append(pool_asset_data)
add_account_pool_assets_to_redis(network_id, redis_key, json.dumps(pool_assets, cls=DecimalEncoder, ensure_ascii=False))
def add_account_pool_assets_to_redis(network_id, key, values):
redis_conn = RedisProvider()
redis_conn.begin_pipe()
redis_conn.add_account_pool_assets(network_id, key, values)
add_redis_data(network_id, Cfg.NETWORK[network_id]["REDIS_ACCOUNT_POOL_ASSETS_KEY"], key, values)
redis_conn.end_pipe()
redis_conn.close()
def update_account_pool_assets_status():
db_conn = get_db_connect(Cfg.NETWORK_ID)
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
sql = "update t_account_assets_data set `status` = '2' where `status` = '1'"
cursor.execute(sql)
# Submit to database for execution
db_conn.commit()
except Exception as e:
# Rollback on error
db_conn.rollback()
print(e)
finally:
cursor.close()
def query_burrow_log(network_id, account_id, page_number, page_size):
start_number = handel_page_number(page_number, page_size)
db_conn = get_db_connect(network_id)
sql = "select `event`, amount, token_id, `timestamp`, '' as tx_id, receipt_id from burrow_event_log " \
"where account_id = %s and `event` in ('borrow','decrease_collateral','deposit'," \
"'increase_collateral','repay','withdraw_succeeded') order by `timestamp` desc " \
"limit %s, %s"
sql_count = "select count(*) as total_number from burrow_event_log where account_id = %s and `event` in " \
"('borrow','decrease_collateral','deposit','increase_collateral','repay','withdraw_succeeded')"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, (account_id, start_number, page_size))
burrow_log = cursor.fetchall()
cursor.execute(sql_count, account_id)
burrow_log_count = cursor.fetchone()
return burrow_log, burrow_log_count["total_number"]
except Exception as e:
print("query burrow_event_log to db error:", e)
finally:
cursor.close()
def handel_page_number(page_number, size):
if page_number <= 1:
start_number = 0
else:
start_number = (page_number - 1) * size
return start_number
def get_history_token_price_by_token(ids, data_time):
db_conn = get_db_connect(Cfg.NETWORK_ID)
token_data_list = {}
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
for token_id in ids:
sql = "select symbol, contract_address,price,`decimal`, `timestamp` from mk_history_token_price where " \
"contract_address = %s and `timestamp` >= %s limit 1"
cursor.execute(sql, (token_id, data_time))
ret = cursor.fetchone()
token_data = {
"symbol": ret["symbol"],
"contract_address": ret["contract_address"],
"price": float(ret["price"]),
"decimal": ret["decimal"],
"timestamp": ret["timestamp"]
}
token_data_list[ret["contract_address"]] = token_data
return token_data_list
except Exception as e:
# Rollback on error
print(e)
finally:
cursor.close()
def query_dcl_pool_log(network_id, start_block_id, end_block_id):
db_conn = get_db_connect(network_id)
sql = "select * from (select tla.event_method, tla.pool_id, '' as order_id, tla.lpt_id, '' as swapper, " \
"'' as token_in, '' as token_out, '' as amount_in, '' as amount_out, '' as created_at, '' as cancel_at, " \
"'' as completed_at, tla.owner_id, '' as `point`, '' as sell_token, '' as buy_token, " \
"'' as request_cancel_amount, '' as actual_cancel_amount, '' as original_amount, '' as cancel_amount, " \
"'' as remain_amount, '' as bought_amount, '' as original_deposit_amount, '' as swap_earn_amount, " \
"tla.left_point, tla.right_point, tla.added_amount, '' as removed_amount, tla.cur_amount, " \
"tla.paid_token_x, tla.paid_token_y, '' as refund_token_x, '' as refund_token_y, tla.tx_id, " \
"tla.block_id, tla.`timestamp`, tla.args,tla.predecessor_id,tla.receiver_id, tla.create_time from " \
"t_liquidity_added tla union all select 'liquidity_removed' as event_method, tlr.pool_id, '' as order_id, " \
"tlr.lpt_id, '' as swapper, '' as token_in, '' as token_out, '' as amount_in, '' as amount_out, " \
"'' as created_at, '' as cancel_at, '' as completed_at, tlr.owner_id, '' as `point`, '' as sell_token, " \
"'' as buy_token, '' as request_cancel_amount, '' as actual_cancel_amount, '' as original_amount, " \
"'' as cancel_amount, '' as remain_amount, '' as bought_amount, '' as original_deposit_amount, " \
"'' as swap_earn_amount, tlr.left_point, tlr.right_point, '' as added_amount, tlr.removed_amount, " \
"tlr.cur_amount, '' as paid_token_x, '' as paid_token_y, tlr.refund_token_x, tlr.refund_token_y, " \
"tlr.tx_id, tlr.block_id, tlr.`timestamp`, tlr.args,tlr.predecessor_id,tlr.receiver_id, create_time " \
"from t_liquidity_removed tlr union all select 'order_added' as event_method, toa.pool_id, toa.order_id, " \
"'' as lpt_id, '' as swapper, '' as token_in, '' as token_out, '' as amount_in, '' as amount_out, " \
"toa.created_at, '' as cancel_at, '' as completed_at, toa.owner_id, toa.`point`, toa.sell_token, " \
"toa.buy_token, '' as request_cancel_amount, '' as actual_cancel_amount, toa.original_amount, " \
"'' as cancel_amount, '' as remain_amount, '' as bought_amount, toa.original_deposit_amount, " \
"toa.swap_earn_amount, '' as left_point, '' as right_point, '' as added_amount, '' as removed_amount, " \
"'' as cur_amount, '' as paid_token_x, '' as paid_token_y, '' as refund_token_x, '' as refund_token_y, " \
"toa.tx_id, toa.block_id, toa.`timestamp`, toa.args,toa.predecessor_id,toa.receiver_id, create_time from " \
"t_order_added toa union all select 'order_cancelled' as event_method, toc.pool_id, toc.order_id, " \
"'' as lpt_id, '' as swapper, '' as token_in, '' as token_out, '' as amount_in, '' as amount_out, " \
"toc.created_at, toc.cancel_at, '' as completed_at, toc.owner_id, toc.`point`, toc.sell_token, " \
"toc.buy_token, toc.request_cancel_amount, toc.actual_cancel_amount, toc.original_amount, " \
"toc.cancel_amount, toc.remain_amount, toc.bought_amount, '' as original_deposit_amount, " \
"'' as swap_earn_amount, '' as left_point, '' as right_point, '' as added_amount, '' as removed_amount, " \
"'' as cur_amount, '' as paid_token_x, '' as paid_token_y, '' as refund_token_x, '' as refund_token_y, " \
"toc.tx_id, toc.block_id, toc.`timestamp`, toc.args,toc.predecessor_id,toc.receiver_id, create_time " \
"from t_order_cancelled toc union all select 'order_completed' as event_method, tocd.pool_id, " \
"tocd.order_id, '' as lpt_id, '' as swapper, '' as token_in, '' as token_out, '' as amount_in, " \
"'' as amount_out, tocd.created_at, '' as cancel_at, tocd.completed_at, tocd.owner_id, tocd.`point`, " \
"tocd.sell_token, tocd.buy_token, '' as request_cancel_amount, '' as actual_cancel_amount, " \
"tocd.original_amount, tocd.cancel_amount, '' as remain_amount, tocd.bought_amount, " \
"tocd.original_deposit_amount, tocd.swap_earn_amount, '' as left_point, '' as right_point, " \
"'' as added_amount, '' as removed_amount, '' as cur_amount, '' as paid_token_x, '' as paid_token_y, " \
"'' as refund_token_x, '' as refund_token_y, tocd.tx_id, tocd.block_id, tocd.`timestamp`, " \
"tocd.args,tocd.predecessor_id,tocd.receiver_id, create_time from t_order_completed tocd union all " \
"select 'swap' as event_method, '' as pool_id, '' as order_id, '' as lpt_id, ts.swapper, " \
"ts.token_in, ts.token_out, ts.amount_in, ts.amount_out, '' as created_at, '' as cancel_at, " \
"'' as completed_at, ts.swapper as owner_id, '' as `point`, '' as sell_token, '' as buy_token, " \
"'' as request_cancel_amount, '' as actual_cancel_amount, '' as original_amount, '' as cancel_amount, " \
"'' as remain_amount, '' as bought_amount, '' as original_deposit_amount, '' as swap_earn_amount, " \
"'' as left_point, '' as right_point, '' as added_amount, '' as removed_amount, '' as cur_amount, " \
"'' as paid_token_x, '' as paid_token_y, '' as refund_token_x, '' as refund_token_y, ts.tx_id, " \
"ts.block_id, ts.`timestamp`, ts.args,ts.predecessor_id,ts.receiver_id, create_time from t_swap ts) as " \
"all_data where block_id >= %s and block_id <= %s order by `timestamp`"
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
cursor.execute(sql, (start_block_id, end_block_id))
dcl_pool_log_data = cursor.fetchall()
return dcl_pool_log_data
except Exception as e:
# Rollback on error
db_conn.rollback()
print("query query_dcl_pool_log to db error:", e)
finally:
cursor.close()
def add_v2_pool_data(data_list, network_id, pool_id_list):
db_conn = get_db_connect(network_id)
sql = "insert into dcl_pool_analysis(pool_id, point, fee_x, fee_y, l, tvl_x_l, " \
"tvl_x_o, tvl_y_l, tvl_y_o, vol_x_in_l, vol_x_in_o, vol_x_out_l, vol_x_out_o, " \
"vol_y_in_l, vol_y_in_o, vol_y_out_l, vol_y_out_o, p_fee_x, p_fee_y, p, cp, timestamp, create_time) " \
"values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, now())"
insert_data = []
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
for data in data_list:
insert_data.append((data["pool_id"], data["point"], data["fee_x"], data["fee_y"], data["l"],
data["tvl_x_l"], data["tvl_x_o"], data["tvl_y_l"],
data["tvl_y_o"], data["vol_x_in_l"], data["vol_x_in_o"],
data["vol_x_out_l"], data["vol_x_out_o"], data["vol_y_in_l"], data["vol_y_in_o"],
data["vol_y_out_l"], data["vol_y_out_o"], data["p_fee_x"],
data["p_fee_y"], data["p"], data["cp"], data["timestamp"]))
cursor.executemany(sql, insert_data)
db_conn.commit()
except Exception as e:
# Rollback on error
db_conn.rollback()
print("insert v2 pool data to db error:", e)
finally:
cursor.close()
handle_pool_point_data_to_redis(network_id, pool_id_list)
def handle_pool_point_data_to_redis(network_id, pool_id_list):
now = int(datetime.now().replace(minute=0, second=0, microsecond=0).timestamp())
timestamp = now - (1 * 24 * 60 * 60)
db_conn = get_db_connect(network_id)
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
redis_conn = RedisProvider()
redis_conn.begin_pipe()
try:
for pool_id in pool_id_list:
sql_24h = "select point,sum(fee_x) as fee_x,sum(fee_y) as fee_y,sum(tvl_x_l) as tvl_x_l," \
"sum(tvl_y_l) as tvl_y_l from dcl_pool_analysis where pool_id = '%s' " \
"and `timestamp` >= %s GROUP BY point order by point" % (pool_id, timestamp)
cursor.execute(sql_24h)
point_data_24h = cursor.fetchall()
redis_conn.add_pool_point_24h_assets(network_id, pool_id, json.dumps(point_data_24h))
add_redis_data(network_id, Cfg.NETWORK[network_id]["REDIS_POOL_POINT_24H_DATA_KEY"], pool_id, json.dumps(point_data_24h))
redis_conn.add_pool_point_24h_assets(network_id, pool_id + "timestamp", now)
add_redis_data(network_id, Cfg.NETWORK[network_id]["REDIS_POOL_POINT_24H_DATA_KEY"], pool_id + "timestamp", now)
except Exception as e:
print("query dcl_pool_analysis to db error:", e)
finally:
cursor.close()
redis_conn.end_pipe()
redis_conn.close()
def add_dcl_user_liquidity_data(data_list, network_id):
db_conn = get_db_connect(network_id)
sql = "insert into dcl_user_liquidity(pool_id, account_id, point, l, tvl_x_l, tvl_y_l, p, timestamp, create_time) " \
"values(%s,%s,%s,%s,%s,%s,%s,%s, now())"
insert_data = []
cursor = db_conn.cursor(cursor=pymysql.cursors.DictCursor)
try:
for data in data_list:
insert_data.append((data["pool_id"], data["account_id"], data["point"], data["l"], data["tvl_x_l"],
data["tvl_y_l"], data["p"], data["timestamp"]))