@reserve-protocol/dtf-rebalance-lib 2.0.0 → 2.0.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.
@@ -67,7 +67,7 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
67
67
  debug = true;
68
68
  }
69
69
  if (debug) {
70
- console.log("getOpenAuction", rebalance, _supply, _initialAssets, _targetBasket, _assets, _decimals, _prices, _priceError, _finalStageAt);
70
+ console.log("getOpenAuction", rebalance, _supply, _initialSupply, _initialAssets, _targetBasket, _assets, _decimals, _prices, _priceError, _finalStageAt);
71
71
  }
72
72
  if (rebalance.tokens.length != _targetBasket.length ||
73
73
  _targetBasket.length != _assets.length ||
@@ -158,11 +158,8 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
158
158
  // calculate progressions
159
159
  // {wholeBU/wholeShare} = {USD/wholeShare} / {USD/wholeBU}
160
160
  const spotLimit = shareValue.div(buValue);
161
- // {wholeBU/wholeShare} = D18{BU/share} / D18
162
- const prevSpotLimit = new utils_1.Decimal(rebalance.limits.spot.toString()).div(numbers_1.D18d);
163
- const maxSpotLimit = spotLimit.gt(prevSpotLimit) ? spotLimit : prevSpotLimit;
164
161
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
165
- const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(maxSpotLimit));
162
+ const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(spotLimit));
166
163
  // absolute
167
164
  // {1} = {USD/wholeShare} / {USD/wholeShare}
168
165
  let progression = folio
@@ -375,14 +372,6 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
375
372
  }
376
373
  // ================================================================
377
374
  // calculate metrics
378
- // {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
379
- weightRanges = newWeights.map((range, i) => {
380
- return {
381
- low: new utils_1.Decimal(range.low.toString()).div(decimalScale[i]).div(numbers_1.D9d),
382
- spot: new utils_1.Decimal(range.spot.toString()).div(decimalScale[i]).div(numbers_1.D9d),
383
- high: new utils_1.Decimal(range.high.toString()).div(decimalScale[i]).div(numbers_1.D9d),
384
- };
385
- });
386
375
  // basket
387
376
  const auctionTokens = [];
388
377
  const auctionWeights = []; // D27{tok/BU}
@@ -399,21 +388,25 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
399
388
  auctionTokens.push(token);
400
389
  auctionWeights.push(newWeights[i]);
401
390
  auctionPrices.push(newPrices[i]);
402
- // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
403
- const buyUpTo = weightRanges[i].low.mul(actualLimits.low);
404
- const sellDownTo = weightRanges[i].high.mul(actualLimits.high);
405
- if (folio[i].lt(buyUpTo)) {
406
- // {USD} = {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
407
- const tokenDeficitValue = buyUpTo.sub(folio[i]).mul(prices[i]).mul(supply);
391
+ // {tok} = D27{tok/BU} * D18{BU/share} * {share} / D18 / D27
392
+ const buyUpTo = (newWeights[i].low * newLimits.low * _supply) / numbers_1.D18n / numbers_1.D27n;
393
+ const sellDownTo = (newWeights[i].high * newLimits.high * _supply + (numbers_1.D18n * numbers_1.D27n - 1n)) / numbers_1.D18n / numbers_1.D27n;
394
+ if (_assets[i] < buyUpTo) {
395
+ // {wholeTok} = {tok} / {tok/wholeTok}
396
+ const deficitAmount = new utils_1.Decimal((buyUpTo - _assets[i]).toString()).div(decimalScale[i]);
397
+ // {USD} = {wholeTok} * {USD/wholeTok}
398
+ const tokenDeficitValue = deficitAmount.mul(prices[i]);
408
399
  // $1 minimum
409
400
  if (tokenDeficitValue.gte(numbers_1.ONE)) {
410
401
  deficitTokens.push(token);
411
402
  deficitTokenSizes.push(tokenDeficitValue.toNumber());
412
403
  }
413
404
  }
414
- else if (folio[i].gt(sellDownTo)) {
415
- // {USD} = {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
416
- const tokenSurplusValue = folio[i].sub(sellDownTo).mul(prices[i]).mul(supply);
405
+ else if (_assets[i] > sellDownTo) {
406
+ // {wholeTok} = {tok} / {tok/wholeTok}
407
+ const surplusAmount = new utils_1.Decimal((_assets[i] - sellDownTo).toString()).div(decimalScale[i]);
408
+ // {USD} = {wholeTok} * {USD/wholeTok}
409
+ const tokenSurplusValue = surplusAmount.mul(prices[i]);
417
410
  // $1 minimum
418
411
  if (tokenSurplusValue.gte(numbers_1.ONE)) {
419
412
  surplusTokens.push(token);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",