Skip to content

Commit 829492c

Browse files
authored
ERC721URIStorage: add a getter for the token specific URI suffix. (#6175)
1 parent bb39b88 commit 829492c

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

.changeset/stale-lizards-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': minor
3+
---
4+
5+
`ERC721URIStorage`: Add `_suffixURI`, an internal getter for retrieving the custom tokenURI without the base prefix.

contracts/token/ERC721/extensions/ERC721URIStorage.sol

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 {
2828
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
2929
_requireOwned(tokenId);
3030

31-
string memory _tokenURI = _tokenURIs[tokenId];
3231
string memory base = _baseURI();
32+
string memory suffix = _suffixURI(tokenId);
3333

3434
// If there is no base URI, return the token URI.
3535
if (bytes(base).length == 0) {
36-
return _tokenURI;
36+
return suffix;
3737
}
3838
// If both are set, concatenate the baseURI and tokenURI (via string.concat).
39-
if (bytes(_tokenURI).length > 0) {
40-
return string.concat(base, _tokenURI);
39+
if (bytes(suffix).length > 0) {
40+
return string.concat(base, suffix);
4141
}
4242

4343
return super.tokenURI(tokenId);
@@ -52,4 +52,11 @@ abstract contract ERC721URIStorage is IERC4906, ERC721 {
5252
_tokenURIs[tokenId] = _tokenURI;
5353
emit MetadataUpdate(tokenId);
5454
}
55+
56+
/**
57+
* @dev Returns the suffix part of the tokenURI for `tokenId`.
58+
*/
59+
function _suffixURI(uint256 tokenId) internal view virtual returns (string memory) {
60+
return _tokenURIs[tokenId];
61+
}
5562
}

test/token/ERC721/extensions/ERC721URIStorage.test.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,22 @@ describe('ERC721URIStorage', function () {
3131
});
3232

3333
it('it is empty by default', async function () {
34-
expect(await this.token.tokenURI(tokenId)).to.equal('');
34+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal('');
35+
await expect(this.token.tokenURI(tokenId)).to.eventually.equal('');
3536
});
3637

3738
it('reverts when queried for non existent token id', async function () {
39+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal('');
3840
await expect(this.token.tokenURI(nonExistentTokenId))
3941
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
4042
.withArgs(nonExistentTokenId);
4143
});
4244

4345
it('can be set for a token id', async function () {
4446
await this.token.$_setTokenURI(tokenId, sampleUri);
45-
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
47+
48+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal(sampleUri);
49+
await expect(this.token.tokenURI(tokenId)).to.eventually.equal(sampleUri);
4650
});
4751

4852
it('setting the uri emits an event', async function () {
@@ -58,38 +62,43 @@ describe('ERC721URIStorage', function () {
5862

5963
// value will be accessible after mint
6064
await this.token.$_mint(this.owner, nonExistentTokenId);
61-
expect(await this.token.tokenURI(nonExistentTokenId)).to.equal(sampleUri);
65+
await expect(this.token.tokenURI(nonExistentTokenId)).to.eventually.equal(sampleUri);
6266
});
6367

6468
it('base URI can be set', async function () {
6569
await this.token.setBaseURI(baseURI);
66-
expect(await this.token.$_baseURI()).to.equal(baseURI);
70+
await expect(this.token.$_baseURI()).to.eventually.equal(baseURI);
6771
});
6872

6973
it('base URI is added as a prefix to the token URI', async function () {
7074
await this.token.setBaseURI(baseURI);
7175
await this.token.$_setTokenURI(tokenId, sampleUri);
7276

73-
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + sampleUri);
77+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal(sampleUri);
78+
await expect(this.token.tokenURI(tokenId)).to.eventually.equal(baseURI + sampleUri);
7479
});
7580

7681
it('token URI can be changed by changing the base URI', async function () {
7782
await this.token.setBaseURI(baseURI);
7883
await this.token.$_setTokenURI(tokenId, sampleUri);
7984

8085
await this.token.setBaseURI(otherBaseURI);
81-
expect(await this.token.tokenURI(tokenId)).to.equal(otherBaseURI + sampleUri);
86+
87+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal(sampleUri);
88+
await expect(this.token.tokenURI(tokenId)).to.eventually.equal(otherBaseURI + sampleUri);
8289
});
8390

8491
it('tokenId is appended to base URI for tokens with no URI', async function () {
8592
await this.token.setBaseURI(baseURI);
8693

87-
expect(await this.token.tokenURI(tokenId)).to.equal(baseURI + tokenId);
94+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal('');
95+
await expect(this.token.tokenURI(tokenId)).to.eventually.equal(baseURI + tokenId);
8896
});
8997

9098
it('tokens without URI can be burnt ', async function () {
9199
await this.token.$_burn(tokenId);
92100

101+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal('');
93102
await expect(this.token.tokenURI(tokenId))
94103
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
95104
.withArgs(tokenId);
@@ -100,6 +109,7 @@ describe('ERC721URIStorage', function () {
100109

101110
await this.token.$_burn(tokenId);
102111

112+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal(sampleUri);
103113
await expect(this.token.tokenURI(tokenId))
104114
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
105115
.withArgs(tokenId);
@@ -110,12 +120,15 @@ describe('ERC721URIStorage', function () {
110120

111121
await this.token.$_burn(tokenId);
112122

123+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal(sampleUri);
113124
await expect(this.token.tokenURI(tokenId))
114125
.to.be.revertedWithCustomError(this.token, 'ERC721NonexistentToken')
115126
.withArgs(tokenId);
116127

117128
await this.token.$_mint(this.owner, tokenId);
118-
expect(await this.token.tokenURI(tokenId)).to.equal(sampleUri);
129+
130+
await expect(this.token.$_suffixURI(tokenId)).to.eventually.equal(sampleUri);
131+
await expect(this.token.tokenURI(tokenId)).to.eventually.equal(sampleUri);
119132
});
120133
});
121134
});

0 commit comments

Comments
 (0)