Ethereum Standard ERC165 Explained – Leo Zhang

0 407


// 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;
}
}
// StoreInterface.solpragma solidity 0.5.8;interface StoreInterface {
function getValue() external view returns (uint256);
}
// StoreReader.solimport “./StoreInterface.sol”;contract StoreReader {
function readStoreValue(address store)
external view returns (uint256) {
return StoreInterface(store).getValue();
}
}
// ERC165/ERC165.solpragma solidity 0.5.8;interface ERC165 {
function supportsInterface(bytes4 interfaceID)
external view returns (bool);
}
// Store.solpragma solidity 0.5.8;import "./StoreInterface.sol";
import “./ERC165/ERC165.sol”;
contract Store is ERC165, StoreInterface {
uint256 internal value;
function setValue(uint256 v) external {
value = v;
}
function getValue() external view returns (uint256) {
return value;
}
function supportsInterface(bytes4 interfaceId) external view returns (bool) {
return interfaceId == 0x01ffc9a7 ||
interfaceId == 0x75b24222;
}
}
// contracts/StoreReader.solpragma solidity 0.5.8;import "./StoreInterface.sol";contract StoreReader is StoreInterfaceId {
function getStoreValue(address store) external view returns (uint256) {
if (ERC165(storeAddress).supportsInterface(0x75b24222)) {
return store.getValue();
}
revert(“Doesn’t support StoreInterface”)
}
}
// StoreInterface.sol
pragma solidity 0.5.8;
contract StoreInterfaceId {
bytes4 internal constant STORE_INTERFACE_ID = 0x75b24222;
}
contract StoreInterface is StoreInterfaceId {
function getValue() external view returns (uint256);
function setValue(uint256 v) external;
}
// StoreReader.solpragma solidity 0.5.8;import "./StoreInterface.sol";
import "./ERC165/ERC165Query.sol";
contract StoreReader is StoreInterfaceId, ERC165Query {
StoreInterface store;
constructor (address storeAddress) public {
require(doesContractImplementInterface(
storeAddress, STORE_INTERFACE_ID),
"Doesn't support StoreInterface");
store = StoreInterface(storeAddress);
}
function readStoreValue() external view returns (uint256) {
return store.getValue();
}
}
// StoreInterfaceIdpragma solidity 0.5.8;contract StoreInterfaceId {
// StoreInterface.getValue.selector ^
// StoreInterface.setValue.selector
bytes4 internal constant STORE_INTERFACE_ID = 0x75b24222;
}
contract StoreInterface is StoreInterfaceId {
function getValue() external view returns (uint256);
function setValue(uint256 v) external;
}
// Selector.solpragma solidity 0.5.8;import "./StoreInterface.sol";contract Selector {
// 0x75b24222
function calcStoreInterfaceId() external pure returns (bytes4) {
StoreInterface i;
return i.getValue.selector ^ i.setValue.selector;
}
}
// Selector.solpragma solidity 0.5.8;import "./StoreInterface.sol";contract Selector {
// 0x20965255
function getValueSelector() external pure returns (bytes4) {
StoreInterface i;
return i.getValue.selector;
}
}

You might also like

Pin It on Pinterest

Share This

Share this post with your friends!

WhatsApp chat