-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
711 lines (655 loc) · 32.3 KB
/
stats.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
import datetime
from collections import defaultdict
from dataclasses import dataclass
from decimal import Decimal
import pandas as pd
from eth_utils.address import to_checksum_address
from eth_utils.currency import from_wei
from sqlalchemy import and_, func
from .database import Session
from .models import (
DelegateChanged,
DelegateVotesChanged,
Staked,
Transfer,
Withdrawn,
DelegationRecord,
EventType,
VoteCast,
VoteStat,
)
class DataExists:
PRESENT = True
ABSENT = False
@dataclass
class EventData:
delegate_changed: DelegateChanged
delegate_votes_changed: DelegateVotesChanged
staked: Staked
transfer: Transfer
withdrawn: Withdrawn
class DelegatesChangedData(DataExists):
...
class DelegateVotesChangedData(DataExists):
...
class StakedData(DataExists):
...
class TransferData(DataExists):
...
class WithdrawnData(DataExists):
...
CTX_DELEGATION_CHECK = [
DelegatesChangedData.PRESENT,
DelegateVotesChangedData.PRESENT,
StakedData.ABSENT,
TransferData.ABSENT,
WithdrawnData.ABSENT
]
STAKED_CHECK = [
DelegatesChangedData.ABSENT,
DelegateVotesChangedData.PRESENT,
StakedData.PRESENT,
TransferData.PRESENT,
WithdrawnData.ABSENT
]
CTX_TRANSFER_CHECK = [
DelegatesChangedData.ABSENT,
DelegateVotesChangedData.PRESENT,
StakedData.ABSENT,
TransferData.PRESENT,
WithdrawnData.ABSENT
]
WITHDRAWN_CHECK = [
DelegatesChangedData.ABSENT,
DelegateVotesChangedData.PRESENT,
StakedData.ABSENT,
TransferData.PRESENT,
WithdrawnData.PRESENT
]
# CTX_DELEGATION_CHECK_1 = [
# DelegatesChangedData,
# DelegateVotesChangedData,
# StakedData,
# TransferData,
# WithdrawnData
# ]
CRYPTEX_TEAM_MULTISIG_ADDRESS = '0xa70b638b70154edfcbb8dbbbd04900f328f32c35'
def convert_epoch_to_datetime(timestamp: int) -> datetime.datetime:
return datetime.datetime.fromtimestamp(timestamp)
def no_of_days(timestamp1: datetime.datetime, timestamp2: datetime.datetime) -> int:
return (timestamp2 - timestamp1).days
START_BLOCK = 13360297
START_DATETIME = convert_epoch_to_datetime(1633453569)
END_BLOCK_TIMESTAMP = 1703980800
class BuildAccountDelegation:
def build(self):
with Session() as session:
subquery = session.query(
DelegateVotesChanged.blockNumber,
DelegateVotesChanged.transactionHash,
DelegateVotesChanged.blockTimestamp,
).distinct(DelegateVotesChanged.transactionHash).subquery()
for delegate_vote_changed in session.query(
subquery.c.transactionHash, subquery.c.blockNumber, subquery.c.blockTimestamp
).order_by(subquery.c.blockNumber).all():
event_type, data = self.classify_event_type(
session,
delegate_vote_changed.transactionHash
)
self.write_record(
session,
delegate_vote_changed.transactionHash,
delegate_vote_changed.blockNumber,
delegate_vote_changed.blockTimestamp,
event_type,
data
)
def write_record(self, session, tx_hash, block_number, block_timestamp, event_type, data):
if event_type == EventType.DIRECT_DELEGATION:
if len(data.delegate_changed) == 1 and len(data.delegate_votes_changed) == 1:
# 0x9c7118eb0c347a84f1411060c580ee1fe354de4bc82f885ffea7e53970c5bf27
latest_balance = self.get_latest_balance(
session,
data.delegate_changed[0].delegator,
data.delegate_changed[0].toDelegate
)
record = DelegationRecord(
delegator=data.delegate_changed[0].delegator,
delegatee=data.delegate_changed[0].toDelegate,
balance=latest_balance + data.delegate_votes_changed[0].newBalance - data.delegate_votes_changed[
0].previousBalance,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
elif len(data.delegate_changed) == 1 and len(data.delegate_votes_changed) == 2 and \
data.delegate_votes_changed[0].newBalance == 0:
# delegator changed; old delegator balance becomes 0, new one gets 1st event previousBalance.
# Also for the 2nd event newBalance - oldBalance = previousBalance of 1st event
# 0x0dc8bca57fbf2d4c283a1130e29d51c927a7cb93c0dcc297b157ea4936fe0fd3
latest_balance2 = self.get_latest_balance(
session,
data.delegate_changed[0].delegator,
data.delegate_changed[0].fromDelegate
)
record1 = DelegationRecord(
delegator=data.delegate_changed[0].delegator,
delegatee=data.delegate_changed[0].fromDelegate,
balance=latest_balance2 + (data.delegate_votes_changed[0].newBalance - data.delegate_votes_changed[
0].previousBalance),
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
latest_balance2 = self.get_latest_balance(
session,
data.delegate_changed[0].delegator,
data.delegate_changed[0].toDelegate
)
record2 = DelegationRecord(
delegator=data.delegate_changed[0].delegator,
delegatee=data.delegate_changed[0].toDelegate,
balance=latest_balance2 + (data.delegate_votes_changed[1].newBalance - data.delegate_votes_changed[
1].previousBalance),
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record1)
session.add(record2)
else:
raise Exception('Unknown type')
elif event_type == EventType.STAKED:
if len(data.staked) == 1 and len(data.delegate_votes_changed) == 1:
# 0xe6d954dc98bf0dcda580463aa1a01ba773943f2480c2780a596cf565622361e1
latest_balance = self.get_latest_balance(
session,
data.staked[0].delegatee,
data.delegate_votes_changed[0].delegate
)
record = DelegationRecord(
delegator=data.staked[0].delegatee,
delegatee=data.delegate_votes_changed[0].delegate,
balance=latest_balance + data.staked[0].amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
elif (
len(data.staked) == 1 and
len(data.delegate_votes_changed) == 2 and
(
data.delegate_votes_changed[0].previousBalance - data.delegate_votes_changed[0].newBalance
) == (
data.delegate_votes_changed[1].newBalance - data.delegate_votes_changed[1].previousBalance
)
):
# 0x0e12001d49195017c6090c5f3ea874e4dabfaa93f8f5081ebc1f5b2a69ff9f2f
# need to update records of two keepers
record1 = DelegationRecord(
delegator=data.staked[0].delegatee,
delegatee=data.delegate_votes_changed[0].delegate,
balance=data.delegate_votes_changed[0].newBalance,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
# make sure its committed as this might affect the latest balance
# when delegatee for both records is the same
session.add(record1)
session.commit()
latest_balance = self.get_latest_balance(
session,
data.staked[0].delegatee,
data.delegate_votes_changed[1].delegate
)
record2 = DelegationRecord(
delegator=data.staked[0].delegatee,
delegatee=data.delegate_votes_changed[1].delegate,
balance=latest_balance + data.staked[0].amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record2)
else:
raise Exception('Unknown type')
elif event_type == EventType.CTX_TRANSFER:
if len(data.transfer) == 1 and len(data.delegate_votes_changed) == 1:
# 0xeab6d157a81eaf7bab8682adf130a6136b566bc686f07fe7da213e54bab3a210
# 0xfb6353f0be46485e28702ee903b59251f1d7fe81b0afdef707244f773fa54a02
transfer_from = self.get_latest_balance(
session,
data.transfer[0].from_,
data.delegate_votes_changed[0].delegate
)
transfer_to = self.get_latest_balance(
session,
data.transfer[0].to,
data.delegate_votes_changed[0].delegate
)
if transfer_from:
# 0xeab6d157a81eaf7bab8682adf130a6136b566bc686f07fe7da213e54bab3a210
record = DelegationRecord(
delegator=data.transfer[0].from_,
delegatee=data.delegate_votes_changed[0].delegate,
balance=transfer_from - data.transfer[0].amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
else:
# 0xfb6353f0be46485e28702ee903b59251f1d7fe81b0afdef707244f773fa54a02
record = DelegationRecord(
delegator=data.transfer[0].to,
delegatee=data.delegate_votes_changed[0].delegate,
balance=transfer_to + data.transfer[0].amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
elif (
len(data.transfer) > 1 and
len(data.delegate_votes_changed) == 1 and
data.transfer[-2].to == data.transfer[-1].from_ and
data.transfer[-1].amount == (
data.delegate_votes_changed[0].newBalance - data.delegate_votes_changed[0].previousBalance)
):
# exchange buy ctx
# 0xd35185f3135f135390874086b095d137d3152f86fbff26b70f4754ff15abf289
# delegatee = data.transfer[1].to
# delegate = data.delegate_votes_changed[0].delegate
# exchange buy ctx
# 0x4e6960173537b3b8c4ecb1aeafd6680b2d0146e5169de70fc277cc24948644ad
# delegatee = data.transfer[2].to
# delegate = data.delegate_votes_changed[0].delegate
latest_balance = self.get_latest_balance(
session,
data.transfer[-1].to,
data.delegate_votes_changed[0].delegate
)
record = DelegationRecord(
delegator=data.transfer[-1].to,
delegatee=data.delegate_votes_changed[0].delegate,
balance=latest_balance + data.transfer[-1].amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
elif (
len(data.transfer) > 1 and
len(data.delegate_votes_changed) == 1 and
data.transfer[0].to == data.transfer[1].from_ and
data.transfer[0].amount == (
data.delegate_votes_changed[0].previousBalance - data.delegate_votes_changed[0].newBalance)
):
# exchange sell
# 0x300666908ec0581ef83e5f535734e6714aff5e46eb908511be773fd3466b310b
# 0xadec3a0a4a5de690f90b9cd608948a748cafd6f8710d6e70a3601e22cd0f2c9e
latest_balance = self.get_latest_balance(
session,
data.transfer[0].from_,
data.delegate_votes_changed[0].delegate
)
record = DelegationRecord(
delegator=data.transfer[0].from_,
delegatee=data.delegate_votes_changed[0].delegate,
balance=latest_balance - data.transfer[0].amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
elif all([
self.check_if_team_multisig(obj.delegate) for obj in data.delegate_votes_changed
]):
# cryptex team multisig distribution
# 0x32e6249c2425b3b9eee65f0a749708db6ae23f66e7b7cc036d4b25f1101f596e
pass
elif any([self.check_if_team_multisig(obj.delegate) for obj in data.delegate_votes_changed]):
# cryptex team multisig distribution
delegations = [obj for obj in data.delegate_votes_changed if
not self.check_if_team_multisig(obj.delegate)]
amounts = [obj.amount for obj in data.transfer]
for _delegate_votes_changed in delegations:
if self.check_if_team_multisig(_delegate_votes_changed.delegate):
continue
for _transfer in data.transfer:
# 0x67e594578941b0bc7e2d1e654d554ac672a307667adcacc11137c5a2067453ce
# 0x04cb7a6842b3398a0a964df8854f04a8de3309940a97735f1392dd3edd3a8e21
if (
_transfer.to == _delegate_votes_changed.delegate and
_delegate_votes_changed.delegate == '0xb8c30017b375bf675c2836c4c6b6ed5be214739d'
# ugly but only special case
):
latest_balance = self.get_latest_balance(
session,
_transfer.to,
_delegate_votes_changed.delegate
)
record = DelegationRecord(
delegator=_transfer.to,
delegatee=_delegate_votes_changed.delegate,
balance=latest_balance + _transfer.amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
session.commit()
break
else:
for _transfer in data.transfer:
# 0x3c3d5c4250aea782d9666c5a99fcbaeb4889294bd26f5f90c7befc093ed96955
if ((
_delegate_votes_changed.newBalance - _delegate_votes_changed.previousBalance
) == _transfer.amount
):
latest_balance = self.get_latest_balance(
session,
_transfer.to,
_delegate_votes_changed.delegate
)
record = DelegationRecord(
delegator=_transfer.to,
delegatee=_delegate_votes_changed.delegate,
balance=latest_balance + _transfer.amount,
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
session.commit()
break
else:
raise Exception('Unknown type')
else:
raise Exception('Unknown type')
elif event_type == EventType.WITHDRAWN:
if len(data.withdrawn) == 1 and len(data.delegate_votes_changed) == 1:
# 0x557200d874e5e6e5ce8b7a2aa23b4d72cf0d05b8ad2691d234480700da3d49a9
latest_balance = self.get_latest_balance(
session,
data.transfer[0].to,
data.delegate_votes_changed[0].delegate
)
record = DelegationRecord(
delegator=data.withdrawn[0].delegatee,
delegatee=data.delegate_votes_changed[0].delegate,
balance=latest_balance + (data.delegate_votes_changed[0].newBalance - data.delegate_votes_changed[
0].previousBalance),
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record)
elif (
len(data.withdrawn) == 1 and
len(data.delegate_votes_changed) == 2 and
(
data.delegate_votes_changed[0].previousBalance - data.delegate_votes_changed[0].newBalance
) == (
data.delegate_votes_changed[1].newBalance - data.delegate_votes_changed[1].previousBalance
)
):
# 0xe95e3675994188e944ecda0ec3865bb4a9b8326f9ec2acfac19d6bdf87c6c4fd
latest_balance1 = self.get_latest_balance(
session,
data.transfer[0].to,
data.delegate_votes_changed[0].delegate
)
record1 = DelegationRecord(
delegator=data.transfer[0].to,
delegatee=data.delegate_votes_changed[0].delegate,
balance=latest_balance1 + (data.delegate_votes_changed[0].newBalance - data.delegate_votes_changed[
0].previousBalance),
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record1)
session.commit()
latest_balance2 = self.get_latest_balance(
session,
data.withdrawn[0].delegatee,
data.delegate_votes_changed[1].delegate
)
record2 = DelegationRecord(
delegator=data.withdrawn[0].delegatee,
delegatee=data.delegate_votes_changed[1].delegate,
balance=latest_balance2 + (data.delegate_votes_changed[1].newBalance - data.delegate_votes_changed[
1].previousBalance),
event_type=event_type,
blockNumber=block_number,
blockTimestamp=block_timestamp,
transactionHash=tx_hash
)
session.add(record2)
else:
raise Exception('Unknown type')
else:
raise Exception('Unknown type')
session.commit()
@staticmethod
def check_if_team_multisig(address):
return address == CRYPTEX_TEAM_MULTISIG_ADDRESS
@staticmethod
def get_latest_balance(session, delegator, delegatee) -> Decimal:
old_record = session.query(DelegationRecord).filter(
and_(
DelegationRecord.delegator == delegator,
DelegationRecord.delegatee == delegatee
),
).order_by(DelegationRecord.blockNumber.desc()).first()
if old_record:
return old_record.balance
else:
return Decimal("0")
@staticmethod
def classify_event_type(session, tx_hash):
delegate_changed = session.query(DelegateChanged).filter(
DelegateChanged.transactionHash == tx_hash
).all()
delegate_votes_changed = session.query(DelegateVotesChanged).filter(
DelegateVotesChanged.transactionHash == tx_hash
).all()
staked = session.query(Staked).filter(
Staked.transactionHash == tx_hash
).all()
transfer = session.query(Transfer).filter(
Transfer.transactionHash == tx_hash
).all()
withdrawn = session.query(Withdrawn).filter(
Withdrawn.transactionHash == tx_hash
).all()
event_data = EventData(
delegate_changed=delegate_changed,
delegate_votes_changed=delegate_votes_changed,
staked=staked,
transfer=transfer,
withdrawn=withdrawn
)
data = [delegate_changed, delegate_votes_changed, staked, transfer, withdrawn]
data_exists_list = [bool(obj) for obj in data]
# any([len(obj) > 1 for obj in [delegate_changed, staked, transfer, withdrawn]]) or
if len(delegate_votes_changed) > 2 and not delegate_votes_changed[
0].delegate == '0xa70b638b70154edfcbb8dbbbd04900f328f32c35':
raise Exception("length > 1 case. Please check the code")
if data_exists_list == CTX_DELEGATION_CHECK:
return EventType.DIRECT_DELEGATION, event_data
elif data_exists_list == STAKED_CHECK:
return EventType.STAKED, event_data
elif data_exists_list == CTX_TRANSFER_CHECK:
return EventType.CTX_TRANSFER, event_data
elif data_exists_list == WITHDRAWN_CHECK:
return EventType.WITHDRAWN, event_data
else:
raise Exception('Unknown type')
def get_keeper_votes_on_proposals():
with Session() as session:
return session.query(VoteCast.voter, func.count(VoteCast.voter)).group_by(VoteCast.voter).order_by(
func.count(VoteCast.voter).desc()).all()
class VoteStatBuilder:
def build(self):
with Session() as session:
for delegator, delegatee in self.fetch_delegator_delegatee_pair(session):
for proposal_id, in self.fetch_all_proposals_id(session):
vote_cast = self.fetch_delegate_voted_on_proposal(session, proposal_id, delegatee)
if not vote_cast:
continue
else:
self.check_days_staked_and_create_record(session, proposal_id, delegator, delegatee, vote_cast)
session.commit()
def check_days_staked_and_create_record(self, session, proposal_id, delegator, delegatee, vote_cast):
no_of_days_staked, balance = self.calculate_no_days_staked_before_vote(session, delegator, delegatee, vote_cast)
if no_of_days_staked:
vote_stat = VoteStat(
delegator=delegator,
delegatee=delegatee,
proposalId=proposal_id,
balance=balance,
no_of_days=no_of_days_staked,
)
session.add(vote_stat)
@staticmethod
def fetch_delegate_voted_on_proposal(session, proposal_id, delegatee):
return session.query(VoteCast).filter(
and_(VoteCast.proposalId == proposal_id, VoteCast.voter == delegatee)
).first()
@staticmethod
def fetch_delegator_delegatee_pair(session):
return session.query(
DelegationRecord.delegator, DelegationRecord.delegatee).group_by(
DelegationRecord.delegator, DelegationRecord.delegatee
).all()
@staticmethod
def fetch_all_proposals_id(session):
return session.query(VoteCast.proposalId).filter(VoteCast.voter != CRYPTEX_TEAM_MULTISIG_ADDRESS).distinct().order_by(VoteCast.proposalId).all()
@staticmethod
def calculate_no_days_staked_before_vote(session, delegator, delegatee, vote_cast):
latest_zero_balance = session.query(DelegationRecord).filter(
and_(DelegationRecord.delegator == delegator,
DelegationRecord.delegatee == delegatee,
DelegationRecord.balance == 0,
DelegationRecord.blockNumber >= START_BLOCK,
DelegationRecord.blockNumber < vote_cast.blockNumber)
).order_by(DelegationRecord.blockNumber.desc()).first()
if latest_zero_balance:
nearest_non_zero_entry = session.query(DelegationRecord).filter(
and_(DelegationRecord.delegator == delegator,
DelegationRecord.delegatee == delegatee,
DelegationRecord.blockNumber > latest_zero_balance.blockNumber,
DelegationRecord.blockNumber < vote_cast.blockNumber)
).order_by(DelegationRecord.blockNumber).first()
if nearest_non_zero_entry:
latest_non_zero_entry = session.query(DelegationRecord).filter(
and_(DelegationRecord.delegator == delegator,
DelegationRecord.delegatee == delegatee,
DelegationRecord.balance > 0,
DelegationRecord.blockNumber < vote_cast.blockNumber)
).order_by(DelegationRecord.blockNumber.desc()).first()
start_date = max(START_DATETIME, convert_epoch_to_datetime(nearest_non_zero_entry.blockTimestamp))
return no_of_days(start_date,
convert_epoch_to_datetime(vote_cast.blockTimestamp)), latest_non_zero_entry.balance
else:
return 0, 0
else:
nearest_non_zero_entry = session.query(DelegationRecord).filter(
and_(DelegationRecord.delegator == delegator,
DelegationRecord.delegatee == delegatee,
DelegationRecord.blockNumber > START_BLOCK,
DelegationRecord.blockNumber < vote_cast.blockNumber)
).order_by(DelegationRecord.blockNumber).first()
if nearest_non_zero_entry:
latest_non_zero_entry = session.query(DelegationRecord).filter(
and_(DelegationRecord.delegator == delegator,
DelegationRecord.delegatee == delegatee,
DelegationRecord.balance > 0,
DelegationRecord.blockNumber < vote_cast.blockNumber)
).order_by(DelegationRecord.blockNumber.desc()).first()
start_date = max(START_DATETIME, convert_epoch_to_datetime(nearest_non_zero_entry.blockTimestamp))
return no_of_days(start_date,
convert_epoch_to_datetime(vote_cast.blockTimestamp)), latest_non_zero_entry.balance
else:
return 0, 0
TOTAL_REWARD = 50_000
KEEPER_SPLIT = 0.2
USER_SPLIT = 1 - KEEPER_SPLIT
excel_file_path = 'distribution_data.xlsx'
def convert_from_wei(amount):
return from_wei(amount, 'ether')
class GenerateDistribution:
def __init__(self):
self.balances = defaultdict(int)
def generate(self):
with Session() as session:
keeper_vote_cast_data = self.fetch_keeper_vote_cast_data(session)
self.fill_keeper_balances(keeper_vote_cast_data)
self.fill_user_balances(session, keeper_vote_cast_data)
df1 = pd.DataFrame(list(sorted(self.balances.items(), key=lambda x: -x[1])), columns=['address', 'reward'])
df2 = pd.read_sql_table(
VoteStat.__tablename__, session.bind
).drop(columns=['id']
).rename(columns={'no_of_days': 'number of days staked', 'balance': 'delegated_amount'})
df3 = pd.read_sql_table(DelegationRecord.__tablename__, session.bind).drop(
columns=['id', 'event_type']).rename(columns={'balance': 'delegated_amount'})
df4 = pd.DataFrame(keeper_vote_cast_data, columns=['Keeper', 'No of Proposals voted on'])
df1['address'] = df1['address'].apply(to_checksum_address)
df2['delegator'] = df2['delegator'].apply(to_checksum_address)
df2['delegatee'] = df2['delegatee'].apply(to_checksum_address)
df2['delegated_amount'] = df2['delegated_amount'].apply(convert_from_wei)
df3['delegator'] = df3['delegator'].apply(to_checksum_address)
df3['delegatee'] = df3['delegatee'].apply(to_checksum_address)
df3['delegated_amount'] = df3['delegated_amount'].apply(convert_from_wei)
with pd.ExcelWriter(excel_file_path, engine='xlsxwriter') as writer:
df1.to_excel(writer, sheet_name='Distribution', index=False)
df2.to_excel(writer, sheet_name='Voting Stats', index=False)
df3.to_excel(writer, sheet_name='Delegation Data', index=False)
df4.to_excel(writer, sheet_name='Keeper Votes', index=False)
df1['reward'] = df1['reward'].apply(int)
df1.to_json('distribution.json', orient='records', lines=True)
def fill_user_balances(self, session, keeper_vote_cast_data):
total_weight_for_distribution = sum(obj[1] for obj in keeper_vote_cast_data)
for keeper, no_proposals in keeper_vote_cast_data:
# keeper_delgators_reward = (no_proposals * USER_SPLIT * TOTAL_REWARD) / total_weight_for_distribution
# keeper_delegators_reward_per_proposal = keeper_delgators_reward / no_proposals
for proposal_id, in session.query(VoteCast.proposalId).filter(VoteCast.voter == keeper):
proposal_delegators = session.query(
VoteStat.delegator, VoteStat.balance, VoteStat.no_of_days
).filter(and_(VoteStat.delegatee == keeper, VoteStat.proposalId == proposal_id)).all()
proposal_keeper_total_weight = sum(obj[1] * obj[2] for obj in proposal_delegators)
for delegator, balance, no_of_days in proposal_delegators:
# self.balances[delegator] += (
# int(balance) * no_of_days * keeper_delegators_reward_per_proposal
# ) / int(proposal_keeper_total_weight)
self.balances[delegator] += (
int(balance) * no_of_days * no_proposals * USER_SPLIT * TOTAL_REWARD
) / (total_weight_for_distribution * no_proposals * int(proposal_keeper_total_weight))
def fill_keeper_balances(self, keeper_vote_cast_data):
total_keeper_reward = KEEPER_SPLIT * TOTAL_REWARD
total_weight = sum(obj[1] for obj in keeper_vote_cast_data)
for voter, no_of_proposals in keeper_vote_cast_data:
self.balances[voter] += (no_of_proposals * total_keeper_reward) / total_weight
@staticmethod
def fetch_keeper_vote_cast_data(session):
return session.query(
VoteCast.voter, func.count(VoteCast.voter)
).filter(VoteCast.voter != CRYPTEX_TEAM_MULTISIG_ADDRESS, VoteCast.votes > 0).group_by(VoteCast.voter).order_by(func.count(VoteCast.voter).desc()).all()