Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a3526ac
Rebase ERC721._update on top of next-v5
Amxx Apr 27, 2023
1ed8f9e
use __unsafe_increaseBalance to react to batch minting
Amxx Jun 21, 2023
7ec3435
Apply suggestions from code review
Amxx Jun 21, 2023
e2fdbac
fix lint
Amxx Jun 21, 2023
e9f03bd
Exclude address(0) in ERC721._isApprovedOrOwner
frangio Jun 30, 2023
78c280b
Merge branch 'master' into refactor/erc721-update-fnPointer
Amxx Jun 30, 2023
1cc7f54
Merge remote-tracking branch 'upstream' into refactor/erc721-update-f…
Amxx Jul 3, 2023
c7303ec
fix lint
Amxx Jul 3, 2023
54cb3ca
Merge branch 'master' into refactor/erc721-update-fnPointer
Amxx Jul 3, 2023
562ddf5
implement hybrid _update
Amxx Jul 5, 2023
0bb98cb
Merge branch 'master' into feature/Governor-storage
Amxx Jul 7, 2023
5ab254c
lint
Amxx Jul 7, 2023
bd0c52e
refactor constraint into an optionalChecks bitmap
Amxx Jul 11, 2023
1a95520
replace constraints with a simple operator check
Amxx Jul 11, 2023
7e9d024
Apply suggestions from code review
Amxx Jul 12, 2023
16f2f15
remove _isApproedOrOwner in favor of _isApproved + refactor _checkOnE…
Amxx Jul 12, 2023
2558c8f
change _increaseBalance type to uint128
Amxx Jul 12, 2023
de570d0
allow using approve/_approve to clean approval
Amxx Jul 12, 2023
7121ff7
Merge branch 'erc721-approve-0' into refactor/erc721-update-fnPointer
Amxx Jul 12, 2023
b973d98
changesets
Amxx Jul 12, 2023
e4b0e72
use whenNotPaused in ERC721Pausable
Amxx Jul 12, 2023
4516803
make the safe function without a data field non virtual
Amxx Jul 12, 2023
7c3f161
Update .changeset/eighty-lemons-shake.md
frangio Jul 12, 2023
9ba0120
Format _increaseBalance NatSpec
ernestognw Jul 13, 2023
1081508
Lint
ernestognw Jul 13, 2023
fb4d951
Apply suggestions from code review
Amxx Jul 13, 2023
d7a6aaf
remove _exists
Amxx Jul 13, 2023
4c25b48
Merge branch 'refactor/erc721-update-fnPointer' of https://github.com…
Amxx Jul 13, 2023
20048ca
Changes suggested in the PR discussions
Amxx Jul 13, 2023
e996ba4
add ERC721 specific details in the 'How to upgrade from 4.x' section …
Amxx Jul 13, 2023
b29e573
rename from → previousOwner
Amxx Jul 13, 2023
328b16b
Authorised → Authorized
Amxx Jul 13, 2023
08da709
refactor _checkAuhtorized
Amxx Jul 13, 2023
12f63b3
add test
Amxx Jul 13, 2023
81aca96
Update CHANGELOG.md
frangio Jul 13, 2023
d037530
Apply suggestions from code review
frangio Jul 13, 2023
5ce49a4
remove unnecessary solhint annotation
frangio Jul 13, 2023
a023cad
wrap long line
frangio Jul 13, 2023
caabbf3
improve warnings and notes
frangio Jul 13, 2023
ca32b45
fix _safeTransfer docs
frangio Jul 13, 2023
b982e2a
Update ERC721.behavior.js
Amxx Jul 14, 2023
f404802
Update ERC721.sol
Amxx Jul 14, 2023
20bb47f
Update contracts/token/ERC721/ERC721.sol
Amxx Jul 14, 2023
a475ffa
Update ERC721.sol
Amxx Jul 14, 2023
e26d5c0
Update IERC721.sol
Amxx Jul 14, 2023
2897abc
Update ERC721.sol
Amxx Jul 14, 2023
52923d1
coverage for internal _transfer and _safeTransfer
Amxx Aug 4, 2023
42e17ee
mint
Amxx Aug 4, 2023
c2e1a55
fix comments _isApproved -> _isAuthorized
frangio Aug 9, 2023
a036284
extend warning for _isAuthorized
frangio Aug 9, 2023
1e4f353
add comment to _approve
frangio Aug 9, 2023
7b26030
Update contracts/token/ERC721/ERC721.sol
frangio Aug 9, 2023
7249414
Update contracts/token/ERC721/ERC721.sol
frangio Aug 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor constraint into an optionalChecks bitmap
  • Loading branch information
Amxx committed Jul 11, 2023
commit bd0c52e34a5503d30d089f18fb0611c1a9cd5f77
8 changes: 4 additions & 4 deletions contracts/mocks/token/ERC721ConsecutiveEnumerableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ contract ERC721ConsecutiveEnumerableMock is ERC721Consecutive, ERC721Enumerable
}

function _update(
address from,
address to,
uint256 tokenId
) internal virtual override(ERC721Consecutive, ERC721Enumerable) {
super._update(from, to, tokenId);
uint256 tokenId,
bytes32 optionalChecks
) internal virtual override(ERC721Consecutive, ERC721Enumerable) returns (address) {
return super._update(to, tokenId, optionalChecks);
}

// solhint-disable-next-line func-name-mixedcase
Expand Down
8 changes: 4 additions & 4 deletions contracts/mocks/token/ERC721ConsecutiveMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ contract ERC721ConsecutiveMock is ERC721Consecutive, ERC721Pausable, ERC721Votes
}

function _update(
address from,
address to,
uint256 tokenId
) internal virtual override(ERC721Consecutive, ERC721Pausable, ERC721Votes) {
return super._update(from, to, tokenId);
uint256 tokenId,
bytes32 optionalChecks
) internal virtual override(ERC721Consecutive, ERC721Pausable, ERC721Votes) returns (address) {
return super._update(to, tokenId, optionalChecks);
}

// solhint-disable-next-line func-name-mixedcase
Expand Down
80 changes: 31 additions & 49 deletions contracts/token/ERC721/ERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {IERC721Errors} from "../../interfaces/draft-IERC6093.sol";
abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Errors {
using Strings for uint256;

bytes32 internal constant CONSTRAINT_MINTED = bytes32(uint256(1) << 0);
bytes32 internal constant CONSTRAINT_NOT_MINTED = bytes32(uint256(1) << 1);
bytes32 internal constant CONSTRAINT_SPENDER_APPROVED_OR_OWNER = bytes32(uint256(1) << 2);

// Token name
string private _name;

Expand Down Expand Up @@ -155,7 +159,7 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
if (to == address(0)) {
revert ERC721InvalidReceiver(address(0));
}
address owner = _updateWithConstraints(to, tokenId, _constraintApprovedOrOwner);
address owner = _update(to, tokenId, CONSTRAINT_MINTED | CONSTRAINT_SPENDER_APPROVED_OR_OWNER);
if (owner != from) {
revert ERC721IncorrectOwner(from, tokenId, owner);
}
Expand Down Expand Up @@ -263,31 +267,35 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
* @dev Transfers `tokenId` from its current owner to `to`, or alternatively mints (or burns) if the current owner
* (or `to`) is the zero address.
*
* The `constraints` argument is used to specify constraints, and eventually revert. For example this can be used
* to ensure that the current owner is what was expected.
* The `optionalChecks` argument is used to specify constraints, that the caller would want to be checked as part
* of the update.
*
* Emits a {Transfer} event.
*/
function _updateWithConstraints(
function _update(
address to,
uint256 tokenId,
function(address, address, uint256) view constraints
bytes32 optionalChecks
) internal virtual returns (address) {
address from = _ownerOf(tokenId);
constraints(from, to, tokenId);
_update(from, to, tokenId);
return from;
}

/**
* @dev Transfers `tokenId` from its current owner to `to`, or alternatively mints (or burns) if the current owner
* (or `to`) is the zero address.
*
* All customizations to transfers, mints, and burns should be done by overriding this function.
*
* Emits a {Transfer} event.
*/
function _update(address from, address to, uint256 tokenId) internal virtual {
// Perform optional checks
if (optionalChecks & CONSTRAINT_MINTED != 0 && from == address(0)) {
revert ERC721NonexistentToken(tokenId);
}

if (optionalChecks & CONSTRAINT_NOT_MINTED != 0 && from != address(0)) {
revert ERC721InvalidSender(address(0));
}

if (optionalChecks & CONSTRAINT_SPENDER_APPROVED_OR_OWNER != 0) {
address spender = _msgSender();
if (from != spender && !isApprovedForAll(from, spender) && getApproved(tokenId) != spender) {
revert ERC721InsufficientApproval(_msgSender(), tokenId);
}
}

// Execute the update
if (from != address(0)) {
delete _tokenApprovals[tokenId];
unchecked {
Expand All @@ -304,6 +312,8 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
_owners[tokenId] = to;

emit Transfer(from, to, tokenId);

return from;
}

/**
Expand All @@ -322,7 +332,7 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
if (to == address(0)) {
revert ERC721InvalidReceiver(address(0));
}
_updateWithConstraints(to, tokenId, _constraintNotMinted);
_update(to, tokenId, CONSTRAINT_NOT_MINTED);
}

/**
Expand All @@ -337,7 +347,7 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
* Emits a {Transfer} event.
*/
function _burn(uint256 tokenId) internal {
_updateWithConstraints(address(0), tokenId, _constraintMinted);
_update(address(0), tokenId, CONSTRAINT_MINTED);
}

/**
Expand All @@ -355,7 +365,7 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
if (to == address(0)) {
revert ERC721InvalidReceiver(address(0));
}
address owner = _updateWithConstraints(to, tokenId, _constraintMinted);
address owner = _update(to, tokenId, CONSTRAINT_MINTED);
if (owner != from) {
revert ERC721IncorrectOwner(from, tokenId, owner);
}
Expand Down Expand Up @@ -393,34 +403,6 @@ abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Er
}
}

/**
* @dev Constraint: revert if token is already minted
*/
function _constraintNotMinted(address from, address, uint256) internal pure {
if (from != address(0)) {
revert ERC721InvalidSender(address(0));
}
}

/**
* @dev Constraint: revert if token is not yet minted
*/
function _constraintMinted(address from, address, uint256 tokenId) internal pure {
if (from == address(0)) {
revert ERC721NonexistentToken(tokenId);
}
}

/**
* @dev Constraint: check that the caller is the current owner, or approved by the current owner
*/
function _constraintApprovedOrOwner(address owner, address, uint256 tokenId) internal view {
address spender = _msgSender();
if (spender != owner && !isApprovedForAll(owner, spender) && getApproved(tokenId) != spender) {
revert ERC721InsufficientApproval(_msgSender(), tokenId);
}
}

/**
* @dev Private function to invoke {IERC721Receiver-onERC721Received} on a target address.
* The call is not executed if the target address is not a contract.
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/ERC721/extensions/ERC721Burnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ abstract contract ERC721Burnable is Context, ERC721 {
* - The caller must own `tokenId` or be an approved operator.
*/
function burn(uint256 tokenId) public virtual {
_updateWithConstraints(address(0), tokenId, _constraintApprovedOrOwner);
_update(address(0), tokenId, CONSTRAINT_MINTED | CONSTRAINT_SPENDER_APPROVED_OR_OWNER);
}
}
6 changes: 4 additions & 2 deletions contracts/token/ERC721/extensions/ERC721Consecutive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ abstract contract ERC721Consecutive is IERC2309, ERC721 {
* Warning: Using {ERC721Consecutive} prevents minting during construction in favor of {_mintConsecutive}.
* After construction, {_mintConsecutive} is no longer available and minting through {_update} becomes available.
*/
function _update(address from, address to, uint256 tokenId) internal virtual override {
super._update(from, to, tokenId);
function _update(address to, uint256 tokenId, bytes32 optionalChecks) internal virtual override returns (address) {
address from = super._update(to, tokenId, optionalChecks);

// only mint after construction
if (from == address(0) && address(this).code.length == 0) {
Expand All @@ -154,6 +154,8 @@ abstract contract ERC721Consecutive is IERC2309, ERC721 {
) {
_sequentialBurn.set(tokenId);
}

return from;
}

/**
Expand Down
6 changes: 4 additions & 2 deletions contracts/token/ERC721/extensions/ERC721Enumerable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
/**
* @dev See {ERC721-_update}.
*/
function _update(address from, address to, uint256 tokenId) internal virtual override {
super._update(from, to, tokenId);
function _update(address to, uint256 tokenId, bytes32 optionalChecks) internal virtual override returns (address) {
address from = super._update(to, tokenId, optionalChecks);

if (from == address(0)) {
_addTokenToAllTokensEnumeration(tokenId);
Expand All @@ -89,6 +89,8 @@ abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
} else if (from != to) {
_addTokenToOwnerEnumeration(to, tokenId);
}

return from;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions contracts/token/ERC721/extensions/ERC721Pausable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ abstract contract ERC721Pausable is ERC721, Pausable {
*
* - the contract must not be paused.
*/
function _update(address from, address to, uint256 tokenId) internal virtual override {
function _update(address to, uint256 tokenId, bytes32 optionalChecks) internal virtual override returns (address) {
_requireNotPaused();
super._update(from, to, tokenId);
return super._update(to, tokenId, optionalChecks);
}
}
6 changes: 4 additions & 2 deletions contracts/token/ERC721/extensions/ERC721Royalty.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ abstract contract ERC721Royalty is ERC2981, ERC721 {
/**
* @dev See {ERC721-_update}. This override additionally clears the royalty information for the token.
*/
function _update(address from, address to, uint256 tokenId) internal virtual override {
super._update(from, to, tokenId);
function _update(address to, uint256 tokenId, bytes32 optionalChecks) internal virtual override returns (address) {
address from = super._update(to, tokenId, optionalChecks);

if (to == address(0)) {
_resetTokenRoyalty(tokenId);
}

return from;
}
}
6 changes: 4 additions & 2 deletions contracts/token/ERC721/extensions/ERC721URIStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 {
* token-specific URI was set for the token, and if so, it deletes the token URI from
* the storage mapping.
*/
function _update(address from, address to, uint256 tokenId) internal virtual override {
super._update(from, to, tokenId);
function _update(address to, uint256 tokenId, bytes32 optionalChecks) internal virtual override returns (address) {
address from = super._update(to, tokenId, optionalChecks);

if (to == address(0) && bytes(_tokenURIs[tokenId]).length != 0) {
delete _tokenURIs[tokenId];
}

return from;
}
}
7 changes: 5 additions & 2 deletions contracts/token/ERC721/extensions/ERC721Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ abstract contract ERC721Votes is ERC721, Votes {
*
* Emits a {IVotes-DelegateVotesChanged} event.
*/
function _update(address from, address to, uint256 tokenId) internal virtual override {
super._update(from, to, tokenId);
function _update(address to, uint256 tokenId, bytes32 optionalChecks) internal virtual override returns (address) {
address from = super._update(to, tokenId, optionalChecks);

_transferVotingUnits(from, to, 1);

return from;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/ERC721/extensions/ERC721Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract contract ERC721Wrapper is ERC721, IERC721Receiver {
uint256 length = tokenIds.length;
for (uint256 i = 0; i < length; ++i) {
uint256 tokenId = tokenIds[i];
_updateWithConstraints(address(0), tokenId, _constraintApprovedOrOwner);
_update(address(0), tokenId, CONSTRAINT_MINTED | CONSTRAINT_SPENDER_APPROVED_OR_OWNER);
// Checks were already performed at this point, and there's no way to retake ownership or approval from
// the wrapped tokenId after this point, so it's safe to remove the reentrancy check for the next line.
// slither-disable-next-line reentrancy-no-eth
Expand Down