@reserve-protocol/dtf-rebalance-lib 1.2.0 → 2.0.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.
@@ -58,12 +58,13 @@ export declare const getTargetBasket: (_initialWeights: WeightRange[], _prices:
58
58
  *
59
59
  * @param rebalance The result of calling folio.getRebalance(), today
60
60
  * @param _supply {share} The totalSupply() of the basket, today
61
- * @param _initialFolio D18{tok/share} Initial balances per share, e.g result of folio.toAssets(1e18, 0) at time rebalance was first proposed
61
+ * @param _initialSupply {share} The totalSupply() at time rebalance was first proposed
62
+ * @param _initialAssets {tok} Initial asset balances in the Folio, e.g result of folio.totalAssets() at time rebalance was first proposed
62
63
  * @param _targetBasket D18{1} Result of calling `getTargetBasket()`
63
- * @param _folio D18{tok/share} Current ratio of token per share, e.g result of folio.toAssets(1e18, 0), today
64
+ * @param _assets {tok} Current asset balances in the Folio, e.g result of folio.totalAssets(), today
64
65
  * @param _decimals Decimals of each token
65
66
  * @param _prices {USD/wholeTok} USD prices for each *whole* token, today
66
67
  * @param _priceError {1} Price error to use for each token during auction pricing; should be smaller than price error during startRebalance
67
68
  * @param _finalStageAt {1} The % rebalanced from the initial Folio to determine when is the final stage of the rebalance
68
69
  */
69
- export declare const getOpenAuction: (rebalance: Rebalance, _supply: bigint, _initialFolio: bigint[] | undefined, _targetBasket: bigint[] | undefined, _folio: bigint[], _decimals: bigint[], _prices: number[], _priceError: number[], _finalStageAt: number, debug?: boolean) => [OpenAuctionArgs, AuctionMetrics];
70
+ export declare const getOpenAuction: (rebalance: Rebalance, _supply: bigint, _initialSupply: bigint, _initialAssets: bigint[] | undefined, _targetBasket: bigint[] | undefined, _assets: bigint[], _decimals: bigint[], _prices: number[], _priceError: number[], _finalStageAt: number, debug?: boolean) => [OpenAuctionArgs, AuctionMetrics];
@@ -53,24 +53,25 @@ exports.getTargetBasket = getTargetBasket;
53
53
  *
54
54
  * @param rebalance The result of calling folio.getRebalance(), today
55
55
  * @param _supply {share} The totalSupply() of the basket, today
56
- * @param _initialFolio D18{tok/share} Initial balances per share, e.g result of folio.toAssets(1e18, 0) at time rebalance was first proposed
56
+ * @param _initialSupply {share} The totalSupply() at time rebalance was first proposed
57
+ * @param _initialAssets {tok} Initial asset balances in the Folio, e.g result of folio.totalAssets() at time rebalance was first proposed
57
58
  * @param _targetBasket D18{1} Result of calling `getTargetBasket()`
58
- * @param _folio D18{tok/share} Current ratio of token per share, e.g result of folio.toAssets(1e18, 0), today
59
+ * @param _assets {tok} Current asset balances in the Folio, e.g result of folio.totalAssets(), today
59
60
  * @param _decimals Decimals of each token
60
61
  * @param _prices {USD/wholeTok} USD prices for each *whole* token, today
61
62
  * @param _priceError {1} Price error to use for each token during auction pricing; should be smaller than price error during startRebalance
62
63
  * @param _finalStageAt {1} The % rebalanced from the initial Folio to determine when is the final stage of the rebalance
63
64
  */
64
- const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket = [], _folio, _decimals, _prices, _priceError, _finalStageAt, debug) => {
65
+ const getOpenAuction = (rebalance, _supply, _initialSupply, _initialAssets = [], _targetBasket = [], _assets, _decimals, _prices, _priceError, _finalStageAt, debug) => {
65
66
  if (debug === undefined) {
66
67
  debug = true;
67
68
  }
68
69
  if (debug) {
69
- console.log("getOpenAuction", rebalance, _supply, _initialFolio, _targetBasket, _folio, _decimals, _prices, _priceError, _finalStageAt);
70
+ console.log("getOpenAuction", rebalance, _supply, _initialAssets, _targetBasket, _assets, _decimals, _prices, _priceError, _finalStageAt);
70
71
  }
71
72
  if (rebalance.tokens.length != _targetBasket.length ||
72
- _targetBasket.length != _folio.length ||
73
- _folio.length != _decimals.length ||
73
+ _targetBasket.length != _assets.length ||
74
+ _assets.length != _decimals.length ||
74
75
  _decimals.length != _prices.length ||
75
76
  _prices.length != _priceError.length) {
76
77
  throw new Error("length mismatch");
@@ -81,6 +82,8 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
81
82
  // ================================================================
82
83
  // {wholeShare} = {share} / {share/wholeShare}
83
84
  const supply = new utils_1.Decimal(_supply.toString()).div(numbers_1.D18d);
85
+ // {wholeShare} = {share} / {share/wholeShare}
86
+ const initialSupply = new utils_1.Decimal(_initialSupply.toString()).div(numbers_1.D18d);
84
87
  // {1} = D18{1} / D18
85
88
  const targetBasket = _targetBasket.map((a) => new utils_1.Decimal(a.toString()).div(numbers_1.D18d));
86
89
  // {USD/wholeTok}
@@ -94,10 +97,10 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
94
97
  const priceError = _priceError.map((a) => new utils_1.Decimal(a.toString()));
95
98
  // {tok/wholeTok}
96
99
  const decimalScale = _decimals.map((a) => new utils_1.Decimal(`1e${a}`));
97
- // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
98
- const initialFolio = _initialFolio.map((c, i) => new utils_1.Decimal(c.toString()).div(decimalScale[i]));
99
- // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
100
- const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(decimalScale[i]));
100
+ // {wholeTok/wholeShare} = {tok} / {tok/wholeTok} / {wholeShare}
101
+ const initialFolio = _initialAssets.map((bal, i) => new utils_1.Decimal(bal.toString()).div(decimalScale[i]).div(initialSupply));
102
+ // {wholeTok/wholeShare} = {tok} / {tok/wholeTok} / {wholeShare}
103
+ const folio = _assets.map((bal, i) => new utils_1.Decimal(bal.toString()).div(decimalScale[i]).div(supply));
101
104
  // {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
102
105
  let weightRanges = rebalance.weights.map((range, i) => {
103
106
  return {
@@ -160,7 +163,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
160
163
  const maxSpotLimit = spotLimit.gt(prevSpotLimit) ? spotLimit : prevSpotLimit;
161
164
  // {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
162
165
  const expectedBalances = weightRanges.map((weightRange) => weightRange.spot.mul(maxSpotLimit));
163
- // absolute scale
166
+ // absolute
164
167
  // {1} = {USD/wholeShare} / {USD/wholeShare}
165
168
  let progression = folio
166
169
  .map((actualBalance, i) => {
@@ -174,7 +177,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
174
177
  })
175
178
  .reduce((a, b) => a.add(b))
176
179
  .div(shareValue);
177
- // absolute scale
180
+ // absolute
178
181
  // {1} = {USD/wholeShare} / {USD/wholeShare}
179
182
  const initialProgression = initialFolio
180
183
  .map((initialBalance, i) => {
@@ -198,7 +201,7 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
198
201
  let relativeProgression = initialProgression.eq(numbers_1.ONE)
199
202
  ? numbers_1.ONE
200
203
  : progression.sub(initialProgression).div(numbers_1.ONE.sub(initialProgression));
201
- let rebalanceTarget = numbers_1.ONE; // absolute scale
204
+ let rebalanceTarget = numbers_1.ONE; // absolute
202
205
  let round = AuctionRound.FINAL;
203
206
  if (debug) {
204
207
  console.log("initialProgression", initialProgression.toString());
@@ -372,7 +375,6 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
372
375
  }
373
376
  // ================================================================
374
377
  // calculate metrics
375
- // update Decimal weightRanges
376
378
  // {wholeTok/wholeBU} = D27{tok/BU} * {BU/wholeBU} / {tok/wholeTok} / D27
377
379
  weightRanges = newWeights.map((range, i) => {
378
380
  return {
@@ -9,9 +9,9 @@ export interface StartRebalanceArgsPartial {
9
9
  *
10
10
  * The `tokens` argument should be paired with the two return values and passed to `startRebalance()`
11
11
  *
12
- * @param _supply {share}
12
+ * @param _supply {share} Current total supply
13
13
  * @param tokens Addresses of tokens in the basket
14
- * @param _folio D18{tok/share} Folio of the basket
14
+ * @param _assets {tok} Current asset balances
15
15
  * @param decimals Decimals of each token
16
16
  * @param _targetBasket D18{1} Ideal basket
17
17
  * @param _prices {USD/wholeTok} USD prices for each *whole* token
@@ -20,4 +20,4 @@ export interface StartRebalanceArgsPartial {
20
20
  * @param weightControl TRACKING=false, NATIVE=true
21
21
  * @param deferWeights Whether to use the full range for weights, only possible for NATIVE DTFs
22
22
  */
23
- export declare const getStartRebalance: (_supply: bigint, tokens: string[], _folio: bigint[], decimals: bigint[], _targetBasket: bigint[], _prices: number[], _priceError: number[], weightControl: boolean, deferWeights: boolean, debug?: boolean) => StartRebalanceArgsPartial;
23
+ export declare const getStartRebalance: (_supply: bigint, tokens: string[], _assets: bigint[], decimals: bigint[], _targetBasket: bigint[], _prices: number[], _priceError: number[], weightControl: boolean, deferWeights: boolean, debug?: boolean) => StartRebalanceArgsPartial;
@@ -8,9 +8,9 @@ const numbers_1 = require("./numbers");
8
8
  *
9
9
  * The `tokens` argument should be paired with the two return values and passed to `startRebalance()`
10
10
  *
11
- * @param _supply {share}
11
+ * @param _supply {share} Current total supply
12
12
  * @param tokens Addresses of tokens in the basket
13
- * @param _folio D18{tok/share} Folio of the basket
13
+ * @param _assets {tok} Current asset balances
14
14
  * @param decimals Decimals of each token
15
15
  * @param _targetBasket D18{1} Ideal basket
16
16
  * @param _prices {USD/wholeTok} USD prices for each *whole* token
@@ -19,15 +19,17 @@ const numbers_1 = require("./numbers");
19
19
  * @param weightControl TRACKING=false, NATIVE=true
20
20
  * @param deferWeights Whether to use the full range for weights, only possible for NATIVE DTFs
21
21
  */
22
- const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl, deferWeights, debug) => {
22
+ const getStartRebalance = (_supply, tokens, _assets, decimals, _targetBasket, _prices, _priceError, weightControl, deferWeights, debug) => {
23
23
  if (debug) {
24
- console.log("getStartRebalance", _supply, tokens, _folio, decimals, _targetBasket, _prices, _priceError, weightControl, deferWeights);
24
+ console.log("getStartRebalance", _supply, tokens, _assets, decimals, _targetBasket, _prices, _priceError, weightControl, deferWeights);
25
25
  }
26
26
  if (deferWeights && !weightControl) {
27
27
  throw new Error("deferWeights is not supported for tracking DTFs");
28
28
  }
29
- // {wholeTok/wholeShare} = D18{tok/share} * {share/wholeShare} / {tok/wholeTok} / D18
30
- const folio = _folio.map((c, i) => new utils_1.Decimal(c.toString()).div(new utils_1.Decimal(`1e${decimals[i]}`)));
29
+ // {wholeShare} = {share} / {share/wholeShare}
30
+ const supply = new utils_1.Decimal(_supply.toString()).div(numbers_1.D18d);
31
+ // {wholeTok} = {tok} * {share/wholeShare} / {tok/wholeTok} / D18
32
+ const assets = _assets.map((c, i) => new utils_1.Decimal(c.toString()).div(new utils_1.Decimal(`1e${decimals[i]}`)));
31
33
  // convert price number inputs to bigints
32
34
  // {USD/wholeTok}
33
35
  const prices = _prices.map((a) => new utils_1.Decimal(a.toString()));
@@ -48,12 +50,12 @@ const getStartRebalance = (_supply, tokens, _folio, decimals, _targetBasket, _pr
48
50
  throw new Error("price error >= 1");
49
51
  }
50
52
  // === newWeights ===
51
- // {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
52
- const dtfPrice = folio
53
+ // {USD} = {wholeTok} * {USD/wholeTok}
54
+ const dtfValue = assets
53
55
  .map((f, i) => f.mul(prices[i]))
54
56
  .reduce((a, b) => a.add(b));
55
- // {wholeTok/wholeShare} = {1} * {USD/wholeShare} / {USD/wholeTok}
56
- const spotWeight = targetBasket[i].mul(dtfPrice).div(prices[i]);
57
+ // {wholeTok/wholeShare} = {1} * {USD} / {USD/wholeTok} / {wholeShare}
58
+ const spotWeight = targetBasket[i].mul(dtfValue).div(prices[i]).div(supply);
57
59
  // D27{tok/share}{wholeShare/wholeTok} = D27 * {tok/wholeTok} / {share/wholeShare}
58
60
  const limitMultiplier = numbers_1.D27d.mul(new utils_1.Decimal(`1e${decimals[i]}`)).div(numbers_1.D18d);
59
61
  if (debug) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",