@reserve-protocol/dtf-rebalance-lib 0.2.8 → 0.2.10

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
@@ -1,11 +1,12 @@
1
- import Decimal from 'decimal.js-light';
1
+ import DecimalLight from 'decimal.js-light';
2
+ import type { Decimal as DecimalType } from 'decimal.js-light';
2
3
  export declare const D27n: bigint;
3
4
  export declare const D18n: bigint;
4
5
  export declare const D9n: bigint;
5
- export declare const D27d: Decimal;
6
- export declare const D18d: Decimal;
7
- export declare const D9d: Decimal;
8
- export declare const ZERO: Decimal;
9
- export declare const ONE: Decimal;
10
- export declare const TWO: Decimal;
11
- export declare const bn: (str: string | Decimal) => bigint;
6
+ export declare const D27d: DecimalType;
7
+ export declare const D18d: DecimalType;
8
+ export declare const D9d: DecimalType;
9
+ export declare const ZERO: DecimalLight;
10
+ export declare const ONE: DecimalLight;
11
+ export declare const TWO: DecimalLight;
12
+ export declare const bn: (str: string | DecimalType) => bigint;
package/dist/numbers.js CHANGED
@@ -5,16 +5,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.bn = exports.TWO = exports.ONE = 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
+ // Create a local Decimal constructor with custom precision
9
+ const Decimal = decimal_js_light_1.default.clone({ precision: 100 });
8
10
  exports.D27n = 10n ** 27n;
9
11
  exports.D18n = 10n ** 18n;
10
12
  exports.D9n = 10n ** 9n;
11
- exports.D27d = new decimal_js_light_1.default('1e27');
12
- exports.D18d = new decimal_js_light_1.default('1e18');
13
- exports.D9d = new decimal_js_light_1.default('1e9');
14
- exports.ZERO = new decimal_js_light_1.default('0');
15
- exports.ONE = new decimal_js_light_1.default('1');
16
- exports.TWO = new decimal_js_light_1.default('2');
13
+ exports.D27d = new Decimal('1e27');
14
+ exports.D18d = new Decimal('1e18');
15
+ exports.D9d = new Decimal('1e9');
16
+ exports.ZERO = new Decimal('0');
17
+ exports.ONE = new Decimal('1');
18
+ exports.TWO = new Decimal('2');
17
19
  const bn = (str) => {
18
- return BigInt(new decimal_js_light_1.default(str).toFixed(0));
20
+ return BigInt(new Decimal(str).toFixed(0));
19
21
  };
20
22
  exports.bn = bn;
@@ -1,13 +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 decimal_js_light_1 = __importDefault(require("decimal.js-light"));
4
+ const utils_1 = require("./utils");
8
5
  const numbers_1 = require("./numbers");
9
6
  const types_1 = require("./types");
10
- decimal_js_light_1.default.set({ precision: 100 });
11
7
  // Call `getOpenAuction()` to get the current auction round
12
8
  var AuctionRound;
13
9
  (function (AuctionRound) {
@@ -31,10 +27,10 @@ const getTargetBasket = (_initialWeights, _prices, _decimals) => {
31
27
  throw new Error("length mismatch");
32
28
  }
33
29
  const vals = _initialWeights.map((initialWeight, i) => {
34
- const price = new decimal_js_light_1.default(_prices[i]);
35
- const decimalScale = new decimal_js_light_1.default(`1e${_decimals[i]}`);
30
+ const price = new utils_1.Decimal(_prices[i]);
31
+ const decimalScale = new utils_1.Decimal(`1e${_decimals[i]}`);
36
32
  // {USD/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27 * {USD/wholeTok}
37
- return new decimal_js_light_1.default(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);
38
34
  });
39
35
  const totalValue = vals.reduce((a, b) => a.add(b));
40
36
  // D18{1} = {USD/wholeBU} / {USD/wholeBU} * D18
@@ -75,33 +71,33 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
75
71
  }
76
72
  // ================================================================
77
73
  // {wholeShare} = {share} / {share/wholeShare}
78
- const supply = new decimal_js_light_1.default(_supply.toString()).div(numbers_1.D18d);
74
+ const supply = new utils_1.Decimal(_supply.toString()).div(numbers_1.D18d);
79
75
  // {1} = D18{1} / D18
80
- const targetBasket = _targetBasket.map((a) => new decimal_js_light_1.default(a.toString()).div(numbers_1.D18d));
76
+ const targetBasket = _targetBasket.map((a) => new utils_1.Decimal(a.toString()).div(numbers_1.D18d));
81
77
  // {USD/wholeTok}
82
- const prices = _prices.map((a) => new decimal_js_light_1.default(a));
78
+ const prices = _prices.map((a) => new utils_1.Decimal(a));
83
79
  for (let i = 0; i < prices.length; i++) {
84
80
  if (prices[i].eq(numbers_1.ZERO)) {
85
81
  throw new Error(`missing price for token ${rebalance.tokens[i]}`);
86
82
  }
87
83
  }
88
84
  // {1}
89
- const priceError = _priceError.map((a) => new decimal_js_light_1.default(a.toString()));
85
+ const priceError = _priceError.map((a) => new utils_1.Decimal(a.toString()));
90
86
  // {tok/wholeTok}
91
- const decimalScale = _decimals.map((a) => new decimal_js_light_1.default(`1e${a}`));
87
+ const decimalScale = _decimals.map((a) => new utils_1.Decimal(`1e${a}`));
92
88
  // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
93
- const initialFolio = _initialFolio.map((c, i) => new decimal_js_light_1.default(c.toString()).div(decimalScale[i]));
89
+ const initialFolio = _initialFolio.map((c, i) => new utils_1.Decimal(c.toString()).div(decimalScale[i]));
94
90
  // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
95
- const folio = _folio.map((c, i) => new decimal_js_light_1.default(c.toString()).div(decimalScale[i]));
91
+ const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(decimalScale[i]));
96
92
  // {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
97
93
  let weightRanges = rebalance.weights.map((range, i) => {
98
94
  return {
99
- low: new decimal_js_light_1.default(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
100
- spot: new decimal_js_light_1.default(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
101
- high: new decimal_js_light_1.default(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),
102
98
  };
103
99
  });
104
- const finalStageAt = new decimal_js_light_1.default(_finalStageAt.toString());
100
+ const finalStageAt = new utils_1.Decimal(_finalStageAt.toString());
105
101
  // ================================================================
106
102
  // calculate ideal spot limit, the actual BU<->share ratio
107
103
  // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
@@ -149,7 +145,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
149
145
  // ================================================================
150
146
  // calculate progressions
151
147
  // {wholeBU/wholeShare} = D18{BU/share} / D18
152
- const prevSpotLimit = new decimal_js_light_1.default(rebalance.limits.spot.toString()).div(numbers_1.D18d);
148
+ const prevSpotLimit = new utils_1.Decimal(rebalance.limits.spot.toString()).div(numbers_1.D18d);
153
149
  // {wholeTok/wholeBU} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
154
150
  const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(prevSpotLimit));
155
151
  // {1} = {USD/wholeShare} / {USD/wholeShare}
@@ -274,9 +270,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
274
270
  // get new weights, constrained by extremes
275
271
  // {wholeBU/wholeShare} = D18{BU/share} / D18
276
272
  const actualLimits = {
277
- low: new decimal_js_light_1.default(newLimits.low.toString()).div(numbers_1.D18d),
278
- spot: new decimal_js_light_1.default(newLimits.spot.toString()).div(numbers_1.D18d),
279
- high: new decimal_js_light_1.default(newLimits.high.toString()).div(numbers_1.D18d),
273
+ low: new utils_1.Decimal(newLimits.low.toString()).div(numbers_1.D18d),
274
+ spot: new utils_1.Decimal(newLimits.spot.toString()).div(numbers_1.D18d),
275
+ high: new utils_1.Decimal(newLimits.high.toString()).div(numbers_1.D18d),
280
276
  };
281
277
  // D27{tok/BU}
282
278
  const newWeights = rebalance.weights.map((weightRange, i) => {
@@ -368,9 +364,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
368
364
  // {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
369
365
  weightRanges = newWeights.map((range, i) => {
370
366
  return {
371
- low: new decimal_js_light_1.default(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
372
- spot: new decimal_js_light_1.default(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
373
- high: new decimal_js_light_1.default(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
367
+ low: new utils_1.Decimal(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
368
+ spot: new utils_1.Decimal(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
369
+ high: new utils_1.Decimal(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
374
370
  };
375
371
  });
376
372
  // {USD}
@@ -1,10 +1,7 @@
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 decimal_js_light_1 = __importDefault(require("decimal.js-light"));
4
+ const utils_1 = require("./utils");
8
5
  const numbers_1 = require("./numbers");
9
6
  /**
10
7
  * Get the arguments needed to call startRebalance
@@ -26,19 +23,19 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
26
23
  console.log("getStartRebalance", _supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl);
27
24
  }
28
25
  // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
29
- const folio = _folio.map((c, i) => new decimal_js_light_1.default(c.toString()).div(new decimal_js_light_1.default(`1e${decimals[i]}`)));
26
+ const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(new utils_1.Decimal(`1e${decimals[i]}`)));
30
27
  // convert price number inputs to bigints
31
28
  // {USD/wholeTok}
32
- const prices = _prices.map((a) => new decimal_js_light_1.default(a.toString()));
29
+ const prices = _prices.map((a) => new utils_1.Decimal(a.toString()));
33
30
  for (let i = 0; i < prices.length; i++) {
34
31
  if (prices[i].eq(numbers_1.ZERO)) {
35
32
  throw new Error(`missing price for token ${tokens[i]}`);
36
33
  }
37
34
  }
38
35
  // {1} = D18{1} / D18
39
- const targetBasket = _targetBasket.map((a) => new decimal_js_light_1.default(a.toString()).div(numbers_1.D18d));
36
+ const targetBasket = _targetBasket.map((a) => new utils_1.Decimal(a.toString()).div(numbers_1.D18d));
40
37
  // {1}
41
- const priceError = _priceError.map((a) => new decimal_js_light_1.default(a.toString()));
38
+ const priceError = _priceError.map((a) => new utils_1.Decimal(a.toString()));
42
39
  // ================================================================
43
40
  const newWeights = [];
44
41
  const newPrices = [];
@@ -60,7 +57,7 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
60
57
  // {wholeTok/wholeShare} = {1} * {USD/wholeShare} / {USD/wholeTok}
61
58
  const spotWeight = targetBasket[i].mul(dtfPrice).div(prices[i]);
62
59
  // D27{tok/share}{wholeShare/wholeTok} = D27 * {tok/wholeTok} / {share/wholeShare}
63
- const limitMultiplier = numbers_1.D27d.mul(new decimal_js_light_1.default(`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);
64
61
  if (debug) {
65
62
  console.log("limitMultiplier", limitMultiplier.toString());
66
63
  }
@@ -86,7 +83,7 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
86
83
  }
87
84
  // === newPrices ===
88
85
  // D27{wholeTok/tok} = D27 / {tok/wholeTok}
89
- const priceMultiplier = numbers_1.D27d.div(new decimal_js_light_1.default(`1e${decimals[i]}`));
86
+ const priceMultiplier = numbers_1.D27d.div(new utils_1.Decimal(`1e${decimals[i]}`));
90
87
  // {USD/wholeTok} = {USD/wholeTok} * {1}
91
88
  const lowPrice = prices[i].mul(numbers_1.ONE.sub(priceError[i]));
92
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,9 +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
+ // Create a local Decimal constructor with custom precision
10
+ exports.Decimal = decimal_js_light_1.default.clone({ precision: 100 });
9
11
  /**
10
12
  * This function can be used to get a basket distribution EITHER from a set of historical basket weights
11
13
  * or from a set of current balances. Make sure to use prices from the right time.
@@ -16,15 +18,13 @@ const numbers_1 = require("./numbers");
16
18
  * @returns D18{1} Current basket, total will be around 1e18 but not exactly
17
19
  */
18
20
  const getBasketDistribution = (_bals, _prices, decimals) => {
19
- const decimalScale = decimals.map((d) => new decimal_js_light_1.default(`1e${d}`));
21
+ const decimalScale = decimals.map((d) => new exports.Decimal(`1e${d}`));
20
22
  // {wholeTok} = {tok} / {tok/wholeTok}
21
- const bals = _bals.map((bal, i) => new decimal_js_light_1.default(bal.toString()).div(decimalScale[i]));
23
+ const bals = _bals.map((bal, i) => new exports.Decimal(bal.toString()).div(decimalScale[i]));
22
24
  // {USD/wholeTok} = {USD/wholeTok}
23
- const prices = _prices.map((a) => new decimal_js_light_1.default(a.toString()));
25
+ const prices = _prices.map((a) => new exports.Decimal(a.toString()));
24
26
  // {USD} = {wholeTok} * {USD/wholeTok}
25
- const totalValue = bals
26
- .map((bal, i) => bal.mul(prices[i]))
27
- .reduce((a, b) => a.add(b));
27
+ const totalValue = bals.map((bal, i) => bal.mul(prices[i])).reduce((a, b) => a.add(b));
28
28
  // D18{1} = {wholeTok} * {USD/wholeTok} / {USD}
29
29
  return bals.map((bal, i) => (0, numbers_1.bn)(bal.mul(prices[i]).div(totalValue).mul(numbers_1.D18d)));
30
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",