-
Notifications
You must be signed in to change notification settings - Fork 20
/
Auction.sol
378 lines (298 loc) · 13.7 KB
/
Auction.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import { UUPS } from "../lib/proxy/UUPS.sol";
import { Ownable } from "../lib/utils/Ownable.sol";
import { ReentrancyGuard } from "../lib/utils/ReentrancyGuard.sol";
import { Pausable } from "../lib/utils/Pausable.sol";
import { SafeCast } from "../lib/utils/SafeCast.sol";
import { AuctionStorageV1 } from "./storage/AuctionStorageV1.sol";
import { Token } from "../token/Token.sol";
import { IManager } from "../manager/IManager.sol";
import { IAuction } from "./IAuction.sol";
import { IWETH } from "../lib/interfaces/IWETH.sol";
/// @title Auction
/// @author Rohan Kulkarni
/// @notice A DAO's auction house
/// Modified from:
/// - NounsAuctionHouse.sol commit 2cbe6c7 - licensed under the BSD-3-Clause license.
/// - Zora V3 ReserveAuctionCoreEth module commit 795aeca - licensed under the GPL-3.0 license.
contract Auction is IAuction, UUPS, Ownable, ReentrancyGuard, Pausable, AuctionStorageV1 {
/// ///
/// IMMUTABLES ///
/// ///
/// @notice The address of WETH
address private immutable WETH;
/// @notice The contract upgrade manager
IManager private immutable manager;
/// ///
/// CONSTRUCTOR ///
/// ///
/// @param _manager The contract upgrade manager address
/// @param _weth The address of WETH
constructor(address _manager, address _weth) payable initializer {
manager = IManager(_manager);
WETH = _weth;
}
/// ///
/// INITIALIZER ///
/// ///
/// @notice Initializes a DAO's auction contract
/// @param _token The ERC-721 token address
/// @param _founder The founder responsible for starting the first auction
/// @param _treasury The treasury address where ETH will be sent
/// @param _duration The duration of each auction
/// @param _reservePrice The reserve price of each auction
function initialize(
address _token,
address _founder,
address _treasury,
uint256 _duration,
uint256 _reservePrice
) external initializer {
// Ensure the caller is the contract manager
if (msg.sender != address(manager)) revert ONLY_MANAGER();
// Initialize the reentrancy guard
__ReentrancyGuard_init();
// Grant initial ownership to a founder
__Ownable_init(_founder);
// Pause the contract until the first auction
__Pausable_init(true);
// Store DAO's ERC-721 token
token = Token(_token);
// Store the auction house settings
settings.duration = SafeCast.toUint40(_duration);
settings.reservePrice = _reservePrice;
settings.treasury = _treasury;
settings.timeBuffer = 5 minutes;
settings.minBidIncrement = 10;
}
/// ///
/// CREATE BID ///
/// ///
/// @notice Creates a bid for the current token
/// @param _tokenId The ERC-721 token id
function createBid(uint256 _tokenId) external payable nonReentrant {
// Get a copy of the current auction
Auction memory _auction = auction;
// Ensure the bid is for the current token
if (_auction.tokenId != _tokenId) revert INVALID_TOKEN_ID();
// Ensure the auction is still active
if (block.timestamp >= _auction.endTime) revert AUCTION_OVER();
// Cache the address of the highest bidder
address highestBidder = _auction.highestBidder;
// If this is the first bid:
if (highestBidder == address(0)) {
// Ensure the bid meets the reserve price
if (msg.value < settings.reservePrice) revert RESERVE_PRICE_NOT_MET();
// Else this is a subsequent bid:
} else {
// Cache the highest bid
uint256 highestBid = _auction.highestBid;
// Used to store the minimum bid required
uint256 minBid;
// Cannot realistically overflow
unchecked {
// Compute the minimum bid
minBid = highestBid + ((highestBid * settings.minBidIncrement) / 100);
}
// Ensure the incoming bid meets the minimum
if (msg.value < minBid) revert MINIMUM_BID_NOT_MET();
// Refund the previous bidder
_handleOutgoingTransfer(highestBidder, highestBid);
}
// Store the new highest bid
auction.highestBid = msg.value;
// Store the new highest bidder
auction.highestBidder = msg.sender;
// Used to store if the auction will be extended
bool extend;
// Cannot underflow as `_auction.endTime` is ensured to be greater than the current time above
unchecked {
// Compute whether the time remaining is less than the buffer
extend = (_auction.endTime - block.timestamp) < settings.timeBuffer;
}
// If the time remaining is within the buffer:
if (extend) {
// Cannot realistically overflow
unchecked {
// Extend the auction by the time buffer
auction.endTime = uint40(block.timestamp + settings.timeBuffer);
}
}
emit AuctionBid(_tokenId, msg.sender, msg.value, extend, auction.endTime);
}
/// ///
/// SETTLE & CREATE AUCTION ///
/// ///
/// @notice Settles the current auction and creates the next one
function settleCurrentAndCreateNewAuction() external nonReentrant whenNotPaused {
_settleAuction();
_createAuction();
}
/// @dev Settles the current auction
function _settleAuction() private {
// Get a copy of the current auction
Auction memory _auction = auction;
// Ensure the auction wasn't already settled
if (auction.settled) revert AUCTION_SETTLED();
// Ensure the auction had started
if (_auction.startTime == 0) revert AUCTION_NOT_STARTED();
// Ensure the auction is over
if (block.timestamp < _auction.endTime) revert AUCTION_ACTIVE();
// Mark the auction as settled
auction.settled = true;
// If a bid was placed:
if (_auction.highestBidder != address(0)) {
// Cache the amount of the highest bid
uint256 highestBid = _auction.highestBid;
// If the highest bid included ETH: Transfer it to the DAO treasury
if (highestBid != 0) _handleOutgoingTransfer(settings.treasury, highestBid);
// Transfer the token to the highest bidder
token.transferFrom(address(this), _auction.highestBidder, _auction.tokenId);
// Else no bid was placed:
} else {
// Burn the token
token.burn(_auction.tokenId);
}
emit AuctionSettled(_auction.tokenId, _auction.highestBidder, _auction.highestBid);
}
/// @dev Creates an auction for the next token
function _createAuction() private {
// Get the next token available for bidding
try token.mint() returns (uint256 tokenId) {
// Store the token id
auction.tokenId = tokenId;
// Cache the current timestamp
uint256 startTime = block.timestamp;
// Used to store the auction end time
uint256 endTime;
// Cannot realistically overflow
unchecked {
// Compute the auction end time
endTime = startTime + settings.duration;
}
// Store the auction start and end time
auction.startTime = uint40(startTime);
auction.endTime = uint40(endTime);
// Reset data from the previous auction
auction.highestBid = 0;
auction.highestBidder = address(0);
auction.settled = false;
emit AuctionCreated(tokenId, startTime, endTime);
// Pause the contract if token minting failed
} catch Error(string memory) {
_pause();
}
}
/// ///
/// PAUSE ///
/// ///
/// @notice Unpauses the auction house
function unpause() external onlyOwner {
_unpause();
// If this is the first auction:
if (auction.tokenId == 0) {
// Transfer ownership of the contract to the DAO
transferOwnership(settings.treasury);
// Start the first auction
_createAuction();
}
// Else if the contract was paused and the previous auction was settled:
else if (auction.settled) {
// Start the next auction
_createAuction();
}
}
/// @notice Pauses the auction house
function pause() external onlyOwner {
_pause();
}
/// @notice Settles the latest auction when the contract is paused
function settleAuction() external nonReentrant whenPaused {
_settleAuction();
}
/// ///
/// AUCTION SETTINGS ///
/// ///
/// @notice The DAO treasury
function treasury() external view returns (address) {
return settings.treasury;
}
/// @notice The time duration of each auction
function duration() external view returns (uint256) {
return settings.duration;
}
/// @notice The reserve price of each auction
function reservePrice() external view returns (uint256) {
return settings.reservePrice;
}
/// @notice The minimum amount of time to place a bid during an active auction
function timeBuffer() external view returns (uint256) {
return settings.timeBuffer;
}
/// @notice The minimum percentage an incoming bid must raise the highest bid
function minBidIncrement() external view returns (uint256) {
return settings.minBidIncrement;
}
/// ///
/// UPDATE SETTINGS ///
/// ///
/// @notice Updates the time duration of each auction
/// @param _duration The new time duration
function setDuration(uint256 _duration) external onlyOwner {
settings.duration = SafeCast.toUint40(_duration);
emit DurationUpdated(_duration);
}
/// @notice Updates the reserve price of each auction
/// @param _reservePrice The new reserve price
function setReservePrice(uint256 _reservePrice) external onlyOwner {
settings.reservePrice = _reservePrice;
emit ReservePriceUpdated(_reservePrice);
}
/// @notice Updates the time buffer of each auction
/// @param _timeBuffer The new time buffer
function setTimeBuffer(uint256 _timeBuffer) external onlyOwner {
settings.timeBuffer = SafeCast.toUint40(_timeBuffer);
emit TimeBufferUpdated(_timeBuffer);
}
/// @notice Updates the minimum bid increment of each subsequent bid
/// @param _percentage The new percentage
function setMinimumBidIncrement(uint256 _percentage) external onlyOwner {
settings.minBidIncrement = SafeCast.toUint8(_percentage);
emit MinBidIncrementPercentageUpdated(_percentage);
}
/// ///
/// TRANSFER UTIL ///
/// ///
/// @notice Transfer ETH/WETH from the contract
/// @param _to The recipient address
/// @param _amount The amount transferring
function _handleOutgoingTransfer(address _to, uint256 _amount) private {
// Ensure the contract has enough ETH to transfer
if (address(this).balance < _amount) revert INSOLVENT();
// Used to store if the transfer succeeded
bool success;
assembly {
// Transfer ETH to the recipient
// Limit the call to 50,000 gas
success := call(50000, _to, _amount, 0, 0, 0, 0)
}
// If the transfer failed:
if (!success) {
// Wrap as WETH
IWETH(WETH).deposit{ value: _amount }();
// Transfer WETH instead
IWETH(WETH).transfer(_to, _amount);
}
}
/// ///
/// AUCTION UPGRADE ///
/// ///
/// @notice Ensures the caller is authorized to upgrade the contract and the new implementation is valid
/// @dev This function is called in `upgradeTo` & `upgradeToAndCall`
/// @param _newImpl The new implementation address
function _authorizeUpgrade(address _newImpl) internal view override onlyOwner {
// Ensure the new implementation is registered by the Builder DAO
if (!manager.isRegisteredUpgrade(_getImplementation(), _newImpl)) revert INVALID_UPGRADE(_newImpl);
}
}