Jumping into Solidity — The ERC721 Standard (Part 2)

“A displayed content of a toolkit displayed showing tools like hammer, axe, box cutter, iron and flashlight” by Todd Quackenbush on Unsplash

Interfaces and the ERC165 Standard

“Every ERC-721 compliant contract must implement the ERC721 and ERC165 interfaces”

function balanceOf(address _owner) external view returns (uint256);

“What?!? Another ERC Standard?? How deep does this rabbit hole go?”

interface ERC165 {
/// @notice Query if a contract implements an interface
/// @param interfaceID The interface identifier, as
/// specified in ERC-165
/// @dev Interface identification is specified in
/// ERC-165. This function uses less than 30,000 gas.
/// @return `true` if the contract implements `interfaceID`
/// and `interfaceID` is not 0xffffffff, `false` otherwise
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
function balanceOf(address _owner) external view returns (uint256){
//...
};
this.balanceOf.selector
bytes4(keccak256("balanceOf(address)"))
interfaceID = this.function1.selector ^ this.function2.selector ^ this.function3.selector;
contract CheckERC165 is ERC165 {

mapping (bytes4 => bool) internal supportedInterfaces;
function supportsInterface(bytes4 interfaceID) external view returns (bool){
return supportedInterfaces[interfaceID];
}
constructor() public {
supportedInterfaces[this.supportsInterface.selector] = true;
}

The ERC721 Interfaces

“Mutability guarantees are, in order weak to strong: payable, implicit nonpayable, view, and pure.”

function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))
contract ValidReceiver is ERC721TokenReceiver {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4){
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
}
contract InvalidReceiver is ERC721TokenReceiver {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4){
return bytes4(keccak256("some invalid return data"));
}
}

Wrapping up

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store