Skip to content

Commit

Permalink
Extract a clearer IERC1155Factory interface
Browse files Browse the repository at this point in the history
Split the concept of a "pool factory" (which encompasses a single method and
a single event) away from all of the other opinionated spellings in the
IERC1155MixedFungible interface.

The parameters of the TokenPoolCreation event have also been altered, but
both the old and new parameters will be parsed by the listener.

Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Jun 21, 2023
1 parent aa3efa8 commit b5ca47f
Show file tree
Hide file tree
Showing 16 changed files with 1,020 additions and 369 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ token IDs (inclusive). Note: Because ERC1155 defines the token ID as uint256, th
is `0x0` to `0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff`.

If `config.address` is specified, the connector will invoke the `create()` method (as defined in the
`IERC1155MixedFungible` interface) of the ERC1155 token contract at the specified address.
[IERC1155Factory](samples/solidity/contracts/IERC1155Factory.sol) interface) of the ERC1155 token
contract at the specified address.

If `config.address` is not specified, and `CONTRACT_ADDRESS` is set in the connector's
environment, the `create()` method of that contract will be invoked.
Expand Down
139 changes: 77 additions & 62 deletions samples/solidity/contracts/ERC1155MixedFungible.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "./IERC1155MixedFungible.sol";
import '@openzeppelin/contracts/token/ERC1155/ERC1155.sol';
import '@openzeppelin/contracts/utils/Context.sol';
import '@openzeppelin/contracts/utils/math/SafeMath.sol';
import './IERC1155MixedFungible.sol';

/**
* Example ERC1155 with mixed fungible/non-fungible token support.
Expand Down Expand Up @@ -36,24 +36,24 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
uint256 constant TYPE_MASK = type(uint128).max << 128;
uint256 constant NF_INDEX_MASK = type(uint128).max;
uint256 constant TYPE_NF_BIT = 1 << 255;
uint256 constant NF_POOL_SIZE = 1 << 128;

uint256 nonce;
mapping (uint256 => address) public creators;
mapping (uint256 => uint256) public maxIndex;
mapping(uint256 => address) public creators;
mapping(uint256 => uint256) public maxIndex;

// 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;
mapping(uint256 => string) private _nfTokenURIs;

event TokenPoolCreation(address indexed operator, uint256 indexed type_id, bytes data);

function isFungible(uint256 id) internal pure returns(bool) {
function isFungible(uint256 id) internal pure returns (bool) {
return id & TYPE_NF_BIT == 0;
}
function isNonFungible(uint256 id) internal pure returns(bool) {

function isNonFungible(uint256 id) internal pure returns (bool) {
return id & TYPE_NF_BIT == TYPE_NF_BIT;
}

Expand All @@ -75,73 +75,85 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {
super.supportsInterface(interfaceId);
}

function create(bool is_fungible, bytes calldata data)
external
virtual
override
returns(uint256 type_id)
{
type_id = (++nonce << 128);
if (!is_fungible)
type_id = type_id | TYPE_NF_BIT;
function create(bool is_fungible, bytes calldata data) external virtual override {
uint256 type_id = (++nonce << 128);
if (!is_fungible) type_id = type_id | TYPE_NF_BIT;

creators[type_id] = _msgSender();

emit TokenPoolCreation(_msgSender(), type_id, data);
emit TokenPoolCreation(
_msgSender(),
is_fungible,
type_id,
is_fungible ? type_id : type_id + NF_POOL_SIZE - 1,
data
);
}

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");
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, bytes calldata data)
external
virtual
override
creatorOnly(type_id)
{
require(isNonFungible(type_id), "ERC1155MixedFungible: id does not represent a non-fungible type");
function mintNonFungible(
uint256 type_id,
address[] calldata to,
bytes calldata data
) external virtual override creatorOnly(type_id) {
require(
isNonFungible(type_id),
'ERC1155MixedFungible: id does not represent a non-fungible type'
);

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

for (uint256 i = 0; i < to.length; ++i) {
_mint(to[i], type_id | index + i, 1, data);
_mint(to[i], type_id | (index + i), 1, data);
}
}

function mintNonFungibleWithURI(uint256 type_id, address[] calldata to, bytes calldata data, string memory _uri)
external
virtual
override
creatorOnly(type_id)
{
require(isNonFungible(type_id), "ERC1155MixedFungible: id does not represent a non-fungible type");
function mintNonFungibleWithURI(
uint256 type_id,
address[] calldata to,
bytes calldata data,
string memory _uri
) external virtual override creatorOnly(type_id) {
require(
isNonFungible(type_id),
'ERC1155MixedFungible: id does not represent a non-fungible type'
);

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

for (uint256 i = 0; i < to.length; ++i) {
uint256 id = type_id | index + i;
uint256 id = type_id | (index + i);
_mint(to[i], id, 1, data);
_setNonFungibleURI(type_id, id, _uri);
}
}

function mintFungible(uint256 type_id, address[] calldata to, uint256[] calldata amounts, bytes calldata data)
external
virtual
override
creatorOnly(type_id)
{
require(isFungible(type_id), "ERC1155MixedFungible: id does not represent a fungible type");
require(to.length == amounts.length, "ERC1155MixedFungible: to and amounts length mismatch");
function mintFungible(
uint256 type_id,
address[] calldata to,
uint256[] calldata amounts,
bytes calldata data
) external virtual override creatorOnly(type_id) {
require(isFungible(type_id), 'ERC1155MixedFungible: id does not represent a fungible type');
require(
to.length == amounts.length,
'ERC1155MixedFungible: to and amounts length mismatch'
);

for (uint256 i = 0; i < to.length; ++i) {
_mint(to[i], type_id, amounts[i], data);
Expand All @@ -150,28 +162,31 @@ contract ERC1155MixedFungible is Context, ERC1155, IERC1155MixedFungible {

// Reference:
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/extensions/ERC1155Burnable.sol
function burn(address from, uint256 id, uint256 amount, bytes calldata data)
external
virtual
override
{
function burn(
address from,
uint256 id,
uint256 amount,
bytes calldata data
) external virtual override {
require(
from == _msgSender() || isApprovedForAll(from, _msgSender()),
"ERC1155MixedFungible: caller is not owner nor approved"
'ERC1155MixedFungible: caller is not owner nor approved'
);

_burn(from, id, amount);
}

function setApprovalForAllWithData(address operator, bool approved, bytes calldata data)
external
virtual
override
{
function setApprovalForAllWithData(
address operator,
bool approved,
bytes calldata data
) external virtual override {
setApprovalForAll(operator, approved);
}

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

Expand Down
23 changes: 23 additions & 0 deletions samples/solidity/contracts/IERC1155Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.0;

import '@openzeppelin/contracts/utils/introspection/IERC165.sol';

/**
* ERC1155 interface that supports creating partitions of fungible and non-fungible tokens.
* The implementation may decide how to allocate these "pools" of value, and should emit the
* TokenPoolCreation event to advertise each newly created partition (with start_id and end_id
* being the inclusive start and end indexes of the ERC1155 id space).
*/
interface IERC1155Factory is IERC165 {
event TokenPoolCreation(
address indexed operator,
bool indexed is_fungible,
uint256 start_id,
uint256 end_id,
bytes data
);

function create(bool is_fungible, bytes calldata data) external;
}
77 changes: 32 additions & 45 deletions samples/solidity/contracts/IERC1155MixedFungible.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,40 @@
pragma solidity ^0.8.0;

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 {
function create(
bool is_fungible,
bytes calldata data
) external returns (uint256);

function mintNonFungible(
uint256 type_id,
address[] calldata to,
bytes calldata data
) external;

function mintNonFungibleWithURI(
uint256 type_id,
address[] calldata to,
bytes calldata data,
string memory _uri
) external;

function mintFungible(
uint256 type_id,
address[] calldata to,
uint256[] calldata amounts,
bytes calldata data
) external;

function burn(
address from,
uint256 id,
uint256 amount,
bytes calldata data
) external;

function setApprovalForAllWithData(
address operator,
bool approved,
bytes calldata data
) external;

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

function baseTokenUri() external returns(string memory);
}
interface IERC1155MixedFungible is IERC165, IERC1155Factory {
function create(bool is_fungible, bytes calldata data) external override;

function mintNonFungible(uint256 type_id, address[] calldata to, bytes calldata data) external;

function mintNonFungibleWithURI(
uint256 type_id,
address[] calldata to,
bytes calldata data,
string memory _uri
) external;

function mintFungible(
uint256 type_id,
address[] calldata to,
uint256[] calldata amounts,
bytes calldata data
) external;

function burn(address from, uint256 id, uint256 amount, bytes calldata data) external;

function setApprovalForAllWithData(
address operator,
bool approved,
bytes calldata data
) external;

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

function baseTokenUri() external returns (string memory);
}
6 changes: 6 additions & 0 deletions samples/solidity/copyabi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -euo pipefail

ABIS=$(ls artifacts/contracts/*/*.json | grep -v ".dbg.json")
cp ${ABIS} ../../src/abi
Loading

0 comments on commit b5ca47f

Please sign in to comment.