-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBuyout.sol
504 lines (471 loc) · 20.5 KB
/
Buyout.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import {IBuyout, Auction, State} from "../interfaces/IBuyout.sol";
import {IERC1155} from "../interfaces/IERC1155.sol";
import {ISupply} from "../interfaces/ISupply.sol";
import {ITransfer} from "../interfaces/ITransfer.sol";
import {IVault} from "../interfaces/IVault.sol";
import {IVaultRegistry, Permission} from "../interfaces/IVaultRegistry.sol";
import {Multicall} from "../utils/Multicall.sol";
import {NFTReceiver} from "../utils/NFTReceiver.sol";
import {SafeSend} from "../utils/SafeSend.sol";
import {SelfPermit} from "../utils/SelfPermit.sol";
/// @title Buyout
/// @author Fractional Art
/// @notice Module contract for vaults to hold buyout pools
/// - A fractional owner starts an auction for a vault by depositing any amount of ether and fractional tokens into a pool.
/// - During the proposal period (2 days) users can sell their fractional tokens into the pool for ether.
/// - During the rejection period (4 days) users can buy fractional tokens from the pool with ether.
/// - If a pool has more than 51% of the total supply after 4 days, the buyout is successful and the proposer
/// gains access to withdraw the underlying assets (ERC20, ERC721, and ERC1155 tokens) from the vault.
/// Otherwise the buyout is considered unsuccessful and a new one may then begin.
/// - NOTE: A vault may only have one active buyout at any given time.
/// - buyoutPrice = (ethDeposit * 100) / (100 - ((fractionDeposit * 100) / totalSupply))
/// - buyoutShare = (tokenBalance * ethBalance) / (totalSupply + tokenBalance)
contract Buyout is IBuyout, Multicall, NFTReceiver, SafeSend, SelfPermit {
/// @notice Address of VaultRegistry contract
address public registry;
/// @notice Address of Supply target contract
address public supply;
/// @notice Address of Transfer target contract
address public transfer;
/// @notice Time length of the proposal period
uint256 public constant PROPOSAL_PERIOD = 2 days;
/// @notice Time length of the rejection period
uint256 public constant REJECTION_PERIOD = 4 days;
/// @notice Mapping of vault address to auction struct
mapping(address => Auction) public buyoutInfo;
/// @notice Initializes registry, supply, and transfer contracts
constructor(
address _registry,
address _supply,
address _transfer
) {
registry = _registry;
supply = _supply;
transfer = _transfer;
}
/// @dev Callback for receiving ether when the calldata is empty
receive() external payable {}
/// @notice Starts the auction for a buyout pool
/// @param _vault Address of the vault
function start(address _vault) external payable {
// Reverts if ether deposit amount is zero
if (msg.value == 0) revert ZeroDeposit();
// Reverts if address is not a registered vault
(address token, uint256 id) = IVaultRegistry(registry).vaultToToken(
_vault
);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not inactive
(, , State current, , , ) = this.buyoutInfo(_vault);
State required = State.INACTIVE;
if (current != required) revert InvalidState(required, current);
// Gets total supply of fractional tokens for the vault
uint256 totalSupply = IVaultRegistry(registry).totalSupply(_vault);
// Gets total balance of fractional tokens owned by caller
uint256 depositAmount = IERC1155(token).balanceOf(msg.sender, id);
// Transfers fractional tokens into the buyout pool
IERC1155(token).safeTransferFrom(
msg.sender,
address(this),
id,
depositAmount,
""
);
// Calculates price of buyout and fractions
// @dev Reverts with division error if called with total supply of tokens
uint256 buyoutPrice = (msg.value * 100) /
(100 - ((depositAmount * 100) / totalSupply));
uint256 fractionPrice = buyoutPrice / totalSupply;
// Sets info mapping of the vault address to auction struct
buyoutInfo[_vault] = Auction(
block.timestamp,
msg.sender,
State.LIVE,
fractionPrice,
msg.value,
totalSupply
);
// Emits event for starting auction
emit Start(
_vault,
msg.sender,
block.timestamp,
buyoutPrice,
fractionPrice
);
}
/// @notice Sells fractional tokens in exchange for ether from a pool
/// @param _vault Address of the vault
/// @param _amount Transfer amount of fractions
function sellFractions(address _vault, uint256 _amount) external {
// Reverts if address is not a registered vault
(address token, uint256 id) = IVaultRegistry(registry).vaultToToken(
_vault
);
if (id == 0) revert NotVault(_vault);
(uint256 startTime, , State current, uint256 fractionPrice, , ) = this
.buyoutInfo(_vault);
// Reverts if auction state is not live
State required = State.LIVE;
if (current != required) revert InvalidState(required, current);
// Reverts if current time is greater than end time of proposal period
uint256 endTime = startTime + PROPOSAL_PERIOD;
if (block.timestamp > endTime)
revert TimeExpired(block.timestamp, endTime);
// Transfers fractional tokens to pool from caller
IERC1155(token).safeTransferFrom(
msg.sender,
address(this),
id,
_amount,
""
);
// Updates ether balance of pool
uint256 ethAmount = fractionPrice * _amount;
buyoutInfo[_vault].ethBalance -= ethAmount;
// Transfers ether amount to caller
_sendEthOrWeth(msg.sender, ethAmount);
// Emits event for selling fractions into pool
emit SellFractions(msg.sender, _amount);
}
/// @notice Buys fractional tokens in exchange for ether from a pool
/// @param _vault Address of the vault
/// @param _amount Transfer amount of fractions
function buyFractions(address _vault, uint256 _amount) external payable {
// Reverts if address is not a registered vault
(address token, uint256 id) = IVaultRegistry(registry).vaultToToken(
_vault
);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not live
(uint256 startTime, , State current, uint256 fractionPrice, , ) = this
.buyoutInfo(_vault);
State required = State.LIVE;
if (current != required) revert InvalidState(required, current);
// Reverts if current time is greater than end time of rejection period
uint256 endTime = startTime + REJECTION_PERIOD;
if (block.timestamp > endTime)
revert TimeExpired(block.timestamp, endTime);
// Reverts if payment amount does not equal price of fractional amount
if (msg.value != fractionPrice * _amount) revert InvalidPayment();
// Transfers fractional tokens to caller
IERC1155(token).safeTransferFrom(
address(this),
msg.sender,
id,
_amount,
""
);
// Updates ether balance of pool
buyoutInfo[_vault].ethBalance += msg.value;
// Emits event for buying fractions from pool
emit BuyFractions(msg.sender, _amount);
}
/// @notice Ends the auction for a live buyout pool
/// @param _vault Address of the vault
/// @param _burnProof Merkle proof for burning fractional tokens
function end(address _vault, bytes32[] calldata _burnProof) external {
// Reverts if address is not a registered vault
(address token, uint256 id) = IVaultRegistry(registry).vaultToToken(
_vault
);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not live
(
uint256 startTime,
address proposer,
State current,
,
uint256 ethBalance,
) = this.buyoutInfo(_vault);
State required = State.LIVE;
if (current != required) revert InvalidState(required, current);
// Reverts if current time is less than or equal to end time of auction
uint256 endTime = startTime + REJECTION_PERIOD;
if (block.timestamp <= endTime)
revert TimeNotElapsed(block.timestamp, endTime);
uint256 tokenBalance = IERC1155(token).balanceOf(address(this), id);
// Checks totalSupply of auction pool to determine if buyout is successful or not
if (
(tokenBalance * 1000) /
IVaultRegistry(registry).totalSupply(_vault) >
500
) {
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ISupply.burn,
(address(this), tokenBalance)
);
// Executes burn of fractional tokens from pool
IVault(payable(_vault)).execute(supply, data, _burnProof);
// Sets buyout state to successful
buyoutInfo[_vault].state = State.SUCCESS;
// Emits event for ending successful auction
emit End(_vault, State.SUCCESS, proposer);
} else {
// Deletes auction info
delete buyoutInfo[_vault];
// Transfers fractions and ether back to proposer of the buyout pool
IERC1155(token).safeTransferFrom(
address(this),
proposer,
id,
tokenBalance,
""
);
_sendEthOrWeth(proposer, ethBalance);
// Emits event for ending unsuccessful auction
emit End(_vault, State.INACTIVE, proposer);
}
}
/// @notice Cashes out proceeds from a successful buyout
/// @param _vault Address of the vault
/// @param _burnProof Merkle proof for burning fractional tokens
function cash(address _vault, bytes32[] calldata _burnProof) external {
// Reverts if address is not a registered vault
(address token, uint256 id) = IVaultRegistry(registry).vaultToToken(
_vault
);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, , State current, , uint256 ethBalance, ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller has a balance of zero fractional tokens
uint256 tokenBalance = IERC1155(token).balanceOf(msg.sender, id);
if (tokenBalance == 0) revert NoFractions();
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ISupply.burn,
(msg.sender, tokenBalance)
);
// Executes burn of fractional tokens from caller
IVault(payable(_vault)).execute(supply, data, _burnProof);
// Transfers buyout share amount to caller based on total supply
uint256 totalSupply = IVaultRegistry(registry).totalSupply(_vault);
uint256 buyoutShare = (tokenBalance * ethBalance) /
(totalSupply + tokenBalance);
_sendEthOrWeth(msg.sender, buyoutShare);
// Emits event for cashing out of buyout pool
emit Cash(_vault, msg.sender, buyoutShare);
}
/// @notice Terminates a vault with an inactive buyout
/// @param _vault Address of the vault
/// @param _burnProof Merkle proof for burning fractional tokens
function redeem(address _vault, bytes32[] calldata _burnProof) external {
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not inactive
(, , State current, , , ) = this.buyoutInfo(_vault);
State required = State.INACTIVE;
if (current != required) revert InvalidState(required, current);
// Initializes vault transaction
uint256 totalSupply = IVaultRegistry(registry).totalSupply(_vault);
bytes memory data = abi.encodeCall(
ISupply.burn,
(msg.sender, totalSupply)
);
// Executes burn of fractional tokens from caller
IVault(payable(_vault)).execute(supply, data, _burnProof);
// Sets buyout state to successful and proposer to caller
(buyoutInfo[_vault].state, buyoutInfo[_vault].proposer) = (
State.SUCCESS,
msg.sender
);
// Emits event for redeem underlying assets from the vault
emit Redeem(_vault, msg.sender);
}
/// @notice Withdraws an ERC-20 token from a vault
/// @param _vault Address of the vault
/// @param _token Address of the token
/// @param _to Address of the receiver
/// @param _value Transfer amount
/// @param _erc20TransferProof Merkle proof for transferring an ERC-20 token
function withdrawERC20(
address _vault,
address _token,
address _to,
uint256 _value,
bytes32[] calldata _erc20TransferProof
) external {
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, address proposer, State current, , , ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller is not the auction winner
if (msg.sender != proposer) revert NotWinner();
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ITransfer.ERC20Transfer,
(_token, _to, _value)
);
// Executes transfer of ERC721 token to caller
IVault(payable(_vault)).execute(transfer, data, _erc20TransferProof);
}
/// @notice Withdraws an ERC-721 token from a vault
/// @param _vault Address of the vault
/// @param _token Address of the token
/// @param _to Address of the receiver
/// @param _tokenId ID of the token
/// @param _erc721TransferProof Merkle proof for transferring an ERC-721 token
function withdrawERC721(
address _vault,
address _token,
address _to,
uint256 _tokenId,
bytes32[] calldata _erc721TransferProof
) external {
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, address proposer, State current, , , ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller is not the auction winner
if (msg.sender != proposer) revert NotWinner();
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ITransfer.ERC721TransferFrom,
(_token, _vault, _to, _tokenId)
);
// Executes transfer of ERC721 token to caller
IVault(payable(_vault)).execute(transfer, data, _erc721TransferProof);
}
/// @notice Withdraws an ERC-1155 token from a vault
/// @param _vault Address of the vault
/// @param _token Address of the token
/// @param _to Address of the receiver
/// @param _id ID of the token type
/// @param _value Transfer amount
/// @param _erc1155TransferProof Merkle proof for transferring an ERC-1155 token
function withdrawERC1155(
address _vault,
address _token,
address _to,
uint256 _id,
uint256 _value,
bytes32[] calldata _erc1155TransferProof
) external {
// Creates local scope to avoid stack too deep
{
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, address proposer, State current, , , ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller is not the auction winner
if (msg.sender != proposer) revert NotWinner();
}
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ITransfer.ERC1155TransferFrom,
(_token, _vault, _to, _id, _value)
);
// Executes transfer of ERC1155 token to caller
IVault(payable(_vault)).execute(transfer, data, _erc1155TransferProof);
}
/// @notice Batch withdraws ERC-1155 tokens from a vault
/// @param _vault Address of the vault
/// @param _token Address of the token
/// @param _to Address of the receiver
/// @param _ids IDs of each token type
/// @param _values Transfer amounts per token type
/// @param _erc1155BatchTransferProof Merkle proof for transferring multiple ERC-1155 tokens
function batchWithdrawERC1155(
address _vault,
address _token,
address _to,
uint256[] calldata _ids,
uint256[] calldata _values,
bytes32[] calldata _erc1155BatchTransferProof
) external {
// Creates local scope to avoid stack too deep
{
// Reverts if address is not a registered vault
(, uint256 id) = IVaultRegistry(registry).vaultToToken(_vault);
if (id == 0) revert NotVault(_vault);
// Reverts if auction state is not successful
(, address proposer, State current, , , ) = this.buyoutInfo(_vault);
State required = State.SUCCESS;
if (current != required) revert InvalidState(required, current);
// Reverts if caller is not the auction winner
if (msg.sender != proposer) revert NotWinner();
}
// Initializes vault transaction
bytes memory data = abi.encodeCall(
ITransfer.ERC1155BatchTransferFrom,
(_token, _vault, _to, _ids, _values)
);
// Executes batch transfer of multiple ERC1155 tokens to caller
IVault(payable(_vault)).execute(
transfer,
data,
_erc1155BatchTransferProof
);
}
/// @notice Gets the list of leaf nodes used to generate a merkle tree
/// @dev Leaf nodes are hashed permissions of the merkle tree
/// @return nodes Hashes of leaf nodes
function getLeafNodes() external view returns (bytes32[] memory nodes) {
nodes = new bytes32[](5);
// Gets list of permissions from this module
Permission[] memory permissions = getPermissions();
for (uint256 i; i < permissions.length; ) {
// Hashes permission into leaf node
nodes[i] = keccak256(abi.encode(permissions[i]));
// Can't overflow since loop is a fixed size
unchecked {
++i;
}
}
}
/// @notice Gets the list of permissions installed on a vault
/// @dev Permissions consist of a module contract, target contract, and function selector
/// @return permissions List of vault permissions
function getPermissions()
public
view
returns (Permission[] memory permissions)
{
permissions = new Permission[](5);
// Burn function selector from supply contract
permissions[0] = Permission(
address(this),
supply,
ISupply(supply).burn.selector
);
// ERC20Transfer function selector from transfer contract
permissions[1] = Permission(
address(this),
transfer,
ITransfer(transfer).ERC20Transfer.selector
);
// ERC721TransferFrom function selector from transfer contract
permissions[2] = Permission(
address(this),
transfer,
ITransfer(transfer).ERC721TransferFrom.selector
);
// ERC1155TransferFrom function selector from transfer contract
permissions[3] = Permission(
address(this),
transfer,
ITransfer(transfer).ERC1155TransferFrom.selector
);
// ERC1155BatchTransferFrom function selector from transfer contract
permissions[4] = Permission(
address(this),
transfer,
ITransfer(transfer).ERC1155BatchTransferFrom.selector
);
}
}