-
Notifications
You must be signed in to change notification settings - Fork 0
/
etherscan_parser.py
45 lines (40 loc) · 1.74 KB
/
etherscan_parser.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
import json
import requests
from termcolor import cprint
class Parser(object):
"""Etherscan Parser used to fetch verified contract source code"""
def __init__(self, address, network, api_key):
super(Parser, self).__init__()
self.address = address
self.network = network
self.api_key = api_key
self.contract_name = None
self.compiler_version = None
if(network == "mainnet"):
self.etherscan_url = "api.etherscan.io"
elif(network == "goerli"):
self.etherscan_url = "api-goerli.etherscan.io"
elif(network == "arbi-main"):
self.etherscan_url = "api.arbiscan.io"
def get_contract(self):
url = "https://%s/api?module=contract&action=getsourcecode&address=%s&apikey=%s" % (self.etherscan_url, self.address, self.api_key)
data = json.loads(requests.get(url).content)["result"][0]
# Check if the contract is verified
if(data["ABI"] == "Contract source code not verified"):
cprint("[ERROR] Contract not verified","red",attrs=["bold"])
exit(1)
# Check if the contract is a proxy
if(int(data["Proxy"])):
cprint("[ERROR] Contract is a proxy.. I'll fix that asap I swear. Expect bugs :(","red",attrs=["bold"])
# exit(1)
self.contract_name = data["ContractName"]
self.compiler_version = data["CompilerVersion"]
# Dirty tweak to manage one-file contract & multiple files contracts
try:
json_result = json.loads(data["SourceCode"][1:][:-1])
json_result["multifile"] = True
except:
# Yeah that's ugly af, you can judge me here (but it works hehe)
json_result = json.loads("""{"sources": { "%s": { "content": %s }}}""" % (self.contract_name, json.dumps(data["SourceCode"].replace('"','\"')).replace('\\r\\n','\\n')))
json_result["multifile"] = False
return json_result