0

I have a naive method where I go thru all the transaction receipts of a block and check if there is a contract address property BUT this is too damn slow so I can't use this.

async function getContractsInBlock(latestBlockNumber, contractAddresses) {
    try {
        const blockNumber = 17575860; //test block number that returns a smart contract

        const block = await web3.eth.getBlock(blockNumber);

        const transactions = block.transactions.filter(tx => tx.input !== '0x');

        const blockContractAddresses = [];

        for (const transactionHash of transactions) {
            const receipt = await web3.eth.getTransactionReceipt(transactionHash);
            if (receipt && receipt.contractAddress) {
                blockContractAddresses.push(receipt.contractAddress);
            }
        }
    } catch (error) {
        console.error('Error getting contracts in block:', error);
    }

}

I'm looking into alternative methods such as event logs and filtering by contract creation events; however, when I call the below function on the same block I get an empty array. I'm not too sure why nor what to do moving forward because I need a fast method.

async function getContractCreationAddy(latestBlockNumber) {
    try {

        const fromBlock = web3.utils.toHex(latestBlockNumber);

        const contractCreationEventSignature = web3.utils.sha3('ContractCreated(address)');

        // Filter all logs to match the contract creation event signature
        const filterOptions = {
            fromBlock,
            toBlock: fromBlock,
            topics: [contractCreationEventSignature]
        };
        const logs = await web3.eth.getPastLogs(filterOptions);

        const contractAddresses = logs.map(log => log.address);
    } catch (error) {
        console.error('Error getting contract creation transactions:', error);
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

0 Answers0