JR_
JR_

Reputation: 193

Get list of recent added/deployed smart contracts

Is there a way to retrieve the most recent added/deployed smart contracts to the ethereum chain?

Thanks a lot for any advice.

Regards,

JR

Upvotes: 2

Views: 2266

Answers (2)

JR_
JR_

Reputation: 193

Thanks to the help of Petr Hejda I created the following Python code:

import sys
import time
from web3 import Web3


def connect():
    # infura API key
    API_KEY = "INFURA_API_KEY"

    # change endpoint to mainnet or ropsten or any other of your account
    url = f"https://mainnet.infura.io/v3/{API_KEY}"

    w3 = Web3(Web3.HTTPProvider(url))
    # res = w3.isConnected()
    # print(res)
    return w3

def get_latest_block(connection):
    return connection.eth.get_block('latest')

def get_latest_block_id(block_information):
    return block_information['number']

def search_new_contracts(connection, block_information):
    block_transactions = block_information['transactions']
    # print(block_transactions)

    for transaction in block_transactions:
        transaction_data = connection.eth.getTransaction(transaction)

        if transaction_data['to'] == "":
            print(f"Contract creation transaction:  {transaction_data}")

    print("Block searching done")

if __name__ == "__main__":

    current_block_number = sys.maxsize

    connection = connect()

    while True:

        latest_block = get_latest_block(connection)
        latest_block_id = get_latest_block_id(latest_block)

        if current_block_number is not latest_block_id:
            print(f'New block detected: {latest_block_id}')
            search_new_contracts(connection, latest_block)
            current_block_number = latest_block_id

Problem is that it is slow so I have to see if I can speed things up.

Upvotes: 0

Petr Hejda
Petr Hejda

Reputation: 43481

Newly deployed contract addresses are not directly available through the node RPC API (and its wrappers such as ethersjs and web3js), but you can build a script that

  1. Subscribes to newly published blocks
  2. Loops through transaction receipts on the block
  3. And searches for contractAddress property of the transaction receipt

The contractAddress property returns null if the transaction does not deploy a contract (most transactions) or an address if the transaction deploys a contract.


The above mentioned approach only retrieves contracts that were deployed directly - by sending a transaction with empty to field and data field containing the contract bytecode.

It does not retrieve contracts that were deployed using an internal transaction. For example it would not catch the following Hello contract deployment (that is deployed by executing the deployHello() function).

pragma solidity ^0.8;

contract Hello {}

contract HelloFactory {
    event Deployed(address);

    function deployHello() external {
        // deploys a new instance of the `Hello` contract to a new address
        address deployedTo = address(
            new Hello()
        );
        emit Deployed(deployedTo);
    }
}

If you want to retrieve deployments of these contracts as well, then you'd need to debug each newly produced block and search for the create and create2 opcodes (each of them deploys a new contract, they take different input arguments).


So overall, it's not a simple task but it's doable.

It's generally discouraged to recommend any specific 3rd party APIs and tools here at StackOverflow. But I'm guessing that there are already some existing services that do all of this on their backend, and are able to return the aggregated list of newly deployed contracts as a result.

Upvotes: 3

Related Questions