-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathgen_list_from_raw.py
176 lines (146 loc) · 6.12 KB
/
gen_list_from_raw.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
# -*- coding: utf-8 -*-
"""
Use this script to generate token list in csv/json folder
Workflow:
1. Edit data in raw folder
2. Run `python3 gen_list_from_raw.py -i raw/* -o bad_tokens`
3. Check output files: *.json *.csv
This script needs web3 package.
`pip3 install web3` before use.
"""
from web3 import Web3
import json
import argparse
import copy
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", nargs='+', required=True, help="input file")
ap.add_argument("-o", "--output", required=True, help="final output file")
args = vars(ap.parse_args())
input_files = args["input"]
final_output = args["output"]
with open('token_detail_dict.json', 'r') as f:
TOKEN_DETAIL_DICT = json.load(f)
print("token_detail_dict.json loaded.")
with open('issues.json', 'r') as f:
issue_dict = json.load(f)
print("issues.json loaded.")
csv_header = "addr,category,name,symbol,exchanges,totalSupply,decimals,info\n"
write_csv = True
def export_data(output_file, data_dict):
csv_saved = output_file + ".o.csv"
csv = open("./csv/" + csv_saved, 'w', encoding = 'utf-8')
csv.write(csv_header)
for addr in data_dict:
detail = data_dict[addr]
name, symbol, exchanges, totalSupply, decimals = "", "", "", "", ""
if 'name' in detail:
name = detail['name']
if 'symbol' in detail:
symbol = detail['symbol']
if 'exchanges' in detail:
exs = detail['exchanges']
if 'totalSupply' in detail:
totalSupply = detail['totalSupply']
if 'decimals' in detail:
decimals = detail['decimals']
if 'exchanges' in detail:
exs = detail['exchanges']
for ex in exs:
exchanges += f"@{ex}"
result = f"{addr},{issue_type},{name},{symbol},{exchanges},{totalSupply},{decimals},_\n"
csv.write(result)
csv.close()
print("---\nsave to %s\n---" % csv_saved)
json_saved = output_file + ".o.json"
with open("./json/" + json_saved, 'w') as outfile:
json.dump(data_dict, outfile, sort_keys=True, indent=4)
print("---\nsave to %s\n---" % json_saved)
def export_data_summary(output_file, data_dict):
csv_saved = output_file + ".csv"
csv = open("./" + csv_saved, 'w', encoding = 'utf-8')
csv.write(csv_header)
for addr in data_dict:
detail = data_dict[addr]
name, symbol, exchanges, totalSupply, decimals = "", "", "", "", ""
if 'name' in detail:
name = detail['name']
if 'symbol' in detail:
symbol = detail['symbol']
issues = []
if 'issues' in detail:
issues = detail['issues']
if 'totalSupply' in detail:
totalSupply = detail['totalSupply']
if 'decimals' in detail:
decimals = detail['decimals']
if 'exchanges' in detail:
exs = detail['exchanges']
for ex in exs:
exchanges += f"@{ex}"
issue_list = []
for issue in issues:
issue_list.append(issue_dict[issue])
issue_list.sort()
category = ""
for issue in issue_list:
category += f"[{issue}]"
result = f"{addr},{category},{name},{symbol},{exchanges},{totalSupply},{decimals},_\n"
csv.write(result)
csv.close()
print("---\nsummary save to %s\n---" % csv_saved)
json_saved = output_file + ".json"
with open("./" + json_saved, 'w') as outfile:
json.dump(data_dict, outfile, sort_keys=True, indent=4)
print("---\nsummary save to %s\n---" % json_saved)
cnt = 0
TOP_ONLY_ALL_IN_ONE_DICT = {}
ALL_IN_ONE_DICT = {}
for input_file in input_files:
FINAL_DICT = {}
TOP_ONLY_FINAL_DICT = {}
issue_type = input_file.split('.')[0].replace("raw/", "")
print("issue_type:", issue_type)
with open(input_file) as f:
for line in f:
addr = line.split('_')[0].strip('\n')
if addr == "":
continue
try:
addr = Web3.toChecksumAddress(addr)
except Exception as err:
print(f"Web3.toChecksumAddress failed, check raw input in {input_file}, err = {err}, addr = {addr}")
exit()
if addr in TOKEN_DETAIL_DICT:
token_detail = copy.deepcopy(TOKEN_DETAIL_DICT[addr])
# print("addr in top token:", addr, "detail:", token_detail)
TOP_ONLY_FINAL_DICT[addr] = token_detail
name = token_detail['name']
symbol = token_detail['symbol']
token_detail['info'] = "_"
token_detail['issues'] = {issue_type: True}
cnt += 1
# print(f"{cnt}: {addr},{issue_type},{name},{symbol},_")
FINAL_DICT[addr] = TOP_ONLY_FINAL_DICT[addr]
try:
old_issues = TOP_ONLY_ALL_IN_ONE_DICT[addr]['issues']
TOP_ONLY_ALL_IN_ONE_DICT[addr]['issues'] = {**old_issues, **token_detail['issues']}
except KeyError:
TOP_ONLY_ALL_IN_ONE_DICT[addr] = token_detail
try:
old_issues = ALL_IN_ONE_DICT[addr]['issues']
ALL_IN_ONE_DICT[addr]['issues'] = {**old_issues, **token_detail['issues']}
except KeyError:
ALL_IN_ONE_DICT[addr] = token_detail
else:
FINAL_DICT[addr] = {'issues': {issue_type: True}}
try:
old_issues = ALL_IN_ONE_DICT[addr]['issues']
ALL_IN_ONE_DICT[addr]['issues'] = {**old_issues, **FINAL_DICT[addr]['issues']}
except KeyError:
ALL_IN_ONE_DICT[addr] = {'issues': {issue_type: True}}
FINAL_DICT_SORTED = dict(sorted(FINAL_DICT.items(), key=lambda x: x[0]))
export_data(issue_type, FINAL_DICT_SORTED)
TOP_ONLY_ALL_IN_ONE_DICT_SORTED = dict(sorted(TOP_ONLY_ALL_IN_ONE_DICT.items(), key=lambda x: x[0]))
export_data_summary(final_output + ".top", TOP_ONLY_ALL_IN_ONE_DICT_SORTED)
ALL_IN_ONE_DICT_SORTED = dict(sorted(ALL_IN_ONE_DICT.items(), key=lambda x: x[0]))
export_data_summary(final_output + ".all", ALL_IN_ONE_DICT_SORTED)