Allow setting tokenURI for non-existent token (#4559)

Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
Hadrien Croubois
2023-09-02 06:24:05 +02:00
committed by GitHub
parent 10e00c8ef5
commit 00c5da2034
3 changed files with 13 additions and 12 deletions

View File

@ -0,0 +1,5 @@
---
'openzeppelin-solidity': major
---
`ERC721URIStorage`: Allow setting the token URI prior to minting.

View File

@ -49,17 +49,9 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 {
* @dev Sets `_tokenURI` as the tokenURI of `tokenId`. * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
* *
* Emits {MetadataUpdate}. * Emits {MetadataUpdate}.
*
* Requirements:
*
* - `tokenId` must exist.
*/ */
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual { function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
if (_ownerOf(tokenId) == address(0)) {
revert ERC721NonexistentToken(tokenId);
}
_tokenURIs[tokenId] = _tokenURI; _tokenURIs[tokenId] = _tokenURI;
emit MetadataUpdate(tokenId); emit MetadataUpdate(tokenId);
} }

View File

@ -50,10 +50,14 @@ contract('ERC721URIStorage', function (accounts) {
}); });
}); });
it('reverts when setting for non existent token id', async function () { it('setting the uri for non existent token id is allowed', async function () {
await expectRevertCustomError(this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'ERC721NonexistentToken', [ expectEvent(await this.token.$_setTokenURI(nonExistentTokenId, sampleUri), 'MetadataUpdate', {
nonExistentTokenId, _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 () { it('base URI can be set', async function () {