-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMarketCapSqrtController.sol
555 lines (496 loc) · 17.3 KB
/
MarketCapSqrtController.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
import { IPool } from "./balancer/IPool.sol";
import { PriceLibrary as Prices } from "./lib/PriceLibrary.sol";
import "./lib/FixedPoint.sol";
import "./lib/Babylonian.sol";
import { MCapSqrtLibrary as MCapSqrt } from "./lib/MCapSqrtLibrary.sol";
import { PoolFactory } from "./PoolFactory.sol";
import { PoolInitializer } from "./PoolInitializer.sol";
import { UnboundTokenSeller } from "./UnboundTokenSeller.sol";
import { DelegateCallProxyManager } from "./proxies/DelegateCallProxyManager.sol";
import { SaltyLib as Salty } from "./proxies/SaltyLib.sol";
import {
MarketCapSortedTokenCategories,
UniSwapV2PriceOracle
} from "./MarketCapSortedTokenCategories.sol";
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/**
* @title MarketCapSqrtController
* @author d1ll0n
* @dev This contract implements the market cap square root index management strategy.
*
* Index pools have a defined size which is used to select the top tokens from the pool's
* category.
*
* REBALANCING
* ===============
* Every 2 weeks, pools are either re-weighed or re-indexed.
* They are re-indexed once for every three re-weighs.
*
* Re-indexing involves selecting the top tokens from the pool's category and weighing them
* by the square root of their market caps.
* Re-weighing involves weighing the tokens which are already indexed by the pool by the
* square root of their market caps.
* When a pool is re-weighed, only the tokens with a desired weight above 0 are included.
* ===============
*/
contract MarketCapSqrtController is MarketCapSortedTokenCategories {
using FixedPoint for FixedPoint.uq112x112;
using FixedPoint for FixedPoint.uq144x112;
using Babylonian for uint144;
using SafeMath for uint256;
using Prices for Prices.TwoWayAveragePrice;
/* --- Constants --- */
// Minimum number of tokens in an index.
uint256 internal constant MIN_INDEX_SIZE = 2;
// Maximum number of tokens in an index.
uint256 internal constant MAX_INDEX_SIZE = 8;
// Identifier for the pool initializer implementation on the proxy manager.
bytes32 internal constant INITIALIZER_IMPLEMENTATION_ID = keccak256("PoolInitializer.sol");
// Identifier for the unbound token seller implementation on the proxy manager.
bytes32 internal constant SELLER_IMPLEMENTATION_ID = keccak256("UnboundTokenSeller.sol");
// Identifier for the index pool implementation on the proxy manager.
bytes32 internal constant POOL_IMPLEMENTATION_ID = keccak256("IPool.sol");
// Default total weight for a pool.
uint256 internal constant WEIGHT_MULTIPLIER = 25e18;
// Time between reweigh/reindex calls.
uint256 internal constant POOL_REWEIGH_DELAY = 2 weeks;
// The number of reweighs which occur before a pool is re-indexed.
uint256 internal constant REWEIGHS_BEFORE_REINDEX = 3;
// Pool factory contract
PoolFactory internal immutable _factory;
// Proxy manager & factory
DelegateCallProxyManager internal immutable _proxyManager;
/* --- Events --- */
/** @dev Emitted when a pool is initialized and made public. */
event PoolInitialized(
address pool,
address unboundTokenSeller,
uint256 categoryID,
uint256 indexSize
);
/** @dev Emitted when a pool and its initializer are deployed. */
event NewPoolInitializer(
address pool,
address initializer,
uint256 categoryID,
uint256 indexSize
);
/** @dev Emitted when a pool using the default implementation is deployed. */
event NewDefaultPool(
address pool,
address controller
);
/** @dev Emitted when a pool using a non-default implementation is deployed. */
event NewNonDefaultPool(
address pool,
address controller,
bytes32 implementationID
);
/* --- Structs --- */
struct IndexPoolMeta {
uint16 categoryID;
uint8 indexSize;
bool initialized;
}
/**
* @dev Data structure with the number of times a pool has been
* either reweighed or re-indexed, as well as the timestamp of
* the last such action.
*
* If `++index % REWEIGHS_BEFORE_REINDEX + 1` is 0, the pool will
* re-index, otherwise it will reweigh.
*
* @param index Number of times the pool has either re-weighed or
* re-indexed.
* @param timestamp Timestamp of last pool re-weigh or re-index.
*/
struct PoolUpdateRecord {
uint128 index;
uint128 timestamp;
}
/* --- Storage --- */
// Default slippage rate for token seller contracts.
uint8 public defaultSellerPremium = 2;
// Metadata about index pools
mapping(address => IndexPoolMeta) internal _poolMeta;
// Records of pool update statuses.
mapping(address => PoolUpdateRecord) internal _poolUpdateRecords;
/* --- Constructor --- */
/**
* @dev Deploy the controller and configure the addresses
* of the related contracts.
*/
constructor(
UniSwapV2PriceOracle oracle,
address owner,
PoolFactory factory,
DelegateCallProxyManager proxyManager
)
public
MarketCapSortedTokenCategories(oracle, owner)
{
_factory = factory;
_proxyManager = proxyManager;
}
/* --- Pool Deployment --- */
/**
* @dev Deploys an index pool and a pool initializer.
* The initializer contract is a pool with specific token
* balance targets which gives pool tokens in the finished
* pool to users who provide the underlying tokens needed
* to initialize it.
*/
function prepareIndexPool(
uint256 categoryID,
uint256 indexSize,
uint256 initialWethValue,
string calldata name,
string calldata symbol
)
external
_owner_
returns (address poolAddress, address initializerAddress)
{
require(indexSize >= MIN_INDEX_SIZE, "ERR_MIN_INDEX_SIZE");
require(indexSize <= MAX_INDEX_SIZE, "ERR_MAX_INDEX_SIZE");
require(initialWethValue < uint144(-1), "ERR_MAX_UINT144");
poolAddress = _factory.deployIndexPool(
keccak256(abi.encodePacked(categoryID, indexSize)),
name,
symbol
);
_poolMeta[poolAddress] = IndexPoolMeta({
categoryID: uint8(categoryID),
indexSize: uint8(indexSize),
initialized: false
});
initializerAddress = _proxyManager.deployProxyManyToOne(
INITIALIZER_IMPLEMENTATION_ID,
keccak256(abi.encodePacked(poolAddress))
);
PoolInitializer initializer = PoolInitializer(initializerAddress);
// Get the initial tokens and balances for the pool.
(
address[] memory tokens,
uint256[] memory balances
) = getInitialTokensAndBalances(categoryID, indexSize, uint144(initialWethValue));
initializer.initialize(poolAddress, tokens, balances);
emit NewPoolInitializer(
poolAddress,
initializerAddress,
categoryID,
indexSize
);
}
/**
* @dev Initializes a pool which has been deployed but not initialized
* and transfers the underlying tokens from the initialization pool to
* the actual pool.
*/
function finishPreparedIndexPool(
address poolAddress,
address[] calldata tokens,
uint256[] calldata balances
) external {
require(
msg.sender == computeInitializerAddress(poolAddress),
"ERR_NOT_PRE_DEPLOY_POOL"
);
uint256 len = tokens.length;
require(balances.length == len, "ERR_ARR_LEN");
IndexPoolMeta memory meta = _poolMeta[poolAddress];
require(!meta.initialized, "ERR_INITIALIZED");
uint96[] memory denormalizedWeights = new uint96[](len);
uint256 valueSum;
uint144[] memory ethValues = oracle.computeAverageAmountsOut(
tokens, balances
);
for (uint256 i = 0; i < len; i++) {
valueSum = valueSum.add(ethValues[i]);
}
for (uint256 i = 0; i < len; i++) {
denormalizedWeights[i] = _denormalizeFractionalWeight(
FixedPoint.fraction(uint112(ethValues[i]), uint112(valueSum))
);
}
address sellerAddress = _proxyManager.deployProxyManyToOne(
SELLER_IMPLEMENTATION_ID,
keccak256(abi.encodePacked(poolAddress))
);
IPool(poolAddress).initialize(
tokens,
balances,
denormalizedWeights,
msg.sender,
sellerAddress
);
_poolMeta[poolAddress].initialized = true;
emit PoolInitialized(
poolAddress,
sellerAddress,
meta.categoryID,
meta.indexSize
);
UnboundTokenSeller(sellerAddress).initialize(
IPool(poolAddress),
defaultSellerPremium
);
}
/* --- Pool Management --- */
/**
* @dev Sets the default premium rate for token seller contracts.
*/
function setDefaultSellerPremium(
uint8 _defaultSellerPremium
) external _owner_ {
require(
_defaultSellerPremium > 0 && _defaultSellerPremium < 20,
"ERR_PREMIUM"
);
defaultSellerPremium = _defaultSellerPremium;
}
/**
* @dev Update the premium rate on `sellerAddress` with the current
* default rate.
*/
function updateSellerPremiumToDefault(
address sellerAddress
) external {
UnboundTokenSeller(sellerAddress).setPremiumPercent(defaultSellerPremium);
}
/**
* @dev Update the premium rate on each unbound token seller in
* `sellerAddresses` with the current default rate.
*/
function updateSellerPremiumToDefault(
address[] calldata sellerAddresses
) external {
for (uint256 i = 0; i < sellerAddresses.length; i++) {
UnboundTokenSeller(
sellerAddresses[i]
).setPremiumPercent(defaultSellerPremium);
}
}
/**
* @dev Sets the maximum number of pool tokens that can be minted
* for a particular pool.
*
* This value will be used in the alpha to limit the maximum damage
* that can be caused by a catastrophic error. It can be gradually
* increased as the pool continues to not be exploited.
*
* If it is set to 0, the limit will be removed.
*
* @param poolAddress Address of the pool to set the limit on.
* @param maxPoolTokens Maximum LP tokens the pool can mint.
*/
function setMaxPoolTokens(
address poolAddress,
uint256 maxPoolTokens
) external _owner_ {
IPool(poolAddress).setMaxPoolTokens(maxPoolTokens);
}
/**
* @dev Sets the swap fee on an index pool.
*/
function setSwapFee(address poolAddress, uint256 swapFee) external _owner_ {
require(_havePool(poolAddress), "ERR_POOL_NOT_FOUND");
IPool(poolAddress).setSwapFee(swapFee);
}
/**
* @dev Updates the minimum balance of an uninitialized token, which is
* useful when the token's price on the pool is too low relative to
* external prices for people to trade it in.
*/
function updateMinimumBalance(IPool pool, address tokenAddress) external {
require(_havePool(address(pool)), "ERR_POOL_NOT_FOUND");
IPool.Record memory record = pool.getTokenRecord(tokenAddress);
require(!record.ready, "ERR_TOKEN_READY");
uint256 poolValue = _estimatePoolValue(pool);
Prices.TwoWayAveragePrice memory price = oracle.computeTwoWayAveragePrice(tokenAddress);
uint256 minimumBalance = price.computeAverageTokensForEth(poolValue) / 100;
pool.setMinimumBalance(tokenAddress, minimumBalance);
}
/* --- Pool Rebalance Actions --- */
/**
* @dev Re-indexes a pool by setting the underlying assets to the top
* tokens in its category by market cap.
*/
function reindexPool(address poolAddress) external {
IndexPoolMeta memory meta = _poolMeta[poolAddress];
require(meta.initialized, "ERR_POOL_NOT_FOUND");
PoolUpdateRecord memory record = _poolUpdateRecords[poolAddress];
require(
now - record.timestamp >= POOL_REWEIGH_DELAY,
"ERR_POOL_REWEIGH_DELAY"
);
require(
(++record.index % (REWEIGHS_BEFORE_REINDEX + 1)) == 0,
"ERR_REWEIGH_INDEX"
);
uint256 size = meta.indexSize;
address[] memory tokens = getTopCategoryTokens(meta.categoryID, size);
Prices.TwoWayAveragePrice[] memory prices = oracle.computeTwoWayAveragePrices(tokens);
FixedPoint.uq112x112[] memory weights = MCapSqrt.computeTokenWeights(tokens, prices);
uint256[] memory minimumBalances = new uint256[](size);
uint96[] memory denormalizedWeights = new uint96[](size);
uint144 totalValue = _estimatePoolValue(IPool(poolAddress));
for (uint256 i = 0; i < size; i++) {
// The minimum balance is the number of tokens worth the minimum weight
// of the pool. The minimum weight is 1/100, so we divide the total value
// by 100 to get the desired weth value, then multiply by the price of eth
// in terms of that token to get the minimum balance.
minimumBalances[i] = prices[i].computeAverageTokensForEth(totalValue) / 100;
denormalizedWeights[i] = _denormalizeFractionalWeight(weights[i]);
}
IPool(poolAddress).reindexTokens(
tokens,
denormalizedWeights,
minimumBalances
);
record.timestamp = uint128(now);
_poolUpdateRecords[poolAddress] = record;
}
/**
* @dev Reweighs the assets in a pool by market cap and sets the
* desired new weights, which will be adjusted over time.
*/
function reweighPool(address poolAddress) external {
require(_havePool(poolAddress), "ERR_POOL_NOT_FOUND");
PoolUpdateRecord memory record = _poolUpdateRecords[poolAddress];
require(
now - record.timestamp >= POOL_REWEIGH_DELAY,
"ERR_POOL_REWEIGH_DELAY"
);
require(
(++record.index % (REWEIGHS_BEFORE_REINDEX + 1)) != 0,
"ERR_REWEIGH_INDEX"
);
address[] memory tokens = IPool(poolAddress).getCurrentDesiredTokens();
Prices.TwoWayAveragePrice[] memory prices = oracle.computeTwoWayAveragePrices(tokens);
FixedPoint.uq112x112[] memory weights = MCapSqrt.computeTokenWeights(tokens, prices);
uint96[] memory denormalizedWeights = new uint96[](tokens.length);
for (uint256 i = 0; i < tokens.length; i++) {
denormalizedWeights[i] = _denormalizeFractionalWeight(weights[i]);
}
IPool(poolAddress).reweighTokens(tokens, denormalizedWeights);
record.timestamp = uint128(now);
_poolUpdateRecords[poolAddress] = record;
}
/* --- Pool Queries --- */
/**
* @dev Compute the create2 address for a pool initializer.
*/
function computeInitializerAddress(address poolAddress)
public
view
returns (address initializerAddress)
{
initializerAddress = Salty.computeProxyAddressManyToOne(
address(_proxyManager),
address(this),
INITIALIZER_IMPLEMENTATION_ID,
keccak256(abi.encodePacked(poolAddress))
);
}
/**
* @dev Compute the create2 address for a pool's unbound token seller.
*/
function computeSellerAddress(address poolAddress)
public
view
returns (address sellerAddress)
{
sellerAddress = Salty.computeProxyAddressManyToOne(
address(_proxyManager),
address(this),
SELLER_IMPLEMENTATION_ID,
keccak256(abi.encodePacked(poolAddress))
);
}
/**
* @dev Compute the create2 address for a pool.
*/
function computePoolAddress(uint256 categoryID, uint256 indexSize)
public
view
returns (address poolAddress)
{
poolAddress = Salty.computeProxyAddressManyToOne(
address(_proxyManager),
address(_factory),
POOL_IMPLEMENTATION_ID,
keccak256(abi.encodePacked(
address(this),
keccak256(abi.encodePacked(categoryID, indexSize))
))
);
}
/**
* @dev Queries the top `indexSize` tokens in a category from the market oracle,
* computes their relative weights by market cap square root and determines
* the weighted balance of each token to meet a specified total value.
*/
function getInitialTokensAndBalances(
uint256 categoryID,
uint256 indexSize,
uint144 wethValue
)
public
view
returns (
address[] memory tokens,
uint256[] memory balances
)
{
tokens = getTopCategoryTokens(categoryID, indexSize);
Prices.TwoWayAveragePrice[] memory prices = oracle.computeTwoWayAveragePrices(tokens);
FixedPoint.uq112x112[] memory weights = MCapSqrt.computeTokenWeights(tokens, prices);
balances = new uint256[](indexSize);
for (uint256 i = 0; i < indexSize; i++) {
balances[i] = MCapSqrt.computeWeightedBalance(wethValue, weights[i], prices[i]);
}
}
/* --- Internal Pool Utility Functions --- */
function _havePool(address pool) internal view returns (bool) {
return _poolMeta[pool].initialized;
}
/**
* @dev Estimate the total value of a pool by taking its first token's
* "virtual balance" (balance * (totalWeight/weight)) and multiplying
* by that token's average ether price from UniSwap.
*/
function _estimatePoolValue(IPool pool) internal view returns (uint144) {
(address token, uint256 value) = pool.extrapolatePoolValueFromToken();
FixedPoint.uq112x112 memory price = oracle.computeAverageTokenPrice(token);
return price.mul(value).decode144();
}
/* --- General Utility Functions --- */
/**
* @dev Converts a fixed point fraction to a denormalized weight.
* Multiply the fraction by the max weight and decode to an unsigned integer.
*/
function _denormalizeFractionalWeight(FixedPoint.uq112x112 memory fraction)
internal
pure
returns (uint96)
{
return uint96(fraction.mul(WEIGHT_MULTIPLIER).decode144());
}
/**
* @dev Re-assigns a uint128 array to a uint256 array.
* This does not affect memory allocation as all Solidity
* uint arrays take 32 bytes per item.
*/
function _to256Array(uint128[] memory arr)
internal
pure
returns (uint256[] memory outArr)
{
assembly { outArr := arr }
}
}