@reserve-protocol/dtf-rebalance-lib 2.2.1 → 2.2.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.
@@ -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) {
@@ -247,11 +237,9 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
247
237
  if (target.lte(numbers_1.ZERO) || target.lt(initialProgression) || target.gt(numbers_1.ONE)) {
248
238
  throw new Error("something has gone very wrong");
249
239
  }
250
- const relativeTarget = target.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
251
240
  if (debug) {
252
241
  console.log("round", round);
253
242
  console.log("target", target.toString());
254
- console.log("relativeTarget", relativeTarget.toString());
255
243
  }
256
244
  // {1}
257
245
  const delta = numbers_1.ONE.sub(target);
@@ -261,12 +249,9 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
261
249
  const newLimits = {
262
250
  low: (0, numbers_1.bn)(spotLimit.sub(spotLimit.mul(delta)).mul(numbers_1.D18d)),
263
251
  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)),
252
+ // hold non-eject surpluses aside if ejecting
253
+ high: round == AuctionRound.EJECT ? rebalance.limits.high : (0, numbers_1.bn)(spotLimit.add(spotLimit.mul(delta)).mul(numbers_1.D18d)),
265
254
  };
266
- // hold some surpluses aside if ejecting
267
- if (round == AuctionRound.EJECT) {
268
- newLimits.high += newLimits.high / 10n;
269
- }
270
255
  // low
271
256
  if (newLimits.low < rebalance.limits.low) {
272
257
  newLimits.low = rebalance.limits.low;
@@ -310,15 +295,15 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
310
295
  .mul(numbers_1.D9d)
311
296
  .mul(decimalScale[i])),
312
297
  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])),
298
+ high:
299
+ // hold surpluses aside if ejecting
300
+ round == AuctionRound.EJECT
301
+ ? weightRange.high
302
+ : (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])),
317
306
  };
318
- // hold some surpluses aside if ejecting
319
- if (round == AuctionRound.EJECT) {
320
- newWeightsD27.high += newWeightsD27.high / 10n;
321
- }
322
307
  if (newWeightsD27.low < weightRange.low) {
323
308
  newWeightsD27.low = weightRange.low;
324
309
  }
@@ -346,18 +331,18 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
346
331
  // get new prices, constrained by extremes
347
332
  // D27{nanoUSD/tok}
348
333
  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));
334
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} * D27 / {tok/wholeTok}
335
+ const spotPrice = (0, numbers_1.bn)(prices[i].mul(numbers_1.D9d).mul(numbers_1.D27d).div(decimalScale[i]));
351
336
  if (spotPrice < initialPrice.low || spotPrice > initialPrice.high) {
352
337
  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
338
  }
354
339
  if (rebalance.priceControl == types_1.PriceControl.NONE) {
355
340
  return initialPrice;
356
341
  }
357
- // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
342
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} * D27 / {tok/wholeTok}
358
343
  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)),
344
+ 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])),
345
+ 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
346
  };
362
347
  // low
363
348
  if (pricesD27.low < initialPrice.low) {
@@ -435,6 +420,10 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
435
420
  const surplusSize = surplusTokenSizes.reduce((a, b) => a + b, 0);
436
421
  const deficitSize = deficitTokenSizes.reduce((a, b) => a + b, 0);
437
422
  const auctionSize = surplusSize > deficitSize ? deficitSize : surplusSize;
423
+ // update targeting estimates
424
+ // {1} = {1} + {USD} * {share} / {USD/share}
425
+ target = progression.add(new utils_1.Decimal(auctionSize).div(shareValue.mul(supply)));
426
+ const relativeTarget = target.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
438
427
  return [
439
428
  {
440
429
  rebalanceNonce: rebalance.nonce,
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.3",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",