Skip to content

Commit 0ba24c2

Browse files
committed
Extend ERC1155URIStorage
This makes the contracts more consistent with OpenZeppelin examples. No signatures are changed, but there is one slight change in behavior: if you specify a "base URI", then individual token URIs are always understood to be a suffix for that base URI. The previous contract would ignore the base URI when a token URI was specified. Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
1 parent cbc1c50 commit 0ba24c2

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

samples/solidity/contracts/ERC1155MixedFungible.sol

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
pragma solidity ^0.8.0;
33

4-
import '@openzeppelin/contracts/token/ERC1155/ERC1155.sol';
4+
import '@openzeppelin/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol';
55
import '@openzeppelin/contracts/utils/Context.sol';
66
import './IERC1155MixedFungible.sol';
77

@@ -27,7 +27,7 @@ import './IERC1155MixedFungible.sol';
2727
* Remember to always consult best practices from other communities and examples (such as OpenZeppelin)
2828
* when crafting your token logic, rather than relying on the FireFly community alone. Happy minting!
2929
*/
30-
contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
30+
contract ERC1155MixedFungible is Context, ERC1155URIStorage, IERC1155MixedFungible {
3131
// Use a split bit implementation:
3232
// - Bit 255: type flag (0 = fungible, 1 = non-fungible)
3333
// - Bits 255-128: type id
@@ -44,10 +44,6 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
4444
// inherited ERC1155 `_uri` is private, so need our own within this contract
4545
string private _baseTokenURI;
4646

47-
// mapping from type ID | index => custom token URIs for non-fungible tokens
48-
// fallback behavior if missing is to use the default base URI
49-
mapping(uint256 => string) private _nfTokenURIs;
50-
5147
function isFungible(uint256 id) internal pure returns (bool) {
5248
return id & TYPE_NF_BIT == 0;
5349
}
@@ -89,18 +85,6 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
8985
);
9086
}
9187

92-
function _setNonFungibleURI(
93-
uint256 type_id,
94-
uint256 id,
95-
string memory _uri
96-
) private creatorOnly(type_id) {
97-
require(
98-
isNonFungible(type_id),
99-
'ERC1155MixedFungible: id does not represent a non-fungible type'
100-
);
101-
_nfTokenURIs[id] = _uri;
102-
}
103-
10488
function mintNonFungible(
10589
uint256 type_id,
10690
address[] calldata to,
@@ -138,7 +122,7 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
138122
for (uint256 i = 0; i < to.length; ++i) {
139123
uint256 id = type_id | (index + i);
140124
_mint(to[i], id, 1, data);
141-
_setNonFungibleURI(type_id, id, _uri);
125+
_setURI(id, _uri);
142126
}
143127
}
144128

@@ -185,15 +169,22 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
185169

186170
function uri(
187171
uint256 id
188-
) public view virtual override(IERC1155MixedFungible, ERC1155) returns (string memory) {
189-
string memory _tokenUri = _nfTokenURIs[id];
190-
bytes memory tempURITest = bytes(_tokenUri);
191-
192-
if (tempURITest.length == 0) {
193-
return _baseTokenURI;
194-
} else {
195-
return _tokenUri;
196-
}
172+
)
173+
public
174+
view
175+
virtual
176+
override(IERC1155MixedFungible, ERC1155URIStorage)
177+
returns (string memory)
178+
{
179+
return super.uri(id);
180+
}
181+
182+
function _setURI(uint256 id, string memory tokenURI) internal virtual override {
183+
require(
184+
isNonFungible(id),
185+
'ERC1155MixedFungible: id does not represent a non-fungible type'
186+
);
187+
super._setURI(id, tokenURI);
197188
}
198189

199190
function baseTokenUri() public view virtual override returns (string memory) {

samples/solidity/contracts/IERC1155MixedFungible.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
pragma solidity ^0.8.0;
44

5+
import '@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol';
56
import '@openzeppelin/contracts/utils/introspection/IERC165.sol';
67
import './IERC1155Factory.sol';
78

89
/**
910
* ERC1155 interface with mint, burn, and attached data support for fungible & non-fungible tokens.
1011
* Non-fungible tokens also have support for custom URI's.
1112
*/
12-
interface IERC1155MixedFungible is IERC165, IERC1155Factory {
13+
interface IERC1155MixedFungible is IERC165, IERC1155Factory, IERC1155MetadataURI {
1314
function create(bool is_fungible, bytes calldata data) external override;
1415

1516
function mintNonFungible(uint256 type_id, address[] calldata to, bytes calldata data) external;
@@ -36,7 +37,7 @@ interface IERC1155MixedFungible is IERC165, IERC1155Factory {
3637
bytes calldata data
3738
) external;
3839

39-
function uri(uint256 id) external returns (string memory);
40+
function uri(uint256 id) external view returns (string memory);
4041

4142
function baseTokenUri() external returns (string memory);
4243
}

0 commit comments

Comments
 (0)