Skip to content

Commit 34717b7

Browse files
authored
Merge pull request #140 from kaleido-io/contracts
Update to latest OpenZeppelin base contracts
2 parents a0ca070 + 0ba24c2 commit 34717b7

File tree

5 files changed

+31
-40
lines changed

5 files changed

+31
-40
lines changed

samples/solidity/contracts/ERC1155MixedFungible.sol

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
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';
6-
import '@openzeppelin/contracts/utils/math/SafeMath.sol';
76
import './IERC1155MixedFungible.sol';
87

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

48-
// mapping from type ID | index => custom token URIs for non-fungible tokens
49-
// fallback behavior if missing is to use the default base URI
50-
mapping(uint256 => string) private _nfTokenURIs;
51-
5247
function isFungible(uint256 id) internal pure returns (bool) {
5348
return id & TYPE_NF_BIT == 0;
5449
}
@@ -90,18 +85,6 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
9085
);
9186
}
9287

93-
function _setNonFungibleURI(
94-
uint256 type_id,
95-
uint256 id,
96-
string memory _uri
97-
) private creatorOnly(type_id) {
98-
require(
99-
isNonFungible(type_id),
100-
'ERC1155MixedFungible: id does not represent a non-fungible type'
101-
);
102-
_nfTokenURIs[id] = _uri;
103-
}
104-
10588
function mintNonFungible(
10689
uint256 type_id,
10790
address[] calldata to,
@@ -114,7 +97,7 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
11497

11598
// Indexes are 1-based.
11699
uint256 index = maxIndex[type_id] + 1;
117-
maxIndex[type_id] = SafeMath.add(to.length, maxIndex[type_id]);
100+
maxIndex[type_id] = to.length + maxIndex[type_id];
118101

119102
for (uint256 i = 0; i < to.length; ++i) {
120103
_mint(to[i], type_id | (index + i), 1, data);
@@ -134,12 +117,12 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
134117

135118
// Indexes are 1-based.
136119
uint256 index = maxIndex[type_id] + 1;
137-
maxIndex[type_id] = SafeMath.add(to.length, maxIndex[type_id]);
120+
maxIndex[type_id] = to.length + maxIndex[type_id];
138121

139122
for (uint256 i = 0; i < to.length; ++i) {
140123
uint256 id = type_id | (index + i);
141124
_mint(to[i], id, 1, data);
142-
_setNonFungibleURI(type_id, id, _uri);
125+
_setURI(id, _uri);
143126
}
144127
}
145128

@@ -186,15 +169,22 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
186169

187170
function uri(
188171
uint256 id
189-
) public view virtual override(IERC1155MixedFungible, ERC1155) returns (string memory) {
190-
string memory _tokenUri = _nfTokenURIs[id];
191-
bytes memory tempURITest = bytes(_tokenUri);
192-
193-
if (tempURITest.length == 0) {
194-
return _baseTokenURI;
195-
} else {
196-
return _tokenUri;
197-
}
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);
198188
}
199189

200190
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
}

samples/solidity/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/solidity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"test": "hardhat test --network hardhat"
77
},
88
"dependencies": {
9-
"@openzeppelin/contracts": "^4.5.0"
9+
"@openzeppelin/contracts": "^5.0.2"
1010
},
1111
"devDependencies": {
1212
"@nomicfoundation/hardhat-toolbox": "^4.0.0",

samples/solidity/test/ERC1155MixedFungible.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ describe('ERC1155MixedFungible - Unit Tests', () => {
241241
1,
242242
'0x00',
243243
),
244-
).to.be.revertedWith('ERC1155: caller is not token owner or approved');
244+
).to.be.revertedWithCustomError(deployedERC1155, 'ERC1155MissingApprovalForAll');
245245
expect(
246246
await deployedERC1155
247247
.connect(deployerSignerA)
@@ -371,7 +371,7 @@ describe('ERC1155MixedFungible - Unit Tests', () => {
371371
1,
372372
'0x00',
373373
),
374-
).to.be.revertedWith('ERC1155: caller is not token owner or approved');
374+
).to.be.revertedWithCustomError(deployedERC1155, 'ERC1155MissingApprovalForAll');
375375
expect(
376376
await deployedERC1155
377377
.connect(deployerSignerA)

0 commit comments

Comments
 (0)