@reserve-protocol/dtf-rebalance-lib 0.3.0 → 1.0.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
@@ -1,5 +1,5 @@
1
- import DecimalLight from 'decimal.js-light';
2
- import type { Decimal as DecimalType } from 'decimal.js-light';
1
+ import DecimalLight from "decimal.js-light";
2
+ import type { Decimal as DecimalType } from "decimal.js-light";
3
3
  export declare const D27n: bigint;
4
4
  export declare const D18n: bigint;
5
5
  export declare const D9n: bigint;
package/dist/numbers.js CHANGED
@@ -10,12 +10,12 @@ const Decimal = decimal_js_light_1.default.clone({ precision: 100 });
10
10
  exports.D27n = 10n ** 27n;
11
11
  exports.D18n = 10n ** 18n;
12
12
  exports.D9n = 10n ** 9n;
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');
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");
19
19
  const bn = (str) => {
20
20
  return BigInt(new Decimal(str).toFixed(0));
21
21
  };
@@ -416,14 +416,6 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
416
416
  });
417
417
  // {USD}
418
418
  const valueBeingTraded = surplusValue.gt(deficitValue) ? deficitValue : surplusValue;
419
- // // completed if nothing to trade
420
- // if (valueBeingTraded.eq(ZERO)) {
421
- // round = AuctionRound.FINAL;
422
- // rebalanceTarget = ONE;
423
- // relativeProgression = ONE;
424
- // progression = ONE;
425
- // }
426
- // // TODO maybe add back
427
419
  // ================================================================
428
420
  // filter tokens that are not in the rebalance
429
421
  const returnTokens = [];
@@ -15,8 +15,9 @@ export interface StartRebalanceArgsPartial {
15
15
  * @param decimals Decimals of each token
16
16
  * @param _targetBasket D18{1} Ideal basket
17
17
  * @param _prices {USD/wholeTok} USD prices for each *whole* token
18
- * @param _priceError {1} Price error per token to use in the rebalanc; should be larger than price error during openAuction
18
+ * @param _priceError {1} Price error per token to use in the rebalance; should be larger than price error during openAuction
19
19
  * @param _dtfPrice {USD/wholeShare} DTF price
20
20
  * @param weightControl TRACKING=false, NATIVE=true
21
+ * @param deferWeights Whether to use the full range for weights, only possible for NATIVE DTFs
21
22
  */
22
- export declare const getStartRebalance: (_supply: bigint, tokens: string[], _folio: bigint[], decimals: bigint[], _targetBasket: bigint[], _prices: number[], _priceError: number[], weightControl: boolean, debug?: boolean) => StartRebalanceArgsPartial;
23
+ export declare const getStartRebalance: (_supply: bigint, tokens: string[], _folio: bigint[], decimals: bigint[], _targetBasket: bigint[], _prices: number[], _priceError: number[], weightControl: boolean, deferWeights: boolean, debug?: boolean) => StartRebalanceArgsPartial;
@@ -14,13 +14,17 @@ const numbers_1 = require("./numbers");
14
14
  * @param decimals Decimals of each token
15
15
  * @param _targetBasket D18{1} Ideal basket
16
16
  * @param _prices {USD/wholeTok} USD prices for each *whole* token
17
- * @param _priceError {1} Price error per token to use in the rebalanc; should be larger than price error during openAuction
17
+ * @param _priceError {1} Price error per token to use in the rebalance; should be larger than price error during openAuction
18
18
  * @param _dtfPrice {USD/wholeShare} DTF price
19
19
  * @param weightControl TRACKING=false, NATIVE=true
20
+ * @param deferWeights Whether to use the full range for weights, only possible for NATIVE DTFs
20
21
  */
21
- const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl, debug) => {
22
+ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl, deferWeights, debug) => {
22
23
  if (debug) {
23
- console.log("getStartRebalance", _supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl);
24
+ console.log("getStartRebalance", _supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl, deferWeights);
25
+ }
26
+ if (deferWeights && !weightControl) {
27
+ throw new Error("deferWeights is not supported for tracking DTFs");
24
28
  }
25
29
  // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
26
30
  const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(new utils_1.Decimal(`1e${decimals[i]}`)));
@@ -39,15 +43,9 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
39
43
  // ================================================================
40
44
  const newWeights = [];
41
45
  const newPrices = [];
42
- const newLimits = {
43
- low: (0, numbers_1.bn)("1e18"),
44
- spot: (0, numbers_1.bn)("1e18"),
45
- high: (0, numbers_1.bn)("1e18"),
46
- };
47
- // ================================================================
48
46
  for (let i = 0; i < tokens.length; i++) {
49
47
  if (priceError[i].gte(numbers_1.ONE)) {
50
- throw new Error("cannot defer prices");
48
+ throw new Error("price error >= 1");
51
49
  }
52
50
  // === newWeights ===
53
51
  // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
@@ -62,6 +60,7 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
62
60
  console.log("limitMultiplier", limitMultiplier.toString());
63
61
  }
64
62
  if (!weightControl) {
63
+ // TRACKING case
65
64
  // D27{tok/BU} = {wholeTok/wholeShare} * D27{tok/share}{wholeShare/wholeTok} / {BU/share}
66
65
  newWeights.push({
67
66
  low: (0, numbers_1.bn)(spotWeight.mul(limitMultiplier)),
@@ -80,6 +79,11 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
80
79
  spot: (0, numbers_1.bn)(spotWeight.mul(limitMultiplier)),
81
80
  high: (0, numbers_1.bn)(highWeight.mul(limitMultiplier)),
82
81
  });
82
+ // deferWeights case
83
+ if (deferWeights && newWeights[i].low > 0n) {
84
+ newWeights[i].low = 1n;
85
+ newWeights[i].high = numbers_1.D27n * numbers_1.D27n; // 1e54 MAX_WEIGHT
86
+ }
83
87
  }
84
88
  // === newPrices ===
85
89
  // D27{wholeTok/tok} = D27 / {tok/wholeTok}
@@ -91,19 +95,21 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
91
95
  high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d)),
92
96
  });
93
97
  }
94
- // update low/high for tracking DTFs
95
- if (!weightControl) {
96
- // sum of dot product of targetBasket and priceError
97
- const totalPortion = targetBasket
98
- .map((portion, i) => portion.mul(priceError[i]))
99
- .reduce((a, b) => a.add(b));
100
- if (totalPortion.gte(numbers_1.ONE)) {
101
- throw new Error("totalPortion > 1");
102
- }
103
- // D18{BU/share} = {1} * D18 * {BU/share}
104
- newLimits.low = (0, numbers_1.bn)(numbers_1.ONE.sub(totalPortion).mul(numbers_1.D18d));
105
- newLimits.high = (0, numbers_1.bn)(numbers_1.ONE.div(numbers_1.ONE.sub(totalPortion)).mul(numbers_1.D18d));
98
+ // ================================================================
99
+ // newLimits
100
+ // sum of dot product of targetBasket and priceError
101
+ const basketError = targetBasket
102
+ .map((portion, i) => portion.mul(priceError[i]))
103
+ .reduce((a, b) => a.add(b));
104
+ if (basketError.gte(numbers_1.ONE)) {
105
+ throw new Error("basketError >= 1");
106
106
  }
107
+ const newLimits = {
108
+ low: weightControl ? (0, numbers_1.bn)("1e18") : (0, numbers_1.bn)(numbers_1.ONE.sub(basketError).mul(numbers_1.D18d)),
109
+ spot: (0, numbers_1.bn)("1e18"),
110
+ high: weightControl ? (0, numbers_1.bn)("1e18") : (0, numbers_1.bn)(numbers_1.ONE.div(numbers_1.ONE.sub(basketError)).mul(numbers_1.D18d)),
111
+ };
112
+ // ================================================================
107
113
  if (debug) {
108
114
  console.log("newWeights", newWeights);
109
115
  console.log("newPrices", newPrices);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.3.0",
3
+ "version": "1.0.1",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",