Is it possible to find out in which block a contract was created using web3?
3 Answers
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).
- 1,720
- 2
- 17
- 35
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
- 111
- 2
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.
- 1
blockNumberor any hash at all. – Joël Aug 14 '17 at 06:13