@reserve-protocol/dtf-rebalance-lib 0.2.18 → 0.3.0

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.
@@ -45,7 +45,7 @@ export interface OpenAuctionArgs {
45
45
  * @param _prices {USD/wholeTok} either CURRENT or HISTORICAL prices
46
46
  * @returns D18{1} The target basket
47
47
  */
48
- export declare const getTargetBasket: (_initialWeights: WeightRange[], _prices: number[], _decimals: bigint[]) => bigint[];
48
+ export declare const getTargetBasket: (_initialWeights: WeightRange[], _prices: number[], _decimals: bigint[], debug?: boolean) => bigint[];
49
49
  /**
50
50
  * Get the values needed to call `folio.openAuction()` as the AUCTION_LAUNCHER
51
51
  *
@@ -22,8 +22,13 @@ var AuctionRound;
22
22
  * @param _prices {USD/wholeTok} either CURRENT or HISTORICAL prices
23
23
  * @returns D18{1} The target basket
24
24
  */
25
- const getTargetBasket = (_initialWeights, _prices, _decimals) => {
26
- console.log("getTargetBasket", _initialWeights, _prices, _decimals);
25
+ const getTargetBasket = (_initialWeights, _prices, _decimals, debug) => {
26
+ if (debug === undefined) {
27
+ debug = true;
28
+ }
29
+ if (debug) {
30
+ console.log("getTargetBasket", _initialWeights, _prices, _decimals);
31
+ }
27
32
  if (_initialWeights.length != _prices.length) {
28
33
  throw new Error("length mismatch");
29
34
  }
@@ -79,7 +84,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
79
84
  // {1} = D18{1} / D18
80
85
  const targetBasket = _targetBasket.map((a) => new utils_1.Decimal(a.toString()).div(numbers_1.D18d));
81
86
  // {USD/wholeTok}
82
- const prices = _prices.map((a) => new utils_1.Decimal(a));
87
+ const prices = _prices.map((a) => new utils_1.Decimal(a.toString()));
83
88
  for (let i = 0; i < prices.length; i++) {
84
89
  if (prices[i].lte(numbers_1.ZERO)) {
85
90
  throw new Error(`missing price for token ${rebalance.tokens[i]}`);
@@ -330,20 +335,20 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
330
335
  }
331
336
  // ================================================================
332
337
  // get new prices, constrained by extremes
333
- // D27{USD/tok}
338
+ // D27{nanoUSD/tok}
334
339
  const newPrices = rebalance.initialPrices.map((initialPrice, i) => {
335
- // revert if price out of bounds
336
- const spotPrice = (0, numbers_1.bn)(prices[i].div(decimalScale[i]).mul(numbers_1.D27d));
340
+ // {nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
341
+ const spotPrice = (0, numbers_1.bn)(prices[i].mul(numbers_1.D9d).div(decimalScale[i]).mul(numbers_1.D27d));
337
342
  if (spotPrice < initialPrice.low || spotPrice > initialPrice.high) {
338
343
  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!`);
339
344
  }
340
345
  if (rebalance.priceControl == types_1.PriceControl.NONE) {
341
346
  return initialPrice;
342
347
  }
343
- // D27{USD/tok} = {USD/wholeTok} * D27 / {tok/wholeTok}
348
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {nanoUSD/USD} / {tok/wholeTok} * D27
344
349
  const pricesD27 = {
345
- low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).div(decimalScale[i]).mul(numbers_1.D27d)),
346
- high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).div(decimalScale[i]).mul(numbers_1.D27d)),
350
+ low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(numbers_1.D9d).div(decimalScale[i]).mul(numbers_1.D27d)),
351
+ high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(numbers_1.D9d).div(decimalScale[i]).mul(numbers_1.D27d)),
347
352
  };
348
353
  // low
349
354
  if (pricesD27.low < initialPrice.low) {
@@ -84,14 +84,11 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
84
84
  // === newPrices ===
85
85
  // D27{wholeTok/tok} = D27 / {tok/wholeTok}
86
86
  const priceMultiplier = numbers_1.D27d.div(new utils_1.Decimal(`1e${decimals[i]}`));
87
- // {USD/wholeTok} = {USD/wholeTok} * {1}
88
- const lowPrice = prices[i].mul(numbers_1.ONE.sub(priceError[i]));
89
- // {USD/wholeTok} = {USD/wholeTok} / {1}
90
- const highPrice = prices[i].div(numbers_1.ONE.sub(priceError[i]));
91
- // D27{USD/tok} = {USD/wholeTok} * D27{wholeTok/tok}
92
87
  newPrices.push({
93
- low: (0, numbers_1.bn)(lowPrice.mul(priceMultiplier)),
94
- high: (0, numbers_1.bn)(highPrice.mul(priceMultiplier)),
88
+ // D27{nanoUSD/tok} = {USD/wholeTok} * {1} * D27{wholeTok/tok} * {nanoUSD/USD}
89
+ low: (0, numbers_1.bn)(prices[i].mul(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d)),
90
+ // D27{nanoUSD/tok} = {USD/wholeTok} / {1} * D27{wholeTok/tok} * {nanoUSD/USD}
91
+ high: (0, numbers_1.bn)(prices[i].div(numbers_1.ONE.sub(priceError[i])).mul(priceMultiplier).mul(numbers_1.D9d)),
95
92
  });
96
93
  }
97
94
  // update low/high for tracking DTFs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.2.18",
3
+ "version": "0.3.0",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",