1

I'm trying to figure out whether a given address is:

  • a contract
  • an EOA
  • unknown

I know that only contracts can have code. Therefore, provider.getCode(address) returning anything but 0x tells me unambiguously that I'm dealing with a contract's address.

The inverse is true only in regard to the current state of the chain: provider.getCode(address) returning 0x could mean that someone deployed a contract and gave me that contract's address straight away (before that the transaction got included in the blockchain). It could also mean that the address was randomly generated or simply contains a typo (that was not caught by the address's checksum or the address given happens to be all lowercase). Therefore, I do not want to conclude that the address must belong to an EOA based on there being no code deployed to it.

I do not worry about someone actively trying to break the system by deploying a contract of size zero. But I do want to prevent accidental misclassifications.

I know that only EOAs can send (non-internal) transactions. Therefore, the given address ever having sent a transactions tells me that it is an EOA, which I could easily figure out using a block explorer. Ether's documentation on provider.getTransactionCount() states:

Returns the number of transactions address has ever sent, as of blockTag. This value is required to be the nonce for the next transaction from address sent to the network.

However, in reality it seems to return the account's nonce. Therefore, this function cannot be used to tell that the given address is an EOA.

Is this task even possible with ethers? Do I have to involve a block explorer's API?

1 Answer 1

1

(No, this was not intended as a self-answered question. I facepalmed pretty hard.)

The solution is: If the contract's tx count is larger than zero and it does not have code, it must be an EOA because any contract (apart from one deliberately deployed with empty code) present on the given address to raise the address's nonce above zero also fills the address's code.

2
  • 1
    if you call an empty contract with provider.getCode(address), it doesn't return 0x
    – Adam Boudj
    Mar 29, 2022 at 18:29
  • @AdamBoudjemaa Yes, it does. Try it on this contract: etherscan.io/address/… What else do you expect it to return for an empty contract?
    – UTF-8
    Mar 30, 2022 at 18:03

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.