-
Notifications
You must be signed in to change notification settings - Fork 208
/
ERC1155MixedFungibleMintable.sol
92 lines (67 loc) · 2.8 KB
/
ERC1155MixedFungibleMintable.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
pragma solidity ^0.5.0;
import "./ERC1155MixedFungible.sol";
/**
@dev Mintable form of ERC1155
Shows how easy it is to mint new items
*/
contract ERC1155MixedFungibleMintable is ERC1155MixedFungible {
uint256 nonce;
mapping (uint256 => address) public creators;
mapping (uint256 => uint256) public maxIndex;
modifier creatorOnly(uint256 _id) {
require(creators[_id] == msg.sender);
_;
}
// This function only creates the type.
function create(
string calldata _uri,
bool _isNF)
external returns(uint256 _type) {
// Store the type in the upper 128 bits
_type = (++nonce << 128);
// Set a flag if this is an NFI.
if (_isNF)
_type = _type | TYPE_NF_BIT;
// This will allow restricted access to creators.
creators[_type] = msg.sender;
// emit a Transfer event with Create semantic to help with discovery.
emit TransferSingle(msg.sender, address(0x0), address(0x0), _type, 0);
if (bytes(_uri).length > 0)
emit URI(_uri, _type);
}
function mintNonFungible(uint256 _type, address[] calldata _to) external creatorOnly(_type) {
// No need to check this is a nf type rather than an id since
// creatorOnly() will only let a type pass through.
require(isNonFungible(_type));
// Index are 1-based.
uint256 index = maxIndex[_type] + 1;
maxIndex[_type] = _to.length.add(maxIndex[_type]);
for (uint256 i = 0; i < _to.length; ++i) {
address dst = _to[i];
uint256 id = _type | index + i;
nfOwners[id] = dst;
// You could use base-type id to store NF type balances if you wish.
// balances[_type][dst] = quantity.add(balances[_type][dst]);
emit TransferSingle(msg.sender, address(0x0), dst, id, 1);
if (dst.isContract()) {
_doSafeTransferAcceptanceCheck(msg.sender, msg.sender, dst, id, 1, '');
}
}
}
function mintFungible(uint256 _id, address[] calldata _to, uint256[] calldata _quantities) external creatorOnly(_id) {
require(isFungible(_id));
for (uint256 i = 0; i < _to.length; ++i) {
address to = _to[i];
uint256 quantity = _quantities[i];
// Grant the items to the caller
balances[_id][to] = quantity.add(balances[_id][to]);
// Emit the Transfer/Mint event.
// the 0x0 source address implies a mint
// It will also provide the circulating supply info.
emit TransferSingle(msg.sender, address(0x0), to, _id, quantity);
if (to.isContract()) {
_doSafeTransferAcceptanceCheck(msg.sender, msg.sender, to, _id, quantity, '');
}
}
}
}