/
AssetSignedAuctionWithAuth.sol
432 lines (397 loc) · 14 KB
/
AssetSignedAuctionWithAuth.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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.2;
import {IERC20} from "@openzeppelin/contracts-0.8/token/ERC20/IERC20.sol";
import {Address} from "@openzeppelin/contracts-0.8/utils/Address.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts-0.8/security/ReentrancyGuard.sol";
import {SigUtil} from "../common/Libraries/SigUtil.sol";
import {PriceUtil} from "../common/Libraries/PriceUtil.sol";
import {TheSandbox712} from "../common/Base/TheSandbox712.sol";
import {MetaTransactionReceiver} from "../common/BaseWithStorage/MetaTransactionReceiver.sol";
import {ERC1271} from "../common/interfaces/ERC1271.sol";
import {ERC1271Constants} from "../common/interfaces/ERC1271Constants.sol";
import {ERC1654} from "../common/interfaces/ERC1654.sol";
import {ERC1654Constants} from "../common/interfaces/ERC1654Constants.sol";
import {IAuthValidator} from "../common/interfaces/IAuthValidator.sol";
import {IERC1155} from "../common/interfaces/IERC1155.sol";
contract AssetSignedAuctionWithAuth is
ReentrancyGuard,
ERC1654Constants,
ERC1271Constants,
TheSandbox712,
MetaTransactionReceiver
{
struct ClaimSellerOfferRequest {
address buyer;
address payable seller;
address token;
uint256[] purchase;
uint256[] auctionData;
uint256[] ids;
uint256[] amounts;
bytes signature;
bytes backendSignature;
}
enum SignatureType {DIRECT, EIP1654, EIP1271}
bytes32 public constant BACKEND_TYPEHASH =
keccak256(
"Auction(address to,address from,address token,bytes auctionData,bytes ids,bytes amounts,bytes purchase)"
);
bytes32 public constant AUCTION_TYPEHASH =
keccak256("Auction(address from,address token,bytes auctionData,bytes ids,bytes amounts)");
event OfferClaimed(
address indexed seller,
address indexed buyer,
uint256 indexed offerId,
uint256 amount,
uint256 pricePaid,
uint256 feePaid
);
event OfferCancelled(address indexed seller, uint256 indexed offerId);
event NewFeeLimit(uint256 feeLimit);
uint256 public constant MAX_UINT256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
// Stack too deep, grouping parameters
// AuctionData:
uint256 public constant AuctionData_OfferId = 0;
uint256 public constant AuctionData_StartingPrice = 1;
uint256 public constant AuctionData_EndingPrice = 2;
uint256 public constant AuctionData_StartedAt = 3;
uint256 public constant AuctionData_Duration = 4;
uint256 public constant AuctionData_Packs = 5;
mapping(address => mapping(uint256 => uint256)) public claimed;
IAuthValidator public _authValidator;
IERC1155 public _asset;
uint256 public _fee10000th = 0;
uint256 public _feeLimit = 500;
uint256 public _maxfeeLimit = 500; // 5%
address payable public _feeCollector;
event FeeSetup(address feeCollector, uint256 fee10000th);
constructor(
IERC1155 asset,
address admin,
address initialMetaTx,
address payable feeCollector,
uint256 fee10000th,
address authValidator
) TheSandbox712() {
require(fee10000th <= _feeLimit, "Fee above the limit");
_asset = asset;
_feeCollector = feeCollector;
_fee10000th = fee10000th;
emit FeeSetup(feeCollector, fee10000th);
_admin = admin;
_setMetaTransactionProcessor(initialMetaTx, true);
_authValidator = IAuthValidator(authValidator);
}
// check backend signature to avoid front-running
modifier isAuthValid(bytes memory signature, bytes32 hashedData) {
require(_authValidator.isAuthValid(signature, hashedData), "INVALID_AUTH");
_;
}
/// @notice set fee parameters
/// @param feeCollector address receiving the fee
/// @param fee10000th fee in 10,000th
function setFee(address payable feeCollector, uint256 fee10000th) external {
require(feeCollector != address(0), "feeCollector cannot be Zero address");
require(msg.sender == _admin, "only admin can change fee");
require(fee10000th <= _feeLimit, "Fee above the limit");
_feeCollector = feeCollector;
_fee10000th = fee10000th;
emit FeeSetup(feeCollector, fee10000th);
}
/// @notice setFeeLimit parameters
/// @param newFeeLimit new fee limit
function setFeeLimit(uint256 newFeeLimit) external {
require(msg.sender == _admin, "only admin can change fee limit");
require(newFeeLimit <= _maxfeeLimit, "new limit above max limit");
_feeLimit = newFeeLimit;
emit NewFeeLimit(newFeeLimit);
}
/// @notice claim offer using EIP712
/// @param input Claim Seller Offer Request
function claimSellerOffer(ClaimSellerOfferRequest memory input)
external
payable
isAuthValid(
input.backendSignature,
_hashBackendSig(
input.buyer,
input.seller,
input.token,
input.auctionData,
input.ids,
input.amounts,
input.purchase
)
)
{
_verifyParameters(
input.buyer,
input.seller,
input.token,
input.purchase[0],
input.auctionData,
input.ids,
input.amounts
);
_ensureCorrectSigner(
input.seller,
input.token,
input.auctionData,
input.ids,
input.amounts,
input.signature,
SignatureType.DIRECT
);
_executeDeal(
input.token,
input.purchase,
input.buyer,
input.seller,
input.auctionData,
input.ids,
input.amounts
);
}
/// @notice claim offer using EIP712 and EIP1271 signature verification scheme
/// @param input Claim Seller Offer Request
function claimSellerOfferViaEIP1271(ClaimSellerOfferRequest memory input)
external
payable
isAuthValid(
input.backendSignature,
_hashBackendSig(
input.buyer,
input.seller,
input.token,
input.auctionData,
input.ids,
input.amounts,
input.purchase
)
)
{
_verifyParameters(
input.buyer,
input.seller,
input.token,
input.purchase[0],
input.auctionData,
input.ids,
input.amounts
);
_ensureCorrectSigner(
input.seller,
input.token,
input.auctionData,
input.ids,
input.amounts,
input.signature,
SignatureType.EIP1271
);
_executeDeal(
input.token,
input.purchase,
input.buyer,
input.seller,
input.auctionData,
input.ids,
input.amounts
);
}
/// @notice claim offer using EIP712 and EIP1654 signature verification scheme
/// @param input Claim Seller Offer Request
function claimSellerOfferViaEIP1654(ClaimSellerOfferRequest memory input)
external
payable
isAuthValid(
input.backendSignature,
_hashBackendSig(
input.buyer,
input.seller,
input.token,
input.auctionData,
input.ids,
input.amounts,
input.purchase
)
)
{
_verifyParameters(
input.buyer,
input.seller,
input.token,
input.purchase[0],
input.auctionData,
input.ids,
input.amounts
);
_ensureCorrectSigner(
input.seller,
input.token,
input.auctionData,
input.ids,
input.amounts,
input.signature,
SignatureType.EIP1654
);
_executeDeal(
input.token,
input.purchase,
input.buyer,
input.seller,
input.auctionData,
input.ids,
input.amounts
);
}
/// @notice cancel a offer previously signed, new offer need to use a id not used yet
/// @param offerId offer to cancel
function cancelSellerOffer(uint256 offerId) external {
require(claimed[msg.sender][offerId] != MAX_UINT256, "Sell offer was already cancelled");
claimed[msg.sender][offerId] = MAX_UINT256;
emit OfferCancelled(msg.sender, offerId);
}
function _executeDeal(
address token,
uint256[] memory purchase,
address buyer,
address payable seller,
uint256[] memory auctionData,
uint256[] memory ids,
uint256[] memory amounts
) internal nonReentrant {
uint256 offer =
PriceUtil.calculateCurrentPrice(
auctionData[AuctionData_StartingPrice],
auctionData[AuctionData_EndingPrice],
auctionData[AuctionData_Duration],
block.timestamp - auctionData[AuctionData_StartedAt]
) * purchase[0];
claimed[seller][auctionData[AuctionData_OfferId]] =
claimed[seller][auctionData[AuctionData_OfferId]] +
purchase[0];
uint256 fee = 0;
if (_fee10000th > 0) {
fee = PriceUtil.calculateFee(offer, _fee10000th);
}
uint256 total = offer + fee;
require(total <= purchase[1], "offer exceeds max amount to spend");
if (token != address(0)) {
require(IERC20(token).transferFrom(buyer, seller, offer), "failed to transfer token price");
if (fee > 0) {
require(IERC20(token).transferFrom(buyer, _feeCollector, fee), "failed to collect fee");
}
} else {
require(msg.value >= total, "ETH < total");
if (msg.value > total) {
Address.sendValue(payable(msg.sender), msg.value - total);
}
Address.sendValue(seller, offer);
if (fee > 0) {
Address.sendValue(_feeCollector, fee);
}
}
uint256[] memory packAmounts = new uint256[](amounts.length);
for (uint256 i = 0; i < packAmounts.length; i++) {
packAmounts[i] = amounts[i] * purchase[0];
}
_asset.safeBatchTransferFrom(seller, buyer, ids, packAmounts, "");
emit OfferClaimed(seller, buyer, auctionData[AuctionData_OfferId], purchase[0], offer, fee);
}
function _ensureCorrectSigner(
address from,
address token,
uint256[] memory auctionData,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory signature,
SignatureType signatureType
) internal view {
bytes memory dataToHash;
dataToHash = abi.encodePacked(
"\x19\x01",
_DOMAIN_SEPARATOR,
_hashAuction(from, token, auctionData, ids, amounts)
);
if (signatureType == SignatureType.EIP1271) {
require(
ERC1271(from).isValidSignature(dataToHash, signature) == ERC1271_MAGICVALUE,
"invalid 1271 signature"
);
} else if (signatureType == SignatureType.EIP1654) {
require(
ERC1654(from).isValidSignature(keccak256(dataToHash), signature) == ERC1654_MAGICVALUE,
"invalid 1654 signature"
);
} else {
address signer = SigUtil.recover(keccak256(dataToHash), signature);
require(signer == from, "signer != from");
}
}
function _verifyParameters(
address buyer,
address payable seller,
address token,
uint256 buyAmount,
uint256[] memory auctionData,
uint256[] memory ids,
uint256[] memory amounts
) internal view {
require(ids.length == amounts.length, "ids and amounts length not matching");
require(
buyer == msg.sender || (token != address(0) && _metaTransactionContracts[msg.sender]),
"not authorized"
);
uint256 amountAlreadyClaimed = claimed[seller][auctionData[AuctionData_OfferId]];
require(amountAlreadyClaimed != MAX_UINT256, "Auction cancelled");
uint256 total = amountAlreadyClaimed + buyAmount;
require(total <= auctionData[AuctionData_Packs], "Buy amount exceeds sell amount");
require(auctionData[AuctionData_StartedAt] <= block.timestamp, "Auction didn't start yet");
require(
auctionData[AuctionData_StartedAt] + auctionData[AuctionData_Duration] > block.timestamp,
"Auction finished"
);
}
function _hashAuction(
address from,
address token,
uint256[] memory auctionData,
uint256[] memory ids,
uint256[] memory amounts
) internal pure returns (bytes32) {
return
keccak256(
abi.encode(
AUCTION_TYPEHASH,
from,
token,
keccak256(abi.encodePacked(auctionData)),
keccak256(abi.encodePacked(ids)),
keccak256(abi.encodePacked(amounts))
)
);
}
function _hashBackendSig(
address to,
address from,
address token,
uint256[] memory auctionData,
uint256[] memory ids,
uint256[] memory amounts,
uint256[] memory purchase
) internal pure returns (bytes32) {
return
keccak256(
abi.encode(
BACKEND_TYPEHASH,
to,
from,
token,
keccak256(abi.encodePacked(auctionData)),
keccak256(abi.encodePacked(ids)),
keccak256(abi.encodePacked(amounts)),
keccak256(abi.encodePacked(purchase))
)
);
}
}