1

I tried etherscan api to get the contract creation code (bytecode for depolying contracts). I tried both getContractABI and getSourceCode, but the return values are not what i am expecting.

I need the cotract creation code as in: https://etherscan.io/address/0xd26114cd6EE289AccF82350c8d8487fedB8A0C07#code

for any contract which i have its address

0

2 Answers 2

3

(Assuming that you don't just want to call web3.eth.getCode(), and you want something web-based... )

Doesn't appear to be supported by the API, as you've found.

Some horrible scrape-y Python:

#!/usr/bin/env python

import requests
from bs4 import BeautifulSoup

BASE_URL = "https://etherscan.io/address/"

def main():
    address = str(input("Please input an address: ").strip())

    url = BASE_URL + address
    page = requests.get(url)
    soup = BeautifulSoup(page.text, 'html.parser')

    print(soup.find("div", { "id": "verifiedbytecode2" }).text)

if __name__ == "__main__":
    main()

Output:

Please input an address: 0xd26114cd6EE289AccF82350c8d8487fedB8A0C07

6003805460a060020a61ffff0219169055600060045560a0604052600860608190527f4f4d47546f6b656e000000000000000000000000000000000000000000000000608090815261005491600591906100c1565b506040805180820190915260038082527f4f4d4700000000000000000000000000000000000000000000000000000000006020909201918252610099916006916100c1565b5060126007555b60038054600160a060020a03191633600160a060020a03161790555b610161565b828054600181600116156101000203166002900490600052602060002090601f0160209...

Note that the contract must have been verified for this data to be available.

I imagine doing something similar in Node with Cheerio/Axios wouldn't be too difficult.

1

You can use the Etherscan API to get the contract creation code (bytecode) for deploying contracts by using the eth_getCode endpoint. This endpoint allows you to retrieve the bytecode at a specific address.

Here's an example API call for eth_getCode:

https://api.etherscan.io/api?module=proxy&action=eth_getCode&address=0xf75e354c5edc8efed9b59ee9f67a80845ade7d0c&tag=latest&apikey=YourApiKeyToken

  • module=proxy specifies that you want to use the proxy module.
  • action=eth_getCode indicates that you want to get the code.
  • address is the address of the contract you're interested in.
  • tag=latest retrieves the latest code at that address.
  • apikey should be replaced with your Etherscan API key token.

Simply replace YourApiKeyToken with your actual API key, and you will receive the bytecode for the specified contract address in the response.

This should provide you with the contract creation code (bytecode) you are looking for. Make sure you have an active API key from Etherscan to access this information.

If you need more details about the contract, such as its ABI or source code, you may want to explore other Etherscan API endpoints or refer to their documentation for additional options.

Here a code example:

import requests

# Replace with your Etherscan API key
api_key = 'YourApiKeyToken'

# Replace with the Ethereum contract address you want to get the bytecode for
contract_address = '0xf75e354c5edc8efed9b59ee9f67a80845ade7d0c'

# Etherscan API URL
url = f'https://api.etherscan.io/api?module=proxy&action=eth_getCode&address={contract_address}&tag=latest&apikey={api_key}'

try:
    # Send a GET request to the Etherscan API
    response = requests.get(url)

    # Check if the request was successful
    if response.status_code == 200:
        # Parse the JSON response
        data = response.json()

        # Check if the API call was successful
        if data['status'] == '1':
            # Get the bytecode
            bytecode = data['result']
            print(f'Contract Bytecode: {bytecode}')
        else:
            print('API call failed. Check your API key and contract address.')
    else:
        print('Request to Etherscan API failed. Please check your network connection or try again later.')
except Exception as e:
    print(f'An error occurred: {str(e)}')

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.