@reserve-protocol/dtf-rebalance-lib 2.0.2 → 2.1.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.
@@ -158,8 +158,11 @@ 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;
161
164
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
162
- const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(spotLimit));
165
+ const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(maxSpotLimit));
163
166
  // absolute
164
167
  // {1} = {USD/wholeShare} / {USD/wholeShare}
165
168
  let progression = folio
package/dist/utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import DecimalLight from "decimal.js-light";
2
+ import { WeightRange, RebalanceLimits } from "./types";
2
3
  export declare const Decimal: typeof DecimalLight;
3
4
  /**
4
5
  * This function can be used to get a basket distribution EITHER from a set of historical basket weights
@@ -10,3 +11,15 @@ export declare const Decimal: typeof DecimalLight;
10
11
  * @returns D18{1} Current basket, total will be around 1e18 but not exactly
11
12
  */
12
13
  export declare const getBasketDistribution: (_bals: bigint[], _prices: number[], decimals: bigint[]) => bigint[];
14
+ /**
15
+ * Calculate basket deviation basket on balances and current weights
16
+ *
17
+ * @param supply {share} Current total supply
18
+ * @param _bals {tok} Current balances
19
+ * @param _prices {USD/wholeTok} Current USD prices for each *whole* token
20
+ * @param decimals Decimals of each token
21
+ * @param weights Current weights from getRebalance.weights
22
+ * @param limits Current limits from getRebalance.limits
23
+ * @returns {1} Basket deviation
24
+ */
25
+ export declare const getBasketDeviation: (supply: bigint, _bals: bigint[], _prices: number[], decimals: bigint[], weights: WeightRange[], limits: RebalanceLimits) => number;
package/dist/utils.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getBasketDistribution = exports.Decimal = void 0;
6
+ exports.getBasketDeviation = exports.getBasketDistribution = exports.Decimal = void 0;
7
7
  const decimal_js_light_1 = __importDefault(require("decimal.js-light"));
8
8
  const numbers_1 = require("./numbers");
9
9
  // Create a local Decimal constructor with custom precision
@@ -29,3 +29,34 @@ const getBasketDistribution = (_bals, _prices, decimals) => {
29
29
  return bals.map((bal, i) => (0, numbers_1.bn)(bal.mul(prices[i]).div(totalValue).mul(numbers_1.D18d)));
30
30
  };
31
31
  exports.getBasketDistribution = getBasketDistribution;
32
+ /**
33
+ * Calculate basket deviation basket on balances and current weights
34
+ *
35
+ * @param supply {share} Current total supply
36
+ * @param _bals {tok} Current balances
37
+ * @param _prices {USD/wholeTok} Current USD prices for each *whole* token
38
+ * @param decimals Decimals of each token
39
+ * @param weights Current weights from getRebalance.weights
40
+ * @param limits Current limits from getRebalance.limits
41
+ * @returns {1} Basket deviation
42
+ */
43
+ const getBasketDeviation = (supply, _bals, _prices, decimals, weights, limits) => {
44
+ const decimalScale = decimals.map((d) => new exports.Decimal(`1e${d}`));
45
+ // {USD/wholeTok} = {USD/wholeTok}
46
+ const prices = _prices.map((a) => new exports.Decimal(a.toString()));
47
+ // {USD}
48
+ let totalValue = numbers_1.ZERO;
49
+ let surplusValue = numbers_1.ZERO;
50
+ for (let i = 0; i < weights.length; i++) {
51
+ // {tok} = D27{tok/BU} * D18{BU/share} * {share} / D27 / D18
52
+ const expectedBal = (weights[i].spot * limits.spot * supply) / numbers_1.D27n / numbers_1.D18n;
53
+ if (_bals[i] > expectedBal) {
54
+ // {USD} = {tok} * {USD/wholeTok} / {tok/wholeTok}
55
+ surplusValue = surplusValue.add(new exports.Decimal((_bals[i] - expectedBal).toString()).mul(prices[i]).div(decimalScale[i]));
56
+ // {USD} = {tok} * {USD/wholeTok} / {tok/wholeTok}
57
+ totalValue = totalValue.add(new exports.Decimal(_bals[i].toString()).mul(prices[i])).div(decimalScale[i]);
58
+ }
59
+ }
60
+ return surplusValue.div(totalValue).toNumber();
61
+ };
62
+ exports.getBasketDeviation = getBasketDeviation;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",