@reserve-protocol/dtf-rebalance-lib 2.2.0 → 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.
@@ -221,16 +221,17 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
221
221
  // EJECT -- used later to adjust weights.high and limits.high
222
222
  if (portionBeingEjected.gt(1e-5)) {
223
223
  round = AuctionRound.EJECT;
224
- // if the ejections are everything that's left, keep the finalStageAt targeting from above
225
- if (progression.add(portionBeingEjected).lt(numbers_1.ONE)) {
226
- // else: get rid of all the dust
227
- let ejectionTarget = progression.add(portionBeingEjected.mul(1.1)); // buy up to 10% extra
228
- if (ejectionTarget.gt(numbers_1.ONE)) {
229
- ejectionTarget = numbers_1.ONE;
230
- }
231
- if (ejectionTarget.gt(target)) {
232
- target = ejectionTarget;
233
- }
224
+ // if the ejections are mostly what's left, target JUST the ejection if that puts us at <100%
225
+ let ejectionTarget = progression.add(portionBeingEjected.mul(1.1)); // buy up to 10% extra
226
+ if (ejectionTarget.gt(target) && ejectionTarget.lt(numbers_1.ONE)) {
227
+ target = ejectionTarget;
228
+ }
229
+ if (debug) {
230
+ console.log(" EJECT round detected:");
231
+ console.log(" portionBeingEjected:", portionBeingEjected.toString());
232
+ console.log(" progression:", progression.toString());
233
+ console.log(" ejectionTarget:", ejectionTarget.toString());
234
+ console.log(" target:", target.toString());
234
235
  }
235
236
  }
236
237
  if (target.lte(numbers_1.ZERO) || target.lt(initialProgression) || target.gt(numbers_1.ONE)) {
@@ -250,12 +251,9 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
250
251
  const newLimits = {
251
252
  low: (0, numbers_1.bn)(spotLimit.sub(spotLimit.mul(delta)).mul(numbers_1.D18d)),
252
253
  spot: (0, numbers_1.bn)(spotLimit.mul(numbers_1.D18d)),
253
- 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)),
254
256
  };
255
- // hold some surpluses aside if ejecting
256
- if (round == AuctionRound.EJECT) {
257
- newLimits.high += newLimits.high / 10n;
258
- }
259
257
  // low
260
258
  if (newLimits.low < rebalance.limits.low) {
261
259
  newLimits.low = rebalance.limits.low;
@@ -299,15 +297,15 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
299
297
  .mul(numbers_1.D9d)
300
298
  .mul(decimalScale[i])),
301
299
  spot: (0, numbers_1.bn)(idealWeight.mul(numbers_1.D9d).mul(decimalScale[i])),
302
- high: (0, numbers_1.bn)(idealWeight
303
- .mul(numbers_1.ONE.add(delta).div(actualLimits.high.div(actualLimits.spot))) // add remaining delta into weight
304
- .mul(numbers_1.D9d)
305
- .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])),
306
308
  };
307
- // hold some surpluses aside if ejecting
308
- if (round == AuctionRound.EJECT) {
309
- newWeightsD27.high += newWeightsD27.high / 10n;
310
- }
311
309
  if (newWeightsD27.low < weightRange.low) {
312
310
  newWeightsD27.low = weightRange.low;
313
311
  }
@@ -335,18 +333,18 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
335
333
  // get new prices, constrained by extremes
336
334
  // D27{nanoUSD/tok}
337
335
  const newPrices = rebalance.initialPrices.map((initialPrice, i) => {
338
- // {nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
339
- 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]));
340
338
  if (spotPrice < initialPrice.low || spotPrice > initialPrice.high) {
341
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!`);
342
340
  }
343
341
  if (rebalance.priceControl == types_1.PriceControl.NONE) {
344
342
  return initialPrice;
345
343
  }
346
- // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
344
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} * D27 / {tok/wholeTok}
347
345
  const pricesD27 = {
348
- 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)),
349
- 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])),
350
348
  };
351
349
  // low
352
350
  if (pricesD27.low < initialPrice.low) {
@@ -411,6 +409,13 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
411
409
  if (tokenSurplusValue.gte(numbers_1.ONE)) {
412
410
  surplusTokens.push(token);
413
411
  surplusTokenSizes.push(tokenSurplusValue.toNumber());
412
+ if (debug && newWeights[i].spot === 0n) {
413
+ console.log(` EJECTING ${token}:`);
414
+ console.log(` assets[${i}]: ${_assets[i].toString()}`);
415
+ console.log(` sellDownTo: ${sellDownTo.toString()}`);
416
+ console.log(` surplusAmount: ${surplusAmount.toString()}`);
417
+ console.log(` surplusValue: ${tokenSurplusValue.toString()}`);
418
+ }
414
419
  }
415
420
  }
416
421
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.2.0",
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",