@reserve-protocol/dtf-rebalance-lib 2.6.2 → 2.6.4

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.
@@ -45,9 +45,10 @@ const getStartRebalance = (_supply, tokens, _assets, decimals, _targetBasket, _p
45
45
  // ================================================================
46
46
  const newWeights = [];
47
47
  const newPrices = [];
48
+ const maxPriceError = new utils_1.Decimal("0.9");
48
49
  for (let i = 0; i < tokens.length; i++) {
49
- if (priceError[i].gte(numbers_1.ONE)) {
50
- throw new Error("price error >= 1");
50
+ if (priceError[i].gt(maxPriceError)) {
51
+ throw new Error("price error > 0.9");
51
52
  }
52
53
  // === newWeights ===
53
54
  // {USD} = {wholeTok} * {USD/wholeTok}
@@ -90,11 +91,24 @@ const getStartRebalance = (_supply, tokens, _assets, decimals, _targetBasket, _p
90
91
  // === newPrices ===
91
92
  // D27{wholeTok/tok} = D27 / {tok/wholeTok}
92
93
  const priceMultiplier = numbers_1.D27d.div(new utils_1.Decimal(`1e${decimals[i]}`));
94
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {1} * D27{wholeTok/tok} * {nanoUSD/USD}
95
+ const low = (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d));
96
+ let high = (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d)) + 1n;
97
+ // check if prices are valid
98
+ if (low < 0n || low > high || high > numbers_1.D18n * numbers_1.D27n) {
99
+ throw new Error(`invalid prices for token ${tokens[i]}: low: ${low}, high: ${high}`);
100
+ }
101
+ // due to floor rounding `low`, `high` can be slightly more than 100x even at 0.9 price error
102
+ if (high > low * 100n) {
103
+ // keep consistent geometric mean
104
+ if (high > low * 100n + 100n) {
105
+ throw new Error("something has gone very wrong");
106
+ }
107
+ high = low * 100n;
108
+ }
93
109
  newPrices.push({
94
- // D27{nanoUSD/tok} = {USD/wholeTok} * {1} * D27{wholeTok/tok} * {nanoUSD/USD}
95
- low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d)),
96
- // D27{nanoUSD/tok} = {USD/wholeTok} / {1} * D27{wholeTok/tok} * {nanoUSD/USD}
97
- high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d)),
110
+ low: low,
111
+ high: high,
98
112
  });
99
113
  }
100
114
  // ================================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",