@reserve-protocol/dtf-rebalance-lib 2.0.1 → 2.0.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.
@@ -375,14 +375,6 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
375
375
  }
376
376
  // ================================================================
377
377
  // 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
378
  // basket
387
379
  const auctionTokens = [];
388
380
  const auctionWeights = []; // D27{tok/BU}
@@ -399,21 +391,25 @@ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [],
399
391
  auctionTokens.push(token);
400
392
  auctionWeights.push(newWeights[i]);
401
393
  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);
394
+ // {tok} = D27{tok/BU} * D18{BU/share} * {share} / D18 / D27
395
+ const buyUpTo = (newWeights[i].low * newLimits.low * _supply) / numbers_1.D18n / numbers_1.D27n;
396
+ const sellDownTo = (newWeights[i].high * newLimits.high * _supply + (numbers_1.D18n * numbers_1.D27n - 1n)) / numbers_1.D18n / numbers_1.D27n;
397
+ if (_assets[i] < buyUpTo) {
398
+ // {wholeTok} = {tok} / {tok/wholeTok}
399
+ const deficitAmount = new utils_1.Decimal((buyUpTo - _assets[i]).toString()).div(decimalScale[i]);
400
+ // {USD} = {wholeTok} * {USD/wholeTok}
401
+ const tokenDeficitValue = deficitAmount.mul(prices[i]);
408
402
  // $1 minimum
409
403
  if (tokenDeficitValue.gte(numbers_1.ONE)) {
410
404
  deficitTokens.push(token);
411
405
  deficitTokenSizes.push(tokenDeficitValue.toNumber());
412
406
  }
413
407
  }
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);
408
+ else if (_assets[i] > sellDownTo) {
409
+ // {wholeTok} = {tok} / {tok/wholeTok}
410
+ const surplusAmount = new utils_1.Decimal((_assets[i] - sellDownTo).toString()).div(decimalScale[i]);
411
+ // {USD} = {wholeTok} * {USD/wholeTok}
412
+ const tokenSurplusValue = surplusAmount.mul(prices[i]);
417
413
  // $1 minimum
418
414
  if (tokenSurplusValue.gte(numbers_1.ONE)) {
419
415
  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.1",
3
+ "version": "2.0.3",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",