@reserve-protocol/dtf-rebalance-lib 2.1.2 → 2.2.0

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  export * from './types';
2
2
  export * from './numbers';
3
- export * from './utils';
4
3
  export * from './open-auction';
5
4
  export * from './start-rebalance';
5
+ export * from './utils';
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);
@@ -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
  }
@@ -163,7 +160,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
163
160
  const maxSpotLimit = spotLimit.gt(prevSpotLimit) ? spotLimit : prevSpotLimit;
164
161
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
165
162
  const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(maxSpotLimit));
166
- // absolute
163
+ // absoluteProgression
167
164
  // {1} = {USD/wholeShare} / {USD/wholeShare}
168
165
  let progression = folio
169
166
  .map((actualBalance, i) => {
@@ -201,7 +198,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
201
198
  let relativeProgression = initialProgression.eq(numbers_1.ONE)
202
199
  ? numbers_1.ONE
203
200
  : progression.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
204
- let rebalanceTarget = numbers_1.ONE; // absolute
201
+ let target = numbers_1.ONE; // absolute
205
202
  let round = AuctionRound.FINAL;
206
203
  if (debug) {
207
204
  console.log("initialProgression", initialProgression.toString());
@@ -213,15 +210,15 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
213
210
  // approach finalStageAt first
214
211
  if (progression.lt(0.99) && relativeProgression.lt(finalStageAt.sub(0.02))) {
215
212
  round = AuctionRound.PROGRESS;
216
- rebalanceTarget = initialProgression.add(numbers_1.ONE.sub(initialProgression).mul(finalStageAt));
217
- if (rebalanceTarget.gte(0.997)) {
218
- rebalanceTarget = numbers_1.ONE;
213
+ target = initialProgression.add(numbers_1.ONE.sub(initialProgression).mul(finalStageAt));
214
+ if (target.gte(0.997)) {
215
+ target = numbers_1.ONE;
219
216
  }
220
- if (rebalanceTarget.eq(numbers_1.ONE)) {
217
+ if (target.eq(numbers_1.ONE)) {
221
218
  round = AuctionRound.FINAL;
222
219
  }
223
220
  }
224
- // EJECT
221
+ // EJECT -- used later to adjust weights.high and limits.high
225
222
  if (portionBeingEjected.gt(1e-5)) {
226
223
  round = AuctionRound.EJECT;
227
224
  // if the ejections are everything that's left, keep the finalStageAt targeting from above
@@ -231,22 +228,22 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
231
228
  if (ejectionTarget.gt(numbers_1.ONE)) {
232
229
  ejectionTarget = numbers_1.ONE;
233
230
  }
234
- if (ejectionTarget.gt(rebalanceTarget)) {
235
- rebalanceTarget = ejectionTarget;
231
+ if (ejectionTarget.gt(target)) {
232
+ target = ejectionTarget;
236
233
  }
237
234
  }
238
235
  }
239
- if (rebalanceTarget.lte(numbers_1.ZERO) || rebalanceTarget.lt(initialProgression) || rebalanceTarget.gt(numbers_1.ONE)) {
236
+ if (target.lte(numbers_1.ZERO) || target.lt(initialProgression) || target.gt(numbers_1.ONE)) {
240
237
  throw new Error("something has gone very wrong");
241
238
  }
242
- const relativeTarget = rebalanceTarget.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
239
+ const relativeTarget = target.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
243
240
  if (debug) {
244
241
  console.log("round", round);
245
- console.log("rebalanceTarget", rebalanceTarget.toString());
242
+ console.log("target", target.toString());
246
243
  console.log("relativeTarget", relativeTarget.toString());
247
244
  }
248
245
  // {1}
249
- const delta = numbers_1.ONE.sub(rebalanceTarget);
246
+ const delta = numbers_1.ONE.sub(target);
250
247
  // ================================================================
251
248
  // get new limits, constrained by extremes
252
249
  // D18{BU/share} = {wholeBU/wholeShare} * D18 * {1}
@@ -433,7 +430,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
433
430
  initialProgression: initialProgression.toNumber(),
434
431
  absoluteProgression: progression.toNumber(),
435
432
  relativeProgression: relativeProgression.toNumber(),
436
- target: rebalanceTarget.toNumber(),
433
+ target: target.toNumber(),
437
434
  relativeTarget: relativeTarget.toNumber(),
438
435
  auctionSize: auctionSize,
439
436
  surplusTokens: surplusTokens,
package/dist/types.d.ts CHANGED
@@ -29,3 +29,10 @@ export interface Rebalance {
29
29
  availableUntil: bigint;
30
30
  priceControl: PriceControl;
31
31
  }
32
+ export interface Folio {
33
+ name: string;
34
+ chainId: number;
35
+ folio: string;
36
+ proxyAdmin: string;
37
+ basketGovernor?: string;
38
+ }
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 deviation
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 deviation
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,11 +51,11 @@ 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} = {tok} * {USD/wholeTok} / {tok/wholeTok}
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
- // {USD} = {tok} * {USD/wholeTok} / {tok/wholeTok}
57
- totalValue = totalValue.add(new exports.Decimal(_bals[i].toString()).mul(prices[i]).div(decimalScale[i]));
58
56
  }
57
+ // {USD} += {tok} * {USD/wholeTok} / {tok/wholeTok}
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();
61
61
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",