Search code examples
ethereumetherscan

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


Solution

  • 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.