Jumping into Solidity — The ERC721 Standard (Part 4)

Photo by Rémi Jacquaint on Unsplash

To the code!

function setApprovalForAll(address _operator, bool _approved) external;
function isApprovedForAll(address _owner, address _operator) external view returns (bool) {
return authorised[_owner][_operator];
}
mapping (address => mapping (address => bool)) internal authorised;
authorised[owner][operator]
function isApprovedForAll(address _owner, address _operator) external view returns (bool) {
return authorised[_owner][_operator];
}
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);
function setApprovalForAll(address _operator, bool _approved) external {
emit ApprovalForAll(msg.sender,_operator, _approved);
authorised[msg.sender][_operator] = _approved;
}
function approve(address _approved, uint256 _tokenId) external payable;
function getApproved(uint256 _tokenId) external view returns (address);
mapping (uint256 => address) internal allowance;
function getApproved(uint256 _tokenId) external view returns (address) {
require(isValidToken(_tokenId));
return allowance[_tokenId];
}
function approve(address _approved, uint256 _tokenId)  external{

“Throws unless `msg.sender` is the current NFT owner, or an authorised operator of the current owner.”

address owner = ownerOf(_tokenId);
require( owner == msg.sender                    
|| authorised[owner][msg.sender]
);
emit Approval(owner, _approved, _tokenId);
allowance[_tokenId] = _approved;

The transfer functions

function transferFrom(address _from, address _to, uint256 _tokenId) public {
address owner = ownerOf(_tokenId);
require ( owner == msg.sender
|| allowance[_tokenId] == msg.sender
|| authorised[owner][msg.sender]
);
require(owner == _from);
require(_to != 0x0);
emit Transfer(_from, _to, _tokenId);
owners[_tokenId] = _to;
balances[_from]--;
balances[_to]++;
if(allowance[_tokenId] != 0x0){
delete allowance[_tokenId];
}
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) public{
transferFrom(_from, _to, _tokenId);

When transfer is complete, this function checks if `_to` is a smart contract (code size > 0). If so, it calls `onERC721Received` on `_to` and throws if the return value is not `bytes4(keccak256(“onERC721Received(address,address,uint256,bytes)”))`.

uint32 size;
assembly {
size := extcodesize(_to)
}
if(size > 0){
ERC721TokenReceiver receiver = ERC721TokenReceiver(_to);
require(receiver.onERC721Received(msg.sender,_from,_tokenId,data) == bytes4(keccak256("onERC721Received(address,address,uint256,bytes)")));
}
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external {
safeTransferFrom(_from,_to,_tokenId,"");
}

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