1

I know that contract accounts are created by making transactions to zero-account(0x0000000000000000000000000000000000000000), and by executing EVM opcode CREATE with contract code.

How do I get the number of contract account creations in a block with Web3.js?

2 Answers 2

2

While the above answer is sort of correct, it's not fool-proof. A transaction can be sent to '0x0', but it may fail to deploy the contract (because of out of gas issues for example). Without checking the error status of the transaction, you can't tell if the contract was created or not.

To be sure, you have to retrieve each transaction's receipt (eth_getTransactionReciept). If the receipt's contractAddress has a non-zero value, the contract was created at that address.

0
1

There is no function in Web3.js which would directly solve your problem, however you could write a little function to solve it.

In short you should use web3.eth.getBlock(arg), where arg is the number of the block you are interested in. Here, in the transactions field you will have a list of transactions (represented by their hashes) happened in that specific block. Then you need to iterate over this list of transaction hashes and check whether web3.eth.getTransaction(transactionHash).to is null or not. If it is null, then it means that the transaction was a contract creation.

2
  • Can I also catch contract creations by CREATE execution (by non-externaly-owned-accounts), with the way above? Sep 28, 2017 at 10:12
  • 1
    hmmm...not sure to be honest. If there is not generated a transaction hash after the execution of these transactions and they are not included in the blocks explicitly, then I'm afraid you can not track those contract creations. At least not with the above mentioned method. You might be interested in this: ethereum.stackexchange.com/questions/3417/… Sep 28, 2017 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.