Skip to content

Commit

Permalink
Merge pull request #140 from kaleido-io/contracts
Browse files Browse the repository at this point in the history
Update to latest OpenZeppelin base contracts
  • Loading branch information
nguyer authored Mar 4, 2024
2 parents a0ca070 + 0ba24c2 commit 34717b7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
52 changes: 21 additions & 31 deletions samples/solidity/contracts/ERC1155MixedFungible.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// 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 '@openzeppelin/contracts/utils/math/SafeMath.sol';
import './IERC1155MixedFungible.sol';

/**
Expand All @@ -28,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 @@ -45,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 @@ -90,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 All @@ -114,7 +97,7 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {

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

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

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

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 @@ -186,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);
}
8 changes: 4 additions & 4 deletions samples/solidity/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion samples/solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"test": "hardhat test --network hardhat"
},
"dependencies": {
"@openzeppelin/contracts": "^4.5.0"
"@openzeppelin/contracts": "^5.0.2"
},
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
Expand Down
4 changes: 2 additions & 2 deletions samples/solidity/test/ERC1155MixedFungible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe('ERC1155MixedFungible - Unit Tests', () => {
1,
'0x00',
),
).to.be.revertedWith('ERC1155: caller is not token owner or approved');
).to.be.revertedWithCustomError(deployedERC1155, 'ERC1155MissingApprovalForAll');
expect(
await deployedERC1155
.connect(deployerSignerA)
Expand Down Expand Up @@ -371,7 +371,7 @@ describe('ERC1155MixedFungible - Unit Tests', () => {
1,
'0x00',
),
).to.be.revertedWith('ERC1155: caller is not token owner or approved');
).to.be.revertedWithCustomError(deployedERC1155, 'ERC1155MissingApprovalForAll');
expect(
await deployedERC1155
.connect(deployerSignerA)
Expand Down

0 comments on commit 34717b7

Please sign in to comment.