@reserve-protocol/dtf-rebalance-lib 2.6.1 → 2.6.3

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.
@@ -239,21 +239,19 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
239
239
  // hold non-eject surpluses aside if ejecting
240
240
  high: round == AuctionRound.EJECT ? rebalance.limits.high : (0, numbers_1.bn)(idealHighLimit.mul(numbers_1.D18d)),
241
241
  };
242
- // low
242
+ // enforce in range
243
243
  if (newLimits.low < rebalance.limits.low) {
244
244
  newLimits.low = rebalance.limits.low;
245
245
  }
246
246
  if (newLimits.low > rebalance.limits.high) {
247
247
  newLimits.low = rebalance.limits.high;
248
248
  }
249
- // spot
250
249
  if (newLimits.spot < rebalance.limits.low) {
251
250
  newLimits.spot = rebalance.limits.low;
252
251
  }
253
252
  if (newLimits.spot > rebalance.limits.high) {
254
253
  newLimits.spot = rebalance.limits.high;
255
254
  }
256
- // high
257
255
  if (newLimits.high < rebalance.limits.low) {
258
256
  newLimits.high = rebalance.limits.low;
259
257
  }
@@ -291,6 +289,14 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
291
289
  .mul(numbers_1.D9d)
292
290
  .mul(decimalScale[i])),
293
291
  };
292
+ // enforce relative ordering
293
+ if (newWeightsD27.low > newWeightsD27.spot) {
294
+ newWeightsD27.low = newWeightsD27.spot;
295
+ }
296
+ if (newWeightsD27.spot > newWeightsD27.high) {
297
+ newWeightsD27.high = newWeightsD27.spot;
298
+ }
299
+ // enforce in range
294
300
  if (newWeightsD27.low < weightRange.low) {
295
301
  newWeightsD27.low = weightRange.low;
296
302
  }
@@ -90,11 +90,23 @@ const getStartRebalance = (_supply, tokens, _assets, decimals, _targetBasket, _p
90
90
  // === newPrices ===
91
91
  // D27{wholeTok/tok} = D27 / {tok/wholeTok}
92
92
  const priceMultiplier = numbers_1.D27d.div(new utils_1.Decimal(`1e${decimals[i]}`));
93
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {1} * D27{wholeTok/tok} * {nanoUSD/USD}
94
+ const low = (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d));
95
+ let high = (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d));
96
+ // check if prices are valid
97
+ if (low < 0n || low > high || high > numbers_1.D18n * numbers_1.D27n) {
98
+ throw new Error(`invalid prices for token ${tokens[i]}: low: ${low}, high: ${high}`);
99
+ }
100
+ // constrain price range if too large, but not by more than 10%
101
+ if (high > low * 110n) {
102
+ throw new Error(`invalid price range for token ${tokens[i]}: low: ${low}, high: ${high}`);
103
+ }
104
+ if (high > low * 100n) {
105
+ high = low * 100n;
106
+ }
93
107
  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)),
108
+ low: low,
109
+ high: high,
98
110
  });
99
111
  }
100
112
  // ================================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.6.1",
3
+ "version": "2.6.3",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",