-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculate_token_holder_stats.py
220 lines (152 loc) · 6.47 KB
/
calculate_token_holder_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
import functools
import os
from datetime import datetime, timedelta
from heapq import nlargest
import config
import known_addresses
from manage_balances import BASE_DIRECTORY, get_first_data_timestamp
from util import logging
STORE_DIRECTORY = '/market-data/final/token_holder_stats/'
log = logging.get_custom_logger(__name__, config.LOG_LEVEL)
def calculate_token_holder_stats(token: dict):
symbol = token['symbol']
symbol_file = STORE_DIRECTORY + symbol
os.makedirs(STORE_DIRECTORY, exist_ok=True)
max_time = datetime.utcnow()
max_time = max_time.replace(hour=0, minute=0, second=0, microsecond=0)
stop_processing = False
date_to_process = get_first_data_timestamp(symbol)
date_last_processed = _get_last_processed_date(symbol)
date_to_process = max(date_to_process, date_last_processed + timedelta(days=1))
log.debug('calculate_token_holder_stats for ' + symbol)
if date_to_process >= max_time:
return
with open(symbol_file, 'a') as file:
while not stop_processing:
data = _get_data_to_process(token['symbol'], date_to_process)
exchange_balance = 0
token_contracts_balance = 0
team_balance = 0
remaining_accounts = list()
for datum in data:
if datum[0].lower() in token['token_contracts'] or datum[0].lower() in token['lending_contracts']:
token_contracts_balance += int(datum[1])
elif datum[0].lower() in token['team_accounts']:
team_balance += int(datum[1])
elif datum[0].lower() in known_addresses.exchange_addresses:
exchange_balance += int(datum[1])
else:
remaining_accounts.append({
'account': datum[0],
'balance': int(datum[1]),
})
remaining_accounts.sort(key=lambda element : element['balance'], reverse=True)
top20 = list()
top50 = list()
top100 = list()
top200 = list()
retail = list()
i = 0
for account in remaining_accounts:
if i < 20:
top20.append(account)
elif i < 50:
top50.append(account)
elif i < 100:
top100.append(account)
elif i < 200:
top200.append(account)
else:
retail.append(account)
i += 1
date_string = date_to_process.strftime('%Y-%m-%d')
result = {
'date': date_string,
'token_contracts_balance': token_contracts_balance,
'team_balance': team_balance,
'exchanges_balance': exchange_balance,
'top20': functools.reduce(lambda a, b : a + b['balance'], top20, 0),
'top50': functools.reduce(lambda a, b: a + b['balance'], top50, 0),
'top100': functools.reduce(lambda a, b: a + b['balance'], top100, 0),
'top200': functools.reduce(lambda a, b: a + b['balance'], top200, 0),
'retail': functools.reduce(lambda a, b: a + b['balance'], retail, 0),
}
file.write(result['date'] + ',' + \
str((result['token_contracts_balance'] / pow(10, 18))) + ',' + \
str((result['team_balance'] / pow(10, 18))) + ',' + \
str((result['exchanges_balance'] / pow(10, 18))) + ',' + \
str((result['top20'] / pow(10, 18))) + ',' + \
str((result['top50'] / pow(10, 18))) + ',' + \
str((result['top100'] / pow(10, 18))) + ',' + \
str((result['top200'] / pow(10, 18))) + ',' + \
str((result['retail'] / pow(10, 18))) + '\n')
file.flush()
log.debug('calculate_token_holder_stats for ' + date_string)
date_to_process += timedelta(days=1)
if date_to_process >= max_time:
stop_processing = True
def _get_last_processed_date(symbol):
symbol_file = STORE_DIRECTORY + symbol
last_file_timestamp = '1970-01-01'
if not os.path.exists(symbol_file):
return datetime.utcfromtimestamp(0)
with open(symbol_file, 'r') as file:
for line in file:
line_parts = line.split(',')
last_file_timestamp = line_parts[0]
return datetime.strptime(last_file_timestamp, '%Y-%m-%d')
##
# return [[<holder_address>, <balance>, <token data>], ...]
#
def _get_data_to_process(symbol, date):
try:
with open(os.path.join(BASE_DIRECTORY, symbol, date.strftime('%Y-%m-%d') + '.csv'), 'rt') as file:
return_data = []
for line in file:
return_data.append(line.split(';'))
return return_data
except:
return []
def _get_top_holder(token):
max_time = datetime.utcnow()
max_time = max_time.replace(hour=0, minute=0, second=0, microsecond=0)
date_to_process = get_first_data_timestamp(token['symbol'])
top_holder = list()
stop_processing = False
while not stop_processing:
data = _get_data_to_process(token['symbol'], date_to_process)
token_balances = {}
for line in data:
value = float(line[1])
if value > 1e20:
token_balances[line[0]] = value
daily_top_holder = nlargest(50, token_balances, key=token_balances.get)
top_holder.extend(daily_top_holder)
# for account in top_holder:
# log.debug(account, ":", token_balances.get(account))
date_to_process += timedelta(days=1)
if date_to_process >= max_time:
stop_processing = True
return _remove_duplicates(top_holder)
def _remove_duplicates(seq):
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
def _analyse_data(data, token_holder):
return_data = []
for holder in token_holder:
found = False
for datum in data:
if holder == datum[0]:
return_data.append({
'holder': holder,
'balance': (int(datum[1]) / pow(10, 18)),
})
found = True
break
if not found:
return_data.append({
'holder': holder,
'balance': 0
})
return return_data