@reserve-protocol/dtf-rebalance-lib 2.4.4 → 2.5.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/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
@@ -178,13 +180,15 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
178
180
  if (!rebalance.inRebalance[i]) {
179
181
  return numbers_1.ZERO;
180
182
  }
183
+ if (expectedBalances[i].eq(numbers_1.ZERO)) {
184
+ return numbers_1.ONE;
185
+ }
181
186
  // {wholeTok/wholeShare}
182
187
  const balanceInBasket = expectedBalances[i].gt(initialBalance) ? initialBalance : expectedBalances[i];
183
- // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
184
- return balanceInBasket.mul(prices[i]);
188
+ // {1} = {wholeTok/wholeShare} / {wholeTok/wholeShare}
189
+ return balanceInBasket.div(expectedBalances[i]);
185
190
  })
186
- .reduce((a, b) => a.add(b))
187
- .div(shareValue);
191
+ .reduce((a, b) => (a.lt(b) ? a : b));
188
192
  if (progression < initialProgression) {
189
193
  if (debug) {
190
194
  console.log("progression < initialProgression", progression.toString(), initialProgression.toString());
@@ -208,7 +212,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
208
212
  if (relativeProgression.lt(finalStageAt.sub(0.01))) {
209
213
  round = AuctionRound.PROGRESS;
210
214
  target = initialProgression.add(numbers_1.ONE.sub(initialProgression).mul(finalStageAt));
211
- if (target.gt(numbers_1.ONE.sub(1e-5))) {
215
+ if (target.gt(numbers_1.ONE.sub(numbers_1.EPSILON))) {
212
216
  target = numbers_1.ONE;
213
217
  }
214
218
  if (target.eq(numbers_1.ONE)) {
@@ -216,7 +220,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
216
220
  }
217
221
  }
218
222
  // EJECT -- used later to adjust weights.high and limits.high
219
- if (portionBeingEjected.gt(1e-5)) {
223
+ if (portionBeingEjected.gt(numbers_1.EPSILON) && target.lt(numbers_1.ONE.sub(numbers_1.EPSILON))) {
220
224
  round = AuctionRound.EJECT;
221
225
  }
222
226
  if (target.lte(numbers_1.ZERO) || target.lt(initialProgression) || target.gt(numbers_1.ONE)) {
@@ -374,15 +378,12 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
374
378
  // {tok} = D27{tok/BU} * D18{BU/share} * {share} / D18 / D27
375
379
  const buyUpTo = (newWeights[i].low * newLimits.low * _supply) / numbers_1.D18n / numbers_1.D27n;
376
380
  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.ZERO : numbers_1.ONE;
379
381
  if (_assets[i] < buyUpTo) {
380
382
  // {wholeTok} = {tok} / {tok/wholeTok}
381
383
  const deficitAmount = new utils_1.Decimal((buyUpTo - _assets[i]).toString()).div(decimalScale[i]);
382
384
  // {USD} = {wholeTok} * {USD/wholeTok}
383
385
  const tokenDeficitValue = deficitAmount.mul(prices[i]);
384
- // $1 minimum
385
- if (tokenDeficitValue.gt(tradeThreshold)) {
386
+ if (tokenDeficitValue.gt(numbers_1.EPSILON)) {
386
387
  deficitTokens.push(token);
387
388
  deficitTokenSizes.push(tokenDeficitValue.toNumber());
388
389
  }
@@ -392,8 +393,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
392
393
  const surplusAmount = new utils_1.Decimal((_assets[i] - sellDownTo).toString()).div(decimalScale[i]);
393
394
  // {USD} = {wholeTok} * {USD/wholeTok}
394
395
  const tokenSurplusValue = surplusAmount.mul(prices[i]);
395
- // $1 minimum
396
- if (tokenSurplusValue.gt(tradeThreshold)) {
396
+ if (tokenSurplusValue.gt(numbers_1.EPSILON)) {
397
397
  surplusTokens.push(token);
398
398
  surplusTokenSizes.push(tokenSurplusValue.toNumber());
399
399
  }
@@ -402,12 +402,6 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
402
402
  const surplusSize = surplusTokenSizes.reduce((a, b) => a + b, 0);
403
403
  const deficitSize = deficitTokenSizes.reduce((a, b) => a + b, 0);
404
404
  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
405
  const relativeTarget = target.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
412
406
  return [
413
407
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.4.4",
3
+ "version": "2.5.1",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",