@reserve-protocol/dtf-rebalance-lib 0.2.4 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/open-auction.js +17 -18
- package/package.json +1 -1
package/dist/open-auction.js
CHANGED
@@ -7,6 +7,7 @@ exports.getOpenAuction = exports.getTargetBasket = exports.AuctionRound = void 0
|
|
7
7
|
const decimal_js_light_1 = __importDefault(require("decimal.js-light"));
|
8
8
|
const numbers_1 = require("./numbers");
|
9
9
|
const types_1 = require("./types");
|
10
|
+
decimal_js_light_1.default.set({ precision: 100 });
|
10
11
|
// Call `getOpenAuction()` to get the current auction round
|
11
12
|
var AuctionRound;
|
12
13
|
(function (AuctionRound) {
|
@@ -33,7 +34,7 @@ const getTargetBasket = (_initialWeights, _prices, _decimals) => {
|
|
33
34
|
const price = new decimal_js_light_1.default(_prices[i]);
|
34
35
|
const decimalScale = new decimal_js_light_1.default(`1e${_decimals[i]}`);
|
35
36
|
// {USD/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27 * {USD/wholeTok}
|
36
|
-
return new decimal_js_light_1.default(initialWeight.spot.toString()).
|
37
|
+
return new decimal_js_light_1.default(initialWeight.spot.toString()).div(decimalScale).mul(price).div(numbers_1.D9d);
|
37
38
|
});
|
38
39
|
const totalValue = vals.reduce((a, b) => a.add(b));
|
39
40
|
// D18{1} = {USD/wholeBU} / {USD/wholeBU} * D18
|
@@ -95,9 +96,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
95
96
|
// {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
|
96
97
|
let weightRanges = rebalance.weights.map((range, i) => {
|
97
98
|
return {
|
98
|
-
low: new decimal_js_light_1.default(range.low.toString()).
|
99
|
-
spot: new decimal_js_light_1.default(range.spot.toString()).
|
100
|
-
high: new decimal_js_light_1.default(range.high.toString()).
|
99
|
+
low: new decimal_js_light_1.default(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
100
|
+
spot: new decimal_js_light_1.default(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
101
|
+
high: new decimal_js_light_1.default(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
101
102
|
};
|
102
103
|
});
|
103
104
|
const finalStageAt = new decimal_js_light_1.default(_finalStageAt.toString());
|
@@ -121,7 +122,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
121
122
|
return weightRange.spot.mul(prices[i]);
|
122
123
|
})
|
123
124
|
.reduce((a, b) => a.add(b));
|
124
|
-
const buPriceChange = buValue.sub(shareValue).
|
125
|
+
const buPriceChange = buValue.sub(shareValue).div(shareValue);
|
125
126
|
console.log(` 🧺 ${buPriceChange.mul(100).toFixed(2)}% basket price difference`);
|
126
127
|
if (debug) {
|
127
128
|
console.log("shareValue", shareValue.toString());
|
@@ -288,15 +289,13 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
288
289
|
const newWeightsD27 = {
|
289
290
|
low: (0, numbers_1.bn)(idealWeight
|
290
291
|
.mul(rebalanceTarget.div(actualLimits.low.div(actualLimits.spot))) // add remaining delta into weight
|
291
|
-
.mul(numbers_1.
|
292
|
-
.mul(decimalScale[i])
|
293
|
-
|
294
|
-
spot: (0, numbers_1.bn)(idealWeight.mul(numbers_1.D27d).mul(decimalScale[i]).div(numbers_1.D18d)),
|
292
|
+
.mul(numbers_1.D9d)
|
293
|
+
.mul(decimalScale[i])),
|
294
|
+
spot: (0, numbers_1.bn)(idealWeight.mul(numbers_1.D9d).mul(decimalScale[i])),
|
295
295
|
high: (0, numbers_1.bn)(idealWeight
|
296
296
|
.mul(numbers_1.ONE.add(delta).div(actualLimits.high.div(actualLimits.spot))) // add remaining delta into weight
|
297
|
-
.mul(numbers_1.
|
298
|
-
.mul(decimalScale[i])
|
299
|
-
.div(numbers_1.D18d)),
|
297
|
+
.mul(numbers_1.D9d)
|
298
|
+
.mul(decimalScale[i])),
|
300
299
|
};
|
301
300
|
// hold some surpluses aside if ejecting
|
302
301
|
if (round == AuctionRound.EJECT) {
|
@@ -335,7 +334,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
335
334
|
// D27{USD/tok}
|
336
335
|
const newPrices = rebalance.initialPrices.map((initialPrice, i) => {
|
337
336
|
// revert if price out of bounds
|
338
|
-
const spotPrice = (0, numbers_1.bn)(prices[i].
|
337
|
+
const spotPrice = (0, numbers_1.bn)(prices[i].div(decimalScale[i]).mul(numbers_1.D27d));
|
339
338
|
if (spotPrice < initialPrice.low || spotPrice > initialPrice.high) {
|
340
339
|
throw new Error(`spot price ${spotPrice.toString()} out of bounds relative to initial range [${initialPrice.low.toString()}, ${initialPrice.high.toString()}]! auction launcher MUST closeRebalance to prevent loss!`);
|
341
340
|
}
|
@@ -344,8 +343,8 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
344
343
|
}
|
345
344
|
// D27{USD/tok} = {USD/wholeTok} * D27 / {tok/wholeTok}
|
346
345
|
const pricesD27 = {
|
347
|
-
low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).
|
348
|
-
high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).
|
346
|
+
low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).div(decimalScale[i]).mul(numbers_1.D27d)),
|
347
|
+
high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).div(decimalScale[i]).mul(numbers_1.D27d)),
|
349
348
|
};
|
350
349
|
// low
|
351
350
|
if (pricesD27.low < initialPrice.low) {
|
@@ -377,9 +376,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
377
376
|
// {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
|
378
377
|
weightRanges = newWeights.map((range, i) => {
|
379
378
|
return {
|
380
|
-
low: new decimal_js_light_1.default(range.low.toString()).
|
381
|
-
spot: new decimal_js_light_1.default(range.spot.toString()).
|
382
|
-
high: new decimal_js_light_1.default(range.high.toString()).
|
379
|
+
low: new decimal_js_light_1.default(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
380
|
+
spot: new decimal_js_light_1.default(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
381
|
+
high: new decimal_js_light_1.default(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
383
382
|
};
|
384
383
|
});
|
385
384
|
// {USD}
|