Skip to content

Commit caba6b9

Browse files
Amxxfrangio
andauthored
Add an internal _setApprovalForAll function (721 & 1155) (#2834)
Co-authored-by: Francisco Giordano <[email protected]>
1 parent e2fa301 commit caba6b9

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* `AccessControl`: add internal `_grantRole(bytes32,address)` and `_revokeRole(bytes32,address)`. ([#2568](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2568))
77
* `AccessControl`: mark `_setupRole(bytes32,address)` as deprecated in favor of `_grantRole(bytes32,address)`. ([#2568](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2568))
88
* `EIP712`: cache `address(this)` to immutable storage to avoid potential issues if a vanilla contract is used in a delegatecall context. ([#2852](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/#2852))
9+
* Add internal `_setApprovalForAll` to `ERC721` and `ERC1155`. ([#2834](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2834))
910

1011
## 4.3.2 (2021-09-14)
1112

contracts/token/ERC1155/ERC1155.sol

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
100100
* @dev See {IERC1155-setApprovalForAll}.
101101
*/
102102
function setApprovalForAll(address operator, bool approved) public virtual override {
103-
require(_msgSender() != operator, "ERC1155: setting approval status for self");
104-
105-
_operatorApprovals[_msgSender()][operator] = approved;
106-
emit ApprovalForAll(_msgSender(), operator, approved);
103+
_setApprovalForAll(_msgSender(), operator, approved);
107104
}
108105

109106
/**
@@ -369,6 +366,21 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
369366
emit TransferBatch(operator, from, address(0), ids, amounts);
370367
}
371368

369+
/**
370+
* @dev Approve `operator` to operate on all of `owner` tokens
371+
*
372+
* Emits a {ApprovalForAll} event.
373+
*/
374+
function _setApprovalForAll(
375+
address owner,
376+
address operator,
377+
bool approved
378+
) internal virtual {
379+
require(owner != operator, "ERC1155: setting approval status for self");
380+
_operatorApprovals[owner][operator] = approved;
381+
emit ApprovalForAll(owner, operator, approved);
382+
}
383+
372384
/**
373385
* @dev Hook that is called before any token transfer. This includes minting
374386
* and burning, as well as batched variants.

contracts/token/ERC721/ERC721.sol

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
133133
* @dev See {IERC721-setApprovalForAll}.
134134
*/
135135
function setApprovalForAll(address operator, bool approved) public virtual override {
136-
require(operator != _msgSender(), "ERC721: approve to caller");
137-
138-
_operatorApprovals[_msgSender()][operator] = approved;
139-
emit ApprovalForAll(_msgSender(), operator, approved);
136+
_setApprovalForAll(_msgSender(), operator, approved);
140137
}
141138

142139
/**
@@ -356,6 +353,21 @@ contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
356353
emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
357354
}
358355

356+
/**
357+
* @dev Approve `operator` to operate on all of `owner` tokens
358+
*
359+
* Emits a {ApprovalForAll} event.
360+
*/
361+
function _setApprovalForAll(
362+
address owner,
363+
address operator,
364+
bool approved
365+
) internal virtual {
366+
require(owner != operator, "ERC721: approve to caller");
367+
_operatorApprovals[owner][operator] = approved;
368+
emit ApprovalForAll(owner, operator, approved);
369+
}
370+
359371
/**
360372
* @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
361373
* The call is not executed if the target address is not a contract.

0 commit comments

Comments
 (0)