From 5e3ca4d01a8991a9900675c2c43aea39403b5144 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Fri, 1 Sep 2023 11:40:51 +0200 Subject: [PATCH 1/3] Allow setting tokenURI for non-existent token --- .changeset/empty-cheetahs-hunt.md | 5 +++++ .../token/ERC721/extensions/ERC721URIStorage.sol | 4 ---- .../token/ERC721/extensions/ERC721URIStorage.test.js | 12 ++++++++---- 3 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 .changeset/empty-cheetahs-hunt.md diff --git a/.changeset/empty-cheetahs-hunt.md b/.changeset/empty-cheetahs-hunt.md new file mode 100644 index 00000000000..5799c042aba --- /dev/null +++ b/.changeset/empty-cheetahs-hunt.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': major +--- + +`ERC721URIStorage`: Allow setting the tokenURI prior to minting. diff --git a/contracts/token/ERC721/extensions/ERC721URIStorage.sol b/contracts/token/ERC721/extensions/ERC721URIStorage.sol index cd4845b7864..784c288488d 100644 --- a/contracts/token/ERC721/extensions/ERC721URIStorage.sol +++ b/contracts/token/ERC721/extensions/ERC721URIStorage.sol @@ -55,11 +55,7 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 { * - `tokenId` must exist. */ function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { - if (_ownerOf(tokenId) == address(0)) { - revert ERC721NonexistentToken(tokenId); - } _tokenURIs[tokenId] = _tokenURI; - emit MetadataUpdate(tokenId); } diff --git a/test/token/ERC721/extensions/ERC721URIStorage.test.js b/test/token/ERC721/extensions/ERC721URIStorage.test.js index 129515514c8..dc208d1eae0 100644 --- a/test/token/ERC721/extensions/ERC721URIStorage.test.js +++ b/test/token/ERC721/extensions/ERC721URIStorage.test.js @@ -50,10 +50,14 @@ contract('ERC721URIStorage', function (accounts) { }); }); - it('reverts when setting for non existent token id', async function () { - await expectRevertCustomError(this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'ERC721NonexistentToken', [ - nonExistentTokenId, - ]); + it('setting the uri for non existent token id is allowed', async function () { + expectEvent(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'MetadataUpdate', { + _tokenId: nonExistentTokenId, + }); + + // value will be accessible after mint + await this.token.$_mint(owner, nonExistentTokenId); + expect(await this.token.tokenURI(nonExistentTokenId)).to.be.equal(sampleUri); }); it('base URI can be set', async function () { From 20e86be58d16474200a8f19d7218a0d6200e2144 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Fri, 1 Sep 2023 11:43:13 +0200 Subject: [PATCH 2/3] Update ERC721URIStorage.sol --- contracts/token/ERC721/extensions/ERC721URIStorage.sol | 4 ---- 1 file changed, 4 deletions(-) diff --git a/contracts/token/ERC721/extensions/ERC721URIStorage.sol b/contracts/token/ERC721/extensions/ERC721URIStorage.sol index 784c288488d..dbcf3d4e602 100644 --- a/contracts/token/ERC721/extensions/ERC721URIStorage.sol +++ b/contracts/token/ERC721/extensions/ERC721URIStorage.sol @@ -49,10 +49,6 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 { * @dev Sets `_tokenURI` as the tokenURI of `tokenId`. * * Emits {MetadataUpdate}. - * - * Requirements: - * - * - `tokenId` must exist. */ function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { _tokenURIs[tokenId] = _tokenURI; From ac1aad9a8eca3b686597b7d98ca18cdfdf9a2282 Mon Sep 17 00:00:00 2001 From: Francisco Date: Sat, 2 Sep 2023 01:23:28 -0300 Subject: [PATCH 3/3] Update empty-cheetahs-hunt.md --- .changeset/empty-cheetahs-hunt.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/empty-cheetahs-hunt.md b/.changeset/empty-cheetahs-hunt.md index 5799c042aba..eb20381a68a 100644 --- a/.changeset/empty-cheetahs-hunt.md +++ b/.changeset/empty-cheetahs-hunt.md @@ -2,4 +2,4 @@ 'openzeppelin-solidity': major --- -`ERC721URIStorage`: Allow setting the tokenURI prior to minting. +`ERC721URIStorage`: Allow setting the token URI prior to minting.