@reserve-protocol/dtf-rebalance-lib 0.1.4 → 0.1.6

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.
@@ -103,16 +103,18 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
103
103
  // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
104
104
  const shareValue = folio
105
105
  .map((f, i) => {
106
- return rebalance.inRebalance[i] ? f.mul(prices[i]) : numbers_1.ZERO;
106
+ return f.mul(prices[i]);
107
107
  })
108
108
  .reduce((a, b) => a.add(b));
109
109
  // {USD/wholeBU} = {wholeTok/wholeBU} * {USD/wholeTok}
110
110
  const buValue = weightRanges.map((weightRange, i) => weightRange.spot.mul(prices[i])).reduce((a, b) => a.add(b));
111
+ const buPriceChange = buValue.sub(shareValue).abs().div(shareValue);
112
+ console.log(` 🧺 ${buPriceChange.mul(100).toFixed(2)}% price change`);
111
113
  if (debug) {
112
114
  console.log("shareValue", shareValue.toString());
113
115
  console.log("buValue", buValue.toString());
114
116
  }
115
- if (buValue.div(shareValue).gt(10) || shareValue.div(buValue).gt(100)) {
117
+ if (buValue.div(shareValue).gt(10) || shareValue.div(buValue).gt(10)) {
116
118
  throw new Error("buValue and shareValue are too different");
117
119
  }
118
120
  // ================================================================
@@ -126,7 +128,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
126
128
  // {1} = {wholeTok/wholeShare} * {USD/wholeTok} / {USD/wholeShare}
127
129
  const portionBeingEjected = ejectionIndices
128
130
  .map((i) => {
129
- return rebalance.inRebalance[i] ? folio[i].mul(prices[i]) : numbers_1.ZERO;
131
+ return folio[i].mul(prices[i]);
130
132
  })
131
133
  .reduce((a, b) => a.add(b), numbers_1.ZERO)
132
134
  .div(shareValue);
@@ -174,7 +176,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
174
176
  if (progression.lt(0.99) && relativeProgression.lt(finalStageAt.sub(0.02))) {
175
177
  round = AuctionRound.PROGRESS;
176
178
  rebalanceTarget = initialProgression.add(numbers_1.ONE.sub(initialProgression).mul(finalStageAt));
177
- if (numbers_1.ONE.sub(rebalanceTarget).lt(0.99)) {
179
+ if (rebalanceTarget.gte(0.99)) {
178
180
  rebalanceTarget = numbers_1.ONE;
179
181
  }
180
182
  if (rebalanceTarget.eq(numbers_1.ONE)) {
@@ -219,18 +221,11 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
219
221
  if (round == AuctionRound.EJECT) {
220
222
  // buy 0.1% more
221
223
  newLimits.low += newLimits.low / 1000n;
222
- // aim 1% higher
224
+ // aim 1% higher in the future
223
225
  newLimits.spot += newLimits.spot / 100n;
224
226
  // leave 10% room to increase low in the future if ejection leaves dust behind
225
227
  newLimits.high += newLimits.high / 10n;
226
228
  }
227
- else if (round == AuctionRound.FINAL && progression.gt(0.999)) {
228
- // if it's the final round and we're within 0.1%, buy 10% more than we need to
229
- const delta = (0, numbers_1.bn)(numbers_1.ONE.sub(progression).mul(numbers_1.D18d).div(10));
230
- newLimits.low += (newLimits.low * delta) / 10n ** 18n;
231
- newLimits.spot += (newLimits.spot * delta) / 10n ** 18n;
232
- newLimits.high += (newLimits.high * delta) / 10n ** 18n;
233
- }
234
229
  // low
235
230
  if (newLimits.low < rebalance.limits.low) {
236
231
  newLimits.low = rebalance.limits.low;
@@ -285,18 +280,11 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
285
280
  if (round == AuctionRound.EJECT) {
286
281
  // buy 0.1% more
287
282
  newWeightsD27.low += newWeightsD27.low / 1000n;
288
- // aim 1% higher
283
+ // aim 1% higher in the future
289
284
  newWeightsD27.spot += newWeightsD27.spot / 100n;
290
285
  // leave 10% room to increase low in the future if ejection leaves dust behind
291
286
  newWeightsD27.high += newWeightsD27.high / 10n;
292
287
  }
293
- else if (round == AuctionRound.FINAL && progression.gt(0.999)) {
294
- // if it's the final round and we're within 0.1%, buy 10% more than we need to
295
- const delta = (0, numbers_1.bn)(numbers_1.ONE.sub(progression).mul(numbers_1.D18d).div(10));
296
- newWeightsD27.low += (newWeightsD27.low * delta) / 10n ** 18n;
297
- newWeightsD27.spot += (newWeightsD27.spot * delta) / 10n ** 18n;
298
- newWeightsD27.high += (newWeightsD27.high * delta) / 10n ** 18n;
299
- }
300
288
  if (newWeightsD27.low < weightRange.low) {
301
289
  newWeightsD27.low = weightRange.low;
302
290
  }
@@ -327,7 +315,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
327
315
  // revert if price out of bounds
328
316
  const spotPrice = (0, numbers_1.bn)(prices[i].mul(numbers_1.D27d).div(decimalScale[i]));
329
317
  if (spotPrice < initialPrice.low || spotPrice > initialPrice.high) {
330
- throw new Error("spot price out of bounds! auction launcher MUST closeRebalance to prevent loss!");
318
+ 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!`);
331
319
  }
332
320
  if (rebalance.priceControl == types_1.PriceControl.NONE) {
333
321
  return initialPrice;
@@ -378,10 +366,10 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
378
366
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
379
367
  const buyUpTo = weightRanges[i].low.mul(actualLimits.low);
380
368
  const sellDownTo = weightRanges[i].high.mul(actualLimits.high);
381
- if (rebalance.inRebalance[i] && folio[i].lt(buyUpTo)) {
369
+ if (folio[i].lt(buyUpTo)) {
382
370
  deficitTokens.push(token);
383
371
  }
384
- else if (rebalance.inRebalance[i] && folio[i].gt(sellDownTo)) {
372
+ else if (folio[i].gt(sellDownTo)) {
385
373
  surplusTokens.push(token);
386
374
  }
387
375
  });
@@ -75,8 +75,8 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
75
75
  else {
76
76
  // NATIVE case
77
77
  // {wholeTok/wholeShare} = {wholeTok/wholeShare} / {1}
78
- const lowWeight = spotWeight.mul(numbers_1.ONE.div(numbers_1.ONE.add(priceError[i])));
79
- const highWeight = spotWeight.mul(numbers_1.ONE.add(priceError[i]));
78
+ const lowWeight = spotWeight.mul(numbers_1.ONE.sub(priceError[i]));
79
+ const highWeight = spotWeight.div(numbers_1.ONE.sub(priceError[i]));
80
80
  // D27{tok/share} = {wholeTok/wholeShare} * D27{tok/share}{wholeShare/wholeTok} / {BU/share}
81
81
  newWeights.push({
82
82
  low: (0, numbers_1.bn)(lowWeight.mul(limitMultiplier)),
@@ -106,8 +106,8 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
106
106
  throw new Error("totalPortion > 1");
107
107
  }
108
108
  // D18{BU/share} = {1} * D18 * {BU/share}
109
- newLimits.low = (0, numbers_1.bn)(numbers_1.ONE.div(numbers_1.ONE.add(totalPortion)).mul(numbers_1.D18d));
110
- newLimits.high = (0, numbers_1.bn)(numbers_1.ONE.add(totalPortion).mul(numbers_1.D18d));
109
+ newLimits.low = (0, numbers_1.bn)(numbers_1.ONE.sub(totalPortion).mul(numbers_1.D18d));
110
+ newLimits.high = (0, numbers_1.bn)(numbers_1.ONE.div(numbers_1.ONE.sub(totalPortion)).mul(numbers_1.D18d));
111
111
  }
112
112
  if (debug) {
113
113
  console.log("newWeights", newWeights);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",