Any way to tell difference between ERC-20 and ERC-721?

I'm currently trying to "categorize" addresses into token types

For a contract address, if name != null then it's a token....

Now for the question. Any sound way to tell the difference between an ERC-20 and ERC-721 (ignoring any others like 1155)?

I don't think looking at contract code is a good solution.

Thanks all!

ERC20 and ERC721 (and others) are definitions of interfaces, this means that if contract A implements the set of methods and events described here, it's a ERC20. If it implements the methods and events described here, it's a ERC721.

To determine if an address implements any kind of interface, you need to parse the contract ABI if available and look for specific sets of methods. in the case the ABI is not available, parsing the contract bytecode is the only option left.

A contract can also implement more than a single implementation. ERC721 is dependent on ERC165, every ERC721 implementation also implements ERC165.

Hmm, maybe you could try using methods which are unique in one standard, for instance ownerOf will only be available in ierc721 (something like

  • define a “mini interface” with ownerOf
  • instantiate it with the target contract
  • call ownerOf, wrapped in a try/catch and don’t forget to treat the “owner of a non-existing id” revert case) ?

Tbc but I don’t think name() is part of standard IERC20 or 721, same as decimal in IERC20 for instance -> you’ll find them in vast majority of token and in oz implementation, but not in the erc itself