|
|
|
pragma solidity ^0.6.0;
|
|
|
|
|
|
|
|
import "../../GSN/Context.sol";
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
import "./IERC721.sol";
|
|
|
|
import "./IERC721Metadata.sol";
|
|
|
|
import "./IERC721Enumerable.sol";
|
|
|
|
import "./IERC721Receiver.sol";
|
|
|
|
import "../../introspection/ERC165.sol";
|
|
|
|
import "../../math/SafeMath.sol";
|
|
|
|
import "../../utils/Address.sol";
|
|
|
|
import "../../utils/Counters.sol";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title ERC721 Non-Fungible Token Standard basic implementation
|
|
|
|
* @dev see https://eips.ethereum.org/EIPS/eip-721
|
|
|
|
*/
|
|
|
|
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Token name
|
|
|
|
string private _name;
|
|
|
|
|
|
|
|
// Token symbol
|
|
|
|
string private _symbol;
|
|
|
|
|
|
|
|
// Optional mapping for token URIs
|
|
|
|
mapping(uint256 => string) private _tokenURIs;
|
|
|
|
|
|
|
|
// Base URI
|
|
|
|
string private _baseURI;
|
|
|
|
|
|
|
|
// Mapping from owner to list of owned token IDs
|
|
|
|
mapping(address => uint256[]) private _ownedTokens;
|
|
|
|
|
|
|
|
// Mapping from token ID to index of the owner tokens list
|
|
|
|
mapping(uint256 => uint256) private _ownedTokensIndex;
|
|
|
|
|
|
|
|
// Array with all token ids, used for enumeration
|
|
|
|
uint256[] private _allTokens;
|
|
|
|
|
|
|
|
// Mapping from token id to position in the allTokens array
|
|
|
|
mapping(uint256 => uint256) private _allTokensIndex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bytes4(keccak256('name()')) == 0x06fdde03
|
|
|
|
* bytes4(keccak256('symbol()')) == 0x95d89b41
|
|
|
|
* bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd
|
|
|
|
*
|
|
|
|
* => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f
|
|
|
|
*/
|
|
|
|
bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bytes4(keccak256('totalSupply()')) == 0x18160ddd
|
|
|
|
* bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59
|
|
|
|
* bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7
|
|
|
|
*
|
|
|
|
* => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63
|
|
|
|
*/
|
|
|
|
bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63;
|
|
|
|
|
|
|
|
constructor (string memory name, string memory symbol) public {
|
|
|
|
_name = name;
|
|
|
|
_symbol = symbol;
|
|
|
|
|
|
|
|
// register the supported interfaces to conform to ERC721 via ERC165
|
|
|
|
_registerInterface(_INTERFACE_ID_ERC721);
|
|
|
|
_registerInterface(_INTERFACE_ID_ERC721_METADATA);
|
|
|
|
_registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 override 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 override returns (address) {
|
|
|
|
address owner = _tokenOwner[tokenId];
|
|
|
|
require(owner != address(0), "ERC721: owner query for nonexistent token");
|
|
|
|
|
|
|
|
return owner;
|
|
|
|
}
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Gets the token name.
|
|
|
|
* @return string representing the token name
|
|
|
|
*/
|
|
|
|
function name() public view override returns (string memory) {
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Gets the token symbol.
|
|
|
|
* @return string representing the token symbol
|
|
|
|
*/
|
|
|
|
function symbol() public view override returns (string memory) {
|
|
|
|
return _symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Returns the URI for a given token ID. May return an empty string.
|
|
|
|
*
|
|
|
|
* If the token's URI is non-empty and a base URI was set (via
|
|
|
|
* {_setBaseURI}), it will be added to the token ID's URI as a prefix.
|
|
|
|
*
|
|
|
|
* Reverts if the token ID does not exist.
|
|
|
|
*/
|
|
|
|
function tokenURI(uint256 tokenId) public view override returns (string memory) {
|
|
|
|
require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
|
|
|
|
|
|
|
|
string memory _tokenURI = _tokenURIs[tokenId];
|
|
|
|
|
|
|
|
// Even if there is a base URI, it is only appended to non-empty token-specific URIs
|
|
|
|
if (bytes(_tokenURI).length == 0) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
// abi.encodePacked is being used to concatenate strings
|
|
|
|
return string(abi.encodePacked(_baseURI, _tokenURI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Returns the base URI set via {_setBaseURI}. This will be
|
|
|
|
* automatically added as a preffix in {tokenURI} to each token's URI, when
|
|
|
|
* they are non-empty.
|
|
|
|
*/
|
|
|
|
function baseURI() public view returns (string memory) {
|
|
|
|
return _baseURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Gets the token ID at a given index of the tokens list of the requested owner.
|
|
|
|
* @param owner address owning the tokens list to be accessed
|
|
|
|
* @param index uint256 representing the index to be accessed of the requested tokens list
|
|
|
|
* @return uint256 token ID at the given index of the tokens list owned by the requested address
|
|
|
|
*/
|
|
|
|
function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
|
|
|
|
require(index < balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
|
|
|
|
return _ownedTokens[owner][index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Gets the total amount of tokens stored by the contract.
|
|
|
|
* @return uint256 representing the total amount of tokens
|
|
|
|
*/
|
|
|
|
function totalSupply() public view override returns (uint256) {
|
|
|
|
return _allTokens.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Gets the token ID at a given index of all the tokens in this contract
|
|
|
|
* Reverts if the index is greater or equal to the total number of tokens.
|
|
|
|
* @param index uint256 representing the index to be accessed of the tokens list
|
|
|
|
* @return uint256 token ID at the given index of the tokens list
|
|
|
|
*/
|
|
|
|
function tokenByIndex(uint256 index) public view override returns (uint256) {
|
|
|
|
require(index < totalSupply(), "ERC721Enumerable: global index out of bounds");
|
|
|
|
return _allTokens[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 virtual override {
|
|
|
|
address owner = ownerOf(tokenId);
|
|
|
|
require(to != owner, "ERC721: approval to current owner");
|
|
|
|
|
|
|
|
require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
|
|
|
|
"ERC721: approve caller is not owner nor approved for all"
|
|
|
|
);
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
_approve(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 override returns (address) {
|
|
|
|
require(_exists(tokenId), "ERC721: approved query for nonexistent token");
|
|
|
|
|
|
|
|
return _tokenApprovals[tokenId];
|
|
|
|
}
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 operator operator address to set the approval
|
|
|
|
* @param approved representing the status of the approval to be set
|
|
|
|
*/
|
|
|
|
function setApprovalForAll(address operator, bool approved) public virtual override {
|
|
|
|
require(operator != _msgSender(), "ERC721: approve to caller");
|
|
|
|
|
|
|
|
_operatorApprovals[_msgSender()][operator] = approved;
|
|
|
|
emit ApprovalForAll(_msgSender(), operator, approved);
|
|
|
|
}
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 override returns (bool) {
|
|
|
|
return _operatorApprovals[owner][operator];
|
|
|
|
}
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 virtual override {
|
|
|
|
//solhint-disable-next-line max-line-length
|
|
|
|
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
|
|
|
|
|
|
|
|
_transfer(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 virtual override {
|
|
|
|
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 _msgSender() 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 virtual override {
|
|
|
|
require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
|
|
|
|
_safeTransfer(from, to, tokenId, _data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Safely transfers the ownership of a given token ID to another address
|
|
|
|
* If the target address is a contract, it must implement `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 _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
|
|
|
|
_transfer(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));
|
|
|
|
}
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Internal function to safely mint a new token.
|
|
|
|
* Reverts if the given token ID already exists.
|
|
|
|
* If the target address is a contract, it must implement `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.
|
|
|
|
* @param to The address that will own the minted token
|
|
|
|
* @param tokenId uint256 ID of the token to be minted
|
|
|
|
*/
|
|
|
|
function _safeMint(address to, uint256 tokenId) internal virtual {
|
|
|
|
_safeMint(to, tokenId, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Internal function to safely mint a new token.
|
|
|
|
* Reverts if the given token ID already exists.
|
|
|
|
* If the target address is a contract, it must implement `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.
|
|
|
|
* @param to The address that will own the minted token
|
|
|
|
* @param tokenId uint256 ID of the token to be minted
|
|
|
|
* @param _data bytes data to send along with a safe transfer check
|
|
|
|
*/
|
|
|
|
function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
|
|
|
|
_mint(to, tokenId);
|
|
|
|
require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 virtual {
|
|
|
|
require(to != address(0), "ERC721: mint to the zero address");
|
|
|
|
require(!_exists(tokenId), "ERC721: token already minted");
|
|
|
|
|
|
|
|
_beforeTokenTransfer(address(0), to, tokenId);
|
|
|
|
|
|
|
|
_addTokenToOwnerEnumeration(to, tokenId);
|
|
|
|
_addTokenToAllTokensEnumeration(tokenId);
|
|
|
|
|
|
|
|
_tokenOwner[tokenId] = to;
|
|
|
|
_ownedTokensCount[to].increment();
|
|
|
|
|
|
|
|
emit Transfer(address(0), to, tokenId);
|
|
|
|
}
|
Rename ERC interfaces to I prefix (#1252)
* rename ERC20 to IERC20
* move ERC20.sol to IERC20.sol
* rename StandardToken to ERC20
* rename StandardTokenMock to ERC20Mock
* move StandardToken.sol to ERC20.sol, likewise test and mock files
* rename MintableToken to ERC20Mintable
* move MintableToken.sol to ERC20Mintable.sol, likewise test and mock files
* rename BurnableToken to ERC20Burnable
* move BurnableToken.sol to ERC20Burnable.sol, likewise for related files
* rename CappedToken to ERC20Capped
* move CappedToken.sol to ERC20Capped.sol, likewise for related files
* rename PausableToken to ERC20Pausable
* move PausableToken.sol to ERC20Pausable.sol, likewise for related files
* rename DetailedERC20 to ERC20Detailed
* move DetailedERC20.sol to ERC20Detailed.sol, likewise for related files
* rename ERC721 to IERC721, and likewise for other related interfaces
* move ERC721.sol to IERC721.sol, likewise for other 721 interfaces
* rename ERC721Token to ERC721
* move ERC721Token.sol to ERC721.sol, likewise for related files
* rename ERC721BasicToken to ERC721Basic
* move ERC721BasicToken.sol to ERC721Basic.sol, likewise for related files
* rename ERC721PausableToken to ERC721Pausable
* move ERC721PausableToken.sol to ERC721Pausable.sol
* rename ERC165 to IERC165
* move ERC165.sol to IERC165.sol
* amend comment that ERC20 is based on FirstBlood
* fix comments mentioning IERC721Receiver
7 years ago
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 virtual {
|
|
|
|
address owner = ownerOf(tokenId);
|
|
|
|
|
|
|
|
_beforeTokenTransfer(owner, address(0), tokenId);
|
|
|
|
|
|
|
|
// Clear metadata (if any)
|
|
|
|
if (bytes(_tokenURIs[tokenId]).length != 0) {
|
|
|
|
delete _tokenURIs[tokenId];
|
|
|
|
}
|
|
|
|
|
|
|
|
_removeTokenFromOwnerEnumeration(owner, tokenId);
|
|
|
|
// Since tokenId will be deleted, we can clear its slot in _ownedTokensIndex to trigger a gas refund
|
|
|
|
_ownedTokensIndex[tokenId] = 0;
|
|
|
|
|
|
|
|
_removeTokenFromAllTokensEnumeration(tokenId);
|
|
|
|
|
|
|
|
// Clear approvals
|
|
|
|
_approve(address(0), tokenId);
|
|
|
|
|
|
|
|
_ownedTokensCount[owner].decrement();
|
|
|
|
_tokenOwner[tokenId] = address(0);
|
|
|
|
|
|
|
|
emit Transfer(owner, address(0), 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 _transfer(address from, address to, uint256 tokenId) internal virtual {
|
|
|
|
require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
|
|
|
|
require(to != address(0), "ERC721: transfer to the zero address");
|
|
|
|
|
|
|
|
_beforeTokenTransfer(from, to, tokenId);
|
|
|
|
|
|
|
|
_removeTokenFromOwnerEnumeration(from, tokenId);
|
|
|
|
_addTokenToOwnerEnumeration(to, tokenId);
|
|
|
|
|
|
|
|
// Clear approvals
|
|
|
|
_approve(address(0), tokenId);
|
|
|
|
|
|
|
|
_ownedTokensCount[from].decrement();
|
|
|
|
_ownedTokensCount[to].increment();
|
|
|
|
|
|
|
|
_tokenOwner[tokenId] = to;
|
|
|
|
|
|
|
|
emit Transfer(from, to, tokenId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Internal function to set the token URI for a given token.
|
|
|
|
*
|
|
|
|
* Reverts if the token ID does not exist.
|
|
|
|
*
|
|
|
|
* TIP: If all token IDs share a prefix (for example, if your URIs look like
|
|
|
|
* `https://api.myproject.com/token/<id>`), use {_setBaseURI} to store
|
|
|
|
* it and save gas.
|
|
|
|
*/
|
|
|
|
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
|
|
|
|
require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
|
|
|
|
_tokenURIs[tokenId] = _tokenURI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Internal function to set the base URI for all token IDs. It is
|
|
|
|
* automatically added as a prefix to the value returned in {tokenURI}.
|
|
|
|
*/
|
|
|
|
function _setBaseURI(string memory baseURI_) internal virtual {
|
|
|
|
_baseURI = baseURI_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Gets the list of token IDs of the requested owner.
|
|
|
|
* @param owner address owning the tokens
|
|
|
|
* @return uint256[] List of token IDs owned by the requested address
|
|
|
|
*/
|
|
|
|
function _tokensOfOwner(address owner) internal view returns (uint256[] storage) {
|
|
|
|
return _ownedTokens[owner];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
|
|
|
|
* The call is not executed if the target address is not a contract.
|
|
|
|
*
|
|
|
|
* @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)
|
|
|
|
private returns (bool)
|
|
|
|
{
|
|
|
|
if (!to.isContract()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// solhint-disable-next-line avoid-low-level-calls
|
|
|
|
(bool success, bytes memory returndata) = to.call(abi.encodeWithSelector(
|
|
|
|
IERC721Receiver(to).onERC721Received.selector,
|
|
|
|
_msgSender(),
|
|
|
|
from,
|
|
|
|
tokenId,
|
|
|
|
_data
|
|
|
|
));
|
|
|
|
if (!success) {
|
|
|
|
if (returndata.length > 0) {
|
|
|
|
// solhint-disable-next-line no-inline-assembly
|
|
|
|
assembly {
|
|
|
|
let returndata_size := mload(returndata)
|
|
|
|
revert(add(32, returndata), returndata_size)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
revert("ERC721: transfer to non ERC721Receiver implementer");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bytes4 retval = abi.decode(returndata, (bytes4));
|
|
|
|
return (retval == _ERC721_RECEIVED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _approve(address to, uint256 tokenId) private {
|
|
|
|
_tokenApprovals[tokenId] = to;
|
|
|
|
emit Approval(ownerOf(tokenId), to, tokenId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Private function to add a token to this extension's ownership-tracking data structures.
|
|
|
|
* @param to address representing the new owner of the given token ID
|
|
|
|
* @param tokenId uint256 ID of the token to be added to the tokens list of the given address
|
|
|
|
*/
|
|
|
|
function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
|
|
|
|
_ownedTokensIndex[tokenId] = _ownedTokens[to].length;
|
|
|
|
_ownedTokens[to].push(tokenId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Private function to add a token to this extension's token tracking data structures.
|
|
|
|
* @param tokenId uint256 ID of the token to be added to the tokens list
|
|
|
|
*/
|
|
|
|
function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
|
|
|
|
_allTokensIndex[tokenId] = _allTokens.length;
|
|
|
|
_allTokens.push(tokenId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
|
|
|
|
* while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
|
|
|
|
* gas optimizations e.g. when performing a transfer operation (avoiding double writes).
|
|
|
|
* This has O(1) time complexity, but alters the order of the _ownedTokens array.
|
|
|
|
* @param from address representing the previous owner of the given token ID
|
|
|
|
* @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
|
|
|
|
*/
|
|
|
|
function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
|
|
|
|
// To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
|
|
|
|
// then delete the last slot (swap and pop).
|
|
|
|
|
|
|
|
uint256 lastTokenIndex = _ownedTokens[from].length.sub(1);
|
|
|
|
uint256 tokenIndex = _ownedTokensIndex[tokenId];
|
|
|
|
|
|
|
|
// When the token to delete is the last token, the swap operation is unnecessary
|
|
|
|
if (tokenIndex != lastTokenIndex) {
|
|
|
|
uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
|
|
|
|
|
|
|
|
_ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
|
|
|
|
_ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deletes the contents at the last position of the array
|
|
|
|
_ownedTokens[from].pop();
|
|
|
|
|
|
|
|
// Note that _ownedTokensIndex[tokenId] hasn't been cleared: it still points to the old slot (now occupied by
|
|
|
|
// lastTokenId, or just over the end of the array if the token was the last one).
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Private function to remove a token from this extension's token tracking data structures.
|
|
|
|
* This has O(1) time complexity, but alters the order of the _allTokens array.
|
|
|
|
* @param tokenId uint256 ID of the token to be removed from the tokens list
|
|
|
|
*/
|
|
|
|
function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
|
|
|
|
// To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
|
|
|
|
// then delete the last slot (swap and pop).
|
|
|
|
|
|
|
|
uint256 lastTokenIndex = _allTokens.length.sub(1);
|
|
|
|
uint256 tokenIndex = _allTokensIndex[tokenId];
|
|
|
|
|
|
|
|
// When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
|
|
|
|
// rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
|
|
|
|
// an 'if' statement (like in _removeTokenFromOwnerEnumeration)
|
|
|
|
uint256 lastTokenId = _allTokens[lastTokenIndex];
|
|
|
|
|
|
|
|
_allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
|
|
|
|
_allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
|
|
|
|
|
|
|
|
// Delete the contents at the last position of the array
|
|
|
|
_allTokens.pop();
|
|
|
|
|
|
|
|
_allTokensIndex[tokenId] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Hook that is called before any token transfer. This includes minting
|
|
|
|
* and burning.
|
|
|
|
*
|
|
|
|
* Calling conditions:
|
|
|
|
*
|
|
|
|
* - when `from` and `to` are both non-zero, `from`'s `tokenId` will be
|
|
|
|
* transferred to `to`.
|
|
|
|
* - when `from` is zero, `tokenId` will be minted for `to`.
|
|
|
|
* - when `to` is zero, `from`'s `tokenId` will be burned.
|
|
|
|
* - `from` and `to` are never both zero.
|
|
|
|
*
|
|
|
|
* To learn more about hooks, head to xref:ROOT:using-hooks.adoc[Using Hooks].
|
|
|
|
*/
|
|
|
|
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { }
|
|
|
|
}
|