15

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

0xcaff
  • 2,477
  • 1
  • 14
  • 29

3 Answers3

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

Joël
  • 1,720
  • 2
  • 17
  • 35
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

skud
  • 111
  • 2
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.

Orwell
  • 1