Created
December 16, 2021 08:56
-
-
Save riordant/d1e5dce1c95b8bca556ac3d8d64e1cfb to your computer and use it in GitHub Desktop.
Download a multifile-verified contract from Etherscan as a simple flattened contract.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from etherscan import Etherscan | |
import os | |
import json | |
from time import sleep | |
# setup | |
ethscan = Etherscan('YOUR_API_KEY_HERE') | |
# NOTE: must be a multi-verified contract to work. Staked Aave used as example here | |
address = "0x4da27a545c0c5b758a6ba100e3a049001de870f5" | |
print('getting multi-file data for ' + str(address) + '...') | |
# get source from etherscan | |
etherscan_result = ethscan.get_contract_source_code(address) | |
# get source code section | |
source_code = etherscan_result[0]['SourceCode'] | |
# remove outer brackets and loads into json | |
source_code = json.loads(source_code[1:-1]) | |
source_code_file = open(os.getcwd() + '/' + address + '.sol', "w") | |
source_code_contracts = source_code['sources'] | |
for contract_name in source_code_contracts: | |
source_code_file.write("// " + contract_name + "\n\n") | |
source_code_file.write(source_code_contracts[contract_name]['content'] + "\n\n") | |
source_code_file.close() | |
print('Done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment