Ethereum Standard ERC165 Explained

Leo Zhang
7 min readOct 20, 2019

In smart contract development, a standard called ERC165 often appears when contract to contract interaction is needed. Solidity already provides a way for a contract to call functions from another contract. But why is ERC165 needed?

In this tutorial, I’ll go through a simple example and explain the motivations behind ERC165, and how it works.

What is ERC165 for?

Let’s say we have a simple Store contract that stores a uint256 value which can be queried or updated:

// Store.solpragma solidity 0.5.8;contract Store {
uint256 internal value;
function setValue(uint256 v) external {
value = v;
}
function getValue() external view returns (uint256) {
return value;
}
}

We have another contract — StoreReader, which needs to be able to read the current value of a Store contract in a readStoreValue function. How do we implement this readStoreValue function that takes a Store contract’s address and returns its current value?

In solidity, we can implement it in two steps:

Step 1, define a contract interface StoreInterface which includes the definition of the function to call. We have to name the function getValue so that it matches the function name in the Store contract we are trying to call:

// StoreInterface.sol

--

--