-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessLoyalty.py
More file actions
218 lines (182 loc) · 13.4 KB
/
Copy pathprocessLoyalty.py
File metadata and controls
218 lines (182 loc) · 13.4 KB
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
from pt_utils import *
from aws_utils import *
from config import *
from fb_utils import *
from pg_utils import *
pg=pg_utils("Loyalty")
fb=fb_utils()
aws=aws_utils()
def processEpochLoyaltySummary(epoch):
#to process after wait stake is done
#pg.cur1_execute("select stake_key from (select stake_key, count(distinct delegated_to_pool) as poolchanges from stake_history where (epoch=%s or epoch=%s) group by stake_key) a where a.poolchanges>1",[epoch,epoch-1])
thevars={"epoch":epoch,"epochm1":epoch-1}
pg.cur1_execute("select stake_key,stake_amount,poolswitch,max_epoch from (select stake_key, max(amount) as stake_amount,max(epoch) as max_epoch, count(distinct delegated_to_pool) as poolchanges,array_agg(delegated_to_pool || delegated_to_ticker ORDER BY epoch desc) as poolswitch from stake_history where (epoch=%(epoch)s or epoch=%(epochm1)s) group by stake_key) a where a.poolchanges>1 or array_length(poolswitch,1)=1 ",thevars)
row=pg.cur1_fetchone()
package={}
while row:
package[row['stake_key']]={"stake_amount":int(row['stake_amount'])}
if len(row['poolswitch'])==2:
package[row['stake_key']]["new_pool"]=row['poolswitch'][0] if row['poolswitch'][0] is not None and row['poolswitch'][0][0:56].strip()!='None' else None
package[row['stake_key']]["old_pool"]=row['poolswitch'][1] if row['poolswitch'][1] is not None and row['poolswitch'][1][0:56].strip()!='None' else None
else:
if row['max_epoch']==epoch:
package[row['stake_key']]["new_pool"]=row['poolswitch'][0] if row['poolswitch'][0] is not None and row['poolswitch'][0][0:56].strip()!='None' else None
package[row['stake_key']]["old_pool"]=None
else:
package[row['stake_key']]["new_pool"]=None
package[row['stake_key']]["old_pool"]=row['poolswitch'][0] if row['poolswitch'][0] is not None and row['poolswitch'][0][0:56].strip()!='None' else None
row=pg.cur1_fetchone()
pg.conn_commit()
aws.dump_s3(package,"stats/loyalty/"+str(epoch)+"/summary.json")
return package
def process3EpochPoolMigrations(epoch):
# load all 3 summaries, and combine together
total_stake_moved=0
stake_gained_by_pool={}
stake_gained_from_by_pool={}
stake_lost_by_pool={}
stake_lost_to_by_pool={}
for targetepoch in range(epoch-3,epoch):
package=aws.load_s3("stats/loyalty/"+str(targetepoch)+"/summary.json")
for stake_key in package:
if "new_pool" in package[stake_key] and package[stake_key]["new_pool"] is not None and "old_pool" in package[stake_key] and package[stake_key]["old_pool"] is not None:
total_stake_moved+=int(package[stake_key]['stake_amount'])
new_pool_id=package[stake_key]["new_pool"][0:56]
new_pool_ticker=package[stake_key]["new_pool"][56:]
old_pool_id=package[stake_key]["old_pool"][0:56]
old_pool_ticker=package[stake_key]["old_pool"][56:]
# this is indivudual pool stats
if new_pool_id not in stake_gained_from_by_pool:
stake_gained_from_by_pool[new_pool_id]={}
if old_pool_id not in stake_gained_from_by_pool[new_pool_id]:
stake_gained_from_by_pool[new_pool_id][old_pool_id]={}
stake_gained_from_by_pool[new_pool_id][old_pool_id][stake_key]={"pool_id":old_pool_id,"epoch":targetepoch,"pool_ticker":old_pool_ticker,"stake_key":stake_key,"stake":int(package[stake_key]['stake_amount'])}
# this is indivudual pool stats
if old_pool_id not in stake_lost_to_by_pool:
stake_lost_to_by_pool[old_pool_id]={}
if new_pool_id not in stake_lost_to_by_pool[old_pool_id]:
stake_lost_to_by_pool[old_pool_id][new_pool_id]={}
stake_lost_to_by_pool[old_pool_id][new_pool_id][stake_key]={"pool_id":new_pool_id,"epoch":targetepoch,"pool_ticker":new_pool_ticker,"stake_key":stake_key,"stake":int(package[stake_key]['stake_amount'])}
# this is for overall gain/loss across all pools
if new_pool_id not in stake_gained_by_pool:
stake_gained_by_pool[new_pool_id]={"pool_id":new_pool_id,"pool_ticker":new_pool_ticker,"delegators":1,"stake":int(package[stake_key]['stake_amount'])}
else:
stake_gained_by_pool[new_pool_id]["delegators"]+=1
stake_gained_by_pool[new_pool_id]["stake"]+=int(package[stake_key]['stake_amount'])
# this is for overall gain/loss across all pools
if old_pool_id not in stake_lost_by_pool:
stake_lost_by_pool[old_pool_id]={"pool_id":old_pool_id,"pool_ticker":old_pool_ticker,"delegators":1,"stake":int(package[stake_key]['stake_amount'])}
else:
stake_lost_by_pool[old_pool_id]["delegators"]+=1
stake_lost_by_pool[old_pool_id]["stake"]+=int(package[stake_key]['stake_amount'])
elif "new_pool" in package[stake_key] and package[stake_key]["new_pool"] is None and "old_pool" in package[stake_key] and package[stake_key]["old_pool"] is not None:
new_pool_id="None"
new_pool_ticker=""
old_pool_id=package[stake_key]["old_pool"][0:56]
old_pool_ticker=package[stake_key]["old_pool"][56:]
# this is indivudual pool stats
if old_pool_id not in stake_lost_to_by_pool:
stake_lost_to_by_pool[old_pool_id]={}
if new_pool_id not in stake_lost_to_by_pool[old_pool_id]:
stake_lost_to_by_pool[old_pool_id][new_pool_id]={}
stake_lost_to_by_pool[old_pool_id][new_pool_id][stake_key]={"pool_id":new_pool_id,"epoch":targetepoch,"pool_ticker":new_pool_ticker,"stake_key":stake_key,"stake":int(package[stake_key]['stake_amount'])}
# this is for overall gain/loss across all pools
if old_pool_id not in stake_lost_by_pool:
stake_lost_by_pool[old_pool_id]={"pool_id":old_pool_id,"pool_ticker":old_pool_ticker,"delegators":1,"stake":int(package[stake_key]['stake_amount'])}
else:
stake_lost_by_pool[old_pool_id]["delegators"]+=1
stake_lost_by_pool[old_pool_id]["stake"]+=int(package[stake_key]['stake_amount'])
elif "new_pool" in package[stake_key] and package[stake_key]["new_pool"] is not None and "old_pool" in package[stake_key] and package[stake_key]["old_pool"] is None:
new_pool_id=package[stake_key]["new_pool"][0:56]
new_pool_ticker=package[stake_key]["new_pool"][56:]
old_pool_id="None"
old_pool_ticker=""
# this is indivudual pool stats
if new_pool_id not in stake_gained_from_by_pool:
stake_gained_from_by_pool[new_pool_id]={}
if old_pool_id not in stake_gained_from_by_pool[new_pool_id]:
stake_gained_from_by_pool[new_pool_id][old_pool_id]={}
stake_gained_from_by_pool[new_pool_id][old_pool_id][stake_key]={"pool_id":old_pool_id,"epoch":targetepoch,"stake_key":stake_key,"pool_ticker":old_pool_ticker,"stake":int(package[stake_key]['stake_amount'])}
# this is for overall gain/loss across all pools
if new_pool_id not in stake_gained_by_pool:
stake_gained_by_pool[new_pool_id]={"pool_id":new_pool_id,"pool_ticker":new_pool_ticker,"delegators":1,"stake":int(package[stake_key]['stake_amount'])}
else:
stake_gained_by_pool[new_pool_id]["delegators"]+=1
stake_gained_by_pool[new_pool_id]["stake"]+=int(package[stake_key]['stake_amount'])
else:
print("ignorning none none")
top_stake_gained_by_pool = sorted(stake_gained_by_pool.values(), key=lambda x: x["stake"], reverse=True)
top_stake_lost_by_pool = sorted(stake_lost_by_pool.values(), key=lambda x: x["stake"], reverse=True)
print("*************************************************1")
#print(top_stake_gained_by_pool)
aws.dump_s3(top_stake_gained_by_pool,"stats/loyalty/"+str(epoch)+"/threeepoch_top_stake_gained_by_pool.json")
#print(top_stake_lost_by_pool)
aws.dump_s3(top_stake_lost_by_pool,"stats/loyalty/"+str(epoch)+"/threeepoch_top_stake_lost_by_pool.json")
# now do each of the pool summaries as well
print("*************************************************2")
for pool_id in stake_gained_from_by_pool:
stake_gained_from=[]
for from_pool_id in stake_gained_from_by_pool[pool_id]:
stake_gained_from.extend(list(stake_gained_from_by_pool[pool_id][from_pool_id].values()))
#print(stake_gained_from)
stake_gained_from.sort(key=lambda x: x["stake"], reverse=True)
aws.dump_s3(stake_gained_from,"stats/loyalty/"+str(epoch)+"/threeepoch_stake_gained_from/"+str(pool_id)+".json")
print("*************************************************3")
for pool_id in stake_lost_to_by_pool:
stake_lost_to=[]
for to_pool_id in stake_lost_to_by_pool[pool_id]:
stake_lost_to.extend(list(stake_lost_to_by_pool[pool_id][to_pool_id].values()))
stake_lost_to.sort(key=lambda x: x["stake"], reverse=True)
aws.dump_s3(stake_lost_to,"stats/loyalty/"+str(epoch)+"/threeepoch_stake_lost_to/"+str(pool_id)+".json")
print("*************************************************4")
def processLoyalty(stake_key):
# if we already have a record, then we process one epoch after the max record already defined:
pg.cur1_execute("select package from delegator_loyalty where stake_key=%s",[stake_key])
row=pg.cur1_fetchone()
if row:
package=row['package']
else:
package={"pool_ids_staked":{},"max_epoch":0,"current_pool":"","epochs_staked":0,"total_staked":0,"recent10":{}}
#print("processing records after epoch: ",package['max_epoch'])
pg.cur1_execute("select epoch,amount,delegated_to_pool, delegated_to_ticker, reward_address_details, stake_rewards from stake_history where stake_key=%s and epoch>=%s and forecast=false order by epoch asc",[stake_key,int(package['max_epoch'])])
rows=pg.cur1_fetchall()
recent10={}
for row in rows:
package['recent10'][int(row['epoch'])]=row['delegated_to_pool']
for epoch in list(package['recent10'].keys()):
if int(epoch)<int(row['epoch'])-9:
del package['recent10'][epoch]
package['max_epoch']=max(package['max_epoch'],int(row['epoch']))
if package['max_epoch']==int(row['epoch']):
package['current_pool']=row['delegated_to_pool']
package['total_staked']+=int(row['amount'])
package['epochs_staked']+=1
# to get rewards we will always pull it out of stake details since we need the exact rewards ONLY for this stake.
if str(stake_key+row['delegated_to_pool']) in row['reward_address_details']:
rewards=int(row['reward_address_details'][str(stake_key+row['delegated_to_pool'])]['stakeRewards']) if row['reward_address_details'][str(stake_key+row['delegated_to_pool'])]['stakeRewards'] is not None else 0
else:
rewards=int(row['stake_rewards'])
if row['delegated_to_pool'] not in package['pool_ids_staked']:
package['pool_ids_staked'][(row['delegated_to_pool'])]={"ticker":row['delegated_to_ticker'].strip() if row['delegated_to_ticker'] is not None else "","amount":int(row['amount']),"epochs":1,"rewards":rewards}
else:
package['pool_ids_staked'][(row['delegated_to_pool'])]["amount"]+=int(row['amount'])
package['pool_ids_staked'][(row['delegated_to_pool'])]["rewards"]+=rewards
package['pool_ids_staked'][(row['delegated_to_pool'])]["epochs"]+=1
recent5 = {k: v for k, v in package['recent10'].items() if int(k) > int(package['max_epoch'])-5}
for pool_id in package['pool_ids_staked']:
package['pool_ids_staked'][pool_id]['lifetime_loyalty']=package['pool_ids_staked'][pool_id]['epochs']/package['epochs_staked'] if package['epochs_staked']>0 else 0
package['pool_ids_staked'][pool_id]['recent10_loyalty']=sum(1 for p in package['recent10'].values() if p==pool_id)/len(package['recent10']) if len(package['recent10'])>0 else 0
package['pool_ids_staked'][pool_id]['recent5_loyalty']=sum(1 for p in recent5.values() if p==pool_id)/len(recent5) if len(recent5)>0 else 0
#calculate loyalty scores
pg.cur1_execute("insert into delegator_loyalty (stake_key,package) values(%s,%s) ON CONFLICT ON CONSTRAINT delegator_loyalty_pkey DO UPDATE set package=%s",[stake_key,Json(package),Json(package)])
pg.conn_commit()
aws.dump_s3(package,"stats/loyalty/bykey/"+str(stake_key)[0:4]+"/"+str(stake_key)+".json")
epoch_processing=fb.getKey(baseNetwork+"/epoch_processing")
epoch=epoch_processing['targetEpoch']
block_production_epoch=epoch+1
print("processing block_production_epoch: "+str(block_production_epoch))
# process epoch loyalty and get list of all stake_keys affected
package=processEpochLoyaltySummary(block_production_epoch)
for stake_key in package:
processLoyalty(stake_key)
process3EpochPoolMigrations(block_production_epoch)