@reserve-protocol/dtf-rebalance-lib 2.2.1 → 2.2.2

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.
@@ -151,16 +151,6 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
151
151
  })
152
152
  .reduce((a, b) => a.add(b), numbers_1.ZERO)
153
153
  .div(shareValue);
154
- if (debug && ejectionIndices.length > 0) {
155
- console.log(" Ejection calculation:");
156
- console.log(" ejectionIndices:", ejectionIndices);
157
- ejectionIndices.forEach(i => {
158
- console.log(` Token ${i}: assets=${_assets[i].toString()}, decimals=${_decimals[i].toString()}`);
159
- console.log(` Token ${i}: folio=${folio[i].toString()}, price=${prices[i].toString()}, value=${folio[i].mul(prices[i]).toString()}`);
160
- });
161
- console.log(" shareValue:", shareValue.toString());
162
- console.log(" Total folio value:", folio.map((f, i) => f.mul(prices[i])).reduce((a, b) => a.add(b)).toString());
163
- }
164
154
  // ================================================================
165
155
  // calculate progressions
166
156
  // {wholeBU/wholeShare} = {USD/wholeShare} / {USD/wholeBU}
@@ -233,7 +223,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
233
223
  round = AuctionRound.EJECT;
234
224
  // if the ejections are mostly what's left, target JUST the ejection if that puts us at <100%
235
225
  let ejectionTarget = progression.add(portionBeingEjected.mul(1.1)); // buy up to 10% extra
236
- if (ejectionTarget.lt(numbers_1.ONE)) {
226
+ if (ejectionTarget.gt(target) && ejectionTarget.lt(numbers_1.ONE)) {
237
227
  target = ejectionTarget;
238
228
  }
239
229
  if (debug) {
@@ -261,12 +251,9 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
261
251
  const newLimits = {
262
252
  low: (0, numbers_1.bn)(spotLimit.sub(spotLimit.mul(delta)).mul(numbers_1.D18d)),
263
253
  spot: (0, numbers_1.bn)(spotLimit.mul(numbers_1.D18d)),
264
- high: (0, numbers_1.bn)(spotLimit.add(spotLimit.mul(delta)).mul(numbers_1.D18d)),
254
+ // hold non-eject surpluses aside if ejecting
255
+ high: round == AuctionRound.EJECT ? rebalance.limits.high : (0, numbers_1.bn)(spotLimit.add(spotLimit.mul(delta)).mul(numbers_1.D18d)),
265
256
  };
266
- // hold some surpluses aside if ejecting
267
- if (round == AuctionRound.EJECT) {
268
- newLimits.high += newLimits.high / 10n;
269
- }
270
257
  // low
271
258
  if (newLimits.low < rebalance.limits.low) {
272
259
  newLimits.low = rebalance.limits.low;
@@ -310,15 +297,15 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
310
297
  .mul(numbers_1.D9d)
311
298
  .mul(decimalScale[i])),
312
299
  spot: (0, numbers_1.bn)(idealWeight.mul(numbers_1.D9d).mul(decimalScale[i])),
313
- high: (0, numbers_1.bn)(idealWeight
314
- .mul(numbers_1.ONE.add(delta).div(actualLimits.high.div(actualLimits.spot))) // add remaining delta into weight
315
- .mul(numbers_1.D9d)
316
- .mul(decimalScale[i])),
300
+ high:
301
+ // hold surpluses aside if ejecting
302
+ round == AuctionRound.EJECT
303
+ ? weightRange.high
304
+ : (0, numbers_1.bn)(idealWeight
305
+ .mul(numbers_1.ONE.add(delta).div(actualLimits.high.div(actualLimits.spot))) // add remaining delta into weight
306
+ .mul(numbers_1.D9d)
307
+ .mul(decimalScale[i])),
317
308
  };
318
- // hold some surpluses aside if ejecting
319
- if (round == AuctionRound.EJECT) {
320
- newWeightsD27.high += newWeightsD27.high / 10n;
321
- }
322
309
  if (newWeightsD27.low < weightRange.low) {
323
310
  newWeightsD27.low = weightRange.low;
324
311
  }
@@ -346,18 +333,18 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
346
333
  // get new prices, constrained by extremes
347
334
  // D27{nanoUSD/tok}
348
335
  const newPrices = rebalance.initialPrices.map((initialPrice, i) => {
349
- // {nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
350
- const spotPrice = (0, numbers_1.bn)(prices[i].mul(numbers_1.D9d).div(decimalScale[i]).mul(numbers_1.D27d));
336
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} * D27 / {tok/wholeTok}
337
+ const spotPrice = (0, numbers_1.bn)(prices[i].mul(numbers_1.D9d).mul(numbers_1.D27d).div(decimalScale[i]));
351
338
  if (spotPrice < initialPrice.low || spotPrice > initialPrice.high) {
352
339
  throw new Error(`spot price ${spotPrice.toString()} out of bounds relative to initial range [${initialPrice.low.toString()}, ${initialPrice.high.toString()}]! auction launcher MUST closeRebalance to prevent loss!`);
353
340
  }
354
341
  if (rebalance.priceControl == types_1.PriceControl.NONE) {
355
342
  return initialPrice;
356
343
  }
357
- // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
344
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} * D27 / {tok/wholeTok}
358
345
  const pricesD27 = {
359
- low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(numbers_1.D9d).div(decimalScale[i]).mul(numbers_1.D27d)),
360
- high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(numbers_1.D9d).div(decimalScale[i]).mul(numbers_1.D27d)),
346
+ low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(numbers_1.D9d).mul(numbers_1.D27d).div(decimalScale[i])),
347
+ high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(numbers_1.D9d).mul(numbers_1.D27d).div(decimalScale[i])),
361
348
  };
362
349
  // low
363
350
  if (pricesD27.low < initialPrice.low) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",