-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trait.sol
541 lines (459 loc) · 17 KB
/
Trait.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
contract Trait is Ownable, ERC721Holder, ERC1155Holder {
event OfferCreated(StructOffer);
event Status(StructOffer, OfferStatus);
event ExecludedFromFees(address contractAddress);
event IncludedFromFees(address contractAddress);
event FeesClaimedByAdmin(uint256 feesValue);
event ExchangeFeesUpdated(uint256 prevFees, uint256 updatedFees);
enum OfferStatus {
pending,
withdrawan,
accepted,
rejected
}
struct StructERC20Value {
address erc20Contract;
uint256 erc20Value;
}
struct StructERC721Value {
address erc721Contract;
uint256 erc721Id;
}
struct StructERC1155Value {
address erc1155Contract;
uint256 erc1155Id;
uint256 amount;
bytes data;
}
struct StructOffer {
uint256 offerId;
address sender;
address receiver;
uint256 offeredETH;
uint256 requestedETH;
StructERC20Value offeredERC20;
StructERC20Value requestedERC20;
StructERC721Value[] offeredERC721;
StructERC721Value[] requestedERC721;
StructERC1155Value[] offeredERC1155;
StructERC1155Value[] requestedERC1155;
uint256 timeStamp;
uint256 validDuration;
OfferStatus status;
}
struct StructAccount {
uint256[] offersReceived;
uint256[] offersCreated;
}
uint256 private _offerIds;
address[] private _excludedFeesContracts;
uint256 private _fees;
uint256 private _feesCollected;
uint256 private _feesClaimed;
bool private _isTransacting;
mapping(uint256 => StructOffer) private _mappingOffer;
mapping(address => StructAccount) private _mappingAccounts;
constructor(uint256 _feesInWei) {
_fees = _feesInWei;
}
receive() external payable {}
modifier noReentrancy() {
require(
!_isTransacting,
"whileTreansacting(): Contract is already transaction"
);
_isTransacting = true;
_;
_isTransacting = false;
}
modifier isOfferValidForWithdrawal(uint256 _offerId) {
StructOffer memory offerAccount = _mappingOffer[_offerId];
require(
offerAccount.sender != address(0),
"Address zero cannot make offer."
);
require(
offerAccount.receiver != address(0),
"Cannot make offer to address zero."
);
require(
offerAccount.status != OfferStatus.accepted,
"Offer already used."
);
require(
offerAccount.status != OfferStatus.withdrawan,
"Offer already withdrawan."
);
_;
}
modifier isValidOffer(uint256 _offerId) {
StructOffer memory offerAccount = _mappingOffer[_offerId];
require(
offerAccount.sender != address(0),
"Address zero cannot make offer."
);
require(
offerAccount.receiver != address(0),
"Cannot make offer to address zero."
);
require(
offerAccount.status == OfferStatus.pending,
"Offer already accepted or withdrawan."
);
_;
}
modifier isReceiver(uint256 _offerId) {
StructOffer memory offerAccount = _mappingOffer[_offerId];
require(
msg.sender == offerAccount.receiver,
"You are not the receiver of this offer."
);
_;
}
function createOffer(StructOffer memory _structOffer)
external
payable
returns (uint256 offerId)
{
require(
_structOffer.receiver != address(0),
"createOffer(): _receiver cannot be address zero."
);
require(
_structOffer.offeredERC721.length < type(uint8).max,
"createOffer(): Offered erc721 cannot be more than 255"
);
require(
_structOffer.requestedERC721.length < type(uint8).max,
"createOffer(): requested erc721 cannot be more than 255"
);
require(
_structOffer.offeredERC1155.length < type(uint8).max,
"createOffer(): Offered erc721 cannot be more than 255"
);
require(
_structOffer.requestedERC1155.length < type(uint8).max,
"createOffer(): Offered erc721 cannot be more than 255"
);
uint256 msgValue = msg.value;
address msgSender = msg.sender;
uint256 currentTime = block.timestamp;
offerId = _offerIds;
if (!_isBalanceExcludedFromFees(msgSender)) {
require(
msgValue >= _fees + _structOffer.offeredETH,
"createOffer(): user is not included in exclude from fees and msgValue no included fees amount."
);
_feesCollected += _fees;
} else {
require(
msgValue == _structOffer.offeredETH,
"createOffer(): msgValue is not equal to ethOffered."
);
}
StructOffer storage offerAccount = _mappingOffer[offerId];
offerAccount.offerId = offerId;
offerAccount.sender = msgSender;
offerAccount.receiver = _structOffer.receiver;
offerAccount.offeredETH = _structOffer.offeredETH;
offerAccount.requestedETH = _structOffer.requestedETH;
offerAccount.offeredERC20 = _structOffer.offeredERC20;
offerAccount.requestedERC20 = _structOffer.requestedERC20;
///@dev please ensure that there is sufficient allowance to successfully invoke the transferFrom function.
if (
_structOffer.offeredERC20.erc20Contract != address(0) &&
_structOffer.offeredERC20.erc20Value > 0
) {
IERC20(_structOffer.offeredERC20.erc20Contract).transferFrom(
msgSender,
address(this),
_structOffer.offeredERC20.erc20Value
);
}
///@dev please ensure that there is sufficient allowance to successfully invoke the transferFrom function.
for (uint8 i; i < _structOffer.offeredERC721.length; ++i) {
IERC721(_structOffer.offeredERC721[i].erc721Contract)
.safeTransferFrom(
msgSender,
address(this),
_structOffer.offeredERC721[i].erc721Id
);
offerAccount.offeredERC721.push(_structOffer.offeredERC721[i]);
}
for (uint8 i; i < _structOffer.requestedERC721.length; ++i) {
offerAccount.requestedERC721.push(_structOffer.requestedERC721[i]);
}
for (uint8 i; i < _structOffer.offeredERC1155.length; ++i) {
IERC1155(_structOffer.offeredERC1155[i].erc1155Contract)
.safeTransferFrom(
msgSender,
address(this),
_structOffer.offeredERC1155[i].erc1155Id,
_structOffer.offeredERC1155[i].amount,
_structOffer.offeredERC1155[i].data
);
offerAccount.offeredERC1155.push(_structOffer.offeredERC1155[i]);
}
for (uint8 i; i < _structOffer.requestedERC1155.length; ++i) {
offerAccount.requestedERC1155.push(
_structOffer.requestedERC1155[i]
);
}
offerAccount.timeStamp = currentTime;
offerAccount.validDuration = _structOffer.validDuration;
offerAccount.status = OfferStatus.pending;
_mappingAccounts[msgSender].offersCreated.push(offerId);
_mappingAccounts[_structOffer.receiver].offersReceived.push(offerId);
emit OfferCreated(_mappingOffer[offerId]);
_offerIds++;
}
function acceptOffer(uint256 _offerId)
external
payable
noReentrancy
isValidOffer(_offerId)
isReceiver(_offerId)
{
address msgSender = msg.sender;
uint256 msgValue = msg.value;
StructOffer storage offerAccount = _mappingOffer[_offerId];
require(
msgValue >= offerAccount.requestedETH,
"Receiver has not sent enough eth, offer creator requested."
);
require(
block.timestamp <
offerAccount.timeStamp + offerAccount.validDuration,
"Offer expired."
);
///@dev please ensure that there is sufficient allowance to successfully invoke the transferFrom function.
for (uint8 i; i < offerAccount.offeredERC721.length; i++) {
IERC721(offerAccount.offeredERC721[i].erc721Contract).transferFrom(
address(this),
offerAccount.receiver,
offerAccount.offeredERC721[i].erc721Id
);
}
for (uint8 i; i < offerAccount.offeredERC1155.length; i++) {
IERC1155(offerAccount.offeredERC1155[i].erc1155Contract)
.safeTransferFrom(
address(this),
offerAccount.receiver,
offerAccount.offeredERC1155[i].erc1155Id,
offerAccount.offeredERC1155[i].amount,
offerAccount.offeredERC1155[i].data
);
}
///@dev please ensure that there is sufficient allowance to successfully invoke the transferFrom function.
for (uint8 i; i < offerAccount.requestedERC721.length; i++) {
IERC721(offerAccount.requestedERC721[i].erc721Contract)
.transferFrom(
offerAccount.receiver,
offerAccount.sender,
offerAccount.requestedERC721[i].erc721Id
);
}
for (uint8 i; i < offerAccount.requestedERC1155.length; i++) {
IERC1155(offerAccount.requestedERC1155[i].erc1155Contract)
.safeTransferFrom(
offerAccount.receiver,
offerAccount.sender,
offerAccount.requestedERC1155[i].erc1155Id,
offerAccount.requestedERC1155[i].amount,
"0x"
);
}
if (offerAccount.offeredETH > 0) {
payable(offerAccount.receiver).transfer(offerAccount.offeredETH);
}
if (offerAccount.requestedETH > 0) {
payable(offerAccount.sender).transfer(offerAccount.requestedETH);
}
if (
offerAccount.requestedERC20.erc20Contract != address(0) &&
offerAccount.requestedERC20.erc20Value > 0
) {
IERC20(offerAccount.requestedERC20.erc20Contract).transferFrom(
msgSender,
offerAccount.sender,
offerAccount.requestedERC20.erc20Value
);
}
///@dev please ensure that there is sufficient allowance to successfully invoke the transferFrom function.
if (
offerAccount.offeredERC20.erc20Contract != address(0) &&
offerAccount.offeredERC20.erc20Value > 0
) {
IERC20(offerAccount.offeredERC20.erc20Contract).transfer(
offerAccount.receiver,
offerAccount.offeredERC20.erc20Value
);
}
offerAccount.status = OfferStatus.accepted;
emit Status(offerAccount, OfferStatus.accepted);
}
function rejectOffer(uint256 _offerId)
external
noReentrancy
isValidOffer(_offerId)
isReceiver(_offerId)
{
StructOffer storage offerAccount = _mappingOffer[_offerId];
offerAccount.status = OfferStatus.rejected;
emit Status(offerAccount, OfferStatus.rejected);
}
function withdrawOffer(uint256 _offerId)
external
noReentrancy
isOfferValidForWithdrawal(_offerId)
{
address msgSender = msg.sender;
StructOffer storage offerAccount = _mappingOffer[_offerId];
require(
msgSender == offerAccount.sender,
"Only offer creator can withdrawOffer."
);
for (uint8 i; i < offerAccount.offeredERC721.length; i++) {
IERC721(offerAccount.offeredERC721[i].erc721Contract).transferFrom(
address(this),
offerAccount.sender,
offerAccount.offeredERC721[i].erc721Id
);
}
for (uint8 i; i < offerAccount.offeredERC1155.length; i++) {
IERC1155(offerAccount.offeredERC1155[i].erc1155Contract)
.safeTransferFrom(
address(this),
offerAccount.sender,
offerAccount.offeredERC1155[i].erc1155Id,
offerAccount.offeredERC1155[i].amount,
offerAccount.offeredERC1155[i].data
);
}
if (offerAccount.offeredETH > 0) {
payable(offerAccount.sender).transfer(offerAccount.offeredETH);
}
if (
offerAccount.offeredERC20.erc20Contract != address(0) &&
offerAccount.offeredERC20.erc20Value > 0
) {
IERC20(offerAccount.offeredERC20.erc20Contract).transfer(
offerAccount.sender,
offerAccount.offeredERC20.erc20Value
);
}
offerAccount.status = OfferStatus.withdrawan;
emit Status(offerAccount, OfferStatus.withdrawan);
}
function getOfferById(uint256 _offerId)
public
view
returns (StructOffer memory)
{
return _mappingOffer[_offerId];
}
function userOffers(address _userAddress)
external
view
returns (
uint256[] memory offersCreated,
uint256[] memory offersReceived
)
{
StructAccount memory userAccount = _mappingAccounts[_userAddress];
offersCreated = userAccount.offersCreated;
offersReceived = userAccount.offersReceived;
}
function allOffersCount() external view returns (uint256 offersCount) {
if (_offerIds > 0) {
offersCount = _offerIds + 1;
}
}
function _isBalanceExcludedFromFees(address _userAddress)
private
view
returns (bool _isExcluded)
{
address[] memory excludedContractsList = _excludedFeesContracts;
if (excludedContractsList.length > 0) {
for (uint8 i; i < excludedContractsList.length; ++i) {
if (
IERC721(excludedContractsList[i]).balanceOf(_userAddress) >
0
) {
_isExcluded = true;
break;
}
}
}
}
function getFeesExcludedList() external view returns (address[] memory) {
return _excludedFeesContracts;
}
function includeInFees(address _contractAddress) external onlyOwner {
address[] memory excludedContractsList = _excludedFeesContracts;
if (excludedContractsList.length > 0) {
for (uint8 i; i < excludedContractsList.length; ++i) {
if (_excludedFeesContracts[i] == _contractAddress) {
_excludedFeesContracts[i] ==
_excludedFeesContracts[
_excludedFeesContracts.length - 1
];
_excludedFeesContracts.pop();
emit IncludedFromFees(_contractAddress);
break;
}
}
}
}
function excludeFromExchangeFees(address _contractAddress)
external
onlyOwner
{
address[] memory excludedContractsList = _excludedFeesContracts;
if (excludedContractsList.length > 0) {
for (uint8 i; i < excludedContractsList.length; ++i) {
if (_excludedFeesContracts[i] == _contractAddress) {
revert("Contract already in exempted list.");
}
}
}
_excludedFeesContracts.push(_contractAddress);
emit ExecludedFromFees(_contractAddress);
}
function getFees() external view returns (uint256) {
return _fees;
}
function setFees(uint256 _feesInWei) external onlyOwner {
emit ExchangeFeesUpdated(_fees, _feesInWei);
_fees = _feesInWei;
}
function getFeesCollected()
external
view
returns (
uint256 feesCollected,
uint256 feesClaimed,
uint256 feesPendingToClaim
)
{
feesCollected = _feesCollected;
feesClaimed = _feesClaimed;
feesPendingToClaim = _feesCollected - _feesClaimed;
}
function claimFees() external noReentrancy onlyOwner {
uint256 pendingFees = _feesCollected - _feesClaimed;
require(pendingFees > 0, "No fees to claimed");
_feesClaimed += pendingFees;
payable(owner()).transfer(pendingFees);
emit FeesClaimedByAdmin(pendingFees);
}
}