@reserve-protocol/dtf-rebalance-lib 2.1.3 → 2.2.1
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/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/open-auction.js +40 -25
- package/dist/types.d.ts +7 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +3 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,6 +16,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./types"), exports);
|
|
18
18
|
__exportStar(require("./numbers"), exports);
|
|
19
|
-
__exportStar(require("./utils"), exports);
|
|
20
19
|
__exportStar(require("./open-auction"), exports);
|
|
21
20
|
__exportStar(require("./start-rebalance"), exports);
|
|
21
|
+
__exportStar(require("./utils"), exports);
|
package/dist/open-auction.js
CHANGED
|
@@ -63,9 +63,6 @@ exports.getTargetBasket = getTargetBasket;
|
|
|
63
63
|
* @param _finalStageAt {1} The % rebalanced from the initial Folio to determine when is the final stage of the rebalance
|
|
64
64
|
*/
|
|
65
65
|
const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [], _targetBasket = [], _assets, _decimals, _prices, _priceError, _finalStageAt, debug) => {
|
|
66
|
-
if (debug === undefined) {
|
|
67
|
-
debug = true;
|
|
68
|
-
}
|
|
69
66
|
if (debug) {
|
|
70
67
|
console.log("getOpenAuction", rebalance, _supply, _initialSupply, _initialAssets, _targetBasket, _assets, _decimals, _prices, _priceError, _finalStageAt);
|
|
71
68
|
}
|
|
@@ -154,6 +151,16 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
|
|
|
154
151
|
})
|
|
155
152
|
.reduce((a, b) => a.add(b), numbers_1.ZERO)
|
|
156
153
|
.div(shareValue);
|
|
154
|
+
if (debug && ejectionIndices.length > 0) {
|
|
155
|
+
console.log(" Ejection calculation:");
|
|
156
|
+
console.log(" ejectionIndices:", ejectionIndices);
|
|
157
|
+
ejectionIndices.forEach(i => {
|
|
158
|
+
console.log(` Token ${i}: assets=${_assets[i].toString()}, decimals=${_decimals[i].toString()}`);
|
|
159
|
+
console.log(` Token ${i}: folio=${folio[i].toString()}, price=${prices[i].toString()}, value=${folio[i].mul(prices[i]).toString()}`);
|
|
160
|
+
});
|
|
161
|
+
console.log(" shareValue:", shareValue.toString());
|
|
162
|
+
console.log(" Total folio value:", folio.map((f, i) => f.mul(prices[i])).reduce((a, b) => a.add(b)).toString());
|
|
163
|
+
}
|
|
157
164
|
// ================================================================
|
|
158
165
|
// calculate progressions
|
|
159
166
|
// {wholeBU/wholeShare} = {USD/wholeShare} / {USD/wholeBU}
|
|
@@ -163,7 +170,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
|
|
|
163
170
|
const maxSpotLimit = spotLimit.gt(prevSpotLimit) ? spotLimit : prevSpotLimit;
|
|
164
171
|
// {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
|
|
165
172
|
const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(maxSpotLimit));
|
|
166
|
-
//
|
|
173
|
+
// absoluteProgression
|
|
167
174
|
// {1} = {USD/wholeShare} / {USD/wholeShare}
|
|
168
175
|
let progression = folio
|
|
169
176
|
.map((actualBalance, i) => {
|
|
@@ -201,7 +208,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
|
|
|
201
208
|
let relativeProgression = initialProgression.eq(numbers_1.ONE)
|
|
202
209
|
? numbers_1.ONE
|
|
203
210
|
: progression.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
|
|
204
|
-
let
|
|
211
|
+
let target = numbers_1.ONE; // absolute
|
|
205
212
|
let round = AuctionRound.FINAL;
|
|
206
213
|
if (debug) {
|
|
207
214
|
console.log("initialProgression", initialProgression.toString());
|
|
@@ -213,40 +220,41 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
|
|
|
213
220
|
// approach finalStageAt first
|
|
214
221
|
if (progression.lt(0.99) && relativeProgression.lt(finalStageAt.sub(0.02))) {
|
|
215
222
|
round = AuctionRound.PROGRESS;
|
|
216
|
-
|
|
217
|
-
if (
|
|
218
|
-
|
|
223
|
+
target = initialProgression.add(numbers_1.ONE.sub(initialProgression).mul(finalStageAt));
|
|
224
|
+
if (target.gte(0.997)) {
|
|
225
|
+
target = numbers_1.ONE;
|
|
219
226
|
}
|
|
220
|
-
if (
|
|
227
|
+
if (target.eq(numbers_1.ONE)) {
|
|
221
228
|
round = AuctionRound.FINAL;
|
|
222
229
|
}
|
|
223
230
|
}
|
|
224
|
-
// EJECT
|
|
231
|
+
// EJECT -- used later to adjust weights.high and limits.high
|
|
225
232
|
if (portionBeingEjected.gt(1e-5)) {
|
|
226
233
|
round = AuctionRound.EJECT;
|
|
227
|
-
// if the ejections are
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
234
|
+
// if the ejections are mostly what's left, target JUST the ejection if that puts us at <100%
|
|
235
|
+
let ejectionTarget = progression.add(portionBeingEjected.mul(1.1)); // buy up to 10% extra
|
|
236
|
+
if (ejectionTarget.lt(numbers_1.ONE)) {
|
|
237
|
+
target = ejectionTarget;
|
|
238
|
+
}
|
|
239
|
+
if (debug) {
|
|
240
|
+
console.log(" EJECT round detected:");
|
|
241
|
+
console.log(" portionBeingEjected:", portionBeingEjected.toString());
|
|
242
|
+
console.log(" progression:", progression.toString());
|
|
243
|
+
console.log(" ejectionTarget:", ejectionTarget.toString());
|
|
244
|
+
console.log(" target:", target.toString());
|
|
237
245
|
}
|
|
238
246
|
}
|
|
239
|
-
if (
|
|
247
|
+
if (target.lte(numbers_1.ZERO) || target.lt(initialProgression) || target.gt(numbers_1.ONE)) {
|
|
240
248
|
throw new Error("something has gone very wrong");
|
|
241
249
|
}
|
|
242
|
-
const relativeTarget =
|
|
250
|
+
const relativeTarget = target.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
|
|
243
251
|
if (debug) {
|
|
244
252
|
console.log("round", round);
|
|
245
|
-
console.log("
|
|
253
|
+
console.log("target", target.toString());
|
|
246
254
|
console.log("relativeTarget", relativeTarget.toString());
|
|
247
255
|
}
|
|
248
256
|
// {1}
|
|
249
|
-
const delta = numbers_1.ONE.sub(
|
|
257
|
+
const delta = numbers_1.ONE.sub(target);
|
|
250
258
|
// ================================================================
|
|
251
259
|
// get new limits, constrained by extremes
|
|
252
260
|
// D18{BU/share} = {wholeBU/wholeShare} * D18 * {1}
|
|
@@ -414,6 +422,13 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
|
|
|
414
422
|
if (tokenSurplusValue.gte(numbers_1.ONE)) {
|
|
415
423
|
surplusTokens.push(token);
|
|
416
424
|
surplusTokenSizes.push(tokenSurplusValue.toNumber());
|
|
425
|
+
if (debug && newWeights[i].spot === 0n) {
|
|
426
|
+
console.log(` EJECTING ${token}:`);
|
|
427
|
+
console.log(` assets[${i}]: ${_assets[i].toString()}`);
|
|
428
|
+
console.log(` sellDownTo: ${sellDownTo.toString()}`);
|
|
429
|
+
console.log(` surplusAmount: ${surplusAmount.toString()}`);
|
|
430
|
+
console.log(` surplusValue: ${tokenSurplusValue.toString()}`);
|
|
431
|
+
}
|
|
417
432
|
}
|
|
418
433
|
}
|
|
419
434
|
});
|
|
@@ -433,7 +448,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
|
|
|
433
448
|
initialProgression: initialProgression.toNumber(),
|
|
434
449
|
absoluteProgression: progression.toNumber(),
|
|
435
450
|
relativeProgression: relativeProgression.toNumber(),
|
|
436
|
-
target:
|
|
451
|
+
target: target.toNumber(),
|
|
437
452
|
relativeTarget: relativeTarget.toNumber(),
|
|
438
453
|
auctionSize: auctionSize,
|
|
439
454
|
surplusTokens: surplusTokens,
|
package/dist/types.d.ts
CHANGED
package/dist/utils.d.ts
CHANGED
|
@@ -20,6 +20,6 @@ export declare const getBasketDistribution: (_bals: bigint[], _prices: number[],
|
|
|
20
20
|
* @param decimals Decimals of each token
|
|
21
21
|
* @param weights Current weights from getRebalance.weights
|
|
22
22
|
* @param limits Current limits from getRebalance.limits
|
|
23
|
-
* @returns {1} Basket
|
|
23
|
+
* @returns {1} Basket accuracy
|
|
24
24
|
*/
|
|
25
25
|
export declare const getBasketAccuracy: (supply: bigint, _bals: bigint[], _prices: number[], decimals: bigint[], weights: WeightRange[], limits: RebalanceLimits) => number;
|
package/dist/utils.js
CHANGED
|
@@ -38,7 +38,7 @@ exports.getBasketDistribution = getBasketDistribution;
|
|
|
38
38
|
* @param decimals Decimals of each token
|
|
39
39
|
* @param weights Current weights from getRebalance.weights
|
|
40
40
|
* @param limits Current limits from getRebalance.limits
|
|
41
|
-
* @returns {1} Basket
|
|
41
|
+
* @returns {1} Basket accuracy
|
|
42
42
|
*/
|
|
43
43
|
const getBasketAccuracy = (supply, _bals, _prices, decimals, weights, limits) => {
|
|
44
44
|
const decimalScale = decimals.map((d) => new exports.Decimal(`1e${d}`));
|
|
@@ -51,10 +51,10 @@ const getBasketAccuracy = (supply, _bals, _prices, decimals, weights, limits) =>
|
|
|
51
51
|
// {tok} = D27{tok/BU} * D18{BU/share} * {share} / D27 / D18
|
|
52
52
|
const expectedBal = (weights[i].spot * limits.spot * supply) / numbers_1.D27n / numbers_1.D18n;
|
|
53
53
|
if (_bals[i] > expectedBal) {
|
|
54
|
-
// {USD}
|
|
54
|
+
// {USD} += {tok} * {USD/wholeTok} / {tok/wholeTok}
|
|
55
55
|
surplusValue = surplusValue.add(new exports.Decimal((_bals[i] - expectedBal).toString()).mul(prices[i]).div(decimalScale[i]));
|
|
56
56
|
}
|
|
57
|
-
// {USD}
|
|
57
|
+
// {USD} += {tok} * {USD/wholeTok} / {tok/wholeTok}
|
|
58
58
|
totalValue = totalValue.add(new exports.Decimal(_bals[i].toString()).mul(prices[i]).div(decimalScale[i]));
|
|
59
59
|
}
|
|
60
60
|
return totalValue.sub(surplusValue).div(totalValue).toNumber();
|