/
RToken.sol
533 lines (455 loc) · 21.8 KB
/
RToken.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// SPDX-License-Identifier: BlueOak-1.0.0
pragma solidity 0.8.17;
// solhint-disable-next-line max-line-length
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "../interfaces/IMain.sol";
import "../interfaces/IRToken.sol";
import "../libraries/Fixed.sol";
import "../libraries/Throttle.sol";
import "../vendor/ERC20PermitUpgradeable.sol";
import "./mixins/Component.sol";
/**
* @title RTokenP1
* An ERC20 with an elastic supply and governable exchange rate to basket units.
*/
contract RTokenP1 is ComponentP1, ERC20PermitUpgradeable, IRToken {
using FixLib for uint192;
using ThrottleLib for ThrottleLib.Throttle;
using SafeERC20Upgradeable for IERC20Upgradeable;
uint256 public constant MIN_THROTTLE_RATE_AMT = 1e18; // {qRTok}
uint256 public constant MAX_THROTTLE_RATE_AMT = 1e48; // {qRTok}
uint192 public constant MAX_THROTTLE_PCT_AMT = 1e18; // {qRTok}
uint192 public constant MIN_EXCHANGE_RATE = 1e9; // D18{BU/rTok}
uint192 public constant MAX_EXCHANGE_RATE = 1e27; // D18{BU/rTok}
/// The mandate describes what goals its governors should try to achieve. By succinctly
/// explaining the RToken’s purpose and what the RToken is intended to do, it provides common
/// ground for the governors to decide upon priorities and how to weigh tradeoffs.
///
/// Example Mandates:
///
/// - Capital preservation first. Spending power preservation second. Permissionless
/// access third.
/// - Capital preservation above all else. All revenues fund the over-collateralization pool.
/// - Risk-neutral pursuit of profit for token holders.
/// Maximize (gross revenue - payments for over-collateralization and governance).
/// - This RToken holds only FooCoin, to provide a trade for hedging against its
/// possible collapse.
///
/// The mandate may also be a URI to a longer body of text, presumably on IPFS or some other
/// immutable data store.
string public mandate;
// ==== Peer components ====
IAssetRegistry private assetRegistry;
IBasketHandler private basketHandler;
IBackingManager private backingManager;
IFurnace private furnace;
// The number of baskets that backingManager must hold
// in order for this RToken to be fully collateralized.
// The exchange rate for issuance and redemption is totalSupply()/basketsNeeded {BU}/{qRTok}.
uint192 public basketsNeeded; // D18{BU}
// === Supply throttles ===
ThrottleLib.Throttle private issuanceThrottle;
ThrottleLib.Throttle private redemptionThrottle;
function init(
IMain main_,
string calldata name_,
string calldata symbol_,
string calldata mandate_,
ThrottleLib.Params calldata issuanceThrottleParams_,
ThrottleLib.Params calldata redemptionThrottleParams_
) external initializer {
require(bytes(name_).length > 0, "name empty");
require(bytes(symbol_).length > 0, "symbol empty");
require(bytes(mandate_).length > 0, "mandate empty");
__Component_init(main_);
__ERC20_init(name_, symbol_);
__ERC20Permit_init(name_);
assetRegistry = main_.assetRegistry();
basketHandler = main_.basketHandler();
backingManager = main_.backingManager();
furnace = main_.furnace();
mandate = mandate_;
setIssuanceThrottleParams(issuanceThrottleParams_);
setRedemptionThrottleParams(redemptionThrottleParams_);
issuanceThrottle.lastTimestamp = uint48(block.timestamp);
redemptionThrottle.lastTimestamp = uint48(block.timestamp);
}
/// Issue an RToken on the current basket
/// @param amount {qTok} The quantity of RToken to issue
/// @custom:interaction nearly CEI, but see comments around handling of refunds
function issue(uint256 amount) public {
issueTo(_msgSender(), amount);
}
/// Issue an RToken on the current basket, to a particular recipient
/// @param recipient The address to receive the issued RTokens
/// @param amount {qRTok} The quantity of RToken to issue
/// @custom:interaction
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
function issueTo(address recipient, uint256 amount) public notIssuancePausedOrFrozen {
require(amount > 0, "Cannot issue zero");
// == Refresh ==
assetRegistry.refresh();
// == Checks-effects block ==
address issuer = _msgSender(); // OK to save: it can't be changed in reentrant runs
// Ensure basket is ready, SOUND and not in warmup period
require(basketHandler.isReady(), "basket not ready");
furnace.melt();
uint256 supply = totalSupply();
// Revert if issuance exceeds either supply throttle
issuanceThrottle.useAvailable(supply, int256(amount)); // reverts on over-issuance
redemptionThrottle.useAvailable(supply, -int256(amount)); // shouldn't revert
// AT THIS POINT:
// all contract invariants hold
// furnace melting is up-to-date
// asset states are up-to-date
// throttle is up-to-date
// amtBaskets: the BU change to be recorded by this issuance
// D18{BU} = D18{BU} * {qRTok} / {qRTok}
// revert-on-overflow provided by FixLib functions
uint192 amtBaskets = supply > 0
? basketsNeeded.muluDivu(amount, supply, CEIL)
: _safeWrap(amount);
emit Issuance(issuer, recipient, amount, amtBaskets);
(address[] memory erc20s, uint256[] memory deposits) = basketHandler.quote(
amtBaskets,
CEIL
);
// == Interactions: Create RToken + transfer tokens to BackingManager ==
_scaleUp(recipient, amtBaskets, supply);
for (uint256 i = 0; i < erc20s.length; ++i) {
IERC20Upgradeable(erc20s[i]).safeTransferFrom(
issuer,
address(backingManager),
deposits[i]
);
}
}
/// Redeem RToken for basket collateral
/// @param amount {qTok} The quantity {qRToken} of RToken to redeem
/// @custom:interaction CEI
function redeem(uint256 amount) external {
redeemTo(_msgSender(), amount);
}
/// Redeem RToken for basket collateral to a particular recipient
// checks:
// amount > 0
// amount <= balanceOf(caller)
//
// effects:
// (so totalSupply -= amount and balanceOf(caller) -= amount)
// basketsNeeded' / totalSupply' >== basketsNeeded / totalSupply
// burn(caller, amount)
//
// actions:
// let erc20s = basketHandler.erc20s()
// for each token in erc20s:
// let tokenAmt = (amount * basketsNeeded / totalSupply) current baskets
// do token.transferFrom(backingManager, caller, tokenAmt)
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
/// @param recipient The address to receive the backing collateral tokens
/// @param amount {qRTok} The quantity {qRToken} of RToken to redeem
/// @custom:interaction RCEI
function redeemTo(address recipient, uint256 amount) public notFrozen {
// == Refresh ==
assetRegistry.refresh();
// solhint-disable-next-line no-empty-blocks
try main.furnace().melt() {} catch {} // nice for the redeemer, but not necessary
// == Checks and Effects ==
require(amount > 0, "Cannot redeem zero");
require(amount <= balanceOf(_msgSender()), "insufficient balance");
require(basketHandler.fullyCollateralized(), "partial redemption; use redeemCustom");
// redemption while IFFY/DISABLED allowed
uint256 supply = totalSupply();
// Revert if redemption exceeds either supply throttle
issuanceThrottle.useAvailable(supply, -int256(amount));
redemptionThrottle.useAvailable(supply, int256(amount)); // reverts on over-redemption
// {BU}
uint192 baskets = _scaleDown(_msgSender(), amount);
emit Redemption(_msgSender(), recipient, amount, baskets);
(address[] memory erc20s, uint256[] memory amounts) = basketHandler.quote(baskets, FLOOR);
// === Interactions ===
for (uint256 i = 0; i < erc20s.length; ++i) {
if (amounts[i] == 0) continue;
// Send withdrawal
IERC20Upgradeable(erc20s[i]).safeTransferFrom(
address(backingManager),
recipient,
amounts[i]
);
}
}
/// Redeem RToken for a linear combination of historical baskets, to a particular recipient
// checks:
// amount > 0
// amount <= balanceOf(caller)
// sum(portions) == FIX_ONE
// nonce >= basketHandler.primeNonce() for nonce in basketNonces
//
// effects:
// (so totalSupply -= amount and balanceOf(caller) -= amount)
// basketsNeeded' / totalSupply' >== basketsNeeded / totalSupply
// burn(caller, amount)
//
// actions:
// for each token in erc20s:
// let tokenAmt = (amount * basketsNeeded / totalSupply) custom baskets
// let prorataAmt = (amount / totalSupply) * token.balanceOf(backingManager)
// do token.transferFrom(backingManager, caller, min(tokenAmt, prorataAmt))
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
/// @dev Allows partial redemptions up to the minAmounts
/// @param recipient The address to receive the backing collateral tokens
/// @param amount {qRTok} The quantity {qRToken} of RToken to redeem
/// @param basketNonces An array of basket nonces to do redemption from
/// @param portions {1} An array of Fix quantities that must add up to FIX_ONE
/// @param expectedERC20sOut An array of ERC20s expected out
/// @param minAmounts {qTok} The minimum ERC20 quantities the caller should receive
/// @custom:interaction RCEI
function redeemCustom(
address recipient,
uint256 amount,
uint48[] memory basketNonces,
uint192[] memory portions,
address[] memory expectedERC20sOut,
uint256[] memory minAmounts
) external notFrozen returns (address[] memory erc20sOut, uint256[] memory amountsOut) {
// == Refresh ==
assetRegistry.refresh();
// solhint-disable-next-line no-empty-blocks
try main.furnace().melt() {} catch {}
// == Checks and Effects ==
require(amount > 0, "Cannot redeem zero");
require(amount <= balanceOf(_msgSender()), "insufficient balance");
uint256 portionsSum;
for (uint256 i = 0; i < portions.length; ++i) {
portionsSum += portions[i];
}
require(portionsSum == FIX_ONE, "portions do not add up to FIX_ONE");
uint256 supply = totalSupply();
// Revert if redemption exceeds either supply throttle
issuanceThrottle.useAvailable(supply, -int256(amount));
redemptionThrottle.useAvailable(supply, int256(amount)); // reverts on over-redemption
// {BU}
uint192 baskets = _scaleDown(_msgSender(), amount);
emit Redemption(_msgSender(), recipient, amount, baskets);
// === Get basket redemption amounts ===
(erc20sOut, amountsOut) = basketHandler.quoteCustomRedemption(
basketNonces,
portions,
baskets
);
// ==== Prorate redemption ====
// i.e, set amounts = min(amounts, balances * amount / totalSupply)
// where balances[i] = erc20sOut[i].balanceOf(backingManager)
// Bound each withdrawal by the prorata share, in case we're currently under-collateralized
for (uint256 i = 0; i < erc20sOut.length; ++i) {
// {qTok} = {qTok} * {qRTok} / {qRTok}
uint256 prorata = mulDiv256(
IERC20(erc20sOut[i]).balanceOf(address(backingManager)),
amount,
supply
); // FLOOR
if (prorata < amountsOut[i]) amountsOut[i] = prorata;
}
// === Save initial recipient balances ===
uint256[] memory pastBals = new uint256[](expectedERC20sOut.length);
for (uint256 i = 0; i < expectedERC20sOut.length; ++i) {
pastBals[i] = IERC20(expectedERC20sOut[i]).balanceOf(recipient);
// we haven't verified this ERC20 is registered but this is always a staticcall
}
// === Interactions ===
// Distribute tokens; revert if empty redemption
{
bool allZero = true;
for (uint256 i = 0; i < erc20sOut.length; ++i) {
if (amountsOut[i] == 0) continue; // unregistered ERC20s will have 0 amount
if (allZero) allZero = false;
// Send withdrawal
IERC20Upgradeable(erc20sOut[i]).safeTransferFrom(
address(backingManager),
recipient,
amountsOut[i]
);
}
if (allZero) revert("empty redemption");
}
// === Post-checks ===
// Check post-balances
for (uint256 i = 0; i < expectedERC20sOut.length; ++i) {
uint256 bal = IERC20(expectedERC20sOut[i]).balanceOf(recipient);
// we haven't verified this ERC20 is registered but this is always a staticcall
require(bal - pastBals[i] >= minAmounts[i], "redemption below minimum");
}
}
/// Mint an amount of RToken equivalent to baskets BUs, scaling basketsNeeded up
/// Callable only by BackingManager
/// @param baskets {BU} The number of baskets to mint RToken for
/// @custom:protected
// checks: caller is backingManager
// effects:
// bal'[recipient] = bal[recipient] + amtRToken
// totalSupply' = totalSupply + amtRToken
// basketsNeeded' = basketsNeeded + baskets
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
function mint(uint192 baskets) external {
require(_msgSender() == address(backingManager), "not backing manager");
_scaleUp(address(backingManager), baskets, totalSupply());
}
/// Melt a quantity of RToken from the caller's account, increasing the basket rate
/// @param amtRToken {qRTok} The amtRToken to be melted
/// @custom:protected
// checks: caller is furnace
// effects:
// bal'[caller] = bal[caller] - amtRToken
// totalSupply' = totalSupply - amtRToken
// BU exchange rate cannot decrease
// BU exchange rate CAN increase, but we already trust furnace to do this slowly
function melt(uint256 amtRToken) external {
require(_msgSender() == address(furnace), "furnace only");
_burn(_msgSender(), amtRToken);
emit Melted(amtRToken);
}
/// Burn an amount of RToken from caller's account and scale basketsNeeded down
/// Callable only by backingManager
/// @param amount {qRTok}
/// @custom:protected
// checks: caller is backingManager
// effects:
// bal'[recipient] = bal[recipient] - amtRToken
// totalSupply' = totalSupply - amtRToken
// basketsNeeded' = basketsNeeded - baskets
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
function dissolve(uint256 amount) external {
require(_msgSender() == address(backingManager), "not backing manager");
_scaleDown(_msgSender(), amount);
}
/// An affordance of last resort for Main in order to ensure re-capitalization
/// @custom:protected
// checks: caller is backingManager
// effects: basketsNeeded' = basketsNeeded_
function setBasketsNeeded(uint192 basketsNeeded_) external notTradingPausedOrFrozen {
require(_msgSender() == address(backingManager), "not backing manager");
emit BasketsNeededChanged(basketsNeeded, basketsNeeded_);
basketsNeeded = basketsNeeded_;
// == P0 exchangeRateIsValidAfter modifier ==
uint256 supply = totalSupply();
require(supply > 0, "0 supply");
// Note: These are D18s, even though they are uint256s. This is because
// we cannot assume we stay inside our valid range here, as that is what
// we are checking in the first place
uint256 low = (FIX_ONE_256 * basketsNeeded) / supply; // D18{BU/rTok}
uint256 high = (FIX_ONE_256 * basketsNeeded + (supply - 1)) / supply; // D18{BU/rTok}
// here we take advantage of an implicit upcast from uint192 exchange rates
require(low >= MIN_EXCHANGE_RATE && high <= MAX_EXCHANGE_RATE, "BU rate out of range");
}
/// Sends all token balance of erc20 (if it is registered) to the BackingManager
/// @custom:interaction
function monetizeDonations(IERC20 erc20) external notTradingPausedOrFrozen {
require(assetRegistry.isRegistered(erc20), "erc20 unregistered");
IERC20Upgradeable(address(erc20)).safeTransfer(
address(backingManager),
erc20.balanceOf(address(this))
);
}
// ==== Throttle setters/getters ====
/// @return {qRTok} The maximum issuance that can be performed in the current block
function issuanceAvailable() external view returns (uint256) {
return issuanceThrottle.currentlyAvailable(issuanceThrottle.hourlyLimit(totalSupply()));
}
/// @return available {qRTok} The maximum redemption that can be performed in the current block
function redemptionAvailable() external view returns (uint256 available) {
uint256 supply = totalSupply();
uint256 hourlyLimit = redemptionThrottle.hourlyLimit(supply);
available = redemptionThrottle.currentlyAvailable(hourlyLimit);
if (supply < available) available = supply;
}
/// @return The issuance throttle parametrization
function issuanceThrottleParams() external view returns (ThrottleLib.Params memory) {
return issuanceThrottle.params;
}
/// @return The redemption throttle parametrization
function redemptionThrottleParams() external view returns (ThrottleLib.Params memory) {
return redemptionThrottle.params;
}
/// @custom:governance
function setIssuanceThrottleParams(ThrottleLib.Params calldata params) public governance {
require(params.amtRate >= MIN_THROTTLE_RATE_AMT, "issuance amtRate too small");
require(params.amtRate <= MAX_THROTTLE_RATE_AMT, "issuance amtRate too big");
require(params.pctRate <= MAX_THROTTLE_PCT_AMT, "issuance pctRate too big");
emit IssuanceThrottleSet(issuanceThrottle.params, params);
issuanceThrottle.params = params;
}
/// @custom:governance
function setRedemptionThrottleParams(ThrottleLib.Params calldata params) public governance {
require(params.amtRate >= MIN_THROTTLE_RATE_AMT, "redemption amtRate too small");
require(params.amtRate <= MAX_THROTTLE_RATE_AMT, "redemption amtRate too big");
require(params.pctRate <= MAX_THROTTLE_PCT_AMT, "redemption pctRate too big");
emit RedemptionThrottleSet(redemptionThrottle.params, params);
redemptionThrottle.params = params;
}
// ==== Private ====
/// Mint an amount of RToken equivalent to amtBaskets and scale basketsNeeded up
/// @param recipient The address to receive the RTokens
/// @param amtBaskets {BU} The number of amtBaskets to mint RToken for
/// @param totalSupply {qRTok} The current totalSupply
// effects:
// bal'[recipient] = bal[recipient] + amtRToken
// totalSupply' = totalSupply + amtRToken
// basketsNeeded' = basketsNeeded + amtBaskets
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
function _scaleUp(
address recipient,
uint192 amtBaskets,
uint256 totalSupply
) private {
// take advantage of 18 decimals during casting
uint256 amtRToken = totalSupply > 0
? amtBaskets.muluDivu(totalSupply, basketsNeeded) // {rTok} = {BU} * {qRTok} * {qRTok}
: amtBaskets; // {rTok}
emit BasketsNeededChanged(basketsNeeded, basketsNeeded + amtBaskets);
basketsNeeded += amtBaskets;
// Mint RToken to recipient
_mint(recipient, amtRToken);
}
/// Burn an amount of RToken and scale basketsNeeded down
/// @param account The address to dissolve RTokens from
/// @param amtRToken {qRTok} The amount of RToken to be dissolved
/// @return amtBaskets {BU} The equivalent number of baskets dissolved
// effects:
// bal'[recipient] = bal[recipient] - amtRToken
// totalSupply' = totalSupply - amtRToken
// basketsNeeded' = basketsNeeded - amtBaskets
// BU exchange rate cannot decrease, and it can only increase when < FIX_ONE.
function _scaleDown(address account, uint256 amtRToken) private returns (uint192 amtBaskets) {
// D18{BU} = D18{BU} * {qRTok} / {qRTok}
amtBaskets = basketsNeeded.muluDivu(amtRToken, totalSupply()); // FLOOR
emit BasketsNeededChanged(basketsNeeded, basketsNeeded - amtBaskets);
basketsNeeded -= amtBaskets;
// Burn RToken from account; reverts if not enough balance
_burn(account, amtRToken);
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*/
function _beforeTokenTransfer(
address,
address to,
uint256
) internal virtual override {
require(to != address(this), "RToken transfer to self");
}
/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[42] private __gap;
}