@reserve-protocol/dtf-rebalance-lib 2.4.3 → 2.5.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/numbers.d.ts CHANGED
@@ -7,6 +7,7 @@ export declare const D27d: DecimalType;
7
7
  export declare const D18d: DecimalType;
8
8
  export declare const D9d: DecimalType;
9
9
  export declare const ZERO: DecimalLight;
10
+ export declare const EPSILON: DecimalLight;
10
11
  export declare const ONE: DecimalLight;
11
12
  export declare const TWO: DecimalLight;
12
13
  export declare const bn: (str: string | DecimalType) => bigint;
package/dist/numbers.js CHANGED
@@ -3,7 +3,7 @@ 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.bn = exports.TWO = exports.ONE = exports.ZERO = exports.D9d = exports.D18d = exports.D27d = exports.D9n = exports.D18n = exports.D27n = void 0;
6
+ exports.bn = exports.TWO = exports.ONE = exports.EPSILON = exports.ZERO = exports.D9d = exports.D18d = exports.D27d = exports.D9n = exports.D18n = exports.D27n = void 0;
7
7
  const decimal_js_light_1 = __importDefault(require("decimal.js-light"));
8
8
  // Create a local Decimal constructor with custom precision
9
9
  const Decimal = decimal_js_light_1.default.clone({ precision: 100 });
@@ -14,6 +14,7 @@ exports.D27d = new Decimal("1e27");
14
14
  exports.D18d = new Decimal("1e18");
15
15
  exports.D9d = new Decimal("1e9");
16
16
  exports.ZERO = new Decimal("0");
17
+ exports.EPSILON = new Decimal("1e-5");
17
18
  exports.ONE = new Decimal("1");
18
19
  exports.TWO = new Decimal("2");
19
20
  const bn = (str) => {
@@ -164,13 +164,15 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
164
164
  if (!rebalance.inRebalance[i]) {
165
165
  return numbers_1.ZERO;
166
166
  }
167
+ if (expectedBalances[i].eq(numbers_1.ZERO)) {
168
+ return numbers_1.ONE;
169
+ }
167
170
  // {wholeTok/wholeShare}
168
171
  const balanceInBasket = expectedBalances[i].gt(actualBalance) ? actualBalance : expectedBalances[i];
169
- // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
170
- return balanceInBasket.mul(prices[i]);
172
+ // {1} = {wholeTok/wholeShare} / {wholeTok/wholeShare}
173
+ return balanceInBasket.div(expectedBalances[i]);
171
174
  })
172
- .reduce((a, b) => a.add(b))
173
- .div(shareValue);
175
+ .reduce((a, b) => (a.lt(b) ? a : b));
174
176
  // absolute
175
177
  // {1} = {USD/wholeShare} / {USD/wholeShare}
176
178
  const initialProgression = initialFolio
@@ -180,11 +182,10 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
180
182
  }
181
183
  // {wholeTok/wholeShare}
182
184
  const balanceInBasket = expectedBalances[i].gt(initialBalance) ? initialBalance : expectedBalances[i];
183
- // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
184
- return balanceInBasket.mul(prices[i]);
185
+ // {1} = {wholeTok/wholeShare} / {wholeTok/wholeShare}
186
+ return balanceInBasket.div(expectedBalances[i]);
185
187
  })
186
- .reduce((a, b) => a.add(b))
187
- .div(shareValue);
188
+ .reduce((a, b) => (a.lt(b) ? a : b));
188
189
  if (progression < initialProgression) {
189
190
  if (debug) {
190
191
  console.log("progression < initialProgression", progression.toString(), initialProgression.toString());
@@ -208,7 +209,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
208
209
  if (relativeProgression.lt(finalStageAt.sub(0.01))) {
209
210
  round = AuctionRound.PROGRESS;
210
211
  target = initialProgression.add(numbers_1.ONE.sub(initialProgression).mul(finalStageAt));
211
- if (target.gt(numbers_1.ONE.sub(1e-5))) {
212
+ if (target.gt(numbers_1.ONE.sub(numbers_1.EPSILON))) {
212
213
  target = numbers_1.ONE;
213
214
  }
214
215
  if (target.eq(numbers_1.ONE)) {
@@ -216,7 +217,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
216
217
  }
217
218
  }
218
219
  // EJECT -- used later to adjust weights.high and limits.high
219
- if (portionBeingEjected.gt(1e-5)) {
220
+ if (portionBeingEjected.gt(numbers_1.EPSILON) && target.lt(numbers_1.ONE.sub(numbers_1.EPSILON))) {
220
221
  round = AuctionRound.EJECT;
221
222
  }
222
223
  if (target.lte(numbers_1.ZERO) || target.lt(initialProgression) || target.gt(numbers_1.ONE)) {
@@ -374,15 +375,12 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
374
375
  // {tok} = D27{tok/BU} * D18{BU/share} * {share} / D18 / D27
375
376
  const buyUpTo = (newWeights[i].low * newLimits.low * _supply) / numbers_1.D18n / numbers_1.D27n;
376
377
  const sellDownTo = (newWeights[i].high * newLimits.high * _supply + (numbers_1.D18n * numbers_1.D27n - 1n)) / numbers_1.D18n / numbers_1.D27n;
377
- // {USD}
378
- const tradeThreshold = round == AuctionRound.EJECT ? numbers_1.ONE : numbers_1.ZERO;
379
378
  if (_assets[i] < buyUpTo) {
380
379
  // {wholeTok} = {tok} / {tok/wholeTok}
381
380
  const deficitAmount = new utils_1.Decimal((buyUpTo - _assets[i]).toString()).div(decimalScale[i]);
382
381
  // {USD} = {wholeTok} * {USD/wholeTok}
383
382
  const tokenDeficitValue = deficitAmount.mul(prices[i]);
384
- // $1 minimum
385
- if (tokenDeficitValue.gt(tradeThreshold)) {
383
+ if (tokenDeficitValue.gt(numbers_1.EPSILON)) {
386
384
  deficitTokens.push(token);
387
385
  deficitTokenSizes.push(tokenDeficitValue.toNumber());
388
386
  }
@@ -392,8 +390,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
392
390
  const surplusAmount = new utils_1.Decimal((_assets[i] - sellDownTo).toString()).div(decimalScale[i]);
393
391
  // {USD} = {wholeTok} * {USD/wholeTok}
394
392
  const tokenSurplusValue = surplusAmount.mul(prices[i]);
395
- // $1 minimum
396
- if (tokenSurplusValue.gt(tradeThreshold)) {
393
+ if (tokenSurplusValue.gt(numbers_1.EPSILON)) {
397
394
  surplusTokens.push(token);
398
395
  surplusTokenSizes.push(tokenSurplusValue.toNumber());
399
396
  }
@@ -402,12 +399,6 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
402
399
  const surplusSize = surplusTokenSizes.reduce((a, b) => a + b, 0);
403
400
  const deficitSize = deficitTokenSizes.reduce((a, b) => a + b, 0);
404
401
  const auctionSize = surplusSize > deficitSize ? deficitSize : surplusSize;
405
- // update targeting estimates
406
- // {1} = {1} + {USD} / ({share} * {USD/share})
407
- const adjustedTarget = progression.add(new utils_1.Decimal(auctionSize).div(shareValue.mul(supply)));
408
- if (adjustedTarget.lte(numbers_1.ONE)) {
409
- target = adjustedTarget;
410
- }
411
402
  const relativeTarget = target.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
412
403
  return [
413
404
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.4.3",
3
+ "version": "2.5.0",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",