I was trying to find the creation block number using contract address , is there is a way to find creation block number when a contract address is deployed using web3.py?
1 Answer
In ethers.js, you just need to be able to get the transaction hash value, you can get the result by the following code
const result = await provider.getTransactionReceipt(txHash)
The result should be like this
{
to: '0x55d398326f99059fF775485246999027B3197955',
from: '0x25dce14C8A77D71BFF84346dDb4e87a912d21424',
contractAddress: null,
transactionIndex: 0,
gasUsed: BigNumber { _hex: '0xad22', _isBigNumber: true },
logsBloom: '0x00000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000200000000000000000000000000000020000000000000080000000020000000000000000000000000000000000000000000000000000000000000100000000200000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xa688cb46a56b92de47c3786ff6ba26a0cbb1b5365ccbe1708ee62e9ba555d66f',
transactionHash: '0x3c36c5b2418cb43d1b461a51a2c97fb7126c92677c79a47a3b27d16aab7851e7',
logs: [
{
transactionIndex: 0,
blockNumber: 18265542,
transactionHash: '0x3c36c5b2418cb43d1b461a51a2c97fb7126c92677c79a47a3b27d16aab7851e7',
address: '0x55d398326f99059fF775485246999027B3197955',
topics: [Array],
data: '0x0000000000000012725dd1d243aba0e75fe645cc4873f9e65afe688c928e1f21',
logIndex: 0,
blockHash: '0xa688cb46a56b92de47c3786ff6ba26a0cbb1b5365ccbe1708ee62e9ba555d66f'
}
],
blockNumber: 18265542,
confirmations: 1,
cumulativeGasUsed: BigNumber { _hex: '0xad22', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x02540be400', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
I think the "blockNumber" inside is the result is what you want :)
Update:
If you want to query by contract address only, you can first use this code to iterate through all the transactions in the contract and take the first transaction hash to know the block when the contract was created.
const ethers = require('ethers')
let address = YOUR_CONTRACT_ADDRESS;
let etherscanProvider = new ethers.providers.EtherscanProvider();
etherscanProvider.getHistory(address).then((history) => {
history.forEach((tx) => {
console.log(tx);
})
});`
By the way,this code is only available for the node provided by Etherscan :)
-
hey, actually i want the block_number result with the help of contract address not with the help of transaction hash Commented Jun 3, 2022 at 10:41
-
Sorry for not reading your question clearly, I have updated it. Commented Jun 3, 2022 at 10:52
-
okay thanks, but i need the solution in python for this Commented Jun 3, 2022 at 10:59