[Ethereum] How to check Ethereum smart contract info

accountscontract-debuggingjson-rpcrpctokens

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?

Best Answer

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.