forked from fgandila/snapshots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot_processor.py
More file actions
324 lines (277 loc) · 14.2 KB
/
Copy pathsnapshot_processor.py
File metadata and controls
324 lines (277 loc) · 14.2 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
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
#!/usr/bin/env python3
import json
import asyncio
from decimal import Decimal, getcontext
from pathlib import Path
from unwrap_metastaking_tokens import unwrap_metastaking_token,load_state_data,get_metastaking_balance,get_metastaking_nonce
import base64
# Set precision for decimal calculations
getcontext().prec = 50
async def unwrap_xexchange_tokens(xexchange_balances, lp_state, farm_states):
"""
Unwrap XExchange tokens to their UTK equivalent using pre-fetched state data
"""
total_utk_equivalent = 0
for token_identifier, balance in xexchange_balances.items():
if balance == '0' or not balance:
continue
if token_identifier == 'UTKWEGLD-c960d1':
# This is an LP token - unwrap directly using pre-fetched state
if lp_state:
lp_balance_decimal = Decimal(balance)
lp_token_supply = Decimal(lp_state['lpTokenSupply'])
reserve_utk = Decimal(lp_state['reserveFirstToken'])
utk_equivalent = int((lp_balance_decimal / lp_token_supply) * reserve_utk)
else:
utk_equivalent = 0
elif token_identifier == 'UTKUSH-587b46':
utkush_state_path = Path("state/state_pool_UTKUSH-587b46.json")
if utkush_state_path.exists():
with open(utkush_state_path, 'r') as f:
utkush_state = json.load(f)
lp_balance_decimal = Decimal(balance)
lp_token_supply = Decimal(utkush_state['lpTokenSupply'])
reserve_utk = Decimal(utkush_state['reserveFirstToken'])
utk_equivalent = int((lp_balance_decimal / lp_token_supply) * reserve_utk)
else:
# If no state file, we can't unwrap properly
utk_equivalent = 0
elif token_identifier in ['UTKWEGLDF-5b9d50', 'UTKWEGLDFL-082aec', 'UTKWEGLDFL-ba26d2']:
if token_identifier in farm_states and lp_state:
farm_state = farm_states[token_identifier]
farm_balance_decimal = Decimal(balance)
lp_tokens = farm_balance_decimal # 1:1 ratio
lp_token_supply = Decimal(lp_state['lpTokenSupply'])
reserve_utk = Decimal(lp_state['reserveFirstToken'])
utk_equivalent = int((lp_tokens / lp_token_supply) * reserve_utk)
else:
utk_equivalent = 0
else:
utk_equivalent = 0
total_utk_equivalent += utk_equivalent
return total_utk_equivalent
async def unwrap_metastaking_tokens_with_state(metastaking_balances, address, state_data):
"""
Unwrap metastaking tokens to their UTK equivalent using proper token attributes
"""
total_utk_equivalent = 0
fallback_count = 0
for token_identifier, token_data in metastaking_balances.items():
if token_identifier in ['METAUTK-291e0e', 'METAUTKLK-e6a445', 'METAUTKLK-112f52']:
# Get balance and nonce from the new structure
balance = get_metastaking_balance(token_data)
nonce = get_metastaking_nonce(token_data)
if balance and balance != '0':
try:
if nonce:
# Use the proper unwrapping logic with nonce information
unwrap_result = await unwrap_metastaking_token(balance, token_identifier, state_data, address)
# Extract the unwrapped UTK value as integer
utk_equivalent = int(Decimal(unwrap_result['unwrapped_utk']))
total_utk_equivalent += utk_equivalent
else:
# Fallback for old format without nonce - use raw balance as UTK equivalent
# This is not ideal but better than ignoring the tokens completely
total_utk_equivalent += int(Decimal(balance))
except Exception as e:
# Log error for monitoring but continue processing
# For voting power, we can still count the raw balance as a fallback
total_utk_equivalent += int(Decimal(balance))
return total_utk_equivalent
async def calculate_voting_power():
try:
with open('utk_snapshot_raw_data.json', 'r') as f:
holders = json.load(f)
# Load cached state data for unwrapping
lp_state_path = Path('state/state_pool_UTKWEGLD-c960d1.json')
if lp_state_path.exists():
with open(lp_state_path, 'r') as f:
lp_state = json.load(f)
else:
lp_state = None
# Load farm states
farm_states = {}
farm_tokens = ['UTKWEGLDF-5b9d50', 'UTKWEGLDFL-082aec', 'UTKWEGLDFL-ba26d2']
for farm_token in farm_tokens:
farm_state_path = Path(f'state/state_farm_{farm_token}.json')
if farm_state_path.exists():
with open(farm_state_path, 'r') as f:
farm_states[farm_token] = json.load(f)
# Load states
print("Loading state data for unwrapping...")
state_data = load_state_data()
# Load XOXNO supply data
xoxno_supply_data = {}
xoxno_supply_path = Path('xoxno_supply_data.json')
if xoxno_supply_path.exists():
with open(xoxno_supply_path, 'r') as f:
xoxno_data = json.load(f)
# Create a mapping of address -> UTK_SUPPLIED
for supply_entry in xoxno_data.get('supply_data', []):
address = supply_entry['address']
utk_supplied = Decimal(supply_entry['UTK_SUPPLIED'])
xoxno_supply_data[address] = utk_supplied
else:
print("No XOXNO supply data found")
# Process each holder
voting_power_data = []
total_voting_power = 0
processed_metastaking = 0
for i, holder in enumerate(holders):
# Progress tracking for large datasets
if (i + 1) % 10000 == 0:
print(f"Processing... {i + 1}/{len(holders)} holders completed")
address = holder['address']
utk_balance = Decimal(holder['UTK'])
utk_voting_power = int(utk_balance)
sutk_balance = Decimal(holder['SUTK'])
sutk_voting_power = int(sutk_balance * Decimal('1.2')) # 1.2x bonus
guild_balances = holder.get('GUILDS', {})
guild_voting_power = 0
total_guild_balance = 0 # For reporting total raw balance
# Process guild tokens with different multipliers
for token, balance in guild_balances.items():
token_balance = int(Decimal(balance))
total_guild_balance += token_balance # Track total raw balance
if token.startswith('UTKFARM-'):
# UTKFARM tokens get 2x voting power
guild_voting_power += token_balance * 2
elif token.startswith('UTKUNBND-'):
# UTKUNBND tokens get 1x voting power
guild_voting_power += token_balance * 1
else:
# Unknown guild tokens default to 1x voting power
guild_voting_power += token_balance * 1
farm_balances = holder.get('FARMS', {})
# Unwrap XExchange tokens using pre-fetched state data
utk_equivalent = await unwrap_xexchange_tokens(farm_balances, lp_state, farm_states)
# Then apply the 1.2x multiplier to the UTK equivalent
xexchange_voting_power = int(utk_equivalent * 1.2) # 1.2x bonus
metastaking_balances = holder.get('METASTAKING', {})
# Unwrap metastaking tokens using proper nonce-based unwrapping
if metastaking_balances:
processed_metastaking += 1
metastaking_utk_equivalent = await unwrap_metastaking_tokens_with_state(metastaking_balances, address, state_data)
else:
metastaking_utk_equivalent = 0
# Metastaking tokens with SUTK are treated as SUTK voting power (1.2x multiplier)
metastaking_voting_power = int(metastaking_utk_equivalent * 1.2) # 1.2x bonus (same as SUTK)
# Calculate XOXNO voting power (from OTHER.XOXNO field + XOXNO supply data)
other_data = holder.get('OTHER', {})
xoxno_tokens = Decimal(other_data.get('XOXNO', '0'))
# Add XOXNO supply data (UTK supplied to XOXNO Lend)
xoxno_supplied = xoxno_supply_data.get(address, Decimal('0'))
xoxno_supplied_wei = xoxno_supplied * Decimal('1000000000000000000') # Convert to wei
total_xoxno_balance = xoxno_tokens
xoxno_voting_power = int(total_xoxno_balance * Decimal('1.2')) # 1.2x multiplier
# Calculate total voting power
hutk_voting_power = int(Decimal(holder['HUTK']) * Decimal('1.2')) # 1.2x bonus
total_holder_voting_power = (
utk_voting_power +
sutk_voting_power +
hutk_voting_power +
guild_voting_power +
xexchange_voting_power +
metastaking_voting_power +
xoxno_voting_power
)
# Add to total
total_voting_power += total_holder_voting_power
# Calculate raw balance for metastaking (for display purposes)
metastaking_raw_balance = 0
metastaking_raw_balances_clean = {}
for token_id, token_data in metastaking_balances.items():
balance = get_metastaking_balance(token_data)
if balance and balance != '0':
metastaking_raw_balance += int(Decimal(balance))
# Only store the balance, not nonce or full_identifier
metastaking_raw_balances_clean[token_id] = balance
voting_power_record = {
'address': address,
'total_voting_power': str(total_holder_voting_power),
'voting_power_breakdown': {
'utk': {
'raw_balance': holder['UTK'],
'voting_power': str(utk_voting_power),
'multiplier': '1.0x'
},
'sutk': {
'raw_balance': holder['SUTK'],
'voting_power': str(sutk_voting_power),
'multiplier': '1.2x'
},
'hutk': {
'raw_balance': holder['HUTK'],
'voting_power': str(hutk_voting_power),
'multiplier': '1.2x'
},
'guilds': {
'raw_balances': guild_balances,
'total': str(total_guild_balance),
'voting_power': str(guild_voting_power),
'multiplier': 'UTKFARM: 2x, UTKUNBND: 1x'
},
'defi': {
'raw_balances': farm_balances,
'total_raw_balance': str(sum(int(Decimal(balance)) for balance in farm_balances.values())),
'utk_unwrapped': str(utk_equivalent),
'voting_power': str(xexchange_voting_power),
'multiplier': '1.2x'
},
'metastaking': {
'raw_balances': metastaking_raw_balances_clean,
'total_raw_balance': str(metastaking_raw_balance),
'utk_unwrapped': str(metastaking_utk_equivalent),
'voting_power': str(metastaking_voting_power),
'multiplier': '1.2x'
},
'xoxno': {
'xoxno_tokens': str(xoxno_tokens),
'xoxno_supplied': str(xoxno_supplied),
'total_balance': str(total_xoxno_balance),
'voting_power': str(xoxno_voting_power),
'multiplier': '1.2x'
}
},
}
voting_power_data.append(voting_power_record)
print(f"Processed {processed_metastaking} holders with metastaking tokens")
# Sort by voting power (descending)
voting_power_data.sort(key=lambda x: Decimal(x['total_voting_power']), reverse=True)
# Save voting power data
with open('utk_voting_power.json', 'w') as f:
json.dump(voting_power_data, f, indent=2)
if voting_power_data:
min_voting_power = None
for record in reversed(voting_power_data):
if Decimal(record['total_voting_power']) > 0:
min_voting_power = record['total_voting_power']
break
statistics = {
'summary': {
'total_holders': len(voting_power_data),
'holders_with_metastaking': processed_metastaking,
'total_voting_power': str(total_voting_power),
'highest_voting_power': voting_power_data[0]['total_voting_power'],
'lowest_voting_power': min_voting_power
},
}
with open('statistics.json', 'w') as f:
json.dump(statistics, f, indent=2)
return voting_power_data
except Exception as e:
print(f"Error calculating voting power: {e}")
# Log error for debugging but don't expose full traceback in production
return []
async def run():
"""
Main function to calculate voting power
"""
voting_power_data = await calculate_voting_power()
if voting_power_data:
print(f"Snapshot processed successfully!")
print(f"Results saved to: utk_voting_power.json")
else:
print(f"Snapshot processor failed!")
if __name__ == "__main__":
asyncio.run(run())