3

There is same question on stackexchange but the answer to that question does not include the new api on etherscan which, given an address as input, allows you to download source code of that verified smart contract.

But I want to download source code of a random verified contracts without knowing its address.

My ultimate goal is to make folders based on versions of contracts and download 50-100 for each version if available enough. I also want to know how many verified smart contracts are there on etherscan so that i can estimate time of download as only last 500 verified contracts are visible on etherscan.

edit: If I know addresses of verified smart contract then the api will work since i can loop over all addresses. Is there any way to get a list of addresses of all verified smart contracts?

4 Answers 4

0

I think your best chance is to build a scraper to harvest the Ethereum address of random smart contracts and if they are verified, then feed the address into the API to grab the source code.

2

It's Official Etherscan API:

https://docs.etherscan.io/api-endpoints/contracts#get-contract-source-code-for-verified-contract-source-codes

Just make ID, and use API.

Free limit:-

  • 5 calls/second limit
  • Up to 100,000 API calls a day

Cost of Etherscan API: https://etherscan.io/apis

If you want to create your own api for this purpose use puppeteer or selenium or something similar.

There maybe many third party APIs also. You may search on internet.

2
  • How can I convert the flattened source to "normal" source code? May 16, 2023 at 0:49
  • there is not such tool made. if you find somewhere, tell me too.
    – aakash4dev
    May 26, 2023 at 13:46
1

When I tried to scrape using API calls, its working perfectly fine with addresses that have a single .sol file. But for multiple .sol files, the extracted data contains lots of noise.

This is my function:

#function

def get_contract_source_codes(api_key, addresses, url):
    # create an empty list to hold the source code for each contract
    source_codes = []

    # iterate through the addresses and get the source code for each contract
    for address in addresses:
        full_url = f"{url}&address={address}&apikey={api_key}"
        response = requests.get(full_url)
        data = response.json()
        print(data)

        # extract the source code from the response
        source_code = data["result"][0]


        # add the source code to the list
        source_codes.append(source_code)

    # create a pandas DataFrame with the source codes and addresses
    df = pd.DataFrame({"Address": addresses, "Source Code": source_codes})
    return df


#for this address, this function works fine
get_contract_source_codes(api_key, ['0x2189455051A1c1E6190276f84f73Ec6Fb2fe62DF'], url)

#for this address, it contains lots of noise
get_contract_source_codes(api_key, ['00xcc42315f99979fC051Eb7Bee82E869ac41eC514F'], url)
0

Given your last edit I think this would help you

Is there any way to get a list of addresses of all verified smart contracts? Blockquote

This will give you the list of contracts in a csv:

https://etherscan.io/exportData?type=open-source-contract-codes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.