Skip to content

Commit 02fcc75

Browse files
takahserAmxx
andauthored
Add ERC1155URIStorage (OpenZeppelin#3210)
* Add ERC721URIStorage-like extension for ERC1155 * Add tests for ERC1155URIStorage extension * add changelog entry for ERC721URIStorage * Fix linting errors * Emit URI event in ERC1155URIStorage * Remove exists check and ERC1155Supply dependency * Fix lint error * Overwrite ERC1155 uri method * Update ERC1155URIStorage specs * Fix ERC1155URIStorageMock * Rename _setTokenURI => _setURI in ERC1155URIStorage * Add baseURI to ERC1155URIStorage * Move super.uri call in ERC1155URIStorage * Clearify ERC1155URIStorage description in change log * reorder changelog & add documentation * improve documentation * fix typo Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
1 parent ae270b0 commit 02fcc75

File tree

5 files changed

+157
-4
lines changed

5 files changed

+157
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
* `AccessControl`: add a virtual `_checkRole(bytes32)` function that can be overridden to alter the `onlyRole` modifier behavior. ([#3137](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3137))
66
* `EnumerableMap`: add new `AddressToUintMap` map type. ([#3150](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3150))
77
* `EnumerableMap`: add new `Bytes32ToBytes32Map` map type. ([#3192](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3192))
8+
* `ERC20FlashMint`: support infinite allowance when paying back a flash loan. ([#3226](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3226))
9+
* `ERC20Wrapper`: the `decimals()` function now tries to fetch the value from the underlying token instance. If that calls revert, then the default value is used. ([#3259](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3259))
10+
* `draft-ERC20Permit`: replace `immutable` with `constant` for `_PERMIT_TYPEHASH` since the `keccak256` of string literals is treated specially and the hash is evaluated at compile time. ([#3196](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3196))
811
* `ERC1155`: Add a `_afterTokenTransfer` hook for improved extensibility. ([#3166](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3166))
12+
* `ERC1155URIStorage`: add a new extension that implements a `_setURI` behavior similar to ERC721's `_setTokenURI`. ([#3210](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3210))
913
* `DoubleEndedQueue`: a new data structure that supports efficient push and pop to both front and back, useful for FIFO and LIFO queues. ([#3153](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3153))
1014
* `Governor`: improved security of `onlyGovernance` modifier when using an external executor contract (e.g. a timelock) that can operate without necessarily going through the governance protocol. ([#3147](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3147))
1115
* `Governor`: Add a way to parameterize votes. This can be used to implement voting systems such as fractionalized voting, ERC721 based voting, or any number of other systems. The `params` argument added to `_countVote` method, and included in the newly added `_getVotes` method, can be used by counting and voting modules respectively for such purposes. ([#3043](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3043))
1216
* `Governor`: rewording of revert reason for consistency. ([#3275](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3275))
1317
* `Governor`: fix an inconsistency in data locations that could lead to invalid bytecode being produced. ([#3295](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3295))
14-
* `ERC20FlashMint`: support infinite allowance when paying back a flash loan. ([#3226](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3226))
15-
* `TimelockController`: Add a separate canceller role for the ability to cancel. ([#3165](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3165))
16-
* `draft-ERC20Permit`: replace `immutable` with `constant` for `_PERMIT_TYPEHASH` since the `keccak256` of string literals is treated specially and the hash is evaluated at compile time. ([#3196](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3196))
17-
* `ERC20Wrapper`: the `decimals()` function now tries to fetch the value from the underlying token instance. If that calls revert, then the default value is used. ([#3259](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3259))
1818
* `Governor`: Implement `IERC721Receiver` and `IERC1155Receiver` to improve token custody by governors. ([#3230](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3230))
1919
* `TimelockController`: Implement `IERC721Receiver` and `IERC1155Receiver` to improve token custody by timelocks. ([#3230](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3230))
20+
* `TimelockController`: Add a separate canceller role for the ability to cancel. ([#3165](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3165))
2021
* `Initializable`: add a reinitializer modifier that enables the initialization of new modules, added to already initialized contracts through upgradeability. ([#3232](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3232))
2122
* `Initializable`: add an Initialized event that tracks initialized version numbers. ([#3294](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3294))
2223

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import "./ERC1155Mock.sol";
6+
import "../token/ERC1155/extensions/ERC1155URIStorage.sol";
7+
8+
contract ERC1155URIStorageMock is ERC1155Mock, ERC1155URIStorage {
9+
constructor(string memory _uri) ERC1155Mock(_uri) {}
10+
11+
function uri(uint256 tokenId) public view virtual override(ERC1155, ERC1155URIStorage) returns (string memory) {
12+
return ERC1155URIStorage.uri(tokenId);
13+
}
14+
15+
function setURI(uint256 tokenId, string memory _tokenURI) public {
16+
_setURI(tokenId, _tokenURI);
17+
}
18+
19+
function setBaseURI(string memory baseURI) public {
20+
_setBaseURI(baseURI);
21+
}
22+
}

contracts/token/ERC1155/README.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ NOTE: This core set of contracts is designed to be unopinionated, allowing devel
3636

3737
{{ERC1155Supply}}
3838

39+
{{ERC1155URIStorage}}
40+
3941
== Presets
4042

4143
These contracts are preconfigured combinations of the above features. They can be used through inheritance or as models to copy and paste their source code.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import "../../../utils/Strings.sol";
6+
import "../ERC1155.sol";
7+
8+
/**
9+
* @dev ERC1155 token with storage based token URI management.
10+
* Inspired by the ERC721URIStorage extension
11+
*
12+
* _Available since v4.6._
13+
*/
14+
abstract contract ERC1155URIStorage is ERC1155 {
15+
using Strings for uint256;
16+
17+
// Optional base URI
18+
string private _baseURI = "";
19+
20+
// Optional mapping for token URIs
21+
mapping(uint256 => string) private _tokenURIs;
22+
23+
/**
24+
* @dev See {IERC1155MetadataURI-uri}.
25+
*
26+
* This implementation returns the concatenation of the `_baseURI`
27+
* and the token-specific uri if the latter is set
28+
*
29+
* This enables the following behaviors:
30+
*
31+
* - if `_tokenURIs[tokenId]` is set, then the result is the concatenation
32+
* of `_baseURI` and `_tokenURIs[tokenId]` (keep in mind that `_baseURI`
33+
* is empty per default);
34+
*
35+
* - if `_tokenURIs[tokenId]` is NOT set then we fallback to `super.uri()`
36+
* which in most cases will contain `ERC1155._uri`;
37+
*
38+
* - if `_tokenURIs[tokenId]` is NOT set, and if the parents do not have a
39+
* uri value set, then the result is empty.
40+
*/
41+
function uri(uint256 tokenId) public view virtual override returns (string memory) {
42+
string memory tokenURI = _tokenURIs[tokenId];
43+
44+
// If token URI is set, concatenate base URI and tokenURI (via abi.encodePacked).
45+
return bytes(tokenURI).length > 0 ? string(abi.encodePacked(_baseURI, tokenURI)) : super.uri(tokenId);
46+
}
47+
48+
/**
49+
* @dev Sets `tokenURI` as the tokenURI of `tokenId`.
50+
*/
51+
function _setURI(uint256 tokenId, string memory tokenURI) internal virtual {
52+
_tokenURIs[tokenId] = tokenURI;
53+
emit URI(uri(tokenId), tokenId);
54+
}
55+
56+
/**
57+
* @dev Sets `baseURI` as the `_baseURI` for all tokens
58+
*/
59+
function _setBaseURI(string memory baseURI) internal virtual {
60+
_baseURI = baseURI;
61+
}
62+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const { BN, expectEvent } = require('@openzeppelin/test-helpers');
2+
3+
const { expect } = require('chai');
4+
const { artifacts } = require('hardhat');
5+
6+
const ERC1155URIStorageMock = artifacts.require('ERC1155URIStorageMock');
7+
8+
contract(['ERC1155URIStorage'], function (accounts) {
9+
const [ holder ] = accounts;
10+
11+
const erc1155Uri = 'https://token.com/nfts/';
12+
const baseUri = 'https://token.com/';
13+
14+
const tokenId = new BN('1');
15+
const amount = new BN('3000');
16+
17+
describe('with base uri set', function () {
18+
beforeEach(async function () {
19+
this.token = await ERC1155URIStorageMock.new(erc1155Uri);
20+
this.token.setBaseURI(baseUri);
21+
22+
await this.token.mint(holder, tokenId, amount, '0x');
23+
});
24+
25+
it('can request the token uri, returning the erc1155 uri if no token uri was set', async function () {
26+
const receivedTokenUri = await this.token.uri(tokenId);
27+
28+
expect(receivedTokenUri).to.be.equal(erc1155Uri);
29+
});
30+
31+
it('can request the token uri, returning the concatenated uri if a token uri was set', async function () {
32+
const tokenUri = '1234/';
33+
const receipt = await this.token.setURI(tokenId, tokenUri);
34+
35+
const receivedTokenUri = await this.token.uri(tokenId);
36+
37+
const expectedUri = `${baseUri}${tokenUri}`;
38+
expect(receivedTokenUri).to.be.equal(expectedUri);
39+
expectEvent(receipt, 'URI', { value: expectedUri, id: tokenId });
40+
});
41+
});
42+
43+
describe('with base uri set to the empty string', function () {
44+
beforeEach(async function () {
45+
this.token = await ERC1155URIStorageMock.new('');
46+
47+
await this.token.mint(holder, tokenId, amount, '0x');
48+
});
49+
50+
it('can request the token uri, returning an empty string if no token uri was set', async function () {
51+
const receivedTokenUri = await this.token.uri(tokenId);
52+
53+
expect(receivedTokenUri).to.be.equal('');
54+
});
55+
56+
it('can request the token uri, returning the token uri if a token uri was set', async function () {
57+
const tokenUri = 'ipfs://1234/';
58+
const receipt = await this.token.setURI(tokenId, tokenUri);
59+
60+
const receivedTokenUri = await this.token.uri(tokenId);
61+
62+
expect(receivedTokenUri).to.be.equal(tokenUri);
63+
expectEvent(receipt, 'URI', { value: tokenUri, id: tokenId });
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)