Squashed commit of the following:
commit fcf35eb806100de300bd9803ce3150dde1ecc424
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 17:16:04 2019 -0300
remove all docsite dependency
commit eeaee9a9d43d70704f6ab17b5126ddbd52b93a50
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 17:15:23 2019 -0300
update solidity-docgen
commit f021ff951829ea0c155186749819403c6b76e803
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 17:05:06 2019 -0300
update docsite script for new setup
commit ff887699d381cfbbe3acf1f1c0de8e22b58480f3
Merge: c938aa1d 84f85a41
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 17 16:46:46 2019 -0300
Merge branch 'master' into antora
commit c938aa1d9ed05ac83a34e2cebd8353f8331ad6d6
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jul 16 18:24:29 2019 -0300
make component name shorter
commit 5bbd6931e02cbbd8864c82655ad0f390ceead5f3
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 20:16:17 2019 -0300
add all info to docs templates
commit 39682c4515d7cf0f0368ed557f50d2709174208a
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 20:13:49 2019 -0300
fix npm docsite script
commit 7ae46bd4a0437abf66150d54d05adf46e3de2cab
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 18:48:05 2019 -0300
convert inline docs to asciidoc
commit cfdfd3dee4b4bf582fde22c8cb6e17a603d6e0c8
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:34:52 2019 -0300
add missing contract names in readmes
commit 15b6a2f9bfb546cf1d3bf4f104278b118bf1b3f4
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:16:47 2019 -0300
fix script path
commit 80d82b909f9460d1450d401f00b3f309da506b29
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:13:53 2019 -0300
update version of solidity-docgen
commit a870b6c607b9c2d0012f8a60a4ed1a1c8b7e8ebd
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 17:03:53 2019 -0300
add nav generation of api ref
commit 069cff4a25b83752650b54b86d85608c2f547e5e
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Wed Jul 10 16:32:14 2019 -0300
initial migration to asciidoc and new docgen version
commit 55216eed0a6551da913c8d1da4b2a0d0d3faa1a8
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 20:39:35 2019 -0300
add basic api doc example
commit 0cbe50ce2173b6d1d9a698329d91220f58822a53
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 19:31:31 2019 -0300
add sidebars
commit 256fc942845307258ac9dc25aace48117fa10f79
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:22:38 2019 -0300
add page titles
commit f4d0effa70e1fc0662729863e8ee72a8821bc458
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:19:41 2019 -0300
add contracts index file
commit b73b06359979f7d933df7f2b283c50cb1c31b2a0
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:14:52 2019 -0300
fix header levels
commit fb57d9b820f09a1b7c04eed1a205be0e45866cac
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:11:47 2019 -0300
switch format to preferred asciidoctor format
commit 032181d8804137332c71534753929d080a31a71f
Author: Francisco Giordano <frangio.1@gmail.com>
Date: Tue Jun 25 15:05:38 2019 -0300
initialize antora component and convert docs to asciidoc
300 lines
12 KiB
Solidity
300 lines
12 KiB
Solidity
pragma solidity ^0.5.0;
|
|
|
|
import "./IERC721.sol";
|
|
import "./IERC721Receiver.sol";
|
|
import "../../math/SafeMath.sol";
|
|
import "../../utils/Address.sol";
|
|
import "../../drafts/Counters.sol";
|
|
import "../../introspection/ERC165.sol";
|
|
|
|
/**
|
|
* @title ERC721 Non-Fungible Token Standard basic implementation
|
|
* @dev see https://eips.ethereum.org/EIPS/eip-721
|
|
*/
|
|
contract ERC721 is ERC165, IERC721 {
|
|
using SafeMath for uint256;
|
|
using Address for address;
|
|
using Counters for Counters.Counter;
|
|
|
|
// Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
|
|
// which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
|
|
bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
|
|
|
|
// Mapping from token ID to owner
|
|
mapping (uint256 => address) private _tokenOwner;
|
|
|
|
// Mapping from token ID to approved address
|
|
mapping (uint256 => address) private _tokenApprovals;
|
|
|
|
// Mapping from owner to number of owned token
|
|
mapping (address => Counters.Counter) private _ownedTokensCount;
|
|
|
|
// Mapping from owner to operator approvals
|
|
mapping (address => mapping (address => bool)) private _operatorApprovals;
|
|
|
|
/*
|
|
* bytes4(keccak256('balanceOf(address)')) == 0x70a08231
|
|
* bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e
|
|
* bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
|
|
* bytes4(keccak256('getApproved(uint256)')) == 0x081812fc
|
|
* bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
|
|
* bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
|
|
* bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
|
|
* bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e
|
|
* bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde
|
|
*
|
|
* => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
|
|
* 0xa22cb465 ^ 0xe985e9c ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd
|
|
*/
|
|
bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;
|
|
|
|
constructor () public {
|
|
// register the supported interfaces to conform to ERC721 via ERC165
|
|
_registerInterface(_INTERFACE_ID_ERC721);
|
|
}
|
|
|
|
/**
|
|
* @dev Gets the balance of the specified address.
|
|
* @param owner address to query the balance of
|
|
* @return uint256 representing the amount owned by the passed address
|
|
*/
|
|
function balanceOf(address owner) public view returns (uint256) {
|
|
require(owner != address(0), "ERC721: balance query for the zero address");
|
|
|
|
return _ownedTokensCount[owner].current();
|
|
}
|
|
|
|
/**
|
|
* @dev Gets the owner of the specified token ID.
|
|
* @param tokenId uint256 ID of the token to query the owner of
|
|
* @return address currently marked as the owner of the given token ID
|
|
*/
|
|
function ownerOf(uint256 tokenId) public view returns (address) {
|
|
address owner = _tokenOwner[tokenId];
|
|
require(owner != address(0), "ERC721: owner query for nonexistent token");
|
|
|
|
return owner;
|
|
}
|
|
|
|
/**
|
|
* @dev Approves another address to transfer the given token ID
|
|
* The zero address indicates there is no approved address.
|
|
* There can only be one approved address per token at a given time.
|
|
* Can only be called by the token owner or an approved operator.
|
|
* @param to address to be approved for the given token ID
|
|
* @param tokenId uint256 ID of the token to be approved
|
|
*/
|
|
function approve(address to, uint256 tokenId) public {
|
|
address owner = ownerOf(tokenId);
|
|
require(to != owner, "ERC721: approval to current owner");
|
|
|
|
require(msg.sender == owner || isApprovedForAll(owner, msg.sender),
|
|
"ERC721: approve caller is not owner nor approved for all"
|
|
);
|
|
|
|
_tokenApprovals[tokenId] = to;
|
|
emit Approval(owner, to, tokenId);
|
|
}
|
|
|
|
/**
|
|
* @dev Gets the approved address for a token ID, or zero if no address set
|
|
* Reverts if the token ID does not exist.
|
|
* @param tokenId uint256 ID of the token to query the approval of
|
|
* @return address currently approved for the given token ID
|
|
*/
|
|
function getApproved(uint256 tokenId) public view returns (address) {
|
|
require(_exists(tokenId), "ERC721: approved query for nonexistent token");
|
|
|
|
return _tokenApprovals[tokenId];
|
|
}
|
|
|
|
/**
|
|
* @dev Sets or unsets the approval of a given operator
|
|
* An operator is allowed to transfer all tokens of the sender on their behalf.
|
|
* @param to operator address to set the approval
|
|
* @param approved representing the status of the approval to be set
|
|
*/
|
|
function setApprovalForAll(address to, bool approved) public {
|
|
require(to != msg.sender, "ERC721: approve to caller");
|
|
|
|
_operatorApprovals[msg.sender][to] = approved;
|
|
emit ApprovalForAll(msg.sender, to, approved);
|
|
}
|
|
|
|
/**
|
|
* @dev Tells whether an operator is approved by a given owner.
|
|
* @param owner owner address which you want to query the approval of
|
|
* @param operator operator address which you want to query the approval of
|
|
* @return bool whether the given operator is approved by the given owner
|
|
*/
|
|
function isApprovedForAll(address owner, address operator) public view returns (bool) {
|
|
return _operatorApprovals[owner][operator];
|
|
}
|
|
|
|
/**
|
|
* @dev Transfers the ownership of a given token ID to another address.
|
|
* Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
|
|
* Requires the msg.sender to be the owner, approved, or operator.
|
|
* @param from current owner of the token
|
|
* @param to address to receive the ownership of the given token ID
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
*/
|
|
function transferFrom(address from, address to, uint256 tokenId) public {
|
|
//solhint-disable-next-line max-line-length
|
|
require(_isApprovedOrOwner(msg.sender, tokenId), "ERC721: transfer caller is not owner nor approved");
|
|
|
|
_transferFrom(from, to, tokenId);
|
|
}
|
|
|
|
/**
|
|
* @dev Safely transfers the ownership of a given token ID to another address
|
|
* If the target address is a contract, it must implement {IERC721Receiver-onERC721Received},
|
|
* which is called upon a safe transfer, and return the magic value
|
|
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
|
|
* the transfer is reverted.
|
|
* Requires the msg.sender to be the owner, approved, or operator
|
|
* @param from current owner of the token
|
|
* @param to address to receive the ownership of the given token ID
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
*/
|
|
function safeTransferFrom(address from, address to, uint256 tokenId) public {
|
|
safeTransferFrom(from, to, tokenId, "");
|
|
}
|
|
|
|
/**
|
|
* @dev Safely transfers the ownership of a given token ID to another address
|
|
* If the target address is a contract, it must implement {IERC721Receiver-onERC721Received},
|
|
* which is called upon a safe transfer, and return the magic value
|
|
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
|
|
* the transfer is reverted.
|
|
* Requires the msg.sender to be the owner, approved, or operator
|
|
* @param from current owner of the token
|
|
* @param to address to receive the ownership of the given token ID
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
* @param _data bytes data to send along with a safe transfer check
|
|
*/
|
|
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public {
|
|
transferFrom(from, to, tokenId);
|
|
require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
|
|
}
|
|
|
|
/**
|
|
* @dev Returns whether the specified token exists.
|
|
* @param tokenId uint256 ID of the token to query the existence of
|
|
* @return bool whether the token exists
|
|
*/
|
|
function _exists(uint256 tokenId) internal view returns (bool) {
|
|
address owner = _tokenOwner[tokenId];
|
|
return owner != address(0);
|
|
}
|
|
|
|
/**
|
|
* @dev Returns whether the given spender can transfer a given token ID.
|
|
* @param spender address of the spender to query
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
* @return bool whether the msg.sender is approved for the given token ID,
|
|
* is an operator of the owner, or is the owner of the token
|
|
*/
|
|
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
|
|
require(_exists(tokenId), "ERC721: operator query for nonexistent token");
|
|
address owner = ownerOf(tokenId);
|
|
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
|
|
}
|
|
|
|
/**
|
|
* @dev Internal function to mint a new token.
|
|
* Reverts if the given token ID already exists.
|
|
* @param to The address that will own the minted token
|
|
* @param tokenId uint256 ID of the token to be minted
|
|
*/
|
|
function _mint(address to, uint256 tokenId) internal {
|
|
require(to != address(0), "ERC721: mint to the zero address");
|
|
require(!_exists(tokenId), "ERC721: token already minted");
|
|
|
|
_tokenOwner[tokenId] = to;
|
|
_ownedTokensCount[to].increment();
|
|
|
|
emit Transfer(address(0), to, tokenId);
|
|
}
|
|
|
|
/**
|
|
* @dev Internal function to burn a specific token.
|
|
* Reverts if the token does not exist.
|
|
* Deprecated, use {_burn} instead.
|
|
* @param owner owner of the token to burn
|
|
* @param tokenId uint256 ID of the token being burned
|
|
*/
|
|
function _burn(address owner, uint256 tokenId) internal {
|
|
require(ownerOf(tokenId) == owner, "ERC721: burn of token that is not own");
|
|
|
|
_clearApproval(tokenId);
|
|
|
|
_ownedTokensCount[owner].decrement();
|
|
_tokenOwner[tokenId] = address(0);
|
|
|
|
emit Transfer(owner, address(0), tokenId);
|
|
}
|
|
|
|
/**
|
|
* @dev Internal function to burn a specific token.
|
|
* Reverts if the token does not exist.
|
|
* @param tokenId uint256 ID of the token being burned
|
|
*/
|
|
function _burn(uint256 tokenId) internal {
|
|
_burn(ownerOf(tokenId), tokenId);
|
|
}
|
|
|
|
/**
|
|
* @dev Internal function to transfer ownership of a given token ID to another address.
|
|
* As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
|
|
* @param from current owner of the token
|
|
* @param to address to receive the ownership of the given token ID
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
*/
|
|
function _transferFrom(address from, address to, uint256 tokenId) internal {
|
|
require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
|
|
require(to != address(0), "ERC721: transfer to the zero address");
|
|
|
|
_clearApproval(tokenId);
|
|
|
|
_ownedTokensCount[from].decrement();
|
|
_ownedTokensCount[to].increment();
|
|
|
|
_tokenOwner[tokenId] = to;
|
|
|
|
emit Transfer(from, to, tokenId);
|
|
}
|
|
|
|
/**
|
|
* @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
|
|
* The call is not executed if the target address is not a contract.
|
|
*
|
|
* This function is deprecated.
|
|
* @param from address representing the previous owner of the given token ID
|
|
* @param to target address that will receive the tokens
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
* @param _data bytes optional data to send along with the call
|
|
* @return bool whether the call correctly returned the expected magic value
|
|
*/
|
|
function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
|
|
internal returns (bool)
|
|
{
|
|
if (!to.isContract()) {
|
|
return true;
|
|
}
|
|
|
|
bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data);
|
|
return (retval == _ERC721_RECEIVED);
|
|
}
|
|
|
|
/**
|
|
* @dev Private function to clear current approval of a given token ID.
|
|
* @param tokenId uint256 ID of the token to be transferred
|
|
*/
|
|
function _clearApproval(uint256 tokenId) private {
|
|
if (_tokenApprovals[tokenId] != address(0)) {
|
|
_tokenApprovals[tokenId] = address(0);
|
|
}
|
|
}
|
|
}
|