I want to know 3 things:

  1. Is an address a smart contract?
  2. Is this smart contract an ERC20 or ERC721 token?
  3. Is the Solidity code public?

For 1 you may use web3.eth.getCode(address) function of Web3 API. For contract addresses it returns contract byte code, while for non-contract addresses it returns something like "0x".

For 3 it depends on what "public" means for you. If you mean whether smart contract has verified source code published at Etherscan.io, then you may use either API call to obtain source code by contract address, or download full list of all contract addresses that has source code verified: https://etherscan.io/apis#contracts .

The 2 is most tricky, because there is no reliable way to know whether smart contract implements particular interface or not, other than analyzing its source code. Though there could be some hints. You may check whether smart contract ever logged Transfer(address indexed, address indexed, uint256) (for ERC-20) or Transfer(address indexed, address indexed, uint256 indexed) (for ERC-721) event, but this way you will not recognize token contracts whose tokens were never transferred yet.

  • 1
    You say that one could, "...analyz[e a smart contract's] source code". First, did you mean to say, "Analyze its BYTE code"? Can you provide more information on that? I've pulled the byte code of various smart contracts, many of which I know to be ERC20. I search the byte code for the encodings of the 'transfer' and 'approve' function (0xa9059cbb and 0x095ea7b3 respectively). In the ERC20 contracts, I find these byte strings. In non-ERC20's I don't. But I'm not confident at all that this is robust. Is it a robust way to tell the difference? May 30 '20 at 1:27
  • 2
    I don't think there is a generic way to find out from contract's byte code, whether the contract is ERC20 token contract or not. May 31 '20 at 13:20
  • Are the 4-byte opcodes present in the byte code? In other words, if the byte code contains all seven or eight required ERC20 4-bytes, then I can 'make an educated guess' that it's an ERC-20. It may not be, but it probably is. Is that correct? Same for ERC 721. Jun 1 '20 at 18:29

It is unclear what layer you want to find out this information on (on-chain or off-chain). In general:

Is an address a smart contract?

This can be checked by seeing if there is associated code at the address.

Is this smart contract ERC20 or ERC721 token?

Off-chain, you can check this by observing the contract on Etherscan. There are also interfaces you can work with that will tell you the same thing.

Is the code public?

The bytecode is public. Technically, you can deconstruct this into Solidity. In some cases, the author publishes the source code. Here is an example of a verified contract on Etherscan where you can see the source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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