Given a valid contract address how can I check if the contract implemented a certain interface i.e Ownable?


ERC165 tackles this problem and has been approved in the meantime, but it cannot be used for older ERCs. For one, ERC20 implementations don't support it (as of Nov 2018, at least OpenZeppelin doesn't). This means that you could try calling the supportsInterface function, but it would revert anyway and you'd rather complicate things.

Nevertheless, here's how it's defined in ERC721:

bytes4 private constant _InterfaceId_ERC721 = 0x80ac58cd;
 * 0x80ac58cd ===
 *   bytes4(keccak256('balanceOf(address)')) ^
 *   bytes4(keccak256('ownerOf(uint256)')) ^
 *   bytes4(keccak256('approve(address,uint256)')) ^
 *   bytes4(keccak256('getApproved(uint256)')) ^
 *   bytes4(keccak256('setApprovalForAll(address,bool)')) ^
 *   bytes4(keccak256('isApprovedForAll(address,address)')) ^
 *   bytes4(keccak256('transferFrom(address,address,uint256)')) ^
 *   bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^
 *   bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)'))

Although it's not guaranteed for all implementations to define the interface id, there's a higher chance it will work in the case of ERC721, given the fact the community agreed on applying ERC165 right from the get-go. If the return value of the query below is true, then it means you have a compliant contract, otherwise just revert the transaction.

// you can call this in your contracts

Also, a useful resource for manually checking the bytes4 of a given method is 4byte.directory


Simply make a transaction using the supposed ABI to the contract and watch the answer. If it succeeds, do not take for assured the interface presence, but test some ancillary supposed methods in order to assess (i.e. if it answer to a totalSupply query, it does not assure you that a real totalSupply() method do exist, but if the result of the method called are consistent, you can use it).

As answered before, there it exists EIP proposal to solve this.

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.