-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
1752 lines (1435 loc) · 65 KB
/
app.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from numpy import hstack
# import sys
# sys.path.append('/home/ubuntu/.local/lib/python3.6/site-packages')
# import numpy
from flask import Flask, render_template, request, send_file, session, g
import os
import traceback
import time
import json
from code.coingecko import Coingecko
from code.signatures import Signatures
from code.chain import Chain
from code.solana import Solana
from code.util import log, ProgressBar, persist, sql_in, normalize_address, is_ethereum, log_error
from code.sqlite import SQLite
from code.fiat_rates import Twelve
# from code.category import Typing
from code.user import User, Import
from code.tax_calc import Calculator
import pickle
import html
import sys,pip
import redis
from code.redis_wrap import Redis
import secrets
import threading
import copy
from collections import defaultdict
import atexit
from dotenv import load_dotenv
FLASK_ENV = 'production'
app = Flask(__name__)
os.environ['debug'] = '0'
os.environ['version'] = '1.42'
os.environ['app_path'] = '/home/ubuntu/hyperboloid'
def init():
os.chdir(os.environ.get('app_path')) if FLASK_ENV == "production" else False
load_dotenv()
log('env check',os.environ.get('api_key_etherscan'),filename='env_check.txt')
@app.route('/')
@app.route('/main')
def main():
init()
address_cookie = request.cookies.get('address')
address = ""
if address_cookie is not None:
# log("address_cookie",address_cookie)
if "|" in address_cookie:
address,_ = address_cookie.split("|")
else:
address = address_cookie
primary = normalize_address(address)#Address(address)
persist(primary)
blockchain_count = len(Chain.CONFIG)
# log('cookie',address,chain_name)
return render_template('main.html', title='Blockchain transactions to US tax form', address=address, blockchain_count=blockchain_count, version=os.environ.get('version'))
@app.route('/services.html')
def services_page():
return render_template('services.html', title='Services we provide', version=os.environ.get('version'))
@app.route('/chains.html')
def chain_support():
init()
chains_support_info = []
support_level_text_map = {10:'High',5:'Medium',3:'Low',0:'None'}
for chain_name in Chain.list(alphabetical=True):
conf = Chain.CONFIG[chain_name]
support_level = conf['support']
support_level_text = support_level_text_map[support_level]
if support_level == 0:
support_level_text = 'Discontinued'
data_source_url = "https://"+conf['scanner']
data_source_name = conf['scanner']
erc1155_support = 0
if '1155_support' in conf:
erc1155_support = conf['1155_support']
balance_token_support = 'Available'
if 'debank_mapping' in conf and conf['debank_mapping'] is None:
balance_token_support = 'Not available'
balance_nft_support = 'Not available'
if 'simplehash_mapping' in conf:
balance_nft_support = 'Available'
cp_availability = 3
if 'blockscout' in conf:
cp_availability = 0
if 'cp_availability' in conf:
cp_availability = conf['cp_availability']
if chain_name == 'Solana':
data_source_url = "https://www.quicknode.com/"
data_source_name = "QuickNode RPC"
balance_nft_support = 'Available'
chains_support_info.append(
{'name':chain_name,
'support_level':support_level,
'support_level_text':support_level_text,
'data_source_name':data_source_name,
'data_source_url': data_source_url,
'cp_availability': support_level_text_map[cp_availability],
'erc1155_support':support_level_text_map[erc1155_support],
'balance_token_support':balance_token_support,
'balance_nft_support': balance_nft_support
})
log('chains_support_info',chains_support_info,filename='chain_support.txt')
return render_template('chains.html', title='Blockchain transactions to US tax form', chains=chains_support_info, version=os.environ.get('version'))
@app.route('/last_update')
def last_update():
init()
address = request.args.get('address')
primary = normalize_address(address)#Address(address)
if primary is None:
persist(primary)
data = {'last_transaction_timestamp': 0,'update_import_needed':False}
else:
# redis = Redis(primary, None)
# qpos = redis.qpos()
# if qpos is not None:
# data = {'queue_position': qpos}
# else:
persist(primary)
user = User(primary)
update_import_needed = False
try:
last = last_update_inner(user)
# update_import_needed = user.check_info('update_import_needed')
data_version = float(user.get_info('data_version'))
log('version comp', data_version,user.version)
# if data_version != user.version:
if user.version - data_version >= 0.1:
update_import_needed = True
except:
last = user.last_db_modification_timestamp
update_import_needed = True
data = {'last_transaction_timestamp':last,'update_import_needed':update_import_needed}
data = json.dumps(data)
return data
def last_update_inner(user):
query = "SELECT max(last_update) FROM user_addresses WHERE address='"+user.address+"'"
rows = user.db.select(query)
log('last_update_inner',rows)
if len(rows) == 0 or rows[0][0] == None:
return 0
return int(rows[0][0])
@app.route('/process')
def process():
init()
address = request.args.get('address')
primary = normalize_address(address)#Address(address)
# chain_name = request.args.get('chain')
uid = request.args.get('uid')
# import_new = int(request.args.get('import_new'))
# U = Users()
# primary = U.lookup(address)
# if primary is None:
# primary = address
# all_previous_addresses = U.all_addresses(primary)
# U.disconnect()
persist(primary)
redis = Redis(primary)
#currently importing transactions for this user -- wait to finish, then recreate from stored
if redis.waitself():
return recreate_data_from_caches(primary)
redis.start()
# progress_bar_update(address, 'Starting', 0)
active_address = None
pb = None
try:
# accepted_chains_str = request.args.get('accepted_chains')
accepted_chains_str = None
user = User(primary, do_logging=False)
all_previous_addresses = list(user.all_addresses.keys())
log('all_previous_addresses 1', all_previous_addresses)
# if len(all_previous_addresses) == 0:
# all_previous_addresses = [primary]
all_chains = {}
for chain_name in Chain.list():
chain = user.chain_factory(chain_name)
all_chains[chain_name] = {'chain': chain, 'import_addresses': [], 'display_addresses': set(), 'is_upload':False}
if 'my account' in all_previous_addresses: #uploads
for chain_name in user.all_addresses['my account']:
chain = user.chain_factory(chain_name,is_upload=True)
all_chains[chain_name] = {'chain': chain, 'import_addresses': [], 'display_addresses': set(), 'is_upload': True}
log('all chains',all_chains)
import_addresses = request.args.get('import_addresses')
log('import_addresses provided',import_addresses)
if import_addresses is not None and import_addresses != '':
if import_addresses == 'all':
if len(all_previous_addresses) == 0:
import_addresses = [primary]
else:
import_addresses = all_previous_addresses
else:
import_addresses = import_addresses.split(",")
# else:
# import_addresses = [primary]
elif len(all_previous_addresses) == 0:
import_addresses = [primary]
else:
import_addresses = []
log('import_addresses processed',import_addresses)
use_previous = True
try:
ac_str = request.args.get('ac_str')
log('ac_str',ac_str)
if ac_str is not None and ac_str != '':
ac_spl = ac_str.split(",")
for entry in ac_spl:
chain_name,address = entry.split(":")
all_chains[chain_name]['display_addresses'].add(normalize_address(address))
use_previous = False
except:
pass
if use_previous:
for address in user.all_addresses:
for chain_name in user.all_addresses[address]:
if user.all_addresses[address][chain_name]['used']:
all_chains[chain_name]['display_addresses'].add(address)
# display_addresses = request.args.get('display_addresses')
# display_addresses = None
# if display_addresses is not None and display_addresses != '':
# display_addresses = display_addresses.split(",")
# else:
# #if no explicit display address request, get the ones displayed previously
# display_addresses = []
# for previous_address in all_previous_addresses:
# for chain_name, chain_data in all_chains.items():
# chain = chain_data['chain']
# if chain.check_validity(previous_address):
# if user.check_address_used(previous_address,chain_name):
# display_addresses.append(previous_address)
# break
#everything that's imported must be displayed
for address in import_addresses:
address = normalize_address(address)
if address not in all_previous_addresses:
all_previous_addresses.append(address)
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
if chain.check_validity(address):
chain_data['display_addresses'].add(address)
# if address not in display_addresses:
# display_addresses.append(address)
# if len(display_addresses) == 0:
# display_addresses = all_previous_addresses
#everything that's displayed must have been previously imported
# relevant_addresses = []
# for address in display_addresses:
# address = normalize_address(address)
# if address not in import_addresses and address not in all_previous_addresses:
# log("appending display address",address,"to import addresses", "previous", all_previous_addresses)
# import_addresses.append(address)
# relevant_addresses.append(normalize_address(address))
#
# user.relevant_addresses = relevant_addresses
# user.all_addresses = all_previous_addresses
all_display_addresses = set()
for chain_name, chain_data in all_chains.items():
chain_data['display_addresses'] = list(chain_data['display_addresses'])
log('display addresses for', chain_name, chain_data['display_addresses'])
for address in chain_data['display_addresses']:
if address not in import_addresses and address not in all_previous_addresses:
import_addresses.append(address)
all_display_addresses.add(address)
all_display_addresses = list(all_display_addresses)
# chain_list = Chain.list()
log('req args',request.args)
log('all_previous_addresses 2',all_previous_addresses)
if 'my account' in import_addresses:
import_addresses.remove('my account')
log('import_addresses',import_addresses)
log('all_chains',all_chains)
import_new = len(import_addresses) > 0
# if import_new:
# display_chains = all_chains
# if accepted_chains_str is None or len(accepted_chains_str) == 0: #not specified -- use previously used chains for these addresses
# display_chains = {}
# for chain_name, chain_data in all_chains.items():
# chain = chain_data['chain']
# for address in display_addresses:
# if chain.check_validity(address):
# if user.check_address_used(address, chain_name):
# display_chains[chain_name] = all_chains[chain_name]
# break
# else:
# accepted_chains = accepted_chains_str.split(",")
# display_chains = {}
# for chain_name in Chain.list():
# if chain_name in accepted_chains:
# display_chains[chain_name] = all_chains[chain_name]
#
# if len(display_chains) == 0:
# display_chains = all_chains
#
# log('display chains', display_chains)
S = Signatures()
# chain = Chain.from_name(chain_name,address_db,address)
pb = ProgressBar(redis)
pb.set('Starting',0)
t = time.time()
user.get_custom_rates()
#
# try:
# Coingecko.init_from_cache(user)
# except:
# log("coingecko cache fail")
# import_new = True
# import_addresses = all_display_addresses
non_fatal_errors = set()
use_derived = False
force_forget_derived = user.check_info('force_forget_derived')
log('force_forget_derived',force_forget_derived)
if import_new:
# user.set_info('update_import_needed',0)
user.set_info('data_version',user.version)
user.start_import(all_chains)
redis.enq(reset=False)
redis.wait(pb=0)
pb.update('Updating FIAT rates',0.1)
user.fiat_rates.download_all_rates()
# chain_sets_to_check = defaultdict(dict)
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
chain_data['addresses_to_check'] = {} #going to send a request to scanner for each address in here
for active_address in import_addresses:
active_address = normalize_address(active_address)
if not chain.is_upload and not chain.check_validity(active_address):
continue
present = user.check_address_present(active_address, chain_name)
if present:
user.set_address_used(active_address, chain_name)
chain_data['import_addresses'].append(active_address)
else:
chain_data['addresses_to_check'][active_address] = False
# chain_sets_to_check[chain_name][active_address] = False
#called in threads, to check in parallel against all scanners
def check_chain_for_addresses(chain_data):
chain = chain_data['chain']
address_dict = chain_data['addresses_to_check']
for active_address in address_dict.keys():
try:
log('checking address present on chain', chain.name, active_address)
present = chain.check_presence(active_address)
log('checked address present on chain',chain.name,active_address,'present?', present)
if present:
address_dict[active_address] = True
except:
user.current_import.add_error(Import.PRESENCE_CHECK_FAILURE, chain=chain, address=active_address, debug_info=traceback.format_exc())
log('failed to check chain',chain.name,'for address',active_address,traceback.format_exc())
chain_data['failure'] = True
return
pb.update('Checking supported chains for your addresses')
threads = []
for chain_name, chain_data in all_chains.items():
if not chain_data['is_upload'] and len(chain_data['addresses_to_check']) > 0:
t = threading.Thread(target=check_chain_for_addresses, args=(chain_data,))
threads.append(t)
t.start()
joined_cnt = 0
for t in threads:
t.join()
joined_cnt += 1
pb.update('Checking supported chains for your addresses: '+str(joined_cnt)+'/'+str(len(threads)), 5. / len(threads))
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
chain.progress_bar = pb
if 'failure' in chain_data:
err = "We were not able to retrieve transactions from " + chain_name + ", " + chain.domain + " might be down or API non-functional. Transactions from " + chain.name + \
" may be missing or outdated."
non_fatal_errors.add(err)
else:
for checked_address in chain_data['addresses_to_check'].keys():
if chain_data['addresses_to_check'][checked_address]:
chain_data['import_addresses'].append(checked_address)
user.set_address_present(checked_address, chain_name)
user.set_address_used(checked_address, chain_name)
log("import addresses per chain",chain_name, chain_data['import_addresses'])
# chain = chain_data['chain']
#
# for active_address in import_addresses:
# active_address = normalize_address(active_address)
# if not chain.check_validity(active_address):
# continue
#
# log('importing address',chain_name,active_address)
#
# pb.update('Checking ' + chain_name + ' for ' + active_address, 5. / (len(all_chains) * len(import_addresses)))
# present = False
# if chain_name not in faulty_chains:
# chain.progress_bar = pb
# # present = user.check_info(chain_name + "_" + active_address + "_presence")
# present = user.check_address_present(active_address,chain_name)
#
# if not present:
# try:
# present = chain.check_presence(active_address)
# log('present?',present)
# except:
# present = False
# faulty_chains.append(chain_name)
# err = "We were not able to retrieve transactions from "+chain_name+", "+chain.domain+" might be down or API non-functional. Transactions from "+chain.name+\
# " may be missing or outdated."
# non_fatal_errors.add(err)
# if present:
# chain_data['import_addresses'].append(active_address)
# user.set_address_present(active_address,chain_name)
# if chain_name in display_chains:
# user.set_address_used(active_address,chain_name)
else:
previous_use = set()
for address in user.all_addresses:
for chain_name in user.all_addresses[address]:
if user.all_addresses[address][chain_name]['used']:
previous_use.add(chain_name + ":" + address)
user.set_address_used(address, chain_name, value=0) #unset all address use
current_use = set()
for chain_name, chain_data in all_chains.items():
for address in chain_data['display_addresses']:
current_use.add(chain_name+":"+address)
user.set_address_used(address, chain_name) #set current address use
if not force_forget_derived:
log("comparing previous use vs current use",str(previous_use),str(current_use))
rows = user.db.select("SELECT id FROM transactions_derived LIMIT 1")
if previous_use == current_use and len(rows) > 0:
use_derived = True
log('import_new',import_new)
log('use_derived',use_derived)
total_request_count = 0
total_request_count_disp = 0
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
if chain.is_upload or chain.discontinued:
continue
addresses = chain_data['import_addresses']
for active_address in addresses:
if chain.check_validity(active_address):
total_request_count += 1
disp_addresses = list(chain_data['display_addresses'])
for active_address in disp_addresses:
if chain.check_validity(active_address):
total_request_count_disp += 1
log('total_request_count',total_request_count,total_request_count_disp)
if import_new or not use_derived:
C = Coingecko(verbose=True)
C.make_contracts_map()
if import_new:
pb.set('Importing transactions',5)
try:
redis.cleanup()
except:
log_error("EXCEPTION trying to cleanup redis", primary)
user.current_import.populate_addresses(user,all_chains)
def threaded_transaction_processing(chain_data):
addresses = chain_data['import_addresses']
chain = chain_data['chain']
for active_address in addresses:
active_address = normalize_address(active_address)
log('checking validity',chain.name,active_address)
if chain.check_validity(active_address):
log('is valid')
try:
transactions = chain.get_transactions(user, active_address, 28. / total_request_count)
except:
log_error("FAILED TO GET TRANSACTIONS FROM "+chain.name+" FOR ADDRESS "+active_address)
user.current_import.add_error(Import.UNKNOWN_ERROR,chain=chain,address=active_address,debug_info=traceback.format_exc())
# chain_data['errors'][active_address] = set(["failed to get transactions, code problem"])
continue
log('retrieved transactions',chain.name,active_address,len(transactions))
chain.correct_transactions(active_address, transactions, 2. / total_request_count)
current_tokens = chain.get_current_tokens(active_address)
# chain_data['transactions'].update(transactions) #PROBLEM IF CROSS-WALLET TRANSFERS! Newly-downloaded overwrite previous. Need to merge transfers by only adding new ones.
for txhash, transaction in transactions.items():
log('txhash proc',txhash)
if txhash not in chain_data['transactions']:
chain_data['transactions'][txhash] = transaction
else:
chain.merge_transaction(transaction,chain_data['transactions'][txhash])
# if len(errors) > 0:
# chain_data['errors'][active_address] = errors
if current_tokens is not None:
chain_data['current_tokens'][active_address] = current_tokens
log("populated current_tokens",chain.name,active_address, len(current_tokens),filename='solana.txt')
else:
log("populated current_tokens - NONE!", chain.name, active_address)
def threaded_covalent(all_chains):
rq_cnt = 0
for chain_name, chain_data in all_chains.items():
if not chain_data['is_upload'] and 'covalent_mapping' in Chain.CONFIG[chain_name]:
rq_cnt += len(chain_data['import_addresses'])
if rq_cnt > 0:
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
if chain.is_upload:
continue
chain.covalent_download(chain_data, pb_alloc=5/float(rq_cnt))
t_covalent = threading.Thread(target=threaded_covalent, args=(all_chains,)) #this asshole is the longest
t_covalent.start()
threads = []
user.load_solana_nfts()
for chain_name, chain_data in all_chains.items():
chain_data['transactions'] = {}
chain_data['current_tokens'] = {}
# chain_data['errors'] = {}
if len(chain_data['import_addresses']) > 0 and not chain_data['is_upload']:
log('calling threaded_transaction_processing', chain_name)
t = threading.Thread(target=threaded_transaction_processing, args=(chain_data,))
threads.append(t)
t.start()
joined_cnt = 0
for t in threads:
t.join()
joined_cnt += 1
def threaded_balances(all_chains):
user.get_thirdparty_data(all_chains, progress_bar=pb) #alloc 5
# pb.set('Loading additional data', 40)
t_balances = threading.Thread(target=threaded_balances, args=(all_chains,))
t_balances.start()
t_balances.join()
t_covalent.join()
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
if chain.is_upload:
continue
chain.covalent_correction(chain_data)
chain.balance_provider_correction(chain_data)
pb.update('Loading coingecko symbols', 0)
try:
C.download_symbols_to_db(drop=True, progress_bar=pb) # alloc 3
except:
log_error("Failed to download coingecko symbols", primary)
pb.update('Storing transactions,',0)
for chain_name, chain_data in all_chains.items():
user.store_transactions(chain_data['chain'], chain_data['transactions'], chain_data['import_addresses'],C)
log('storing transactions', chain_name, len(chain_data['transactions']))
user.store_current_tokens(chain_data['chain'], chain_data['current_tokens'])
for active_address in chain_data['import_addresses']:
active_address = normalize_address(active_address)
user.set_address_update(active_address,chain_name)
user.store_solana_nfts()
pb.set('Loading transactions from database',50)
user.load_addresses()
user.load_tx_counts()
if import_new or not use_derived:
pb.update('Loading transactions')
transactions, _ = user.load_transactions(all_chains, load_derived=True)
log("loaded transactions",len(transactions),filename='derived.txt')
pb.update('Loading known counterparties')
contract_dict, counterparty_by_chain, input_list = user.get_contracts(transactions)
address_db = SQLite('addresses', read_only=True)
for chain_name, chain_data in all_chains.items():
if not chain_data['is_upload'] and len(chain_data['display_addresses']):
pb.update('Loading known counterparties for '+chain_name)
chain_data['chain'].init_addresses(address_db,counterparty_by_chain[chain_name])
address_db.disconnect()
pb.set('Looking up unknown counterparties')
if total_request_count == 0:
total_request_count = total_request_count_disp
def threaded_update_progenitors(chain_name,chain_data,filtered_counterparty_list):
chain = chain_data['chain']
try:
chain_db_writes = chain.update_progenitors(filtered_counterparty_list, 10. / total_request_count) # alloc 10
# all_db_writes.extend(chain_db_writes)
chain_data['progenitor_db_writes'] = chain_db_writes
log('new writes', chain_name, len(chain_db_writes), filename='address_update.txt')
except:
log_error('error updating progenitors', primary, chain_name, traceback.format_exc())
threads = []
for chain_name, chain_data in all_chains.items():
chain = chain_data['chain']
if not chain.blockscout and not chain.is_upload and not chain.use_routescan_backup:
filtered_counterparty_list = chain.filter_progenitors(list(counterparty_by_chain[chain_name]))
log('filtered_counterparty_list', chain_name, filtered_counterparty_list, filename='address_update.txt')
if len(filtered_counterparty_list) > 0:
t = threading.Thread(target=threaded_update_progenitors, args=(chain_name,chain_data,filtered_counterparty_list))
threads.append(t)
t.start()
joined_cnt = 0
for t in threads:
t.join()
joined_cnt += 1
all_db_writes = []
for chain_name, chain_data in all_chains.items():
if 'progenitor_db_writes' in chain_data:
all_db_writes.extend(chain_data['progenitor_db_writes'])
if len(all_db_writes):
insert_cnt = 0
address_db = SQLite('addresses')
for write in all_db_writes:
chain_name, values = write
cn = chain_name.upper().replace(" ","_")
entity = values[-2]
address_to_add = values[0]
rc = address_db.insert_kw(cn + '_addresses', values=values, ignore=(entity == 'unknown'))
if rc > 0:
address_db.insert_kw(cn + '_labels', values=[address_to_add, 'auto'], ignore=True)
insert_cnt += 1
if insert_cnt > 0:
address_db.commit()
log('New addresses added',insert_cnt, filename='address_lookups.txt')
address_db.disconnect()
t = time.time()
log('contract_dict',contract_dict)
S.init_from_db(input_list)
pb.set('Loading coingecko rates', 63)
needed_token_times = user.get_needed_token_times(transactions)
log("needed_token_times",needed_token_times)
C.init_from_db_2(all_chains,needed_token_times, progress_bar=pb)
# C.init_from_db(all_chains, contract_dict, progress_bar=pb) #alloc 17
else:
pb.update('Loading transactions')
transactions, _ = user.load_transactions(all_chains, load_derived=True)
needed_token_times = user.get_needed_token_times(transactions)
try:
C = Coingecko.init_from_cache(user)
for coingecko_id in needed_token_times:
assert coingecko_id in C.rates
except:
C = Coingecko(verbose=True)
pb.set('Loading coingecko symbols', 60)
try:
C.download_symbols_to_db(drop=True, progress_bar=pb) # alloc 3
except:
log_error("Failed to download coingecko symbols", primary)
pb.set('Loading coingecko rates', 63)
C.make_contracts_map()
C.init_from_db_2(all_chains, needed_token_times, progress_bar=pb)
S = None
# if import_new:
# pb.set('Loading coingecko rates', 63)
# needed_token_times = user.get_needed_token_times(transactions)
# C.init_from_db_2(all_chains, needed_token_times, progress_bar=pb)
# else:
# transactions, _ = user.load_transactions(all_chains, load_derived=True)
# try:
# C = Coingecko.init_from_cache(user)
# except:
# C = Coingecko(verbose=True)
# pb.set('Loading coingecko symbols', 60)
# try:
# C.download_symbols_to_db(drop=True, progress_bar=pb) # alloc 3
# except:
# log_error("Failed to download coingecko symbols", primary)
#
# pb.set('Loading coingecko rates', 63)
# needed_token_times = user.get_needed_token_times(transactions)
# C.init_from_db_2(all_chains, needed_token_times, progress_bar=pb)
# S = None
if import_new:
user.finish_import()
current_tokens = user.load_current_tokens(C)
log("loaded current tokens",current_tokens)
# user.load_last_import(all_chains)
if import_new:
redis.deq()
tl = time.time()
log("coingecko initialized",C.initialized)
pb.set('Classifying transactions', 80)
store_derived = import_new or not use_derived
if store_derived:
user.wipe_derived_data()
# if len(transactions) == 0:
# data = {'error': 'We didn\'t find any transactions for this address.'}
# data = json.dumps(data)
# return data
transactions_js = user.transactions_to_log(C,S, transactions,progress_bar=pb, store_derived=store_derived) #alloc 10
log("all transactions", transactions_js)
# T = Typing()
# builtin_types = T.load_builtin_types()
pb.set('Loading custom types', 90)
custom_types = user.load_custom_types()
pb.set('Calculating taxes', 90)
calculator = Calculator(user, C)
calculator.process_transactions(transactions_js, user) #alloc
#process_transactions affects coingecko rates! Need to cache it after, not before.
C.dump(user)
pb.set('Calculating taxes', 95)
calculator.matchup()
pb.set('Calculating taxes', 97)
calculator.cache()
js_file = open('data/users/' + primary + '/transactions.json', 'w', newline='')
js_file.write(json.dumps(transactions_js,indent=2, sort_keys=True))
js_file.close()
info_fields = ['tx_per_page','high_impact_amount','dc_fix_shutup','matchups_visible','fiat','opt_tx_costs','opt_vault_gain','opt_vault_loss']
info = {}
for field in info_fields:
value = user.get_info(field)
if value is not None:
info[field] = value
# address_info = {}
# for address in all_previous_addresses:
# address_info[address] = {'displayed':address in display_addresses,'imported':address in import_addresses}
# for chain_name, chain_data in all_chains.items():
# if 'errors' in chain_data:
# for address, errors in chain_data['errors'].items():
# if len(all_previous_addresses) == 1:
# prefix = "Problem on " + chain_name
# else:
# prefix = "Problem with " + address + " on " + chain_name
# for err in list(errors):
# non_fatal_errors.add(prefix+": "+err)
non_fatal_errors = non_fatal_errors.union(set(user.load_relevant_errors()))
data_version = float(user.get_info('data_version'))
if user.version - data_version >= 0.1:
non_fatal_errors.add('Software has been updated since your previous import. We recommend importing new transactions to enable all the features.')
user.load_import_versions()
data = {'info':info,'transactions':transactions_js,'custom_types':custom_types,
'CA_long':calculator.CA_long,'CA_short':calculator.CA_short,'CA_errors':calculator.errors,'incomes':calculator.incomes,'interest':calculator.interest_payments,'expenses':calculator.business_expenses,
'vaults':calculator.vaults_json()
,'loans':calculator.loans_json(),
'tokens':calculator.tokens_json(),
'non_fatal_errors':list(non_fatal_errors),
# 'address_info':address_info, 'chain_list':list(display_chains.keys()),
'latest_tokens':current_tokens,
'fiat_info':Twelve.FIAT_SYMBOLS,
'all_address_info':user.all_addresses,
'chain_config':Chain.config_json(),
'version':{'software':user.version,'data':data_version}
}
pb.set('Uploading to your browser', 98)
# data = {'placeholder':'stuff'}
user.done()
dump = json.dumps(data)
to_empty_in_cache = ['transactions','CA_long','CA_short','CA_errors','incomes','interest','vaults','loans','tokens']
for entry in to_empty_in_cache:
data[entry] = ''
except:
log("EXCEPTION in process", primary,active_address, traceback.format_exc())
log_error("EXCEPTION in process", primary,active_address, request.args)
data = {'error':'An error has occurred while processing transactions. '
'Please let us know on Discord if you received this message.'}
dump = json.dumps(data)
try:
pb.set('Uploading to your browser', 98)
except:
pass
js_file = open('data/users/' + primary + '/data_cache.json', 'w', newline='')
js_file.write(json.dumps(data, indent=2, sort_keys=True))
js_file.close()
# data = json.dumps(data)
if pb is not None:
pb.set('Uploading to your browser', 100)
redis.finish()
# data.set_cookie('address', address + "|" + chain_name)
return dump
def recreate_data_from_caches(primary):
js_file = open('data/users/' + primary + '/data_cache.json', 'r')
js = js_file.read()
js_file.close()
data = json.loads(js)
if not 'error' in data:
js_file = open('data/users/' + primary + '/transactions.json', 'r')
js = js_file.read()
js_file.close()
data['transactions'] = json.loads(js)
user = User(primary)
calculator = Calculator(user, None)
calculator.from_cache()
data['CA_long'] = calculator.CA_long
data['CA_short'] = calculator.CA_short
data['CA_errors'] = calculator.errors
data['incomes'] = calculator.incomes
data['interest'] = calculator.interest_payments
data['expenses'] = calculator.business_expenses
data['vaults'] = calculator.vaults_json()
data['loans'] = calculator.loans_json()
data['tokens'] = calculator.tokens_json()
data = json.dumps(data)
return data
@app.route('/calc_tax',methods=['GET', 'POST'])
def calc_tax():
init()
address = normalize_address(request.args.get('address'))
# chain_name = request.args.get('chain')
persist(address)
try:
mtm = request.args.get('mtm')
if mtm == 'false':
mtm = False
else:
mtm = True
data = request.get_json()
# log('data',data)
transactions_js = json.loads(data)
# js_file = open('data/users/' + address + '/transactions.json', 'w', newline='')
# js_file.write(json.dumps(transactions_js,indent=2, sort_keys=True))
# js_file.close()
# log('tran0',transactions_js[0])
log("all transactions", transactions_js)
user = User(address)
# address_db = SQLite('addresses')
# chain = Chain.from_name(chain_name, address_db, address)
user.get_custom_rates()
C = Coingecko.init_from_cache(user)
calculator = Calculator(user, C, mtm=mtm)
calculator.process_transactions(transactions_js, user)
calculator.matchup()
calculator.cache()
js = {'CA_long': calculator.CA_long, 'CA_short': calculator.CA_short, 'CA_errors': calculator.errors, 'incomes': calculator.incomes, 'interest': calculator.interest_payments,
'expenses':calculator.business_expenses,'vaults':calculator.vaults_json(),'loans':calculator.loans_json(),'tokens':calculator.tokens_json()}