Skip to content

Commit

Permalink
re-enabling virtual with the addition of internal getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Jan 25, 2021
1 parent 7ba26ce commit e87ce61
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 36 deletions.
12 changes: 10 additions & 2 deletions contracts/token/ERC1155/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
* - `account` cannot be the zero address.
*/
function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
require(account != address(0), "ERC1155: balance query for the zero address");
return _balances[id][account];
return _getBalanceOf(account, id);
}

/**
Expand Down Expand Up @@ -359,6 +358,15 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
virtual
{ }

/**
* @dev Internal, non-virtual, getter for tokenByIndex. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getBalanceOf(address account, uint256 id) internal view returns (uint256) {
require(account != address(0), "ERC1155: balance query for the zero address");
return _balances[id][account];
}

function _doSafeTransferAcceptanceCheck(
address operator,
address from,
Expand Down
24 changes: 20 additions & 4 deletions contracts/token/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ contract ERC20 is Context, IERC20 {
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view override returns (uint256) {
return _totalSupply;
function totalSupply() public view virtual override returns (uint256) {
return _getTotalSupply();
}

/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
function balanceOf(address account) public view virtual override returns (uint256) {
return _getBalanceOf(account);
}

/**
Expand Down Expand Up @@ -288,6 +288,22 @@ contract ERC20 is Context, IERC20 {
_decimals = decimals_;
}

/**
* @dev Internal, non-virtual, getter for tokenByIndex. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getTotalSupply() internal view returns (uint256) {
return _totalSupply;
}

/**
* @dev Internal, non-virtual, getter for tokenByIndex. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getBalanceOf(address account) internal view returns (uint256) {
return _balances[account];
}

/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
Expand Down
85 changes: 59 additions & 26 deletions contracts/token/ERC721/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,15 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
/**
* @dev See {IERC721-balanceOf}.
*/
function balanceOf(address owner) public view override returns (uint256) {
require(owner != address(0), "ERC721: balance query for the zero address");

return _holderTokens[owner].length();
function balanceOf(address owner) public view virtual override returns (uint256) {
return _getBalanceOf(owner);
}

/**
* @dev See {IERC721-ownerOf}. Overridable
* @dev See {IERC721-ownerOf}.
*/
function ownerOf(uint256 tokenId) public view virtual override returns (address) {
return _ownerOf(tokenId);
}

/**
* @dev Owner of the token, as stored internally. Not overridable
*/
function _ownerOf(uint256 tokenId) internal view returns (address) {
return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token");
return _getOwnerOf(tokenId);
}

/**
Expand Down Expand Up @@ -169,31 +160,29 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
/**
* @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
*/
function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
return _holderTokens[owner].at(index);
function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
return _getTokenOfOwnerByIndex(owner, index);
}

/**
* @dev See {IERC721Enumerable-totalSupply}.
*/
function totalSupply() public view override returns (uint256) {
// _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds
return _tokenOwners.length();
function totalSupply() public view virtual override returns (uint256) {
return _getTotalSupply();
}

/**
* @dev See {IERC721Enumerable-tokenByIndex}.
*/
function tokenByIndex(uint256 index) public view override returns (uint256) {
(uint256 tokenId, ) = _tokenOwners.at(index);
return tokenId;
function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
return _getTokenByIndex(index);
}

/**
* @dev See {IERC721-approve}.
*/
function approve(address to, uint256 tokenId) public virtual override {
address owner = _ownerOf(tokenId);
address owner = _getOwnerOf(tokenId);
require(to != owner, "ERC721: approval to current owner");

require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
Expand Down Expand Up @@ -298,7 +287,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
*/
function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
require(_exists(tokenId), "ERC721: operator query for nonexistent token");
address owner = _ownerOf(tokenId);
address owner = _getOwnerOf(tokenId);
return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
}

Expand Down Expand Up @@ -361,7 +350,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
* Emits a {Transfer} event.
*/
function _burn(uint256 tokenId) internal virtual {
address owner = _ownerOf(tokenId); // internal owner
address owner = _getOwnerOf(tokenId); // internal owner

_beforeTokenTransfer(owner, address(0), tokenId);

Expand Down Expand Up @@ -392,7 +381,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
* Emits a {Transfer} event.
*/
function _transfer(address from, address to, uint256 tokenId) internal virtual {
require(_ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); // internal owner
require(_getOwnerOf(tokenId) == from, "ERC721: transfer of token that is not own"); // internal owner
require(to != address(0), "ERC721: transfer to the zero address");

_beforeTokenTransfer(from, to, tokenId);
Expand Down Expand Up @@ -429,6 +418,50 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
_baseURI = baseURI_;
}


/**
* @dev Internal, non-virtual, getter for balanceOf. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getBalanceOf(address owner) internal view returns (uint256) {
require(owner != address(0), "ERC721: balance query for the zero address");
return _holderTokens[owner].length();
}

/**
* @dev Internal, non-virtual, getter for ownerOf. Provide an access to the
* stored values in case the public getter in overloaded.
*/
function _getOwnerOf(uint256 tokenId) internal view returns (address) {
return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token");
}

/**
* @dev Internal, non-virtual, getter for tokenOfOwnerByIndex. Provide an
* access to the stored values in case the public getter in overloaded.
*/
function _getTokenOfOwnerByIndex(address owner, uint256 index) internal view returns (uint256) {
return _holderTokens[owner].at(index);
}

/**
* @dev Internal, non-virtual, getter for totalSupply. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getTotalSupply() internal view returns (uint256) {
// _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds
return _tokenOwners.length();
}

/**
* @dev Internal, non-virtual, getter for tokenByIndex. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getTokenByIndex(uint256 index) internal view returns (uint256) {
(uint256 tokenId, ) = _tokenOwners.at(index);
return 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.
Expand Down Expand Up @@ -458,7 +491,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable

function _approve(address to, uint256 tokenId) private {
_tokenApprovals[tokenId] = to;
emit Approval(_ownerOf(tokenId), to, tokenId); // internal owner
emit Approval(_getOwnerOf(tokenId), to, tokenId); // internal owner
}

/**
Expand Down
24 changes: 20 additions & 4 deletions contracts/token/ERC777/ERC777.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ contract ERC777 is Context, IERC777, IERC20 {
/**
* @dev See {IERC777-totalSupply}.
*/
function totalSupply() public view override(IERC20, IERC777) returns (uint256) {
return _totalSupply;
function totalSupply() public view virtual override(IERC20, IERC777) returns (uint256) {
return _getTotalSupply();
}

/**
* @dev Returns the amount of tokens owned by an account (`tokenHolder`).
*/
function balanceOf(address tokenHolder) public view override(IERC20, IERC777) returns (uint256) {
return _balances[tokenHolder];
function balanceOf(address tokenHolder) public view virtual override(IERC20, IERC777) returns (uint256) {
return _getBalanceOf(tokenHolder);
}

/**
Expand Down Expand Up @@ -434,6 +434,22 @@ contract ERC777 is Context, IERC777, IERC20 {
emit Approval(holder, spender, value);
}

/**
* @dev Internal, non-virtual, getter for totalSupply. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getTotalSupply() internal view returns (uint256) {
return _totalSupply;
}

/**
* @dev Internal, non-virtual, getter for balanceOf. Provide an access to
* the stored values in case the public getter in overloaded.
*/
function _getBalanceOf(address tokenHolder) internal view returns (uint256) {
return _balances[tokenHolder];
}

/**
* @dev Call from.tokensToSend() if the interface is registered
* @param operator address operator requesting the transfer
Expand Down

0 comments on commit e87ce61

Please sign in to comment.