-
Notifications
You must be signed in to change notification settings - Fork 54
/
ERC721XToken.sol
213 lines (171 loc) · 7.77 KB
/
ERC721XToken.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
pragma solidity ^0.5.6;
import "./../../Interfaces/ERC721X.sol";
import "./../../Interfaces/ERC721XReceiver.sol";
import "./ERC721XTokenNFT.sol";
import "openzeppelin-solidity/contracts/utils/Address.sol";
import "./../../Libraries/ObjectsLib.sol";
// Additional features over NFT token that is compatible with batch transfers
contract ERC721XToken is ERC721X, ERC721XTokenNFT {
using ObjectLib for ObjectLib.Operations;
using Address for address;
bytes4 internal constant ERC721X_RECEIVED = 0x660b3370;
bytes4 internal constant ERC721X_BATCH_RECEIVE_SIG = 0xe9e5be6a;
event BatchTransfer(address from, address to, uint256[] tokenTypes, uint256[] amounts);
constructor(string memory _baseTokenURI) public ERC721XTokenNFT(_baseTokenURI) {}
modifier isOperatorOrOwner(address _from) {
require((msg.sender == _from) || operators[_from][msg.sender], "msg.sender is neither _from nor operator");
_;
}
function implementsERC721X() public pure returns (bool) {
return true;
}
/**
* @dev transfer objects from different tokenIds to specified address
* @param _from The address to BatchTransfer objects from.
* @param _to The address to batchTransfer objects to.
* @param _tokenIds Array of tokenIds to update balance of
* @param _amounts Array of amount of object per type to be transferred.
* Note: Arrays should be sorted so that all tokenIds in a same bin are adjacent (more efficient).
*/
function _batchTransferFrom(address _from, address _to, uint256[] memory _tokenIds, uint256[] memory _amounts)
internal
isOperatorOrOwner(_from)
{
// Requirements
require(_tokenIds.length == _amounts.length, "Inconsistent array length between args");
require(_to != address(0), "Invalid recipient");
if (tokenType[_tokenIds[0]] == NFT) {
tokenOwner[_tokenIds[0]] = _to;
emit Transfer(_from, _to, _tokenIds[0]);
}
// Load first bin and index where the object balance exists
(uint256 bin, uint256 index) = ObjectLib.getTokenBinIndex(_tokenIds[0]);
// Balance for current bin in memory (initialized with first transfer)
// Written with bad library syntax instead of as below to bypass stack limit error
uint256 balFrom = ObjectLib.updateTokenBalance(
packedTokenBalance[_from][bin], index, _amounts[0], ObjectLib.Operations.SUB
);
uint256 balTo = ObjectLib.updateTokenBalance(
packedTokenBalance[_to][bin], index, _amounts[0], ObjectLib.Operations.ADD
);
// Number of transfers to execute
uint256 nTransfer = _tokenIds.length;
// Last bin updated
uint256 lastBin = bin;
for (uint256 i = 1; i < nTransfer; i++) {
// If we're transferring an NFT we additionally should update the tokenOwner and emit the corresponding event
if (tokenType[_tokenIds[i]] == NFT) {
tokenOwner[_tokenIds[i]] = _to;
emit Transfer(_from, _to, _tokenIds[i]);
}
(bin, index) = _tokenIds[i].getTokenBinIndex();
// If new bin
if (bin != lastBin) {
// Update storage balance of previous bin
packedTokenBalance[_from][lastBin] = balFrom;
packedTokenBalance[_to][lastBin] = balTo;
// Load current bin balance in memory
balFrom = packedTokenBalance[_from][bin];
balTo = packedTokenBalance[_to][bin];
// Bin will be the most recent bin
lastBin = bin;
}
// Update memory balance
balFrom = balFrom.updateTokenBalance(index, _amounts[i], ObjectLib.Operations.SUB);
balTo = balTo.updateTokenBalance(index, _amounts[i], ObjectLib.Operations.ADD);
}
// Update storage of the last bin visited
packedTokenBalance[_from][bin] = balFrom;
packedTokenBalance[_to][bin] = balTo;
// Emit batchTransfer event
emit BatchTransfer(_from, _to, _tokenIds, _amounts);
}
function batchTransferFrom(address _from, address _to, uint256[] memory _tokenIds, uint256[] memory _amounts) public {
// Batch Transfering
_batchTransferFrom(_from, _to, _tokenIds, _amounts);
}
/**
* @dev transfer objects from different tokenIds to specified address
* @param _from The address to BatchTransfer objects from.
* @param _to The address to batchTransfer objects to.
* @param _tokenIds Array of tokenIds to update balance of
* @param _amounts Array of amount of object per type to be transferred.
* @param _data Data to pass to onERC721XReceived() function if recipient is contract
* Note: Arrays should be sorted so that all tokenIds in a same bin are adjacent (more efficient).
*/
function safeBatchTransferFrom(
address _from,
address _to,
uint256[] memory _tokenIds,
uint256[] memory _amounts,
bytes memory _data
)
public
{
// Batch Transfering
_batchTransferFrom(_from, _to, _tokenIds, _amounts);
// Pass data if recipient is contract
if (_to.isContract()) {
bytes4 retval = ERC721XReceiver(_to).onERC721XBatchReceived(
msg.sender, _from, _tokenIds, _amounts, _data
);
require(retval == ERC721X_BATCH_RECEIVE_SIG);
}
}
function transfer(address _to, uint256 _tokenId, uint256 _amount) public {
_transferFrom(msg.sender, _to, _tokenId, _amount);
}
function transferFrom(address _from, address _to, uint256 _tokenId, uint256 _amount) public {
_transferFrom(_from, _to, _tokenId, _amount);
}
function _transferFrom(address _from, address _to, uint256 _tokenId, uint256 _amount)
internal
isOperatorOrOwner(_from)
{
require(tokenType[_tokenId] == FT);
require(_amount <= balanceOf(_from, _tokenId), "Quantity greater than from balance");
require(_to != address(0), "Invalid to address");
_updateTokenBalance(_from, _tokenId, _amount, ObjectLib.Operations.SUB);
_updateTokenBalance(_to, _tokenId, _amount, ObjectLib.Operations.ADD);
emit TransferWithQuantity(_from, _to, _tokenId, _amount);
}
function safeTransferFrom(address _from, address _to, uint256 _tokenId, uint256 _amount) public {
safeTransferFrom(_from, _to, _tokenId, _amount, "");
}
function safeTransferFrom(address _from, address _to, uint256 _tokenId, uint256 _amount, bytes memory _data) public {
_transferFrom(_from, _to, _tokenId, _amount);
require(
checkAndCallSafeTransfer(_from, _to, _tokenId, _amount, _data),
"Sent to a contract which is not an ERC721X receiver"
);
}
function _mint(uint256 _tokenId, address _to, uint256 _supply) internal {
// If the token doesn't exist, add it to the tokens array
if (!exists(_tokenId)) {
tokenType[_tokenId] = FT;
allTokens.push(_tokenId);
} else {
// if the token exists, it must be a FT
require(tokenType[_tokenId] == FT, "Not a FT");
}
_updateTokenBalance(_to, _tokenId, _supply, ObjectLib.Operations.ADD);
emit TransferWithQuantity(address(this), _to, _tokenId, _supply);
}
function checkAndCallSafeTransfer(
address _from,
address _to,
uint256 _tokenId,
uint256 _amount,
bytes memory _data
)
internal
returns (bool)
{
if (!_to.isContract()) {
return true;
}
bytes4 retval = ERC721XReceiver(_to).onERC721XReceived(
msg.sender, _from, _tokenId, _amount, _data);
return(retval == ERC721X_RECEIVED);
}
}