-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAuctionEscapeHatch.sol
240 lines (191 loc) · 7.32 KB
/
AuctionEscapeHatch.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
pragma solidity >=0.6.6;
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/upgrades/contracts/Initializable.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "./Permissions.sol";
import "./interfaces/IAuction.sol";
import "./interfaces/IDexHandler.sol";
import "./interfaces/IBurnMintableERC20.sol";
import "./Auction.sol";
struct EarlyExitData {
uint256 exitedEarly;
uint256 earlyExitReturn;
uint256 maltUsed;
}
struct AuctionExits {
uint256 exitedEarly;
uint256 earlyExitReturn;
uint256 maltUsed;
mapping(address => EarlyExitData) accountExits;
}
/// @title Auction Escape Hatch
/// @author 0xScotch <scotch@malt.money>
/// @notice Functionality to reduce risk profile of holding arbitrage tokens by allowing early exit
contract AuctionEscapeHatch is Initializable, Permissions {
using SafeMath for uint256;
using SafeERC20 for ERC20;
IAuction public auction;
IDexHandler public dexHandler;
ERC20 public collateralToken;
IBurnMintableERC20 public malt;
uint256 public maxEarlyExitBps = 200; // 20%
uint256 public cooloffPeriod = 60 * 60 * 24; // 24 hours
mapping(uint256 => AuctionExits) internal auctionEarlyExits;
event EarlyExit(address account, uint256 amount, uint256 received);
function initialize(
address _timelock,
address initialAdmin,
address _collateralToken,
address _malt,
address _auction,
address _handler
) external initializer {
_adminSetup(_timelock);
_setupRole(ADMIN_ROLE, initialAdmin);
collateralToken = ERC20(_collateralToken);
malt = IBurnMintableERC20(_malt);
auction = IAuction(_auction);
dexHandler = IDexHandler(_handler);
}
function exitEarly(uint256 _auctionId, uint256 amount, uint256 minOut) external notSameBlock {
uint256 maltQuantity = _calculateMaltRequiredForExit(_auctionId, amount);
// TODO ensure this contract is added as a mint requester Sat 06 Nov 2021 20:13:49 GMT
malt.mint(address(dexHandler), maltQuantity);
uint256 amountOut = dexHandler.sellMalt();
require(amountOut > minOut, "EarlyExit: Insufficient output");
AuctionExits storage auctionExits = auctionEarlyExits[_auctionId];
auctionExits.exitedEarly = auctionExits.exitedEarly + amount;
auctionExits.earlyExitReturn = auctionExits.earlyExitReturn + amountOut;
auctionExits.maltUsed = auctionExits.maltUsed + maltQuantity;
auctionExits.accountExits[msg.sender].exitedEarly = auctionExits.accountExits[msg.sender].exitedEarly + amount;
auctionExits.accountExits[msg.sender].earlyExitReturn = auctionExits.accountExits[msg.sender].earlyExitReturn + amountOut;
auctionExits.accountExits[msg.sender].maltUsed = auctionExits.accountExits[msg.sender].maltUsed + maltQuantity;
auction.amendAccountParticipation(
msg.sender,
_auctionId,
amount,
maltQuantity
);
collateralToken.safeTransfer(msg.sender, amountOut);
emit EarlyExit(msg.sender, amount, amountOut);
}
function earlyExitReturn(address account, uint256 _auctionId, uint256 amount) public view returns(uint256) {
// We don't need all the values
(,,,,,
uint256 pegPrice,
,
uint256 auctionEndTime,
bool active
) = auction.getAuctionCore(_auctionId);
if(active || block.timestamp < auctionEndTime) {
return 0;
}
(
uint256 userCommitment,
uint256 userRedeemed,
uint256 userMaltPurchased
) = auction.getAuctionParticipationForAccount(account, _auctionId);
// This should never overflow due to guards in redemption code
uint256 userOutstanding = userCommitment - userRedeemed;
if (amount > userOutstanding) {
amount = userOutstanding;
}
if (amount == 0) {
return 0;
}
(uint256 currentPrice,) = dexHandler.maltMarketPrice();
uint256 maltQuantity = userMaltPurchased.mul(amount).div(userCommitment);
uint256 fullReturn = maltQuantity.mul(currentPrice) / pegPrice;
// setCooloffPeriod guards against cooloffPeriod ever being 0
uint256 progressionBps = (block.timestamp - auctionEndTime) * 10000 / cooloffPeriod;
if (progressionBps > 10000) {
progressionBps = 10000;
}
if (fullReturn > amount) {
// Allow a % of profit to be realised
uint256 maxProfit = (fullReturn - amount) * (maxEarlyExitBps * progressionBps / 10000) / 1000;
return amount + maxProfit;
}
return fullReturn;
}
function accountAuctionExits(address account, uint256 auctionId) external view returns (
uint256 exitedEarly,
uint256 earlyExitReturn,
uint256 maltUsed
) {
EarlyExitData storage accountExits = auctionEarlyExits[auctionId].accountExits[account];
return (accountExits.exitedEarly, accountExits.earlyExitReturn, accountExits.maltUsed);
}
function globalAuctionExits(uint256 auctionId) external view returns (
uint256 exitedEarly,
uint256 earlyExitReturn,
uint256 maltUsed
) {
AuctionExits storage auctionExits = auctionEarlyExits[auctionId];
return (auctionExits.exitedEarly, auctionExits.earlyExitReturn, auctionExits.maltUsed);
}
/*
* INTERNAL METHODS
*/
function _calculateMaltRequiredForExit(uint256 _auctionId, uint256 amount) internal returns(uint256) {
// We don't need all the values
(,,,,,
uint256 pegPrice,
,
uint256 auctionEndTime,
bool active
) = auction.getAuctionCore(_auctionId);
require(!active, "Cannot exit early on an active auction");
require(block.timestamp > auctionEndTime, "Auction not over");
(
uint256 userCommitment,
uint256 userRedeemed,
uint256 userMaltPurchased
) = auction.getAuctionParticipationForAccount(msg.sender, _auctionId);
// This should never overflow due to guards in redemption code
if (amount > (userCommitment - userRedeemed)) {
amount = userCommitment - userRedeemed;
}
require(amount > 0, "Nothing to claim");
(uint256 currentPrice,) = dexHandler.maltMarketPrice();
uint256 maltQuantity = userMaltPurchased.mul(amount).div(userCommitment);
uint256 fullReturn = maltQuantity.mul(currentPrice) / pegPrice;
// setCooloffPeriod guards against cooloffPeriod ever being 0
uint256 progressionBps = (block.timestamp - auctionEndTime) * 10000 / cooloffPeriod;
if (progressionBps > 10000) {
progressionBps = 10000;
}
if (fullReturn > amount) {
// Allow a % of profit to be realised
uint256 maxProfit = (fullReturn - amount) * (maxEarlyExitBps * progressionBps / 10000) / 1000;
uint256 desiredReturn = amount + maxProfit;
maltQuantity = desiredReturn.mul(pegPrice) / currentPrice;
}
return maltQuantity;
}
/*
* PRIVILEDGED METHODS
*/
function setEarlyExitBps(uint256 _earlyExitBps)
external
onlyRole(ADMIN_ROLE, "Must have admin privilege")
{
require(_earlyExitBps > 0 && _earlyExitBps <= 1000, "Must be between 0-100%");
maxEarlyExitBps = _earlyExitBps;
}
function setCooloffPeriod(uint256 _period)
external
onlyRole(ADMIN_ROLE, "Must have admin privilege")
{
require(_period > 0, "Cannot have 0 lookback period");
cooloffPeriod = _period;
}
function setDexHandler(address _handler)
external
onlyRole(ADMIN_ROLE, "Must have admin privilege")
{
dexHandler = IDexHandler(_handler);
}
}