@reserve-protocol/dtf-rebalance-lib 0.2.5 → 0.2.7

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.
@@ -132,7 +132,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
132
132
  throw new Error("buValue and shareValue are too different, something probably went wrong");
133
133
  }
134
134
  // ================================================================
135
- // calculate rebalanceTarget
135
+ // calculate portionBeingEjected
136
136
  const ejectionIndices = [];
137
137
  for (let i = 0; i < rebalance.weights.length; i++) {
138
138
  if (rebalance.inRebalance[i] && rebalance.weights[i].spot == 0n) {
@@ -146,37 +146,41 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
146
146
  })
147
147
  .reduce((a, b) => a.add(b), numbers_1.ZERO)
148
148
  .div(shareValue);
149
- // {1} = {USD/wholeShare} / {USD/wholeShare}
149
+ // ================================================================
150
+ // {wholeBU/wholeShare} = D18{BU/share} / D18
151
+ const prevSpotLimit = new decimal_js_light_1.default(rebalance.limits.spot.toString()).div(numbers_1.D18d);
152
+ // calculate progressions
153
+ // {1} = {USD/wholeBU} / {USD/wholeBU}
150
154
  let progression = folio
151
155
  .map((actualBalance, i) => {
152
156
  if (!rebalance.inRebalance[i]) {
153
157
  return numbers_1.ZERO;
154
158
  }
155
- // {wholeTok/wholeShare} = {USD/wholeShare} * {1} / {USD/wholeTok}
156
- const balanceExpected = shareValue.mul(targetBasket[i]).div(prices[i]);
157
- // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
158
- const balanceInBasket = balanceExpected.gt(actualBalance) ? actualBalance : balanceExpected;
159
- // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
159
+ // {wholeTok/wholeBU} = {wholeTok/wholeShare} / {wholeBU/wholeShare}
160
+ const actualInBU = actualBalance.div(prevSpotLimit);
161
+ // {wholeTok/wholeBU}
162
+ const balanceInBasket = weightRanges[i].spot.gt(actualInBU) ? actualInBU : weightRanges[i].spot;
163
+ // {USD/wholeBU} = {wholeTok/wholeBU} * {USD/wholeTok}
160
164
  return balanceInBasket.mul(prices[i]);
161
165
  })
162
166
  .reduce((a, b) => a.add(b))
163
- .div(shareValue);
164
- // {1} = {USD/wholeShare} / {USD/wholeShare}
167
+ .div(buValue);
168
+ // {1} = {USD/wholeBU} / {USD/wholeBU}
165
169
  const initialProgression = initialFolio
166
170
  .map((initialBalance, i) => {
167
171
  // make sure to include tokens that were already ejected
168
172
  if (!rebalance.inRebalance[i] && rebalance.weights[i].spot > 0n) {
169
173
  return numbers_1.ZERO;
170
174
  }
171
- // {wholeTok/wholeShare} = {USD/wholeShare} * {1} / {USD/wholeTok}
172
- const balanceExpected = shareValue.mul(targetBasket[i]).div(prices[i]);
173
- // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
174
- const balanceInBasket = balanceExpected.gt(initialBalance) ? initialBalance : balanceExpected;
175
- // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
175
+ // {wholeTok/wholeBU} = {wholeTok/wholeShare} / {wholeBU/wholeShare}
176
+ const initialInBU = initialBalance.div(prevSpotLimit);
177
+ // {wholeTok/wholeBU}
178
+ const balanceInBasket = weightRanges[i].spot.gt(initialInBU) ? initialInBU : weightRanges[i].spot;
179
+ // {USD/wholeBU} = {wholeTok/wholeBU} * {USD/wholeTok}
176
180
  return balanceInBasket.mul(prices[i]);
177
181
  })
178
182
  .reduce((a, b) => a.add(b))
179
- .div(shareValue);
183
+ .div(buValue);
180
184
  if (progression < initialProgression) {
181
185
  progression = initialProgression; // don't go backwards
182
186
  }
@@ -210,7 +214,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
210
214
  // if the ejections are everything that's left, keep the finalStageAt targeting from above
211
215
  if (progression.add(portionBeingEjected).lt(numbers_1.ONE)) {
212
216
  // else: get rid of all the dust
213
- let ejectionTarget = progression.add(portionBeingEjected.mul(1.02)); // buy 2% extra
217
+ let ejectionTarget = progression.add(portionBeingEjected.mul(1.05)); // buy 5% extra
214
218
  if (ejectionTarget.gt(numbers_1.ONE)) {
215
219
  ejectionTarget = numbers_1.ONE;
216
220
  }
@@ -242,11 +246,6 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
242
246
  };
243
247
  // hold some surpluses aside if ejecting
244
248
  if (round == AuctionRound.EJECT) {
245
- // buy 0.1% more
246
- newLimits.low += newLimits.low / 1000n;
247
- // aim 1% higher in the future
248
- newLimits.spot += newLimits.spot / 100n;
249
- // leave 10% room to increase low in the future if ejection leaves dust behind
250
249
  newLimits.high += newLimits.high / 10n;
251
250
  }
252
251
  // low
@@ -299,11 +298,6 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
299
298
  };
300
299
  // hold some surpluses aside if ejecting
301
300
  if (round == AuctionRound.EJECT) {
302
- // buy 0.1% more
303
- newWeightsD27.low += newWeightsD27.low / 1000n;
304
- // aim 1% higher in the future
305
- newWeightsD27.spot += newWeightsD27.spot / 100n;
306
- // leave 10% room to increase low in the future if ejection leaves dust behind
307
301
  newWeightsD27.high += newWeightsD27.high / 10n;
308
302
  }
309
303
  if (newWeightsD27.low < weightRange.low) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",