IPSProtocol / web3_project_scrapper

1 stars 1 forks source link

python client for explorer #1

Open theexoticman opened 5 months ago

theexoticman commented 5 months ago

create a standard python class that is responsible for the connecting to blockchain explorers such as etherscan for the following features:

here are the endpoints: get contract source code

https://api.etherscan.io/api?module=contract&action=getcontractcreation&contractaddresses=0xB83c27805aAcA5C7082eB45C868d955Cf04C337F,0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45,0xe4462eb568E2DFbb5b0cA2D3DbB1A35C9Aa98aad,0xdAC17F958D2ee523a2206206994597C13D831ec7,0xf5b969064b91869fBF676ecAbcCd1c5563F591d0&apikey=YourApiKeyToken

Get a list of 'Normal' Transactions By Address Returns the list of transactions performed by an address, with optional pagination.

https://api.etherscan.io/api?module=account&action=txlist&address=0xc5102fE9359FD9a28f877a67E36B0F050d81a3CC&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=YourApiKeyToken

theexoticman commented 5 months ago

Sure! Here's a Python class that connects to the etherscan API and provides the requested features:

import requests

class BlockchainExplorer:
    def __init__(self, api_key: str):
        self.api_key = api_key

    def get_contract_source_code(self, address: str):
        url = f"https://api.etherscan.io/api?module=contract&action=getsourcecode&address={address}&apikey={self.api_key}"
        response = requests.get(url)
        return response.json()

    def get_contract_creator_and_creation_tx(self, addresses: str):
        url = f"https://api.etherscan.io/api?module=contract&action=getcontractcreation&contractaddresses={addresses}&apikey={self.api_key}"
        response = requests.get(url)
        return response.json()

    def get_transactions_per_address(self, address: str, start_block: int = 0, end_block: int = 99999999, page: int = 1, offset: int = 10, sort: str = "asc"):
        url = f"https://api.etherscan.io/api?module=account&action=txlist&address={address}&startblock={start_block}&endblock={end_block}&page={page}&offset={offset}&sort={sort}&apikey={self.api_key}"
        response = requests.get(url)
        return response.json()

You can create an instance of the BlockchainExplorer class by providing your Etherscan API key. Then you can call the various methods to retrieve the desired information. For example:

# Create an instance of BlockchainExplorer
explorer = BlockchainExplorer(api_key="YourApiKeyToken")

# Get contract source code
source_code = explorer.get_contract_source_code(address="0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413")
print(source_code)

# Get contract creator and creation tx
creator_info = explorer.get_contract_creator_and_creation_tx(addresses="0xB83c27805aAcA5C7082eB45C868d955Cf04C337F,0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45")
print(creator_info)

# Get list of transactions per address
transactions = explorer.get_transactions_per_address(address="0xc5102fE9359FD9a28f877a67E36B0F050d81a3CC")
print(transactions)

Make sure to replace "YourApiKeyToken" with your actual API key from etherscan.io.

Feel free to modify the class or add more features as needed. Don't forget to handle exception cases and add appropriate error handling.