15

Is it possible to find out in which block a contract was created using web3?

3 Answers 3

4

If you have the transaction hash that created the contract, do a web3.eth.getTransactionReceipt(*hash*). The resulting object will contain a blockNumber.

You can make sure that this is the creation hash by checking whether contractAddress in the object is populated with your contract's address (to should be empty).

8
  • 11
    What if you don't have the txhash?
    – 0xcaff
    Aug 14, 2017 at 6:07
  • 1
    is this on the public chain?
    – Joël
    Aug 14, 2017 at 6:07
  • 5
    Yes. I know I can look on a site etherscan to find this information, but I want to do it programatically.
    – 0xcaff
    Aug 14, 2017 at 6:09
  • 1
    What if I'm analyzing contracts I didn't deploy? How does etherscan do it?
    – 0xcaff
    Aug 14, 2017 at 6:13
  • 1
    I meant contracts which were deployed by other parties.
    – 0xcaff
    Aug 14, 2017 at 6:14
1

This is not using web3, but I found it pretty easy to find the block id in a bigquery Public dataset by running this:

SELECT block_number FROM `bigquery-public-data.ethereum_blockchain.contracts` WHERE address = CONTRACT_ADDRESS

0

You can binary search on all blocks for existence of w3.eth.get_code. Given that at the time of this answer the ethereum chain is on block 15581647, it'll take about log2(15581647) = 24 calls to chain node to get to the creation transaction for that contract. The blockNumber field of that transaction will give you the block in which that contract was created.

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.