-
Notifications
You must be signed in to change notification settings - Fork 1
/
MarketTrades.sol
806 lines (758 loc) · 29.9 KB
/
MarketTrades.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
//*~~~> 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/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";
import "./interfaces/IEscrow.sol";
import "./interfaces/INFTMarket.sol";
import "./interfaces/IRoleProvider.sol";
contract MarketTrades is ReentrancyGuard {
//*~~~> counter increments NFTs Trade Offers
uint private tradeCount;
uint private blindTradeCount;
//*~~~> Roles for designated accessibility
bytes32 public constant PROXY_ROLE = keccak256("PROXY_ROLE");
bytes32 public constant DEV = keccak256("DEV");
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");
_;
}
//*~~~> Upgradable addresses
address public roleAdd;
//*~~~> global address variable from Role Provider contract
bytes32 public constant MARKET = keccak256("MARKET");
bytes32 public constant BIDS = keccak256("BIDS");
bytes32 public constant OFFERS = keccak256("OFFERS");
uint[] private openStorage;
uint[] private blindOpenStorage;
//*~~~> Fee constructor initially set to .005%
constructor(address role){
roleAdd = role;
}
//*~~~> Declaring object struct for trades entered
struct Trade {
bool is1155;
uint itemId;
uint tradeId;
uint tokenId;
uint amount1155;
address nftCont;
address trader;
address seller;
}
struct BlindTrade {
bool is1155;
bool isWant1155;
bool isActive;
bool isSpecific;
uint wantedId;
uint tradeId;
uint tokenId;
uint amount1155;
uint wAmount1155;
address nftCont;
address wantCont;
address trader;
}
//*~~~> Memory array of all Trades
mapping (uint256 => Trade) private idToNftTrade;
mapping (uint256 => BlindTrade) private idToBlindTrade;
mapping (uint => uint) private marketIdToTradeId;
//*~~~> Declaring event object structure for trade entered
event TradeEntered(
bool is1155,
uint itemId,
uint tradeId,
uint tokenId,
uint amount1155,
address indexed nftCont,
address indexed trader,
address indexed seller
);
event BlindTradeEntered(
bool isWanted1155,
bool isGiven1155,
bool isSpecific,
uint wantedId,
uint tokenId,
uint tradeId,
uint amount1155,
uint wAmount1155,
address indexed nftCont,
address indexed wantCont,
address indexed trader
);
//*~~~> Declaring event object structure for trade withdrawn
event TradeWithdrawn(
bool is1155,
uint itemId,
uint tradeId,
uint indexed tokenId,
address indexed nftCont,
address indexed trader
);
//*~~~> Declaring event object structure for trade accepted
event TradeAccepted(
bool is1155,
bool isActive,
uint indexed itemId,
uint indexed tradeId,
uint tokenId,
address nftCont,
address indexed trader,
address seller
);
//*~~~> Declaring event object structure for trade updated
event TradeUpdated(
bool is1155,
bool isActive,
uint indexed itemId,
uint indexed tradeId,
uint tokenId,
address nftCont,
address indexed trader,
address seller
);
///@notice
/*~~~>
Public function to enter a trade of an ERC721 or ERC1155 NFT for any item listed on market
<~~~*/
///@dev
/*~~~>
amount1155: how many ERC1155 to be offered for trade;
itemId: Market contract internal state itemId;
tokenId: specific token Id to trade listed item for;
nftContract: contract address of the NFT to trade;
seller: ownerOf NFT desired;
<~~~*/
function enterTrade(
uint[] memory amount1155,
uint[] memory itemId,
uint[] memory tokenId,
address[] memory nftContract,
address[] memory seller
) external nonReentrant returns(bool){
for (uint i=0;i<itemId.length;i++) {
uint tradeId;
if (openStorage.length>=1) {
tradeId = openStorage[openStorage.length-1];
_remove(0);
} else {
tradeId = tradeCount+=1;
}
if (amount1155[i]>0){
IERC1155(nftContract[i]).safeTransferFrom(msg.sender, address(this), tokenId[i], amount1155[i], "");
IERC1155(nftContract[i]).setApprovalForAll(address(this), true);
marketIdToTradeId[itemId[i]] = tradeId;
idToNftTrade[tradeId] = Trade(true, itemId[i], tradeId, tokenId[i], amount1155[i], nftContract[i], msg.sender, seller[i]);
emit TradeEntered(
true,
itemId[i],
tradeId,
tokenId[i],
amount1155[i],
nftContract[i],
msg.sender,
seller[i]);
} else {
require(transferFromERC721(nftContract[i], tokenId[i], address(this)));
require(approveERC721(nftContract[i], address(this), tokenId[i]));
marketIdToTradeId[itemId[i]] = tradeId;
idToNftTrade[tradeId] = Trade(false, itemId[i], tradeId, tokenId[i], amount1155[i], nftContract[i], msg.sender, seller[i]);
emit TradeEntered(
false,
itemId[i],
tradeId,
tokenId[i],
amount1155[i],
nftContract[i],
msg.sender,
seller[i]);
}
}
return true;
}
///@notice
//*~~~> Public function to enter blind trades
///@dev
/*~~~>
let Given = nft given to trade
let Wanted = nft wanted from trade
isWanted1155: (true) if Wanted is ERC1155;
isGiven1155: (true) if Given is ERC1155;
isSpecific: (true) if Wanted is specific;
wantedId: token Id of the Wanted;
tokenId: token Id of the Given;
amount1155: how many Given 1155;
wAmount1155: how many Wanted 1155;
nftContract: Given contract address;
wantContract: Wanted contract address;
<~~~*/
function enterBlindTrade(
bool[] memory isWanted1155,
bool[] memory isGiven1155,
bool[] memory isSpecific,
uint[] memory wantedId,
uint[] memory tokenId,
uint[] memory amount1155,
uint[] memory wAmount1155,
address[] memory nftContract,
address[] memory wantContract
) external nonReentrant{
for (uint i=0;i<tokenId.length;i++) {
uint tradeId;
if (blindOpenStorage.length>=1) {
tradeId = blindOpenStorage[blindOpenStorage.length-1];
_remove(1);
} else {
tradeId = blindTradeCount+=1;
}
if (isGiven1155[i]){
IERC1155(nftContract[i]).safeTransferFrom(msg.sender, address(this), tokenId[i], amount1155[i], "");
IERC1155(nftContract[i]).setApprovalForAll(address(this), true);
} else {
require(transferFromERC721(nftContract[i], tokenId[i], address(this)));
require(approveERC721(nftContract[i], address(this), tokenId[i]));
}
idToBlindTrade[tradeId] = BlindTrade(isWanted1155[i], isGiven1155[i], true, isSpecific[i], wantedId[i], tradeId, tokenId[i], amount1155[i], wAmount1155[i], nftContract[i], wantContract[i], msg.sender);
emit BlindTradeEntered(
isWanted1155[i],
isGiven1155[i],
isSpecific[i],
wantedId[i],
tokenId[i],
tradeId,
amount1155[i],
wAmount1155[i],
nftContract[i],
wantContract[i],
msg.sender);
}
}
///@notice
//*~~~>Public function to withdraw trade
///@dev
/*~~~>
isBlind: (true) if trade is blind;
itemId: Market internal storage id;
tradeId: internal this storade id;
<~~~*/
///@return Bool
function withdrawTrade(
bool[] memory isBlind,
uint[] memory itemId,
uint[] memory tradeId
) external nonReentrant returns(bool){
for (uint i=0; i<itemId.length; i++) {
if(isBlind[i]){
BlindTrade memory trade = idToBlindTrade[tradeId[i]];
require(trade.isActive == true, "Item is not listed for trade...");
if (trade.trader != msg.sender) revert();
if ( trade.is1155 ){
IERC1155(trade.nftCont).safeTransferFrom(address(this), trade.trader, trade.tokenId, trade.amount1155, "");
} else {
require(transferERC721(trade.nftCont, trade.trader, trade.tokenId));
}
blindOpenStorage.push(tradeId[i]);
idToBlindTrade[tradeId[i]] = BlindTrade(false, false, false, false, 0, 0, 0, 0, 0, address(0x0), address(0x0), address(0x0));
emit TradeWithdrawn(
false,
itemId[i],
tradeId[i],
trade.tokenId,
trade.nftCont,
trade.trader
);
} else {
Trade memory trade = idToNftTrade[tradeId[i]];
require(trade.tradeId > 0, "Item is not listed for trade...");
if (trade.trader != msg.sender) revert();
if ( trade.is1155 ){
IERC1155(trade.nftCont).safeTransferFrom(address(this), msg.sender, trade.tokenId, trade.amount1155, "");
} else {
require(transferERC721(trade.nftCont, trade.trader, trade.tokenId));
}
openStorage.push(tradeId[i]);
marketIdToTradeId[itemId[i]] = 0;
idToNftTrade[tradeId[i]] = Trade(false, 0, 0, 0, 0, address(0x0), address(0x0), address(0x0));
emit TradeWithdrawn(
false,
itemId[i],
tradeId[i],
trade.tokenId,
trade.nftCont,
trade.trader
);
}
}
return true;
}
///@notice
//*~~~>Function to refund trade if the item sells
///@dev
/*~~~>
itemId: Market item id internal storage;
tradeId: trade item id for this internal storage;
<~~~*/
///@return Bool
function refundTrade(uint itemId, uint tradeId) public nonReentrant hasContractAdmin returns(bool){
Trade memory trade = idToNftTrade[tradeId];
if ( trade.is1155 ){
IERC1155(trade.nftCont).safeTransferFrom(address(this), trade.trader, trade.tokenId, trade.amount1155, "");
} else {
require(transferERC721(trade.nftCont, trade.trader, trade.tokenId));
}
idToNftTrade[tradeId] = Trade(false, 0, 0, 0, 0, address(0x0), address(0x0), address(0x0));
openStorage.push(tradeId);
emit TradeUpdated(
trade.is1155,
false,
itemId,
tradeId,
trade.tokenId,
trade.nftCont,
trade.trader,
trade.seller
);
return true;
}
///@notice
//*~~~>Internal function to refund trade if the item sells
///@dev
/*~~~> The contract will throw an access control error if not done internally
itemId: Market item id internal storage;
tradeId: trade item id for this internal storage;
<~~~*/
function _refundTradeFromSale(uint itemId, uint tradeId) internal returns(bool){
Trade memory trade = idToNftTrade[tradeId];
if ( trade.is1155 ){
IERC1155(trade.nftCont).safeTransferFrom(address(this), trade.trader, trade.tokenId, trade.amount1155, "");
} else {
require(transferERC721(trade.nftCont, trade.trader, trade.tokenId));
}
idToNftTrade[tradeId] = Trade(false, 0, 0, 0, 0, address(0x0), address(0x0), address(0x0));
openStorage.push(tradeId);
emit TradeUpdated(
trade.is1155,
false,
itemId,
tradeId,
trade.tokenId,
trade.nftCont,
trade.trader,
trade.seller
);
return true;
}
///@notice
/*~~~>
Public function to accept trade
<~~~*/
///@notice
/*~~~>
itemId: Market item Id internal storage;
tradeId: Id of trade for internal storage;
<~~~*/
///@return Bool
function acceptTrade(
uint[] calldata itemId,
uint[] calldata tradeId
) public nonReentrant returns(bool){
address marketAdd = IRoleProvider(roleAdd).fetchAddress(MARKET);
address bidsAdd = IRoleProvider(roleAdd).fetchAddress(BIDS);
address offersAdd = IRoleProvider(roleAdd).fetchAddress(OFFERS);
for(uint i=0; i<itemId.length;i++) {
Trade memory trade = idToNftTrade[tradeId[i]];
require(msg.sender == trade.seller,"Not Owner");
if ( trade.is1155 ){
IERC1155(trade.nftCont).safeTransferFrom(address(this), trade.seller, trade.tokenId, trade.amount1155, "");
} else {
require(transferERC721(trade.nftCont, trade.seller, trade.tokenId));
}
/*~~~> 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));
}
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));
}
openStorage.push(tradeId[i]);
marketIdToTradeId[itemId[i]] = 0;
idToNftTrade[tradeId[i]] = Trade(
false,
0,
0,
0,
0,
address(0x0),
address(0x0),
address(0x0)
);
Trade[] memory trades = fetchTradesById(itemId[i]);
for(uint j; j<trades.length;j++){
require(_refundTradeFromSale(trades[i].itemId, trades[i].tradeId));
}
require(INFTMarket(marketAdd).transferNftForSale(trade.trader, itemId[i]));
emit TradeAccepted(
trade.is1155,
false,
itemId[i],
tradeId[i],
trade.tokenId,
trade.nftCont,
trade.trader,
trade.seller
);
}
return true;
}
///@notice
/*~~~>
Public function to accept trade
<~~~*/
///@notice
/*~~~>
tradeId: Id of trade for internal storage;
tokenId: (if specific) token id of the wanted NFT;
listedId: (0) if the item is not listed for sale on the marketplace contract;
<~~~*/
///@return Bool
function acceptBlindTrade(
uint[] memory tradeId,
uint[] memory tokenId,
uint[] memory listedId
) public nonReentrant returns(bool){
address marketAdd = IRoleProvider(roleAdd).fetchAddress(MARKET);
for(uint i=0; i<tradeId.length;i++) {
uint j = tradeId[i];
BlindTrade memory trade = idToBlindTrade[j];
//*~~~> Disallow random acceptances if specific
if(trade.isSpecific){
require(tokenId[i]==trade.wantedId,"Wrong item!");
}
/// Send the given NFT
if (trade.is1155){
if(listedId[i]==0){
IERC1155(trade.nftCont).safeTransferFrom(address(this), msg.sender, trade.tokenId, trade.amount1155, "");
} else {
require(INFTMarket(marketAdd).transferNftForSale(msg.sender, listedId[i]));
}
} else {
if(listedId[i]==0){
require(transferERC721(trade.nftCont, msg.sender, trade.tokenId));
} else {
require(INFTMarket(marketAdd).transferNftForSale(msg.sender, listedId[i]));
}
}
/// Send the wanted NFT
if (trade.isWant1155){
IERC1155(trade.wantCont).safeTransferFrom(msg.sender, trade.trader, tokenId[i], trade.wAmount1155, "");
} else {
require(IERC721(trade.wantCont).ownerOf(trade.wantedId) == msg.sender, "Not the token owner!");
require(transferERC721(trade.wantCont, trade.trader, tokenId[i]));
}
blindOpenStorage.push(tradeId[i]);
idToBlindTrade[tradeId[i]] = BlindTrade(
false,
false,
false,
false,
0,
0,
0,
0,
0,
address(0x0),
address(0x0),
address(0x0)
);
emit TradeAccepted(
trade.is1155,
false,
listedId[i],
tradeId[i],
tokenId[i],
trade.nftCont,
trade.trader,
msg.sender
);
}
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.
// Fix here for frontrun attack. Added in v1.0.2.
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 the vault 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
/* Public read functions for internal state */
function fetchTrades() public view returns (Trade[] memory) {
uint itemCount = tradeCount;
uint currentIndex;
Trade[] memory trades = new Trade[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToNftTrade[i + 1].tradeId > 0) {
Trade storage currentItem = idToNftTrade[i + 1];
trades[currentIndex] = currentItem;
currentIndex++;
}
}
return trades;
}
function fetchUserTrades(address user) public view returns (Trade[] memory) {
uint itemCount = tradeCount;
uint currentIndex;
Trade[] memory trades = new Trade[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToNftTrade[i + 1].trader == user) {
Trade storage currentItem = idToNftTrade[i + 1];
trades[currentIndex] = currentItem;
currentIndex++;
}
}
return trades;
}
function fetchBlindTrades() public view returns (BlindTrade[] memory) {
uint itemCount = blindTradeCount;
uint currentIndex;
BlindTrade[] memory trades = new BlindTrade[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToBlindTrade[i + 1].isActive) {
BlindTrade storage currentItem = idToBlindTrade[i + 1];
trades[currentIndex] = currentItem;
currentIndex++;
}
}
return trades;
}
function fetchUserBlindTrades(address user) public view returns (BlindTrade[] memory) {
uint itemCount = blindTradeCount;
uint currentIndex;
BlindTrade[] memory trades = new BlindTrade[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToBlindTrade[i + 1].trader == user) {
BlindTrade storage currentItem = idToBlindTrade[i + 1];
trades[currentIndex] = currentItem;
currentIndex++;
}
}
return trades;
}
function fetchTradesById(uint itemId) public view returns (Trade[] memory) {
uint itemCount = tradeCount;
uint currentIndex;
Trade[] memory trades = new Trade[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToNftTrade[i + 1].tradeId > 0) {
if (idToNftTrade[i + 1].itemId == itemId) {
Trade storage currentItem = idToNftTrade[i + 1];
trades[currentIndex] = currentItem;
currentIndex++;
}
}
}
return trades;
}
function fetchTrade(uint itemId) public view returns (Trade memory item) {
uint id = marketIdToTradeId[itemId];
return idToNftTrade[id];
}
function fetchTradeId(uint itemId) public view returns(uint tradeId){
uint id = marketIdToTradeId[itemId];
return id;
}
/// @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 re-used by referring to their index,
else a new storage spot is created;
We use the last item in the storage (length of array - 1),
in order to pop off the item and avoid rewriting
<~~~*/
function _remove(uint store) internal {
if (store==0){
openStorage.pop();
} else if (store==1){
blindOpenStorage.pop();
}
}
/// @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
/*~~~> External ETH transfer 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 memory
)external pure returns(bytes4) {
return bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"));
}
}