Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting tokenURI for non-existent token #4559

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/empty-cheetahs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': major
---

`ERC721URIStorage`: Allow setting the token URI prior to minting.
8 changes: 0 additions & 8 deletions contracts/token/ERC721/extensions/ERC721URIStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,9 @@ 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 {
if (_ownerOf(tokenId) == address(0)) {
revert ERC721NonexistentToken(tokenId);
}
_tokenURIs[tokenId] = _tokenURI;

emit MetadataUpdate(tokenId);
}

Expand Down
12 changes: 8 additions & 4 deletions test/token/ERC721/extensions/ERC721URIStorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down