@reserve-protocol/dtf-rebalance-lib 0.2.9 → 0.2.11
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 +27 -29
- package/dist/start-rebalance.js +7 -12
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +6 -8
- package/package.json +1 -1
package/dist/open-auction.js
CHANGED
@@ -1,14 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.getOpenAuction = exports.getTargetBasket = exports.AuctionRound = void 0;
|
7
|
-
const
|
4
|
+
const utils_1 = require("./utils");
|
8
5
|
const numbers_1 = require("./numbers");
|
9
6
|
const types_1 = require("./types");
|
10
|
-
// Create a local Decimal constructor with custom precision
|
11
|
-
const Decimal = decimal_js_light_1.default.clone({ precision: 100 });
|
12
7
|
// Call `getOpenAuction()` to get the current auction round
|
13
8
|
var AuctionRound;
|
14
9
|
(function (AuctionRound) {
|
@@ -32,10 +27,10 @@ const getTargetBasket = (_initialWeights, _prices, _decimals) => {
|
|
32
27
|
throw new Error("length mismatch");
|
33
28
|
}
|
34
29
|
const vals = _initialWeights.map((initialWeight, i) => {
|
35
|
-
const price = new Decimal(_prices[i]);
|
36
|
-
const decimalScale = new Decimal(`1e${_decimals[i]}`);
|
30
|
+
const price = new utils_1.Decimal(_prices[i]);
|
31
|
+
const decimalScale = new utils_1.Decimal(`1e${_decimals[i]}`);
|
37
32
|
// {USD/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27 * {USD/wholeTok}
|
38
|
-
return new Decimal(initialWeight.spot.toString()).div(decimalScale).mul(price).div(numbers_1.D9d);
|
33
|
+
return new utils_1.Decimal(initialWeight.spot.toString()).div(decimalScale).mul(price).div(numbers_1.D9d);
|
39
34
|
});
|
40
35
|
const totalValue = vals.reduce((a, b) => a.add(b));
|
41
36
|
// D18{1} = {USD/wholeBU} / {USD/wholeBU} * D18
|
@@ -76,33 +71,33 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
76
71
|
}
|
77
72
|
// ================================================================
|
78
73
|
// {wholeShare} = {share} / {share/wholeShare}
|
79
|
-
const supply = new Decimal(_supply.toString()).div(numbers_1.D18d);
|
74
|
+
const supply = new utils_1.Decimal(_supply.toString()).div(numbers_1.D18d);
|
80
75
|
// {1} = D18{1} / D18
|
81
|
-
const targetBasket = _targetBasket.map((a) => new Decimal(a.toString()).div(numbers_1.D18d));
|
76
|
+
const targetBasket = _targetBasket.map((a) => new utils_1.Decimal(a.toString()).div(numbers_1.D18d));
|
82
77
|
// {USD/wholeTok}
|
83
|
-
const prices = _prices.map((a) => new Decimal(a));
|
78
|
+
const prices = _prices.map((a) => new utils_1.Decimal(a));
|
84
79
|
for (let i = 0; i < prices.length; i++) {
|
85
80
|
if (prices[i].eq(numbers_1.ZERO)) {
|
86
81
|
throw new Error(`missing price for token ${rebalance.tokens[i]}`);
|
87
82
|
}
|
88
83
|
}
|
89
84
|
// {1}
|
90
|
-
const priceError = _priceError.map((a) => new Decimal(a.toString()));
|
85
|
+
const priceError = _priceError.map((a) => new utils_1.Decimal(a.toString()));
|
91
86
|
// {tok/wholeTok}
|
92
|
-
const decimalScale = _decimals.map((a) => new Decimal(`1e${a}`));
|
87
|
+
const decimalScale = _decimals.map((a) => new utils_1.Decimal(`1e${a}`));
|
93
88
|
// {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
|
94
|
-
const initialFolio = _initialFolio.map((c, i) => new Decimal(c.toString()).div(decimalScale[i]));
|
89
|
+
const initialFolio = _initialFolio.map((c, i) => new utils_1.Decimal(c.toString()).div(decimalScale[i]));
|
95
90
|
// {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
|
96
|
-
const folio = _folio.map((c, i) => new Decimal(c.toString()).div(decimalScale[i]));
|
91
|
+
const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(decimalScale[i]));
|
97
92
|
// {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
|
98
93
|
let weightRanges = rebalance.weights.map((range, i) => {
|
99
94
|
return {
|
100
|
-
low: new Decimal(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
101
|
-
spot: new Decimal(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
102
|
-
high: new Decimal(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
95
|
+
low: new utils_1.Decimal(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
96
|
+
spot: new utils_1.Decimal(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
97
|
+
high: new utils_1.Decimal(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
103
98
|
};
|
104
99
|
});
|
105
|
-
const finalStageAt = new Decimal(_finalStageAt.toString());
|
100
|
+
const finalStageAt = new utils_1.Decimal(_finalStageAt.toString());
|
106
101
|
// ================================================================
|
107
102
|
// calculate ideal spot limit, the actual BU<->share ratio
|
108
103
|
// {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
|
@@ -150,7 +145,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
150
145
|
// ================================================================
|
151
146
|
// calculate progressions
|
152
147
|
// {wholeBU/wholeShare} = D18{BU/share} / D18
|
153
|
-
const prevSpotLimit = new Decimal(rebalance.limits.spot.toString()).div(numbers_1.D18d);
|
148
|
+
const prevSpotLimit = new utils_1.Decimal(rebalance.limits.spot.toString()).div(numbers_1.D18d);
|
154
149
|
// {wholeTok/wholeBU} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
|
155
150
|
const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(prevSpotLimit));
|
156
151
|
// {1} = {USD/wholeShare} / {USD/wholeShare}
|
@@ -181,6 +176,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
181
176
|
.reduce((a, b) => a.add(b))
|
182
177
|
.div(shareValue);
|
183
178
|
if (progression < initialProgression) {
|
179
|
+
if (debug) {
|
180
|
+
console.log("progression < initialProgression", progression.toString(), initialProgression.toString());
|
181
|
+
}
|
184
182
|
progression = initialProgression; // don't go backwards
|
185
183
|
}
|
186
184
|
// {1} = {1} / {1}
|
@@ -275,9 +273,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
275
273
|
// get new weights, constrained by extremes
|
276
274
|
// {wholeBU/wholeShare} = D18{BU/share} / D18
|
277
275
|
const actualLimits = {
|
278
|
-
low: new Decimal(newLimits.low.toString()).div(numbers_1.D18d),
|
279
|
-
spot: new Decimal(newLimits.spot.toString()).div(numbers_1.D18d),
|
280
|
-
high: new Decimal(newLimits.high.toString()).div(numbers_1.D18d),
|
276
|
+
low: new utils_1.Decimal(newLimits.low.toString()).div(numbers_1.D18d),
|
277
|
+
spot: new utils_1.Decimal(newLimits.spot.toString()).div(numbers_1.D18d),
|
278
|
+
high: new utils_1.Decimal(newLimits.high.toString()).div(numbers_1.D18d),
|
281
279
|
};
|
282
280
|
// D27{tok/BU}
|
283
281
|
const newWeights = rebalance.weights.map((weightRange, i) => {
|
@@ -369,9 +367,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
369
367
|
// {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
|
370
368
|
weightRanges = newWeights.map((range, i) => {
|
371
369
|
return {
|
372
|
-
low: new Decimal(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
373
|
-
spot: new Decimal(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
374
|
-
high: new Decimal(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
370
|
+
low: new utils_1.Decimal(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
371
|
+
spot: new utils_1.Decimal(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
372
|
+
high: new utils_1.Decimal(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
|
375
373
|
};
|
376
374
|
});
|
377
375
|
// {USD}
|
@@ -386,10 +384,10 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
386
384
|
const sellDownTo = weightRanges[i].high.mul(actualLimits.high);
|
387
385
|
if (folio[i].lt(buyUpTo)) {
|
388
386
|
// {USD} = {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
|
389
|
-
const tokenDeficitValue =
|
387
|
+
const tokenDeficitValue = buyUpTo.sub(folio[i]).mul(prices[i]).mul(supply);
|
390
388
|
// $1 minimum
|
391
389
|
if (tokenDeficitValue.gte(numbers_1.ONE)) {
|
392
|
-
deficitValue = tokenDeficitValue;
|
390
|
+
deficitValue = deficitValue.add(tokenDeficitValue);
|
393
391
|
deficitTokens.push(token);
|
394
392
|
}
|
395
393
|
}
|
package/dist/start-rebalance.js
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.getStartRebalance = void 0;
|
7
|
-
const
|
4
|
+
const utils_1 = require("./utils");
|
8
5
|
const numbers_1 = require("./numbers");
|
9
|
-
// Create a local Decimal constructor with custom precision
|
10
|
-
const Decimal = decimal_js_light_1.default.clone({ precision: 100 });
|
11
6
|
/**
|
12
7
|
* Get the arguments needed to call startRebalance
|
13
8
|
*
|
@@ -28,19 +23,19 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
|
|
28
23
|
console.log("getStartRebalance", _supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl);
|
29
24
|
}
|
30
25
|
// {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
|
31
|
-
const folio = _folio.map((c, i) => new Decimal(c.toString()).div(new Decimal(`1e${decimals[i]}`)));
|
26
|
+
const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(new utils_1.Decimal(`1e${decimals[i]}`)));
|
32
27
|
// convert price number inputs to bigints
|
33
28
|
// {USD/wholeTok}
|
34
|
-
const prices = _prices.map((a) => new Decimal(a.toString()));
|
29
|
+
const prices = _prices.map((a) => new utils_1.Decimal(a.toString()));
|
35
30
|
for (let i = 0; i < prices.length; i++) {
|
36
31
|
if (prices[i].eq(numbers_1.ZERO)) {
|
37
32
|
throw new Error(`missing price for token ${tokens[i]}`);
|
38
33
|
}
|
39
34
|
}
|
40
35
|
// {1} = D18{1} / D18
|
41
|
-
const targetBasket = _targetBasket.map((a) => new Decimal(a.toString()).div(numbers_1.D18d));
|
36
|
+
const targetBasket = _targetBasket.map((a) => new utils_1.Decimal(a.toString()).div(numbers_1.D18d));
|
42
37
|
// {1}
|
43
|
-
const priceError = _priceError.map((a) => new Decimal(a.toString()));
|
38
|
+
const priceError = _priceError.map((a) => new utils_1.Decimal(a.toString()));
|
44
39
|
// ================================================================
|
45
40
|
const newWeights = [];
|
46
41
|
const newPrices = [];
|
@@ -62,7 +57,7 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
|
|
62
57
|
// {wholeTok/wholeShare} = {1} * {USD/wholeShare} / {USD/wholeTok}
|
63
58
|
const spotWeight = targetBasket[i].mul(dtfPrice).div(prices[i]);
|
64
59
|
// D27{tok/share}{wholeShare/wholeTok} = D27 * {tok/wholeTok} / {share/wholeShare}
|
65
|
-
const limitMultiplier = numbers_1.D27d.mul(new Decimal(`1e${decimals[i]}`)).div(numbers_1.D18d);
|
60
|
+
const limitMultiplier = numbers_1.D27d.mul(new utils_1.Decimal(`1e${decimals[i]}`)).div(numbers_1.D18d);
|
66
61
|
if (debug) {
|
67
62
|
console.log("limitMultiplier", limitMultiplier.toString());
|
68
63
|
}
|
@@ -88,7 +83,7 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
|
|
88
83
|
}
|
89
84
|
// === newPrices ===
|
90
85
|
// D27{wholeTok/tok} = D27 / {tok/wholeTok}
|
91
|
-
const priceMultiplier = numbers_1.D27d.div(new Decimal(`1e${decimals[i]}`));
|
86
|
+
const priceMultiplier = numbers_1.D27d.div(new utils_1.Decimal(`1e${decimals[i]}`));
|
92
87
|
// {USD/wholeTok} = {USD/wholeTok} * {1}
|
93
88
|
const lowPrice = prices[i].mul(numbers_1.ONE.sub(priceError[i]));
|
94
89
|
const highPrice = prices[i].mul(numbers_1.ONE.add(priceError[i]));
|
package/dist/utils.d.ts
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
import DecimalLight from "decimal.js-light";
|
2
|
+
export declare const Decimal: typeof DecimalLight;
|
1
3
|
/**
|
2
4
|
* This function can be used to get a basket distribution EITHER from a set of historical basket weights
|
3
5
|
* or from a set of current balances. Make sure to use prices from the right time.
|
package/dist/utils.js
CHANGED
@@ -3,11 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.getBasketDistribution = void 0;
|
6
|
+
exports.getBasketDistribution = exports.Decimal = void 0;
|
7
7
|
const decimal_js_light_1 = __importDefault(require("decimal.js-light"));
|
8
8
|
const numbers_1 = require("./numbers");
|
9
9
|
// Create a local Decimal constructor with custom precision
|
10
|
-
|
10
|
+
exports.Decimal = decimal_js_light_1.default.clone({ precision: 100 });
|
11
11
|
/**
|
12
12
|
* This function can be used to get a basket distribution EITHER from a set of historical basket weights
|
13
13
|
* or from a set of current balances. Make sure to use prices from the right time.
|
@@ -18,15 +18,13 @@ const Decimal = decimal_js_light_1.default.clone({ precision: 100 });
|
|
18
18
|
* @returns D18{1} Current basket, total will be around 1e18 but not exactly
|
19
19
|
*/
|
20
20
|
const getBasketDistribution = (_bals, _prices, decimals) => {
|
21
|
-
const decimalScale = decimals.map((d) => new Decimal(`1e${d}`));
|
21
|
+
const decimalScale = decimals.map((d) => new exports.Decimal(`1e${d}`));
|
22
22
|
// {wholeTok} = {tok} / {tok/wholeTok}
|
23
|
-
const bals = _bals.map((bal, i) => new Decimal(bal.toString()).div(decimalScale[i]));
|
23
|
+
const bals = _bals.map((bal, i) => new exports.Decimal(bal.toString()).div(decimalScale[i]));
|
24
24
|
// {USD/wholeTok} = {USD/wholeTok}
|
25
|
-
const prices = _prices.map((a) => new Decimal(a.toString()));
|
25
|
+
const prices = _prices.map((a) => new exports.Decimal(a.toString()));
|
26
26
|
// {USD} = {wholeTok} * {USD/wholeTok}
|
27
|
-
const totalValue = bals
|
28
|
-
.map((bal, i) => bal.mul(prices[i]))
|
29
|
-
.reduce((a, b) => a.add(b));
|
27
|
+
const totalValue = bals.map((bal, i) => bal.mul(prices[i])).reduce((a, b) => a.add(b));
|
30
28
|
// D18{1} = {wholeTok} * {USD/wholeTok} / {USD}
|
31
29
|
return bals.map((bal, i) => (0, numbers_1.bn)(bal.mul(prices[i]).div(totalValue).mul(numbers_1.D18d)));
|
32
30
|
};
|