-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarketOffers.sol
661 lines (598 loc) · 26 KB
/
MarketOffers.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
//*~~~> 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 "./interfaces/IEscrow.sol";
import "./interfaces/INFTMarket.sol";
import "./interfaces/IRoleProvider.sol";
import "./interfaces/IRewardsController.sol";
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
contract MarketOffers is ReentrancyGuard {
//*~~~> counter increments NFTs Offers
uint private offerIds;
//*~~~> counter increments Blind Offers
uint private blindOfferIds;
//*~~~> 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");
_;
}
//*~~~> State variables
address public roleAdd;
uint[] private openStorage;
uint[] private blindOpenStorage;
//*~~~> global address variable from Role Provider contract
bytes32 public constant NFTADD = keccak256("NFT");
bytes32 public constant REWARDS = keccak256("REWARDS");
bytes32 public constant MARKET = keccak256("MARKET");
bytes32 public constant BIDS = keccak256("BIDS");
bytes32 public constant TRADES = keccak256("TRADES");
bytes32 public constant MINT = keccak256("MINT");
//*~~~> sets deployment address as default admin role
constructor(address role) {
roleAdd = role;
}
//*~~~> Declaring object structures for market item offers
struct Offer {
bool isActive;
uint offerId;
uint itemId;
uint amount;
address tokenCont;
address offerer;
address seller;
}
//*~~~> Declaring object structures for blind offers
struct BlindOffer {
bool isSpecific;
uint amount1155;
uint tokenId;
uint blindOfferId;
uint amount;
address tokenCont;
address collectionOffer;
address offerer;
}
//*~~~> Mapping of all Offers
mapping (uint => Offer) private idToMktOffer;
//*~~~> Mapping of all Blind Offers
mapping (uint => BlindOffer) private idToBlindOffer;
//*~~~> Mapping of market itemId to offerId
mapping (uint => uint) private marketIdToOfferId;
//*~~~> Declaring event object struct for market item offers
event Offered(
uint offerId,
uint itemId,
address indexed token_address,
address indexed seller,
address indexed offerer,
uint amount
);
//*~~~> Declaring event object struct for blind offer
event BlindOffered(
bool isSpecific,
uint amount1155,
uint tokenId,
uint offerId,
uint amount,
address indexed offerer,
address indexed collectionOffer
);
//*~~~> Declaring the object structure for offer withdrawn
event OfferWithdrawn(
uint indexed offerId,
uint indexed itemId,
address indexed offerer
);
//*~~~> Declaring the object structure for blind offer withdrawn
event BlindOfferWithdrawn(
uint indexed offerId,
address indexed offerer
);
//*~~~> Declaring the object structure for offer accepted
event OfferAccepted(
uint offerId,
uint indexed itemId,
address indexed offerer,
address indexed seller
);
//*~~~> Declaring the object structure for offer accepted
event BlindOfferAccepted(
uint blindOfferId,
address indexed offerer,
address indexed seller
);
//*~~~> Declaring event object structure for offer refund
event OfferRefunded(
uint indexed offerId,
uint indexed itemId,
address indexed offerer
);
///@notice
/*~~~>
Public function to offer ERC20 tokens to swap with any ERC721 or ERC1155
<~~~*/
///@dev
/*~~~>
itemId: market item Id;
amount: ERC20 amount;
tokenCont: Contract of token to be offered;
seller: ownerOf the NFT item listed for sale
<~~~*/
///@return Bool
function enterOfferForNft(
uint[] memory itemId,
uint[] memory amount,
address[] memory tokenCont,
address[] memory seller
) external nonReentrant returns(bool){
for (uint i=0; i< itemId.length; i++) {
require (amount[i] > 0,"Amount needs to be > 0");
IERC20 tokenContract = IERC20(tokenCont[i]);
uint256 allowance = tokenContract.allowance(msg.sender, address(this));
require(allowance >= amount[i], "Check the token allowance");
require((tokenContract).transferFrom(msg.sender, (address(this)), amount[i]));
uint offerId;
uint len = openStorage.length;
if (len>=1) {
offerId = openStorage[len-1];
_remove(0);
} else {
offerIds+=1;
offerId = offerIds;
}
idToMktOffer[offerId] = Offer(true, offerId, itemId[i], amount[i], tokenCont[i], msg.sender, seller[i]);
marketIdToOfferId[itemId[i]] = offerId;
emit Offered(
offerId,
itemId[i],
tokenCont[i],
seller[i],
msg.sender,
amount[i]);
}
return true;
}
///@notice
/*~~~>
Public function to enter blind offer for specific or collection-wide NFT(s)
<~~~*/
///@dev
/*~~~>
isSpecific: (true) if the offer is for a specific NFT, else false;
amount1155: how many 1155 desired?
tokenId: Id of specific NFT offered in exchange for ERC20;
amount: amount of ERC20 tokens to offer;
tokenCont: token contract address of the offer;
collection: Collection address of the desired NFT(s)
<~~~*/
function enterBlindOffer(
bool[] memory isSpecific,
uint[] memory amount1155,
uint[] memory tokenId,
uint[] memory amount,
address[] memory tokenCont,
address[] memory collection
) external nonReentrant{
for (uint i=0; i<tokenCont.length;i++){
uint256 allowance = IERC20(tokenCont[i]).allowance(msg.sender, address(this));
require(allowance >= amount[i], "Check the token allowance");
require(IERC20(tokenCont[i]).transferFrom(msg.sender, (address(this)), amount[i]));
uint offerId;
uint len = blindOpenStorage.length;
if (len>=1) {
offerId = blindOpenStorage[len-1];
_remove(1);
} else {
blindOfferIds+=1;
offerId = blindOfferIds;
}
idToBlindOffer[offerId] = BlindOffer(isSpecific[i], amount1155[i], tokenId[i], offerId, amount[i], tokenCont[i], collection[i], msg.sender);
emit BlindOffered(
isSpecific[i],
amount1155[i],
tokenId[i],
offerId,
amount[i],
payable(msg.sender),
tokenCont[i]
);
}
}
///@notice
/*~~~>
Public offer to accept blind offer in exchange for NFT;
If specific, only the NFT owner can call.
else, any collection holder can accept the offer;
<~~~*/
///@dev
/*~~~>
blindOfferId: internal Id of blind offer;
tokenId: token Id of the NFT to swap;
offerId: offer Id if the item is listed between the time the offer was made and accepted;
listedId: listed items Id from the marketplace;
isListed: (true) if item is listed on the marketplace;
is1155: (true) if NFT desired is ERC1155;
<~~~*/
function acceptBlindOffer(
uint[] memory blindOfferId,
uint[] memory tokenId,
uint[] memory offerId,
uint[] memory listedId,
bool[] memory isListed,
bool[] memory is1155
) external nonReentrant returns(bool){
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
address mrktAdd = IRoleProvider(roleAdd).fetchAddress(MARKET);
uint balance = IERC721(IRoleProvider(roleAdd).fetchAddress(NFTADD)).balanceOf(msg.sender);
for (uint i=0; i<blindOfferId.length;i++){
BlindOffer memory offer = idToBlindOffer[blindOfferId[i]];
IERC20 tokenContract = IERC20(offer.tokenCont);
// Verify the item's specificity
if(offer.isSpecific){
require(tokenId[i]==offer.tokenId,"Wrong item!");
}
if(balance<1){
/// Calculate fee and send to rewards contract
uint256 saleFee = calcFee(offer.amount);
uint256 userAmnt = (offer.amount-saleFee);
/// send (saleFee) to rewards contract
require(IRewardsController(rewardsAdd).depositERC20Rewards(saleFee, offer.tokenCont));
require((tokenContract).transfer(rewardsAdd, saleFee));
/// send (offerAmount - saleFee) to user
require((tokenContract).transfer(msg.sender, userAmnt));
} else {
require((tokenContract).transfer(msg.sender, offer.amount));
}
// If the item is listed on the marketplace proxy, transfer it
if(isListed[i]){
require(INFTMarket(mrktAdd).transferNftForSale(offer.offerer, listedId[i]));
} else {
if (is1155[i]){
IERC1155(offer.collectionOffer).safeTransferFrom(address(this), msg.sender, tokenId[i], offer.amount1155, "");
} else {
require(transferFromERC721(offer.collectionOffer, tokenId[i], offer.offerer));
}
}
blindOpenStorage.push(offerId[i]);
idToBlindOffer[offerId[i]] = BlindOffer(false, 0, 0, 0, 0, address(0x0), address(0x0), address(0x0));
emit BlindOfferAccepted(
blindOfferId[i],
offer.offerer,
msg.sender
);
}
return true;
}
///@notice
/*~~~>
public function to accept an offer for a listed NFT on the market
<~~~*/
///@dev
/*~~~>
offerId: Internal id of offer;
<~~~*/
///@return Bool
function acceptOfferForNft(uint[] calldata offerId) external nonReentrant returns(bool){
address mrktNft = IRoleProvider(roleAdd).fetchAddress(NFTADD);
address rewardsAdd = IRoleProvider(roleAdd).fetchAddress(REWARDS);
address bidsAdd = IRoleProvider(roleAdd).fetchAddress(BIDS);
address tradesAdd = IRoleProvider(roleAdd).fetchAddress(TRADES);
address mrktAdd = IRoleProvider(roleAdd).fetchAddress(MARKET);
uint balance = IERC721(mrktNft).balanceOf(msg.sender);
for (uint i=0; i<offerId.length; i++) {
Offer memory offer = idToMktOffer[offerId[i]];
if (msg.sender != offer.seller) revert();
IERC20 tokenContract = IERC20(offer.tokenCont);
if(balance<1){
/// Calculate fee and send to rewards contract
uint256 saleFee = calcFee(offer.amount);
uint256 userAmnt = (offer.amount - saleFee);
require(IRewardsController(rewardsAdd).depositERC20Rewards(saleFee, offer.tokenCont));
require((tokenContract).transfer(rewardsAdd, saleFee));
require((tokenContract).transfer(offer.seller, userAmnt));
} else {
require((tokenContract).transfer(offer.seller, offer.amount));
}
if (IBids(bidsAdd).fetchBidId(offer.itemId) > 0) {
/*~~~> Kill bid and refund bidValue <~~~*/
//~~~> Call the contract to refund the ETH offered for a bid
require(IBids(bidsAdd).refundBid(IBids(bidsAdd).fetchBidId(offer.itemId)));
}
/*~~~> Check for the case where there is an offer and refund it. <~~~*/
if (ITrades(tradesAdd).fetchTradeId(offer.itemId) > 0) {
/*~~~> Kill offer and refund amount <~~~*/
//*~~~> Call the contract to refund the ERC20 offered for trade
require(ITrades(tradesAdd).refundTrade(offer.itemId, ITrades(tradesAdd).fetchTradeId(offer.itemId)));
}
marketIdToOfferId[offer.itemId] = 0;
openStorage.push(offerId[i]);
idToMktOffer[offerId[i]] = Offer(false, 0, 0, 0, address(0x0), address(0x0), address(0x0));
require(INFTMarket(mrktAdd).transferNftForSale(offer.offerer, offer.itemId));
emit OfferAccepted(
offerId[i],
offer.itemId,
offer.offerer,
offer.seller
);
}
return true;
}
///@notice
/*~~~>
Public function to withdraw offers that only offer owners can call
<~~~*/
///@dev
/*~~~>
offerId: internal Id of the offer item
isBlind: external bool needed to determine type of offer
<~~~*/
function withdrawOffer(uint[] memory offerId, bool[] memory isBlind) external nonReentrant returns(bool){
for (uint i=0; i< offerId.length; i++) {
if (isBlind[i]){
BlindOffer memory offer = idToBlindOffer[offerId[i]];
///*~~~> Require the message sender to be the offerer
if (offer.offerer != msg.sender) revert();
IERC20 tokenContract = IERC20(offer.tokenCont);
require((tokenContract).transfer(offer.offerer, offer.amount));
/// push old offerId to blind open storage
blindOpenStorage.push(offerId[i]);
idToBlindOffer[offerId[i]] = BlindOffer(false, 0, 0, 0, 0, address(0x0), address(0x0), address(0x0));
emit BlindOfferWithdrawn(
offerId[i],
msg.sender);
} else {
Offer memory offer = idToMktOffer[offerId[i]];
if (offer.offerer != msg.sender) revert();
IERC20 tokenContract = IERC20(offer.tokenCont);
require((tokenContract).transfer(offer.offerer, offer.amount));
/// push old offerId to open storage
openStorage.push(offerId[i]);
/// reset offerId
marketIdToOfferId[offer.itemId] = 0;
idToMktOffer[offerId[i]] = Offer(false, 0, 0, 0, address(0x0), address(0x0), address(0x0));
emit OfferWithdrawn(
offerId[i],
offer.itemId,
msg.sender);
}
}
return true;
}
///@notice
/*~~~>
Only Contract function used for manually refunding offers when items sell
<~~*/
///@dev
/*~~~>
itemId: Market storage Id of the item to be refunded
offerId: Internal storage Id of the offer
<~~~*/
function refundOffer(uint itemId, uint offerId) external nonReentrant hasContractAdmin returns(bool){
Offer memory offer = idToMktOffer[itemId];
/// verifying that the refunded offer is the correct one
require(offer.offerId == offerId);
IERC20 tokenContract = IERC20(offer.tokenCont);
require((tokenContract).transfer(offer.offerer, offer.amount));
/// recycle old offerId
openStorage.push(offerId);
/// reset internal memory of itemId
marketIdToOfferId[itemId] = 0;
idToMktOffer[offerId] = Offer(false, 0, 0, 0, address(0x0), address(0x0), address(0x0));
emit OfferRefunded(offerId, itemId, offer.offerer);
return true;
}
/// @notice
/*~~~>
Internal function to transfer ERC721 NFTs, including crypto kitties/punks
<~~~*/
/// @dev
/*~~~>
assetAddr: address of the token to be transfered;
tokenId: Id of the token to be transfered;
<~~~*/
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 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
/*~~~>
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 //*~~~> Public read function of internal state
function fetchOffers() public view returns (Offer[] memory) {
uint itemCount = offerIds;
Offer[] memory items = new Offer[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToMktOffer[i + 1].isActive == true) {
Offer storage currentItem = idToMktOffer[i + 1];
items[i] = currentItem;
}
}
return items;
}
///@notice //*~~~> Public read function of internal state
function fetchOffersByItemId(uint itemId) public view returns (Offer[] memory) {
uint itemCount = offerIds;
Offer[] memory items = new Offer[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToMktOffer[i + 1].isActive == true) {
if (idToMktOffer[i + 1].itemId == itemId){
Offer storage currentItem = idToMktOffer[i + 1];
items[i] = currentItem;
}
}
}
return items;
}
function fetchOffersByOfferer(address user) public view returns (Offer[] memory) {
uint itemCount = offerIds;
Offer[] memory items = new Offer[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToMktOffer[i + 1].isActive == true) {
if (idToMktOffer[i + 1].offerer == user){
Offer storage currentItem = idToMktOffer[i + 1];
items[i] = currentItem;
}
}
}
return items;
}
function fetchBlindOffers() public view returns (BlindOffer[] memory) {
uint itemCount = blindOfferIds;
BlindOffer[] memory items = new BlindOffer[](itemCount);
for (uint i=0; i < itemCount; i++) {
BlindOffer storage currentItem = idToBlindOffer[i + 1];
items[i] = currentItem;
}
return items;
}
function fetchBlindOffersByOfferer(address user) public view returns (BlindOffer[] memory) {
uint itemCount = blindOfferIds;
BlindOffer[] memory items = new BlindOffer[](itemCount);
for (uint i=0; i < itemCount; i++) {
if (idToBlindOffer[i + 1].offerer == user){
BlindOffer storage currentItem = idToBlindOffer[i + 1];
items[i] = currentItem;
}
}
return items;
}
function fetchOfferId(uint itemId) public view returns (uint) {
uint id = marketIdToOfferId[itemId];
return id;
}
///@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);
}
}