What is ERC165 and Why You Should Use It
And why does it seem like every smart contract implements it in some way?
ERC165 or EIP-165 as it was previously known is a standard used by many open-source smart contracts like the contracts that are available from Open Zeppelin or the smart contracts for Aavegotchi.
But what is it exactly? Should you have to implement it yourself? Do we actually need it, and what do we need it for? I’ll explain a bit about the standard and hopefully answer these questions for you.
What is ERC165
ERC165 is a standard to detect and publish what interfaces a smart contract implements. What does this mean exactly? Well, it standardizes how interfaces are identified, how to detect if they implement ERC165 or another interface, and how a contract will publish the interfaces it implements. Let’s see how it roughly works.
Why would we want to implement ERC165? Sometimes, it can be useful to be able to query which specific interfaces a contract implements, and more importantly which version that the smart contract implements.
How Interfaces are Identified
A function within an interface has a function selector. This is a way to check what a function is for the ABI. In this standard, a whole interface is defined as the XOR
of all function selectors. You can see how that works with the following code.
We do not need to remember this code exactly, but it helps get the idea across of function selectors and how a whole interface has a single selector that can be calculated from the functions it implements.
You can run that snippet in Remix to mess around with it a bit and seeing how changes to the interface functions will alter the output of the contract functions.
How a Contract publishes the Interfaces it Implements
We now know how interfaces are identified. Now we need to take a look at how to publish which interfaces we’re actually implementing. First, we need to import IERC165 which will look something like this.
Of course, since this is just an interface
, we still need to implement it in our smart contract itself. A very nice example of this is the ERC721 smart contract from OpenZeppelin.
I’ve removed all unrelated code. We can see that the smart contract imports both IERC721
and IERC721Metadata
alongside ERC165
. We implement all three with the is
keyword at the declaration of the smart contract.
Note that type(interface).interfaceId
returns the same as the interface selector.
Within the smart contract, we override supportsInterface
to return a boolean that checks if the parameter interfaceId
(or interface selector) is the same as one of the implemented contracts.
It also calls super.supportsInterface()
which calls the following code from ERC165. It checks whether or not the supplied interfaceId
is IERC165.
So, if we call supportsInterface
with an interfaceId
, the function in our contract would return a boolean true
if that interfaceId
is implemented, and false
if it isn’t. It will return true
for IERC721
,IERC721Metadata
andIERC165
.
Conclusion
I hope that this article has given you the right information to understand and use the ERC165 standard, and more so, why it might be used.
Thank you so much for reading and have an excellent day.
Support me by supporting Medium and becoming a member. It helps me out a lot, it won’t cost you extra, and you can read as many Medium articles as you like!
Follow me on Twitter and gm.xyz to keep up with me.
Check out the project I’m the dev of here.
Check out my latest NFT collection on Polygon.