-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.py
894 lines (784 loc) · 30.4 KB
/
archive.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
import time
import boto3
import argparse
import asyncio
import pandas as pd
import datetime as dt
from scripts.load_markets import PerpMarket, SpotMarket, initialize_state
from scripts.event_parser import parse_event
from scripts.log_parser import get_logs_from_topledger
import io
import gc
from scripts.utils import chunks
from concurrent.futures import ThreadPoolExecutor, as_completed
import awswrangler as wr
from scripts.utils import snake_to_camel_df
PROFILE_NAME = "" # aws profile name with permissions to access destination bucket
DESTINATION_BUCKET_NAME = "" # destination bucket name
RPC_URL = "" # rpc url
PROGRAM_ID = "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
EVENT_TYPES = [
"OrderActionRecord",
"SettlePnlRecord",
"DepositRecord",
"InsuranceFundRecord",
"InsuranceFundStakeRecord",
"LiquidationRecord",
"LPRecord",
"FundingRateRecord",
"FundingPaymentRecord",
]
session_default = boto3.Session(PROFILE_NAME) # Change here to profile name
s3_resource = session_default.resource("s3")
DESTINATION_BUCKET = s3_resource.Bucket(DESTINATION_BUCKET_NAME) # Change here to destination bucket name
def assume_role(arn, session_name):
sts_client = boto3.client("sts")
assumed_role = sts_client.assume_role(RoleArn=arn, RoleSessionName=session_name)
credentials = assumed_role["Credentials"]
return credentials
def filter_df(df, event_type):
rez = df[df["event_type"] == event_type]
if event_type == "OrderActionRecord":
rez = rez[
rez["args"].apply(
lambda x: x.get("action") if isinstance(x, dict) else None
)
== "Fill"
]
elif event_type == "SettlePnlRecord":
rez["authority"] = rez["args"].apply(lambda x: x["userAuthority"])
rez = rez[
~rez["authority"].isin(
[
"5civ8nag6sdWWqqxu4oGwsNxAMXAPCSJbfevQGJ3QXNV",
"FionaRdGscg9iK3RA4H4pZrmFyFPDhRAUExeEHfZtUPm",
"HPVkxurXKioCF5AkgmzXxjLrZuXKFzBFd6UhNTNnnxvS",
"DJXgcKKf2n3rdpXt2mqCsqCm3vrDWyCPWw6kKGuGCLyv",
"GontTwDeBduvbW85oHyC8A7GekuT8X1NkZHDDdUWWvsV",
"D3VukrhsSXNM8f2wqx595cJaQtS22enTm9EgfumJT7Gv",
"ErenqwMN7zuun3NQh8dfgknNtfZGw6VRyw8dYybWdZ8",
]
)
]
elif event_type == "DepositRecord":
## Filter out the bad user
rez["authority"] = rez["args"].apply(lambda x: x["userAuthority"])
rez = rez[rez["authority"] != "882DFRCi5akKFyYxT4PP2vZkoQEGvm2Nsind2nPDuGqu"]
return rez
def sanity_check(trades: pd.DataFrame):
block_times = pd.to_datetime(trades["block_time"], format="%m/%d/%y %H:%M")
date = block_times.iloc[0].date()
after_midnight = dt.datetime.combine(date, dt.time(minute=1))
noon = dt.datetime.combine(date, dt.time(hour=12))
before_midnight = dt.datetime.combine(date, dt.time(hour=23, minute=59))
window = pd.Timedelta(minutes=30)
after_midnight_check = (block_times - after_midnight).abs().min() < window
noon_check = (block_times - noon).abs().min() < window
before_midnight_check = (block_times - before_midnight).abs().min() < window
if not after_midnight_check:
print(f"Missing data around 0:01 on {date}")
if not noon_check:
print(f"Missing data around 12:00 on {date}")
if not before_midnight_check:
print(f"Missing data around 23:59 on {date}")
return after_midnight_check and noon_check and before_midnight_check
def process_trades(trades: pd.DataFrame, date, logs):
from scripts.load_markets import PERP_MARKETS, SPOT_MARKETS
userTradesMap = {}
marketTradesMap = {}
trades["logs"] = trades["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "OrderActionRecord", logs.get(x, []))
)
)
if not sanity_check(trades):
print("Potentially missing data around 0:01, 12:00, or 23:59")
for _, row in trades.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
marketType = "perp" if parsed["marketType"] == "perp" else "spot"
market: PerpMarket | SpotMarket = next(
filter(
lambda m: m.marketIndex == parsed["marketIndex"],
PERP_MARKETS if marketType == "perp" else SPOT_MARKETS,
),
None,
)
marketSymbol = market.symbol
## Log for maker
if parsed["maker"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/tradeRecords/{}".format(parsed["maker"], date.year)
)
if userPrefix not in userTradesMap:
userTradesMap[userPrefix] = []
userTradesMap[userPrefix].append(parsed)
## Log for taker
if parsed["taker"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/tradeRecords/{}".format(parsed["taker"], date.year)
)
if userPrefix not in userTradesMap:
userTradesMap[userPrefix] = []
userTradesMap[userPrefix].append(parsed)
# Log for market
marketPrefix = (
"program/"
+ PROGRAM_ID
+ "/market/{}/tradeRecords/{}".format(marketSymbol, date.year)
)
if marketPrefix not in marketTradesMap:
marketTradesMap[marketPrefix] = []
marketTradesMap[marketPrefix].append(parsed)
for marketPrefix in marketTradesMap.keys():
df_to_write = pd.DataFrame(marketTradesMap[marketPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=[
"txSig",
"taker",
"maker",
"takerOrderId",
"makerOrderId",
"marketIndex",
"marketType",
"action",
"fillRecordId",
"baseAssetAmountFilled",
]
)
## Spot check missing fills before writing
df_to_write = df_to_write[df_to_write["baseAssetAmountFilled"] != 0]
df_to_write.dropna(subset=["fillRecordId"], inplace=True)
if len(df_to_write) == 0:
print(f"No fills for {marketPrefix} on {date}")
continue
df_to_write = df_to_write.sort_values("fillRecordId")
full_range = pd.Series(
range(
int(df_to_write["fillRecordId"].min()),
int(df_to_write["fillRecordId"].max()) + 1,
)
)
missing_values = set(full_range) - set(df_to_write["fillRecordId"])
if len(missing_values) > 0:
print(
f"Missing values for market {marketPrefix}, length: ",
len(missing_values),
)
print(missing_values)
else:
print(f"No missing fill record ids for {marketPrefix} on {date}")
csv_buffer = io.BytesIO()
df_to_write = df_to_write.drop_duplicates(
subset=[
"txSig",
"taker",
"maker",
"takerOrderId",
"makerOrderId",
"marketIndex",
"marketType",
"action",
"fillRecordId",
"baseAssetAmountFilled",
]
)
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(marketPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
for userPrefix in userTradesMap.keys():
df_to_write = pd.DataFrame(userTradesMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=[
"txSig",
"taker",
"maker",
"takerOrderId",
"makerOrderId",
"marketIndex",
"marketType",
"action",
"fillRecordId",
"baseAssetAmountFilled",
]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_settle_pnl(records, date, logs):
userMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "SettlePnlRecord", logs.get(x, []))
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for taker
if parsed["user"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/settlePnlRecords/{}".format(parsed["user"], date.year)
)
if userPrefix not in userMap:
userMap[userPrefix] = []
userMap[userPrefix].append(parsed)
for userPrefix in userMap.keys():
df_to_write = pd.DataFrame(userMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=["txSig", "marketIndex", "user"]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_deposit(records, date, logs):
userMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "DepositRecord", logs.get(x, []))
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for taker
if parsed["user"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/depositRecords/{}".format(parsed["user"], date.year)
)
if userPrefix not in userMap:
userMap[userPrefix] = []
userMap[userPrefix].append(parsed)
for userPrefix in userMap.keys():
df_to_write = pd.DataFrame(userMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=[
"txSig",
"marketIndex",
"depositRecordId",
]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_insurance_fund(records, date, logs):
from scripts.load_markets import SPOT_MARKETS
marketMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "InsuranceFundRecord", logs.get(x, []))
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for market
market: SpotMarket = next(
filter(
lambda m: m.marketIndex == parsed["spotMarketIndex"],
SPOT_MARKETS,
),
None,
)
marketPrefix = (
"program/"
+ PROGRAM_ID
+ "/market/{}/insuranceFundRecords/{}".format(market.symbol, date.year)
)
if marketPrefix not in marketMap:
marketMap[marketPrefix] = []
marketMap[marketPrefix].append(parsed)
for marketPrefix in marketMap.keys():
df_to_write = pd.DataFrame(marketMap[marketPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=["txSig", "ts", "perpMarketIndex", "spotMarketIndex"]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(marketPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_insurance_fund_stake(records, date, logs):
userMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(
lambda event: event.name == "InsuranceFundStakeRecord",
logs.get(x, []),
)
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for taker
if parsed["userAuthority"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/authority/{}/insuranceFundStakeRecords/{}".format(
parsed["userAuthority"], date.year
)
)
if userPrefix not in userMap:
userMap[userPrefix] = []
userMap[userPrefix].append(parsed)
for userPrefix in userMap.keys():
df_to_write = pd.DataFrame(userMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=["txSig", "ts", "marketIndex", "userAuthority"]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_liquidation(records, date, logs):
userMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "LiquidationRecord", logs.get(x, []))
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for taker
if parsed["user"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/liquidationRecords/{}".format(parsed["user"], date.year)
)
if userPrefix not in userMap:
userMap[userPrefix] = []
userMap[userPrefix].append(parsed)
for userPrefix in userMap.keys():
df_to_write = pd.DataFrame(userMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=[
"txSig",
"user",
"liquidationId",
"marginRequirement",
]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_lp(records, date, logs):
userMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(filter(lambda event: event.name == "LPRecord", logs.get(x, [])))
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for taker
if parsed["user"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/lpRecord/{}".format(parsed["user"], date.year)
)
if userPrefix not in userMap:
userMap[userPrefix] = []
userMap[userPrefix].append(parsed)
for userPrefix in userMap.keys():
df_to_write = pd.DataFrame(userMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=["txSig", "user", "marketIndex"]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_funding_rate(records, date, logs):
from scripts.load_markets import PERP_MARKETS
marketMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "FundingRateRecord", logs.get(x, []))
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for market
market: SpotMarket = next(
filter(lambda m: m.marketIndex == parsed["marketIndex"], PERP_MARKETS),
None,
)
marketPrefix = (
"program/"
+ PROGRAM_ID
+ "/market/{}/fundingRateRecords/{}".format(market.symbol, date.year)
)
if marketPrefix not in marketMap:
marketMap[marketPrefix] = []
marketMap[marketPrefix].append(parsed)
for marketPrefix in marketMap.keys():
df_to_write = pd.DataFrame(marketMap[marketPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=[
"txSig",
"marketIndex",
"recordId",
]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(marketPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def process_funding_payment(records, date, logs):
userMap = {}
records["logs"] = records["tx_id"].apply(
lambda x: list(
filter(lambda event: event.name == "FundingPaymentRecord", logs.get(x, []))
)
)
for _, row in records.iterrows():
logs = row["logs"]
for log in logs:
parsed = parse_event(
log, {"slot": row["block_slot"], "tx_sig": row["tx_id"]}
)
parsed["programId"] = PROGRAM_ID
## Log for taker
if parsed["user"] is not None:
userPrefix = (
"program/"
+ PROGRAM_ID
+ "/user/{}/fundingPaymentRecords/{}".format(
parsed["user"], date.year
)
)
if userPrefix not in userMap:
userMap[userPrefix] = []
userMap[userPrefix].append(parsed)
for userPrefix in userMap.keys():
df_to_write = pd.DataFrame(userMap[userPrefix])
## De-duplicate
df_to_write = df_to_write.drop_duplicates(
subset=["txSig", "user", "marketIndex", "userLastCumulativeFunding"]
)
csv_buffer = io.BytesIO()
df_to_write.to_csv(csv_buffer, index=False, compression="gzip")
object_path = "{}/{}".format(userPrefix, date.strftime("%Y%m%d"))
DESTINATION_BUCKET.put_object(
Key=object_path,
Body=csv_buffer.getvalue(),
ContentType="text/csv",
ContentEncoding="gzip",
)
def read_and_filter_file(file_key, read_credentials, file_date):
event_types_to_read = []
for event_type in EVENT_TYPES:
try:
with open("./out/{}.txt".format(event_type), "r") as file:
last_processed_date = pd.to_datetime(file.read()).date()
if file_date > last_processed_date:
event_types_to_read.append(event_type)
else:
print("Date already processed for event {}".format(event_type))
except:
event_types_to_read.append(event_type)
attempts = 0
while attempts < 3:
try:
return pd.read_parquet(
f"s3://drift-topledger/{file_key}",
filters=[[("event_type", "=", y)] for y in event_types_to_read],
storage_options={
"key": read_credentials["access_key"],
"secret": read_credentials["secret_key"],
},
)
except Exception as e:
attempts += 1
if attempts < 3:
print(f"Retrying file {file_key}...")
time.sleep(3)
continue
else:
print(f"Failed to read file {file_key}. Error: {e}")
return pd.DataFrame() # Return empty DataFrame in case of failure
def process_event_type(event, df_filtered, date, logs):
try:
with open("./out/{}.txt".format(event), "r") as file:
last_processed_date = pd.to_datetime(file.read()).date()
if date <= last_processed_date:
print("Date already processed for event {}".format(event))
return
except:
pass
print(f"Processing event {event}")
if event == "OrderActionRecord":
process_trades(
df_filtered[(df_filtered["event_type"] == "OrderActionRecord")], date, logs
)
elif event == "SettlePnlRecord":
process_settle_pnl(
df_filtered[df_filtered["event_type"] == "SettlePnlRecord"], date, logs
)
elif event == "DepositRecord":
process_deposit(
df_filtered[df_filtered["event_type"] == "DepositRecord"], date, logs
)
elif event == "InsuranceFundRecord":
process_insurance_fund(
df_filtered[df_filtered["event_type"] == "InsuranceFundRecord"], date, logs
)
elif event == "InsuranceFundStakeRecord":
process_insurance_fund_stake(
df_filtered[df_filtered["event_type"] == "InsuranceFundStakeRecord"],
date,
logs,
)
elif event == "LiquidationRecord":
process_liquidation(
df_filtered[df_filtered["event_type"] == "LiquidationRecord"], date, logs
)
elif event == "LPRecord":
process_lp(df_filtered[df_filtered["event_type"] == "LPRecord"], date, logs)
elif event == "FundingRateRecord":
process_funding_rate(
df_filtered[df_filtered["event_type"] == "FundingRateRecord"], date, logs
)
elif event == "FundingPaymentRecord":
process_funding_payment(
df_filtered[df_filtered["event_type"] == "FundingPaymentRecord"], date, logs
)
with open(f"./out/{event}.txt", "w") as file:
file.write(date.strftime("%Y%m%d"))
def archive(start_date, end_date):
frozen_credentials = session_default.get_credentials().get_frozen_credentials()
read_credentials = {
"access_key": frozen_credentials.access_key,
"secret_key": frozen_credentials.secret_key,
}
s3 = boto3.client(
"s3",
aws_access_key_id=read_credentials["access_key"],
aws_secret_access_key=read_credentials["secret_key"],
)
bucket_name = "drift-topledger"
response = s3.list_objects_v2(
Bucket=bucket_name,
StartAfter="drift/events/{}/*".format(
(start_date - dt.timedelta(days=7)).strftime("%Y-%m-%d")
),
Prefix="drift/events",
)
txns_response = s3.list_objects_v2(
Bucket=bucket_name,
StartAfter="drift/txns/{}/*".format(
(start_date - dt.timedelta(days=7)).strftime("%Y-%m-%d")
),
Prefix="drift/txns",
)
files_to_process = {}
for obj in response["Contents"]:
date = pd.to_datetime(obj["Key"].split("/")[2]).date()
if (date >= start_date) and (date <= end_date):
for event in EVENT_TYPES:
try:
with open("./out/{}.txt".format(event), "r") as file:
last_processed_date = pd.to_datetime(file.read()).date()
if date > last_processed_date:
if date not in files_to_process:
files_to_process[date] = []
files_to_process[date].append(obj["Key"])
break
except FileNotFoundError:
if date not in files_to_process:
files_to_process[date] = []
files_to_process[date].append(obj["Key"])
break
txn_files_to_process = {}
for obj in txns_response["Contents"]:
if not obj["Key"] or not obj["Key"].endswith(".parquet"):
continue
date = pd.to_datetime(obj["Key"].split("/")[2]).date()
if (date >= start_date) and (date <= end_date):
for event in EVENT_TYPES:
try:
with open("./out/{}.txt".format(event), "r") as file:
last_processed_date = pd.to_datetime(file.read()).date()
if date > last_processed_date:
if date not in txn_files_to_process:
txn_files_to_process[date] = []
txn_files_to_process[date].append(obj["Key"])
break
except FileNotFoundError:
if date not in txn_files_to_process:
txn_files_to_process[date] = []
txn_files_to_process[date].append(obj["Key"])
break
for (events_date, events_files), (txns_date, txns_files) in zip(
files_to_process.items(), txn_files_to_process.items()
):
if str(events_date) != str(txns_date):
print(
f"Events date {events_date} and txns date {txns_date} do not match"
)
raise ValueError("Events date and txns date do not match")
print(f"Processing events date {events_date}")
print(f"Processing txs date {txns_date}")
print(f"Events Files to process: {events_files}")
print(f"Txns Files to process: {txns_files}")
with ThreadPoolExecutor(max_workers=5) as executor:
future_to_file = {
executor.submit(
read_and_filter_file, file, read_credentials, events_date
): file
for file in events_files
}
daily_dfs = []
for future in as_completed(future_to_file):
daily_df = future.result()
if not daily_df.empty:
daily_dfs.append(daily_df)
print("Read files")
if len(daily_dfs) == 0:
continue
df_filtered = pd.concat(daily_dfs, ignore_index=True)
condition = (df_filtered["event_type"] == "OrderActionRecord") & (
df_filtered["args"].apply(
lambda x: x.get("action") if isinstance(x, dict) else None
)
!= "Fill"
)
df_filtered.drop(df_filtered[condition].index, inplace=True)
if df_filtered.empty:
continue
print("Number of unique txs: {}".format(df_filtered["tx_id"].nunique()))
logs = asyncio.run(
get_logs_from_topledger(
df_filtered["tx_id"].unique().tolist(), read_credentials, txns_files
)
)
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [
executor.submit(
process_event_type, event, df_filtered, events_date, logs
)
for event in EVENT_TYPES
]
for future in futures:
future.result()
print("All done!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Archive data between two dates.")
parser.add_argument(
"--start-date",
type=lambda s: dt.datetime.strptime(s, "%Y-%m-%d").date(),
help="Start date in YYYY-MM-DD format",
default=dt.date.today() - dt.timedelta(days=1),
)
parser.add_argument(
"--end-date",
type=lambda s: dt.datetime.strptime(s, "%Y-%m-%d").date(),
help="End date in YYYY-MM-DD format",
default=dt.date.today() - dt.timedelta(days=1),
)
args = parser.parse_args()
loop = asyncio.get_event_loop()
loop.run_until_complete(initialize_state())
archive(args.start_date, args.end_date)