Skip to content

Commit

Permalink
Extend ERC1155URIStorage
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
awrichar committed Mar 4, 2024
1 parent cbc1c50 commit 0ba24c2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
47 changes: 19 additions & 28 deletions samples/solidity/contracts/ERC1155MixedFungible.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC1155/ERC1155.sol';
import '@openzeppelin/contracts/token/ERC1155/extensions/ERC1155URIStorage.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import './IERC1155MixedFungible.sol';

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

// mapping from type ID | index => custom token URIs for non-fungible tokens
// fallback behavior if missing is to use the default base URI
mapping(uint256 => string) private _nfTokenURIs;

function isFungible(uint256 id) internal pure returns (bool) {
return id & TYPE_NF_BIT == 0;
}
Expand Down Expand Up @@ -89,18 +85,6 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
);
}

function _setNonFungibleURI(
uint256 type_id,
uint256 id,
string memory _uri
) private creatorOnly(type_id) {
require(
isNonFungible(type_id),
'ERC1155MixedFungible: id does not represent a non-fungible type'
);
_nfTokenURIs[id] = _uri;
}

function mintNonFungible(
uint256 type_id,
address[] calldata to,
Expand Down Expand Up @@ -138,7 +122,7 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
for (uint256 i = 0; i < to.length; ++i) {
uint256 id = type_id | (index + i);
_mint(to[i], id, 1, data);
_setNonFungibleURI(type_id, id, _uri);
_setURI(id, _uri);
}
}

Expand Down Expand Up @@ -185,15 +169,22 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {

function uri(
uint256 id
) public view virtual override(IERC1155MixedFungible, ERC1155) returns (string memory) {
string memory _tokenUri = _nfTokenURIs[id];
bytes memory tempURITest = bytes(_tokenUri);

if (tempURITest.length == 0) {
return _baseTokenURI;
} else {
return _tokenUri;
}
)
public
view
virtual
override(IERC1155MixedFungible, ERC1155URIStorage)
returns (string memory)
{
return super.uri(id);
}

function _setURI(uint256 id, string memory tokenURI) internal virtual override {
require(
isNonFungible(id),
'ERC1155MixedFungible: id does not represent a non-fungible type'
);
super._setURI(id, tokenURI);
}

function baseTokenUri() public view virtual override returns (string memory) {
Expand Down
5 changes: 3 additions & 2 deletions samples/solidity/contracts/IERC1155MixedFungible.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

pragma solidity ^0.8.0;

import '@openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol';
import '@openzeppelin/contracts/utils/introspection/IERC165.sol';
import './IERC1155Factory.sol';

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

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

function uri(uint256 id) external returns (string memory);
function uri(uint256 id) external view returns (string memory);

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

0 comments on commit 0ba24c2

Please sign in to comment.