-
Notifications
You must be signed in to change notification settings - Fork 1
/
NFTMarket.sol
625 lines (571 loc) · 26.9 KB
/
NFTMarket.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//*~~~> SPDX-License-Identifier: MIT make it better, stronger, faster
/*~~~>
Thank you Phunks for your inspiration and phriendship.
Never stop phighting, never surrender, always stand up for what is right and make the best of all situations towards all people.
Phunks are phreedom phighters!
"When the power of love overcomes the love of power the world will know peace." - Jimi Hendrix <3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################%%%%%@@@@@((((((((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################%%%%%@@@@@((((((((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@########################################%%%%%@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@########################################%%%%%@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###############@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@###############@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@##########@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@PhunkyJON was here programming trustless unstoppable contracts@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@#PHUNKYJON///////////////#PHUNKYJON//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@#PHUNKYJON///////////////#PHUNKYJON//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////@EYES////////////////////@EYES///////////////@@@@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////@EYES////////////////////@EYES///////////////EAR@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////////////////////////////////////////////EAR@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////////////////////////////////////////////EAR@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@@@@@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@/////////////////////////////////////////////@@@@@@@@@@((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////NOSE@NOSE@////////////////////#####@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((SMOKE((((((((((@@@@@//////////NOSE@NOSE@////////////////////#####@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@#####//////////////////////////////##########@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((@@@@@#####//////////////////////////////##########@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@###################################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@###################################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((EMBER(((((,,,,,,,,,,,,,,,,,,,,,,,,,@@@@@MOUTH&&&&&####################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((EMBER(((((,,,,,,,,,,,,,,,,,,,,,,,,,@@@@@MOUTH&&&&&####################@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@##############################/////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((@SPLIFF@SPLIFF@SPLIFF@SPLIFF@@##############################/////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%(((((((((((((((((((((((((((((((((((@@@@@##############################//////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((@@@@@@@@@@@@@@@@@@@@@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((@@@@@///////////////@@@@@(((((((((((((((((((((((((%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@///////////////@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%@@@@@///////////////@@@@@%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
<~~~*/
pragma solidity 0.8.7;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "./interfaces/IEscrow.sol";
import "./interfaces/IRewardsController.sol";
import "./interfaces/IRoleProvider.sol";
interface IERC20 {
function transfer(address to, uint value) external returns (bool);
}
contract NFTMarket is ReentrancyGuard {
/*~~~>
Roles allow for designated accessibility
<~~~*/
bytes32 public constant PROXY_ROLE = keccak256("PROXY_ROLE");
bytes32 public constant DEV = keccak256("DEV");
address public roleAdd;
modifier hasAdmin(){
require(IRoleProvider(roleAdd).hasTheRole(PROXY_ROLE, msg.sender), "DOES NOT HAVE ADMIN ROLE");
_;
}
modifier hasContractAdmin(){
require(IRoleProvider(roleAdd).hasContractRole(msg.sender), "DOES NOT HAVE CONTRACT ROLE");
_;
}
modifier hasDevAdmin(){
require(IRoleProvider(roleAdd).hasTheRole(DEV, msg.sender), "DOES NOT HAVE DEV ROLE");
_;
}
/*~~~> increments NFT items upon creation <~~~*/
uint256 public itemIds;
//*~~~> global address variable from Role Provider contract
bytes32 public constant REWARDS = keccak256("REWARDS");
bytes32 public constant BIDS = keccak256("BIDS");
bytes32 public constant OFFERS = keccak256("OFFERS");
bytes32 public constant TRADES = keccak256("TRADES");
bytes32 public constant NFTADD = keccak256("NFT");
/*~~~> Open storage indexes <~~~*/
uint[] private openStorage;
/*~~~> Minimum listing value <~~~*/
uint public minVal;
/*~~~> sets deployment address as default admin role <~~~*/
constructor(address newrole) {
roleAdd = newrole;
minVal = 1e15;
}
/*~~~> Declaring object structures for listed items for sale <~~~*/
struct MktItem {
bool is1155;
uint itemId;
uint amount1155;
uint price;
uint tokenId;
address nftContract;
address payable seller;
}
/*~~~> Memory array of item id to market item <~~~*/
mapping(uint256 => MktItem) private idToMktItem;
// Maps the balance of items that the user has listed for sale
mapping(address => uint) public addressToUserBal;
/*~~~> Declaring event object structure for Nft Listed for sale <~~~*/
event ItemListed (
uint itemId,
uint amount1155,
uint price,
uint indexed tokenId,
address indexed nftContract,
address indexed seller
);
/*~~~> Declaring event object structures for delistings <~~~*/
event ItemDelisted(
uint indexed itemId,
uint indexed tokenId,
address indexed nftContract
);
/*~~~> Declaring event object structures for NFTs bought <~~~*/
event ItemBought(
uint itemId,
uint indexed tokenId,
address indexed nftContract,
address fromAddress,
address indexed toAddress
);
/*~~~> Declaring event object structure for Item price updated <~~~*/
event ItemUpdated(
uint itemId,
uint indexed tokenId,
uint price,
address indexed nftContract,
address indexed seller
);
//~~~> To set the minimum listing price
function setMinimumValue(uint minWei) external hasDevAdmin returns(bool){
minVal = minWei;
return true;
}
/// @notice
/*~~~> Public function to list NFTs for sale <~~~*/
///@dev
/*~~~>
is1155: (true) if item is ERC1155;
amount1155: amount of ERC1155 to trade;
tokenId: token Id of the item to list;
price: eth value wanted for purchase;
nftContract: contract address of item to list on the market;
<~~~*/
///@return Bool
function listMarketItems(
bool[] memory is1155,
uint[] memory amount1155,
uint[] memory tokenId,
uint[] memory price,
address[] memory nftContract
) external nonReentrant returns(bool){
require(tokenId.length>0);
require(tokenId.length == nftContract.length);
uint user = addressToUserBal[msg.sender];
if (user==0) {
require(IRewardsController(IRoleProvider(roleAdd).fetchAddress(REWARDS)).createUser(msg.sender));
}
uint tokenLen = tokenId.length;
for (uint i=0;i<tokenLen;i++){
require(price[i] >= minVal);
uint itemId;
uint len = openStorage.length;
if (len>=1){
itemId=openStorage[len-1];
_remove();
} else {
itemId = itemIds+=1;
}
if(!is1155[i]){
require(transferFromERC721(nftContract[i], tokenId[i], address(this)));
require(approveERC721(nftContract[i], address(this), tokenId[i]));
idToMktItem[itemId] = MktItem(false, itemId, amount1155[i], price[i], tokenId[i], nftContract[i], payable(msg.sender));
} else {
IERC1155(nftContract[i]).safeTransferFrom(msg.sender, address(this), tokenId[i], amount1155[i], "");
IERC1155(nftContract[i]).setApprovalForAll(address(this), true);
idToMktItem[itemId] = MktItem(true, itemId, amount1155[i], price[i], tokenId[i], nftContract[i], payable(msg.sender));
}
emit ItemListed(itemId, amount1155[i], price[i], tokenId[i], nftContract[i], msg.sender);
}
//*~~~> Add new count to user balances
addressToUserBal[msg.sender] += tokenLen;
return true;
}
/// @notice
/*~~~> Public function to delist NFTs <~~~*/
///@dev
/*~~~>
itemId: itemId for internal storage location;
<~~~*/
///@return Bool
function delistMarketItems(
uint256[] calldata itemId
) public nonReentrant returns(bool){
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
address bidsAdd = IRoleProvider(roleAdd).fetchAddress(BIDS);
address offersAdd = IRoleProvider(roleAdd).fetchAddress(OFFERS);
address tradesAdd = IRoleProvider(roleAdd).fetchAddress(TRADES);
for (uint i=0;i<itemId.length;i++){
MktItem memory it = idToMktItem[itemId[i]];
require(it.seller == msg.sender, "Not owner");
uint bidId = IBids(bidsAdd).fetchBidId(itemId[i]);
if (bidId>0) {
/*~~~> Kill bid and refund bidValue <~~~*/
//~~~> Call the contract to refund the ETH offered for a bid
require(IBids(bidsAdd).refundBid(bidId));
}
/*~~~> Check for the case where there is a trade and refund it. <~~~*/
uint offerId = IOffers(offersAdd).fetchOfferId(itemId[i]);
if (offerId > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the NFT offered for trade
require(IOffers(offersAdd).refundOffer(itemId[i], offerId));
}
/*~~~> Check for the case where there is an offer and refund it. <~~~*/
uint tradeId = ITrades(tradesAdd).fetchTradeId(itemId[i]);
if (tradeId > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the ERC20 offered for trade
require(ITrades(tradesAdd).refundTrade(itemId[i], tradeId));
}
if(it.is1155){
IERC1155(it.nftContract).safeTransferFrom(address(this), msg.sender, it.tokenId, it.amount1155, "");
} else {
require(transferERC721(it.nftContract, it.seller, it.tokenId));
}
openStorage.push(itemId[i]);
idToMktItem[itemId[i]] = MktItem(false, itemId[i], 0, 0, 0, address(0x0), payable(0x0));
emit ItemDelisted(itemId[i], it.tokenId, it.nftContract);
//*~~~> remove count from user balances
addressToUserBal[msg.sender] -= 1;
}
//*~~~> Check to see if user has any remaining items listed after iteration
if (addressToUserBal[msg.sender]==0){
//*~~~> If not, remove them from claims allowance
require(IRewardsController(rewardsAdd).setUser(false, msg.sender));
} else { //*~~~> Allow claims
require(IRewardsController(rewardsAdd).setUser(true, msg.sender));
}
return true;
}
/// @notice
/*~~~> Public function to buy(purchase) NFTs <~~~*/
///@dev
/*~~~>
itemId: itemId for internal storage location;
<~~~*/
///@return Bool
function buyMarketItems(
uint256[] memory itemId
) public payable nonReentrant returns(bool) {
address bidsAdd = IRoleProvider(roleAdd).fetchAddress(BIDS);
address offersAdd = IRoleProvider(roleAdd).fetchAddress(OFFERS);
address tradesAdd = IRoleProvider(roleAdd).fetchAddress(TRADES);
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
uint balance = IERC721(IRoleProvider(roleAdd).fetchAddress(NFTADD)).balanceOf(msg.sender);
uint prices=0;
uint length = itemId.length;
for (uint i=0; i < length; i++) {
MktItem memory it = idToMktItem[itemId[i]];
prices += it.price;
}
require(msg.value == prices);
for (uint i=0; i<length; i++) {
MktItem memory it = idToMktItem[itemId[i]];
if(balance<1){
/*~~~> Calculating the platform fee <~~~*/
uint256 saleFee = calcFee(it.price);
uint256 userAmnt = it.price - saleFee;
// send saleFee to rewards controller
require(sendEther(rewardsAdd, saleFee));
// send (listed amount - saleFee) to seller
require(sendEther(it.seller, useramnt));
} else {
require(sendEther(it.seller, it.price));
}
if (IBids(bidsAdd).fetchBidId(itemId[i])>0) {
/*~~~> Kill bid and refund bidValue <~~~*/
//~~~> Call the contract to refund the ETH offered for a bid
require(IBids(bidsAdd).refundBid(IBids(bidsAdd).fetchBidId(itemId[i])));
}
/*~~~> Check for the case where there is a trade and refund it. <~~~*/
if (IOffers(offersAdd).fetchOfferId(itemId[i]) > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the NFT offered for trade
require(IOffers(offersAdd).refundOffer(itemId[i], IOffers(offersAdd).fetchOfferId(itemId[i])));
}
/*~~~> Check for the case where there is an offer and refund it. <~~~*/
if (ITrades(tradesAdd).fetchTradeId(itemId[i]) > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the ERC20 offered for trade
require(ITrades(tradesAdd).refundTrade(itemId[i], ITrades(tradesAdd).fetchTradeId(itemId[i])));
}
addressToUserBal[it.seller] -= 1;
emit ItemBought(itemId[i], it.tokenId, it.nftContract, it.seller, msg.sender);
if(it.is1155){
IERC1155(it.nftContract).safeTransferFrom(address(this), msg.sender, it.tokenId, it.amount1155, "");
idToMktItem[itemId[i]] = MktItem(true, itemId[i], 0, 0, 0, address(0x0), payable(0x0));
} else {
require(transferERC721(it.nftContract, msg.sender, it.tokenId));
idToMktItem[itemId[i]] = MktItem(false, itemId[i], 0, 0, 0, address(0x0), payable(0x0));
}
openStorage.push(itemId[i]);
//*~~~> Check to see if user has any remaining items listed after iteration
if (addressToUserBal[it.seller]==0){
//*~~~> If not, remove them from claims allowance
require(IRewardsController(rewardsAdd).setUser(false, it.seller));
} else { //*~~~> Allow claims
require(IRewardsController(rewardsAdd).setUser(true, it.seller));
}
}
return true;
}
/// @notice
/*~~~>
Internal function to transferFrom ERC721 NFTs, including crypto kitties/punks
<~~~*/
/// @dev
/*~~~>
assetAddr: address of the token to be transfered;
tokenId: Id of the token to be transfered;
to: address of recipient;
<~~~*/
function transferFromERC721(address assetAddr, uint256 tokenId, address to) internal virtual returns(bool) {
address kitties = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
address punks = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
bytes memory data;
if (assetAddr == kitties) {
// Cryptokitties.
data = abi.encodeWithSignature("transferFrom(address,address,uint256)", msg.sender, to, tokenId);
} else if (assetAddr == punks) {
// CryptoPunks.
bytes memory punkIndexToAddress = abi.encodeWithSignature("punkIndexToAddress(uint256)", tokenId);
(bool checkSuccess, bytes memory result) = address(assetAddr).staticcall(punkIndexToAddress);
(address nftOwner) = abi.decode(result, (address));
require(checkSuccess && nftOwner == msg.sender, "Not the NFT owner");
data = abi.encodeWithSignature("transferPunk(address,uint256)", msg.sender, tokenId);
} else {
// Default.
// We push to avoid an unneeded transfer.
data = abi.encodeWithSignature("safeTransferFrom(address,address,uint256)", msg.sender, to, tokenId);
}
(bool success, bytes memory resultData) = address(assetAddr).call(data);
require(success, string(resultData));
return true;
}
/// @notice
/*~~~>
Internal function to approve ERC721 NFTs for transfer, including crypto kitties/punks
<~~~*/
/// @dev
/*~~~>
assetAddr: address of the token to be transfered;
to: address of recipient;
tokenId: Id of the token to be transfered;
<~~~*/
function approveERC721(address assetAddr, address to, uint256 tokenId) internal virtual returns(bool) {
address punks = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
bytes memory data;
if (assetAddr == punks) {
// CryptoPunks.
data = abi.encodeWithSignature("offerPunkForSaleToAddress(uint256,uint256,address)", tokenId, 0, to);
(bool success, bytes memory resultData) = address(assetAddr).call(data);
require(success, string(resultData));
} else {
data = abi.encodeWithSignature("approve(address,uint256)", to, tokenId);
}
return(true);
}
/// @notice
/*~~~>
Internal function to transfer ERC721 NFTs, including crypto kitties/punks
<~~~*/
/// @dev
/*~~~>
assetAddr: address of the token to be transfered;
to: address of the recipient;
tokenId: Id of the token to be transfered;
<~~~*/
function transferERC721(address assetAddr, address to, uint256 tokenId) internal virtual returns(bool) {
address kitties = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
address punks = 0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB;
bytes memory data;
if (assetAddr == kitties) {
// Changed in v1.0.4.
data = abi.encodeWithSignature("transfer(address,uint256)", to, tokenId);
} else if (assetAddr == punks) {
// CryptoPunks.
data = abi.encodeWithSignature("transferPunk(address,uint256)", to, tokenId);
} else {
// Default.
data = abi.encodeWithSignature("safeTransferFrom(address,address,uint256)", address(this), to, tokenId);
}
(bool success, bytes memory returnData) = address(assetAddr).call(data);
require(success, string(returnData));
return true;
}
/// @notice
/*~~~>
Internal function for sending ether
<~~~*/
/// @return Bool
function sendEther(address recipient, uint ethvalue) internal nonReentrant returns (bool){
(bool success, bytes memory data) = address(recipient).call{value: ethvalue}("");
return(success);
}
/// @notice
/*~~~>
Calculating the platform fee,
Base fee set at 2% (i.e. value * 200 / 10,000)
Future fees can be set by the controlling DAO
<~~~*/
/// @return platform fee
function calcFee(uint256 value) internal returns (uint256) {
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
uint fee = IRewardsController(rewardsAdd).getFee();
uint256 percent = ((value * fee) / 10000);
return percent;
}
///@notice
/*~~~> Function to update price, only seller can call <~~~*/
///@dev
/*~~~>
itemId: internal id of item listed for sale;
_price: market price update
<~~~*/
function updateMarketItemPrice(uint itemId, uint price) external nonReentrant {
MktItem memory it = idToMktItem[itemId];
require(msg.sender == it.seller);
idToMktItem[it.itemId] = MktItem(it.is1155, it.itemId, price, it.amount1155, it.tokenId, it.nftContract, it.seller);
emit ItemUpdated(itemId, it.tokenId, price, it.nftContract, it.seller);
}
///@notice //*~~~> Read functions for internal contract state
function fetchMarketItems() public view returns (MktItem[] memory) {
uint itemCount = itemIds;
MktItem[] memory items = new MktItem[](itemCount);
for (uint i = 0; i < itemCount; i++) {
if (idToMktItem[i + 1].itemId > 0) {
MktItem storage currentItem = idToMktItem[i + 1];
items[i] = currentItem;
}
}
return items;
}
function fetchItemsBySeller(address userAdd) public view returns (MktItem[] memory) {
uint itemCount = itemIds;
MktItem[] memory items = new MktItem[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToMktItem[i + 1].seller == userAdd) {
MktItem storage currentItem = idToMktItem[i + 1];
items[i] = currentItem;
}
}
return items;
}
function fetchAmountListed(address userAdd) public view returns (uint howMany){
uint user = addressToUserBal[userAdd];
return user;
}
/// @notice
/*~~~>
Internal function for removing elements from an array
Only used for internal storage array index recycling
In order to reduce storage array size of listed items
while maintaining specific enumerable bidId's,
any sold or removed item spots are recycled by referring to their index,
else a new storage spot is created;
We use the last item in the storage (length of array - 1 for 0 based index position),
in order to pop off the item and avoid rewriting
<~~~*/
function _remove() internal {
openStorage.pop();
}
///@notice
/*~~~> ERC20 withdraw ability for funds accidentally sent
Only ETH is exchanged here, so there is no avenue for attack.
<~~~*/
function withdrawToken(address receiver, address tokenContract, uint256 amount) external nonReentrant hasDevAdmin returns(bool) {
require(IERC20(tokenContract).transfer(receiver, amount));
return true;
}
///@notice internal function to transfer NFT only this contract can call
function transferForSale(address receiver, uint itemId) internal {
address bidsAdd = IRoleProvider(roleAdd).fetchAddress(BIDS);
address tradesAdd = IRoleProvider(roleAdd).fetchAddress(TRADES);
address offersAdd = IRoleProvider(roleAdd).fetchAddress(OFFERS);
MktItem memory it = idToMktItem[itemId];
if ( it.is1155 ){
IERC1155(it.nftContract).safeTransferFrom(address(this), payable(receiver), it.tokenId, it.amount1155, "");
} else {
require(transferERC721(it.nftContract, payable(receiver), it.tokenId));
}
uint bidId = IBids(bidsAdd).fetchBidId(itemId);
if (bidId>0) {
/*~~~> Kill bid and refund bidValue <~~~*/
//~~~> Call the contract to refund the ETH offered for a bid
require(IBids(bidsAdd).refundBid(bidId));
}
/*~~~> Check for the case where there is a trade and refund it. <~~~*/
uint offerId = IOffers(offersAdd).fetchOfferId(itemId);
if (offerId > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the NFT offered for trade
require(IOffers(offersAdd).refundOffer(itemId, offerId));
}
/*~~~> Check for the case where there is an offer and refund it. <~~~*/
uint tradeId = ITrades(tradesAdd).fetchTradeId(itemId);
if (tradeId > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the ERC20 offered for trade
require(ITrades(tradesAdd).refundTrade(itemId, tradeId));
}
openStorage.push(itemId);
idToMktItem[itemId] = MktItem(false, itemId, 0, 0, 0, address(0x0), payable(0x0));
emit ItemBought(itemId, it.tokenId, it.nftContract, it.seller, receiver);
}
///@notice external function to transfer NFT
/*~~~>
Only marketplace proxy contracts can call the function.
<~~~*/
function transferNftForSale(address receiver, uint itemId) public nonReentrant hasContractAdmin returns(bool) {
transferForSale(receiver, itemId);
return true;
}
/// @notice
/*~~~>
Internal function for sending ether
<~~~*/
/// @return Bool
function sendEther(address recipient, uint ethvalue) internal nonReentrant returns (bool){
(bool success, bytes memory data) = address(recipient).call{value: ethvalue}("");
return(success);
}
//*~~~> Fallback functions
///@notice
/*~~~> External ETH transfer without function call forwarded to role provider contract <~~~*/
event FundsForwarded(uint value, address from, address to);
receive() external payable {
require(sendEther(roleAdd, msg.value));
emit FundsForwarded(msg.value, msg.sender, roleAdd);
}
function onERC1155Received(address, address, uint256, uint256, bytes memory) public virtual returns (bytes4) {
return this.onERC1155Received.selector;
}
function onERC721Received(
address,
address,
uint256,
bytes calldata
)external pure returns(bytes4) {
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
}