@reserve-protocol/dtf-rebalance-lib 0.2.0 → 0.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.
@@ -56,6 +56,9 @@ exports.getTargetBasket = getTargetBasket;
56
56
  * @param _finalStageAt {1} The % rebalanced from the initial Folio to determine when is the final stage of the rebalance
57
57
  */
58
58
  const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket = [], _folio, _decimals, _prices, _priceError, _finalStageAt, debug) => {
59
+ if (debug === undefined) {
60
+ debug = true;
61
+ }
59
62
  if (debug) {
60
63
  console.log("getOpenAuction", rebalance, _supply, _initialFolio, _targetBasket, _folio, _decimals, _prices, _priceError, _finalStageAt);
61
64
  }
@@ -103,25 +106,35 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
103
106
  // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
104
107
  const shareValue = folio
105
108
  .map((f, i) => {
109
+ if (!rebalance.inRebalance[i]) {
110
+ return numbers_1.ZERO;
111
+ }
106
112
  return f.mul(prices[i]);
107
113
  })
108
114
  .reduce((a, b) => a.add(b));
109
115
  // {USD/wholeBU} = {wholeTok/wholeBU} * {USD/wholeTok}
110
- const buValue = weightRanges.map((weightRange, i) => weightRange.spot.mul(prices[i])).reduce((a, b) => a.add(b));
116
+ const buValue = weightRanges
117
+ .map((weightRange, i) => {
118
+ if (!rebalance.inRebalance[i]) {
119
+ return numbers_1.ZERO;
120
+ }
121
+ return weightRange.spot.mul(prices[i]);
122
+ })
123
+ .reduce((a, b) => a.add(b));
111
124
  const buPriceChange = buValue.sub(shareValue).abs().div(shareValue);
112
- console.log(` 🧺 ${buPriceChange.mul(100).toFixed(2)}% price change`);
125
+ console.log(` 🧺 ${buPriceChange.mul(100).toFixed(2)}% basket price difference`);
113
126
  if (debug) {
114
127
  console.log("shareValue", shareValue.toString());
115
128
  console.log("buValue", buValue.toString());
116
129
  }
117
130
  if (buValue.div(shareValue).gt(10) || shareValue.div(buValue).gt(10)) {
118
- throw new Error("buValue and shareValue are too different");
131
+ throw new Error("buValue and shareValue are too different, something probably went wrong");
119
132
  }
120
133
  // ================================================================
121
134
  // calculate rebalanceTarget
122
135
  const ejectionIndices = [];
123
136
  for (let i = 0; i < rebalance.weights.length; i++) {
124
- if (rebalance.weights[i].spot == 0n) {
137
+ if (rebalance.inRebalance[i] && rebalance.weights[i].spot == 0n) {
125
138
  ejectionIndices.push(i);
126
139
  }
127
140
  }
@@ -135,24 +148,31 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
135
148
  // {1} = {USD/wholeShare} / {USD/wholeShare}
136
149
  let progression = folio
137
150
  .map((actualBalance, i) => {
151
+ if (!rebalance.inRebalance[i]) {
152
+ return numbers_1.ZERO;
153
+ }
138
154
  // {wholeTok/wholeShare} = {USD/wholeShare} * {1} / {USD/wholeTok}
139
155
  const balanceExpected = shareValue.mul(targetBasket[i]).div(prices[i]);
140
156
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
141
- const balanceInBU = balanceExpected.gt(actualBalance) ? actualBalance : balanceExpected;
157
+ const balanceInBasket = balanceExpected.gt(actualBalance) ? actualBalance : balanceExpected;
142
158
  // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
143
- return balanceInBU.mul(prices[i]);
159
+ return balanceInBasket.mul(prices[i]);
144
160
  })
145
161
  .reduce((a, b) => a.add(b))
146
162
  .div(shareValue);
147
163
  // {1} = {USD/wholeShare} / {USD/wholeShare}
148
164
  const initialProgression = initialFolio
149
165
  .map((initialBalance, i) => {
166
+ // make sure to include tokens that were already ejected
167
+ if (!rebalance.inRebalance[i] && rebalance.weights[i].spot > 0n) {
168
+ return numbers_1.ZERO;
169
+ }
150
170
  // {wholeTok/wholeShare} = {USD/wholeShare} * {1} / {USD/wholeTok}
151
171
  const balanceExpected = shareValue.mul(targetBasket[i]).div(prices[i]);
152
172
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
153
- const balanceInBU = balanceExpected.gt(initialBalance) ? initialBalance : balanceExpected;
173
+ const balanceInBasket = balanceExpected.gt(initialBalance) ? initialBalance : balanceExpected;
154
174
  // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
155
- return balanceInBU.mul(prices[i]);
175
+ return balanceInBasket.mul(prices[i]);
156
176
  })
157
177
  .reduce((a, b) => a.add(b))
158
178
  .div(shareValue);
@@ -160,7 +180,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
160
180
  progression = initialProgression; // don't go backwards
161
181
  }
162
182
  // {1} = {1} / {1}
163
- const relativeProgression = initialProgression.eq(numbers_1.ONE)
183
+ let relativeProgression = initialProgression.eq(numbers_1.ONE)
164
184
  ? numbers_1.ONE
165
185
  : progression.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
166
186
  let rebalanceTarget = numbers_1.ONE;
@@ -351,8 +371,6 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
351
371
  }
352
372
  // ================================================================
353
373
  // calculate metrics
354
- // {USD} = {1} * {USD/wholeShare} * {wholeShare}
355
- const valueBeingTraded = rebalanceTarget.sub(progression).mul(shareValue).mul(supply);
356
374
  const surplusTokens = [];
357
375
  const deficitTokens = [];
358
376
  // update Decimal weightRanges
@@ -364,19 +382,39 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
364
382
  high: new decimal_js_light_1.default(range.high.toString()).mul(numbers_1.D18d).div(decimalScale[i]).div(numbers_1.D27d),
365
383
  };
366
384
  });
385
+ // {USD}
386
+ let surplusValue = numbers_1.ZERO;
387
+ let deficitValue = numbers_1.ZERO;
367
388
  rebalance.tokens.forEach((token, i) => {
389
+ if (!rebalance.inRebalance[i]) {
390
+ return;
391
+ }
368
392
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
369
393
  const buyUpTo = weightRanges[i].low.mul(actualLimits.low);
370
394
  const sellDownTo = weightRanges[i].high.mul(actualLimits.high);
371
395
  if (folio[i].lt(buyUpTo)) {
372
396
  deficitTokens.push(token);
397
+ // {USD} += {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
398
+ deficitValue = deficitValue.add(folio[i].sub(buyUpTo).mul(prices[i]).mul(supply));
373
399
  }
374
400
  else if (folio[i].gt(sellDownTo)) {
375
401
  surplusTokens.push(token);
402
+ // {USD} += {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
403
+ surplusValue = surplusValue.add(folio[i].sub(sellDownTo).mul(prices[i]).mul(supply));
376
404
  }
377
405
  });
406
+ // {USD}
407
+ const valueBeingTraded = surplusValue.gt(deficitValue) ? deficitValue : surplusValue;
408
+ // // completed if nothing to trade
409
+ // if (valueBeingTraded.eq(ZERO)) {
410
+ // round = AuctionRound.FINAL;
411
+ // rebalanceTarget = ONE;
412
+ // relativeProgression = ONE;
413
+ // progression = ONE;
414
+ // }
415
+ // // TODO maybe add back
378
416
  // ================================================================
379
- // only return tokens that are in the rebalance
417
+ // filter tokens that are not in the rebalance
380
418
  const returnTokens = [];
381
419
  const returnWeights = [];
382
420
  const returnPrices = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",