-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathDebtIssuanceModuleV2.sol
More file actions
393 lines (338 loc) · 17.4 KB
/
DebtIssuanceModuleV2.sol
File metadata and controls
393 lines (338 loc) · 17.4 KB
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
/*
Copyright 2021 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/
pragma solidity 0.6.10;
pragma experimental "ABIEncoderV2";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { DebtIssuanceModule } from "./DebtIssuanceModule.sol";
import { IController } from "../../interfaces/IController.sol";
import { Invoke } from "../lib/Invoke.sol";
import { ISetToken } from "../../interfaces/ISetToken.sol";
import { IssuanceValidationUtils } from "../lib/IssuanceValidationUtils.sol";
import { Position } from "../lib/Position.sol";
/**
* @title DebtIssuanceModuleV2
* @author Set Protocol
*
* The DebtIssuanceModuleV2 is a module that enables users to issue and redeem SetTokens that contain default and all
* external positions, including debt positions. Module hooks are added to allow for syncing of positions, and component
* level hooks are added to ensure positions are replicated correctly. The manager can define arbitrary issuance logic
* in the manager hook, as well as specify issue and redeem fees.
*
* NOTE:
* DebtIssuanceModule contract confirms increase/decrease in balance of component held by the SetToken after every transfer in/out
* for each component during issuance/redemption. This contract replaces those strict checks with slightly looser checks which
* ensure that the SetToken remains collateralized after every transfer in/out for each component during issuance/redemption.
* This module should be used to issue/redeem SetToken whose one or more components return a balance value with +/-1 wei error.
* For example, this module can be used to issue/redeem SetTokens which has one or more aTokens as its components.
* The new checks do NOT apply to any transfers that are part of an external position. A token that has rounding issues may lead to
* reverts if it is included as an external position unless explicitly allowed in a module hook.
*
* The getRequiredComponentIssuanceUnits function on this module assumes that Default token balances will be synced on every issuance
* and redemption. If token balances are not being synced it will over-estimate the amount of tokens required to issue a Set.
*
* CHANGELOG 12/15/2021:
* - call manager pre-redeem hooks on redemption
*/
contract DebtIssuanceModuleV2 is DebtIssuanceModule {
using Position for uint256;
/* ============ Constructor ============ */
constructor(IController _controller) public DebtIssuanceModule(_controller) {}
/* ============ External Functions ============ */
/**
* Deposits components to the SetToken, replicates any external module component positions and mints
* the SetToken. If the token has a debt position all collateral will be transferred in first then debt
* will be returned to the minting address. If specified, a fee will be charged on issuance.
*
* NOTE: Overrides DebtIssuanceModule#issue external function and adds undercollateralization checks in place of the
* previous default strict balances checks. The undercollateralization checks are implemented in IssuanceValidationUtils library and they
* revert upon undercollateralization of the SetToken post component transfer.
*
* @param _setToken Instance of the SetToken to issue
* @param _quantity Quantity of SetToken to issue
* @param _to Address to mint SetToken to
*/
function issue(
ISetToken _setToken,
uint256 _quantity,
address _to
)
external
override
nonReentrant
onlyValidAndInitializedSet(_setToken)
{
require(_quantity > 0, "Issue quantity must be > 0");
address hookContract = _callManagerPreIssueHooks(_setToken, _quantity, msg.sender, _to);
_callModulePreIssueHooks(_setToken, _quantity);
uint256 initialSetSupply = _setToken.totalSupply();
(
uint256 quantityWithFees,
uint256 managerFee,
uint256 protocolFee
) = calculateTotalFees(_setToken, _quantity, true);
// Prevent stack too deep
{
(
address[] memory components,
uint256[] memory equityUnits,
uint256[] memory debtUnits
) = _calculateRequiredComponentIssuanceUnits(_setToken, quantityWithFees, true);
uint256 finalSetSupply = initialSetSupply.add(quantityWithFees);
_resolveEquityPositions(_setToken, quantityWithFees, _to, true, components, equityUnits, initialSetSupply, finalSetSupply);
_resolveDebtPositions(_setToken, quantityWithFees, true, components, debtUnits, initialSetSupply, finalSetSupply);
_resolveFees(_setToken, managerFee, protocolFee);
}
_setToken.mint(_to, _quantity);
emit SetTokenIssued(
_setToken,
msg.sender,
_to,
hookContract,
_quantity,
managerFee,
protocolFee
);
}
/**
* Returns components from the SetToken, unwinds any external module component positions and burns the SetToken.
* If the token has debt positions, the module transfers in the required debt amounts from the caller and uses
* those funds to repay the debts on behalf of the SetToken. All debt will be paid down first then equity positions
* will be returned to the minting address. If specified, a fee will be charged on redeem.
*
* NOTE: Overrides DebtIssuanceModule#redeem internal function and adds undercollateralization checks in place of the
* previous default strict balances checks. The undercollateralization checks are implemented in IssuanceValidationUtils library
* and they revert upon undercollateralization of the SetToken post component transfer.
*
* @param _setToken Instance of the SetToken to redeem
* @param _quantity Quantity of SetToken to redeem
* @param _to Address to send collateral to
*/
function redeem(
ISetToken _setToken,
uint256 _quantity,
address _to
)
external
override
nonReentrant
onlyValidAndInitializedSet(_setToken)
{
require(_quantity > 0, "Redeem quantity must be > 0");
address hookContract = _callManagerPreRedeemHooks(_setToken, _quantity, msg.sender, _to);
_callModulePreRedeemHooks(_setToken, _quantity);
uint256 initialSetSupply = _setToken.totalSupply();
// Place burn after pre-redeem hooks because burning tokens may lead to false accounting of synced positions
_setToken.burn(msg.sender, _quantity);
(
uint256 quantityNetFees,
uint256 managerFee,
uint256 protocolFee
) = calculateTotalFees(_setToken, _quantity, false);
// Prevent stack too deep
{
(
address[] memory components,
uint256[] memory equityUnits,
uint256[] memory debtUnits
) = _calculateRequiredComponentIssuanceUnits(_setToken, quantityNetFees, false);
uint256 finalSetSupply = initialSetSupply.sub(quantityNetFees);
_resolveDebtPositions(_setToken, quantityNetFees, false, components, debtUnits, initialSetSupply, finalSetSupply);
_resolveEquityPositions(_setToken, quantityNetFees, _to, false, components, equityUnits, initialSetSupply, finalSetSupply);
_resolveFees(_setToken, managerFee, protocolFee);
}
emit SetTokenRedeemed(
_setToken,
msg.sender,
_to,
hookContract,
_quantity,
managerFee,
protocolFee
);
}
/* ============ External View Functions ============ */
/**
* Calculates the amount of each component needed to collateralize passed issue quantity plus fees of Sets as well as amount of debt
* that will be returned to caller. Default equity alues are calculated based on token balances and not position units in order to more
* closely track any accrued tokens that will be synced during issuance. External equity and debt positions will use the stored position
* units. IF TOKEN VALUES ARE NOT BEING SYNCED DURING ISSUANCE THIS FUNCTION WILL OVER ESTIMATE THE AMOUNT OF REQUIRED TOKENS.
*
* @param _setToken Instance of the SetToken to issue
* @param _quantity Amount of Sets to be issued
*
* @return address[] Array of component addresses making up the Set
* @return uint256[] Array of equity notional amounts of each component, respectively, represented as uint256
* @return uint256[] Array of debt notional amounts of each component, respectively, represented as uint256
*/
function getRequiredComponentIssuanceUnits(
ISetToken _setToken,
uint256 _quantity
)
external
view
override
returns (address[] memory, uint256[] memory, uint256[] memory)
{
(
uint256 totalQuantity,,
) = calculateTotalFees(_setToken, _quantity, true);
if(_setToken.totalSupply() == 0) {
return _calculateRequiredComponentIssuanceUnits(_setToken, totalQuantity, true);
} else {
(
address[] memory components,
uint256[] memory equityUnits,
uint256[] memory debtUnits
) = _getTotalIssuanceUnitsFromBalances(_setToken);
uint256 componentsLength = components.length;
uint256[] memory totalEquityUnits = new uint256[](componentsLength);
uint256[] memory totalDebtUnits = new uint256[](componentsLength);
for (uint256 i = 0; i < components.length; i++) {
// Use preciseMulCeil to round up to ensure overcollateration of equity when small issue quantities are provided
// and use preciseMul to round debt calculations down to make sure we don't return too much debt to issuer
totalEquityUnits[i] = equityUnits[i].preciseMulCeil(totalQuantity);
totalDebtUnits[i] = debtUnits[i].preciseMul(totalQuantity);
}
return (components, totalEquityUnits, totalDebtUnits);
}
}
/* ============ Internal Functions ============ */
/**
* Resolve equity positions associated with SetToken. On issuance, the total equity position for an asset (including default and external
* positions) is transferred in. Then any external position hooks are called to transfer the external positions to their necessary place.
* On redemption all external positions are recalled by the external position hook, then those position plus any default position are
* transferred back to the _to address.
*/
function _resolveEquityPositions(
ISetToken _setToken,
uint256 _quantity,
address _to,
bool _isIssue,
address[] memory _components,
uint256[] memory _componentEquityQuantities,
uint256 _initialSetSupply,
uint256 _finalSetSupply
)
internal
{
for (uint256 i = 0; i < _components.length; i++) {
address component = _components[i];
uint256 componentQuantity = _componentEquityQuantities[i];
if (componentQuantity > 0) {
if (_isIssue) {
// Call SafeERC20#safeTransferFrom instead of ExplicitERC20#transferFrom
SafeERC20.safeTransferFrom(
IERC20(component),
msg.sender,
address(_setToken),
componentQuantity
);
IssuanceValidationUtils.validateCollateralizationPostTransferInPreHook(_setToken, component, _initialSetSupply, componentQuantity);
_executeExternalPositionHooks(_setToken, _quantity, IERC20(component), true, true);
} else {
_executeExternalPositionHooks(_setToken, _quantity, IERC20(component), false, true);
// Call Invoke#invokeTransfer instead of Invoke#strictInvokeTransfer
_setToken.invokeTransfer(component, _to, componentQuantity);
IssuanceValidationUtils.validateCollateralizationPostTransferOut(_setToken, component, _finalSetSupply);
}
}
}
}
/**
* Resolve debt positions associated with SetToken. On issuance, debt positions are entered into by calling the external position hook. The
* resulting debt is then returned to the calling address. On redemption, the module transfers in the required debt amount from the caller
* and uses those funds to repay the debt on behalf of the SetToken.
*/
function _resolveDebtPositions(
ISetToken _setToken,
uint256 _quantity,
bool _isIssue,
address[] memory _components,
uint256[] memory _componentDebtQuantities,
uint256 _initialSetSupply,
uint256 _finalSetSupply
)
internal
{
for (uint256 i = 0; i < _components.length; i++) {
address component = _components[i];
uint256 componentQuantity = _componentDebtQuantities[i];
if (componentQuantity > 0) {
if (_isIssue) {
_executeExternalPositionHooks(_setToken, _quantity, IERC20(component), true, false);
// Call Invoke#invokeTransfer instead of Invoke#strictInvokeTransfer
_setToken.invokeTransfer(component, msg.sender, componentQuantity);
IssuanceValidationUtils.validateCollateralizationPostTransferOut(_setToken, component, _finalSetSupply);
} else {
// Call SafeERC20#safeTransferFrom instead of ExplicitERC20#transferFrom
SafeERC20.safeTransferFrom(
IERC20(component),
msg.sender,
address(_setToken),
componentQuantity
);
IssuanceValidationUtils.validateCollateralizationPostTransferInPreHook(_setToken, component, _initialSetSupply, componentQuantity);
_executeExternalPositionHooks(_setToken, _quantity, IERC20(component), false, false);
}
}
}
}
/**
* Reimplementation of _getTotalIssuanceUnits but instead derives Default equity positions from token balances on Set instead of from
* position units. This function is ONLY to be used in getRequiredComponentIssuanceUnits in order to return more accurate required
* token amounts to issuers when positions are being synced on issuance.
*
* @param _setToken Instance of the SetToken to issue
*
* @return address[] Array of component addresses making up the Set
* @return uint256[] Array of equity unit amounts of each component, respectively, represented as uint256
* @return uint256[] Array of debt unit amounts of each component, respectively, represented as uint256
*/
function _getTotalIssuanceUnitsFromBalances(
ISetToken _setToken
)
internal
view
returns (address[] memory, uint256[] memory, uint256[] memory)
{
address[] memory components = _setToken.getComponents();
uint256 componentsLength = components.length;
uint256[] memory equityUnits = new uint256[](componentsLength);
uint256[] memory debtUnits = new uint256[](componentsLength);
uint256 totalSupply = _setToken.totalSupply();
for (uint256 i = 0; i < components.length; i++) {
address component = components[i];
int256 cumulativeEquity = totalSupply
.getDefaultPositionUnit(IERC20(component).balanceOf(address(_setToken)))
.toInt256();
int256 cumulativeDebt = 0;
address[] memory externalPositions = _setToken.getExternalPositionModules(component);
if (externalPositions.length > 0) {
for (uint256 j = 0; j < externalPositions.length; j++) {
int256 externalPositionUnit = _setToken.getExternalPositionRealUnit(component, externalPositions[j]);
// If positionUnit <= 0 it will be "added" to debt position
if (externalPositionUnit > 0) {
cumulativeEquity = cumulativeEquity.add(externalPositionUnit);
} else {
cumulativeDebt = cumulativeDebt.add(externalPositionUnit);
}
}
}
equityUnits[i] = cumulativeEquity.toUint256();
debtUnits[i] = cumulativeDebt.mul(-1).toUint256();
}
return (components, equityUnits, debtUnits);
}
}