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?