-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_transactions.py
475 lines (354 loc) · 18.4 KB
/
manage_transactions.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
import base64
import os
import traceback
from datetime import datetime, timezone, timedelta
import pytz
import config
from provider.terra import Terra
from util import logging
# structure: /terra-data/raw/transactions/<type>/<date>.csv
BASE_DIRECTORY = '/terra-data/v2/raw/transactions'
log = logging.get_custom_logger(__name__, config.LOG_LEVEL)
token = dict()
def update_token_transactions():
"""
fetches all transactions.
:return:
Nothing
"""
os.makedirs(BASE_DIRECTORY, exist_ok=True)
# symbol_dir = BASE_DIRECTORY + symbol
#
# os.makedirs(symbol_dir, exist_ok=True)
max_time = datetime.utcnow()
max_time = max_time.replace(hour=0, minute=0, second=0, microsecond=0)
last_timestamp, last_block, last_hash = _get_last_transaction()
log.debug('starting update from block: ' + str(last_block))
if last_hash:
log.debug('with hash: ' + last_hash)
log.debug('with timestamp: ' + str(last_timestamp))
transactions = Terra.get_transaction(last_block)
_clear_last_block(last_block)
max_time_exceeded = False
while not max_time_exceeded:
log.debug('storing block ' + str(last_block))
# TODO add correct tracking for gas price and taxes
for transaction in transactions:
# last_batch_block = last_block
# last_batch_timestamp = last_timestamp
# last_batch_hash = last_hash
block_number = transaction['block']
timestamp = datetime.utcfromtimestamp(int(transaction['timestamp']))
hash = transaction['txhash']
type = transaction['type']
if timestamp > max_time:
max_time_exceeded = True
break
if type not in token.keys():
token[type] = {
'directory': os.path.join(BASE_DIRECTORY, type.replace('/', '_')),
'file': None,
'filename': None
}
os.makedirs(token[type]['directory'], exist_ok=True)
act_filename = timestamp.strftime('%Y-%m-%d') + '.csv'
if not token[type]['file'] or act_filename != token[type]['filename']:
token[type]['filename'] = act_filename
if token[type]['file']:
token[type]['file'].close()
token[type]['file'] = open(os.path.join(token[type]['directory'], token[type]['filename']), 'a')
# TODO message type cosmos/MsgUnjail (see block 806047)
# TODO message type staking/MsgBeginRedelegate (see block 2846428)
if type == 'distribution/MsgWithdrawDelegationReward':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['delegator'],
transaction['validator'],
transaction['reward_from'],
transaction['delegation_reward'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'distribution/MsgWithdrawValidatorCommission':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['validator'],
transaction['commission_from'],
transaction['commission'],
])
elif type == 'gov/MsgSubmitProposal':
title_encoded = base64.b64encode(transaction['proposal_title'].encode('utf-8'))
text_encoded = base64.b64encode(transaction['proposal_text'].encode('utf-8'))
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['proposer'],
str(transaction['init_deposit_amount']),
transaction['init_deposit_currency'],
transaction['proposal_id'],
title_encoded.decode('utf-8'),
text_encoded.decode('utf-8'),
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'gov/MsgDeposit':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['depositor'],
transaction['proposal_id'],
transaction['amount'],
transaction['currency'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'staking/MsgDelegate':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['delegator'],
transaction['validator'],
transaction['amount'],
transaction['currency'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'staking/MsgUndelegate':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['delegator'],
transaction['validator'],
transaction['amount'],
transaction['currency'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'market/MsgSwap':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['ask_address'],
transaction['ask_amount'],
transaction['ask_currency'],
transaction['bid_address'],
transaction['bid_amount'],
transaction['bid_currency'],
str(transaction['swap_fee_amount']),
transaction['swap_fee_currency'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'staking/MsgEditValidator':
details_encoded = base64.b64encode(transaction['details'].encode('utf-8'))
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['address'],
details_encoded.decode('utf-8'),
transaction['moniker'],
transaction['website'],
transaction['identity'],
transaction['commission_rate'] or '-1',
transaction.get('min_self_delegation') or '',
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'staking/MsgCreateValidator':
details_encoded = base64.b64encode(transaction['details'].encode('utf-8'))
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['pubkey'],
transaction['amount'],
transaction['currency'],
transaction['commission_rate'],
transaction['commission_max_rate'],
transaction['commission_max_change_rate'],
details_encoded.decode('utf-8'),
transaction['moniker'],
transaction['website'],
transaction['identity'],
transaction['min_self_delegation'],
transaction['delegator'],
transaction['validator'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'oracle/MsgExchangeRateVote':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
str(transaction['exchange_rate']),
transaction['currency'],
transaction['feeder'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'oracle/MsgExchangeRatePrevote':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
transaction['feeder'],
transaction['currency'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'oracle/MsgDelegateFeedConsent':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
])
elif type == 'bank/MsgMultiSend':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
str(transaction['amount']),
transaction['currency'],
transaction['from_address'],
transaction['to_address'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
elif type == 'bank/MsgSend':
new_line = ','.join([str(transaction['block']),
str(transaction['timestamp']),
transaction['txhash'],
str(transaction['amount']),
transaction['currency'],
transaction['from_address'],
transaction['to_address'],
str(transaction['tax_amount']),
transaction['tax_currency'],
])
else:
new_line = ''
log.warning('transaction type not known: ' + type)
token[type]['file'].write(new_line + '\n')
last_timestamp = timestamp
# last_block = block_number
last_hash = hash
# log.debug('last block: ' + str(last_batch_block))
# log.debug('last timestamp: ' + str(last_batch_timestamp))
last_block += 1
transactions = Terra.get_transaction(last_block)
# if last_timestamp == last_batch_timestamp and last_block == last_batch_block and last_hash == last_batch_hash:
# break
# last_timestamp = last_batch_timestamp
# last_block = last_batch_block
# last_hash = last_batch_hash
for key in token.keys():
if token[key]['file']:
token[key]['file'].flush()
os.fsync(token[key]['file'].fileno())
token[key]['file'].close()
token[key]['file'] = None
def _clear_last_block(block_number):
directories = [f for f in os.listdir(BASE_DIRECTORY) if os.path.isdir(os.path.join(BASE_DIRECTORY, f))]
for directory in directories:
path = os.path.join(BASE_DIRECTORY, directory)
last_file_timestamp = None
last_file = None
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
# get the file with the highest timestamp
for file in files:
if file.startswith('.'):
continue
filename = file.split('.')[0]
timestamp = datetime.strptime(filename, '%Y-%m-%d')
if not last_file_timestamp or timestamp > last_file_timestamp:
last_file_timestamp = timestamp
last_file = file
if not last_file:
return
log.debug('removing data from the last block')
log.debug('scanning for block number: ' + str(block_number) + ' in directory \'' + directory + '\'')
removed_lines = 0
new_lines = []
with open(os.path.join(path, last_file), 'rt') as file:
for line in file:
line_split = line.split(',')
if str(line_split[0]) != str(block_number):
new_lines.append(line)
else:
removed_lines += 1
file.flush()
file.close()
log.debug('removing number of lines: ' + str(removed_lines))
with open(os.path.join(path, last_file), 'w') as file:
for line in new_lines:
file.write(line)
file.flush()
file.close()
def _get_last_transaction():
last_timestamp = 0
# TODO change back to 0
last_block = 0
last_hash = None
directories = [f for f in os.listdir(BASE_DIRECTORY) if os.path.isdir(os.path.join(BASE_DIRECTORY, f))]
for directory in directories:
path = os.path.join(BASE_DIRECTORY, directory)
last_file_timestamp = None
last_file = None
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
# get the file with the highest timestamp
for file in files:
if file.startswith('.'):
continue
filename = file.split('.')[0]
timestamp = datetime.strptime(filename, '%Y-%m-%d')
if not last_file_timestamp or timestamp > last_file_timestamp:
last_file_timestamp = timestamp
last_file = file
# if we don't have stored data for the given symbol
if not last_file:
return 0, 0, None
# if the file exists, but is empty
if os.stat(os.path.join(path, last_file)).st_size <= 0:
continue
# getting the last line of the file an extract the timestamp
with open(os.path.join(path, last_file), 'rt') as file:
last_line = file.readlines()[-1]
last_line = last_line.split(',')
if last_block is None or last_timestamp < int(last_line[1]):
last_timestamp = int(last_line[1])
last_block = int(last_line[0])
last_hash = last_line[2]
return last_timestamp, last_block, last_hash
def get_first_transaction_timestamp():
last_file_timestamp = None
directories = [f for f in os.listdir(BASE_DIRECTORY) if os.path.isdir(os.path.join(BASE_DIRECTORY, f))]
for dir in directories:
dir = os.path.join(BASE_DIRECTORY, dir)
files = [f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f))]
# get the file with the highest timestamp
for file in files:
if file.startswith('.'):
continue
filename = file.split('.')[0]
timestamp = datetime.strptime(filename, '%Y-%m-%d')
timestamp = timestamp.replace(tzinfo=pytz.UTC)
if not last_file_timestamp or timestamp < last_file_timestamp:
last_file_timestamp = timestamp
print(last_file_timestamp)
return last_file_timestamp
def get_transaction_data(date, type_filter=None):
return_data = []
directories = [f for f in os.listdir(BASE_DIRECTORY) if os.path.isdir(os.path.join(BASE_DIRECTORY, f))]
for dir in directories:
if type_filter and dir not in type_filter:
continue
try:
filename = os.path.join(BASE_DIRECTORY, dir, date.strftime('%Y-%m-%d') + '.csv')
if not os.path.isfile(filename):
continue
with open(filename, 'rt') as file:
for line in file:
data = line.strip().split(',')
data.insert(0, dir)
return_data.append(data)
except:
traceback.print_exc()
return return_data