@reserve-protocol/dtf-rebalance-lib 0.0.10 → 0.0.11

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.
@@ -59,4 +59,4 @@ export declare const getTargetBasket: (_initialWeights: WeightRange[], _prices:
59
59
  * @param _priceError {1} Price error to use for each token during auction pricing; should be smaller than price error during startRebalance
60
60
  * @param _finalStageAt {1} The % rebalanced from the initial Folio to determine when is the final stage of the rebalance
61
61
  */
62
- export declare const getOpenAuction: (rebalance: Rebalance, _supply: bigint, _initialFolio: bigint[] | undefined, _targetBasket: bigint[] | undefined, _folio: bigint[], _decimals: bigint[], _prices: number[], _priceError: number[], _finalStageAt?: number) => [OpenAuctionArgs, AuctionMetrics];
62
+ export declare const getOpenAuction: (rebalance: Rebalance, _supply: bigint, _initialFolio: bigint[] | undefined, _targetBasket: bigint[] | undefined, _folio: bigint[], _decimals: bigint[], _prices: number[], _priceError: number[], _finalStageAt?: number, logging?: boolean) => [OpenAuctionArgs, AuctionMetrics];
@@ -59,7 +59,10 @@ exports.getTargetBasket = getTargetBasket;
59
59
  * @param _priceError {1} Price error to use for each token during auction pricing; should be smaller than price error during startRebalance
60
60
  * @param _finalStageAt {1} The % rebalanced from the initial Folio to determine when is the final stage of the rebalance
61
61
  */
62
- const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket = [], _folio, _decimals, _prices, _priceError, _finalStageAt = 0.9) => {
62
+ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket = [], _folio, _decimals, _prices, _priceError, _finalStageAt = 0.9, logging = false) => {
63
+ if (logging) {
64
+ console.log('getOpenAuction', rebalance, _supply, _initialFolio, _targetBasket, _folio, _decimals, _prices, _priceError, _finalStageAt);
65
+ }
63
66
  if (rebalance.tokens.length != _targetBasket.length ||
64
67
  _targetBasket.length != _folio.length ||
65
68
  _folio.length != _decimals.length ||
@@ -118,6 +121,10 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
118
121
  const buValue = weightRanges
119
122
  .map((weightRange, i) => weightRange.spot.mul(prices[i]))
120
123
  .reduce((a, b) => a.add(b));
124
+ if (logging) {
125
+ console.log('shareValue', shareValue.toString());
126
+ console.log('buValue', buValue.toString());
127
+ }
121
128
  // ================================================================
122
129
  // calculate rebalanceTarget
123
130
  const ejectionIndices = [];
@@ -184,6 +191,14 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
184
191
  throw new Error('something has gone very wrong');
185
192
  }
186
193
  }
194
+ if (logging) {
195
+ console.log('rebalanceTarget', rebalanceTarget.toString());
196
+ console.log('initialProgression', initialProgression.toString());
197
+ console.log('progression', progression.toString());
198
+ console.log('relativeProgression', relativeProgression.toString());
199
+ console.log('portionBeingEjected', portionBeingEjected.toString());
200
+ console.log('finalStageAt', finalStageAt.toString());
201
+ }
187
202
  // {1}
188
203
  const delta = numbers_1.ONE.sub(rebalanceTarget);
189
204
  // ================================================================
@@ -217,6 +232,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
217
232
  if (newLimits.high > rebalance.limits.high) {
218
233
  newLimits.high = rebalance.limits.high;
219
234
  }
235
+ if (logging) {
236
+ console.log('newLimits', newLimits);
237
+ }
220
238
  // ================================================================
221
239
  // get new weights, constrained by extremes
222
240
  // {wholeBU/wholeShare} = D18{BU/share} / D18
@@ -266,6 +284,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
266
284
  }
267
285
  return newWeightsD27;
268
286
  });
287
+ if (logging) {
288
+ console.log('newWeights', newWeights);
289
+ }
269
290
  // ================================================================
270
291
  // get new prices, constrained by extremes
271
292
  // D27{USD/tok}
@@ -302,6 +323,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
302
323
  }
303
324
  return pricesD27;
304
325
  });
326
+ if (logging) {
327
+ console.log('newPrices', newPrices);
328
+ }
305
329
  // ================================================================
306
330
  // calculate metrics
307
331
  // {USD} = {1} * {USD/wholeShare} * {wholeShare}
@@ -18,4 +18,4 @@ export interface StartRebalanceArgsPartial {
18
18
  * @param _dtfPrice {USD/wholeShare} DTF price
19
19
  * @param weightControl TRACKING=false, NATIVE=true
20
20
  */
21
- export declare const getStartRebalance: (_supply: bigint, tokens: string[], decimals: bigint[], _targetBasket: bigint[], _prices: number[], _priceError: number[], _dtfPrice: number, weightControl: boolean) => StartRebalanceArgsPartial;
21
+ export declare const getStartRebalance: (_supply: bigint, tokens: string[], decimals: bigint[], _targetBasket: bigint[], _prices: number[], _priceError: number[], _dtfPrice: number, weightControl: boolean, logging?: boolean) => StartRebalanceArgsPartial;
@@ -20,7 +20,10 @@ const numbers_1 = require("./numbers");
20
20
  * @param _dtfPrice {USD/wholeShare} DTF price
21
21
  * @param weightControl TRACKING=false, NATIVE=true
22
22
  */
23
- const getStartRebalance = (_supply, tokens, decimals, _targetBasket, _prices, _priceError, _dtfPrice, weightControl) => {
23
+ const getStartRebalance = (_supply, tokens, decimals, _targetBasket, _prices, _priceError, _dtfPrice, weightControl, logging = false) => {
24
+ if (logging) {
25
+ console.log('getStartRebalance', _supply, tokens, decimals, _targetBasket, _prices, _priceError, _dtfPrice, weightControl);
26
+ }
24
27
  // convert price number inputs to bigints
25
28
  // {USD/wholeTok}
26
29
  const prices = _prices.map((a) => new decimal_js_light_1.default(a.toString()));
@@ -53,6 +56,9 @@ const getStartRebalance = (_supply, tokens, decimals, _targetBasket, _prices, _p
53
56
  const spotWeight = targetBasket[i].mul(dtfPrice).div(prices[i]);
54
57
  // D27{tok/share}{wholeShare/wholeTok} = D27 * {tok/wholeTok} / {share/wholeShare}
55
58
  const limitMultiplier = numbers_1.D27d.mul(new decimal_js_light_1.default(`1e${decimals[i]}`)).div(numbers_1.D18d);
59
+ if (logging) {
60
+ console.log('limitMultiplier', limitMultiplier.toString());
61
+ }
56
62
  if (!weightControl) {
57
63
  // D27{tok/BU} = {wholeTok/wholeShare} * D27{tok/share}{wholeShare/wholeTok} / {BU/share}
58
64
  newWeights.push({
@@ -98,6 +104,11 @@ const getStartRebalance = (_supply, tokens, decimals, _targetBasket, _prices, _p
98
104
  newLimits.low = (0, numbers_1.bn)(numbers_1.ONE.div(numbers_1.ONE.add(totalPortion)).mul(numbers_1.D18d));
99
105
  newLimits.high = (0, numbers_1.bn)(numbers_1.ONE.add(totalPortion).mul(numbers_1.D18d));
100
106
  }
107
+ if (logging) {
108
+ console.log("newWeights", newWeights);
109
+ console.log('newPrices', newPrices);
110
+ console.log('newLimits', newLimits);
111
+ }
101
112
  return {
102
113
  weights: newWeights,
103
114
  prices: newPrices,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reserve-protocol/dtf-rebalance-lib",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "Rebalancing library for DTFs in typescript",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -110,8 +110,12 @@ export const getOpenAuction = (
110
110
  _decimals: bigint[],
111
111
  _prices: number[],
112
112
  _priceError: number[],
113
- _finalStageAt: number = 0.9
113
+ _finalStageAt: number = 0.9,
114
+ logging: boolean = false
114
115
  ): [OpenAuctionArgs, AuctionMetrics] => {
116
+ if (logging) {
117
+ console.log('getOpenAuction', rebalance, _supply, _initialFolio, _targetBasket, _folio, _decimals, _prices, _priceError, _finalStageAt)
118
+ }
115
119
 
116
120
  if (
117
121
  rebalance.tokens.length != _targetBasket.length ||
@@ -196,6 +200,10 @@ export const getOpenAuction = (
196
200
  .map((weightRange, i) => weightRange.spot.mul(prices[i]))
197
201
  .reduce((a, b) => a.add(b))
198
202
 
203
+ if (logging) {
204
+ console.log('shareValue', shareValue.toString())
205
+ console.log('buValue', buValue.toString())
206
+ }
199
207
 
200
208
  // ================================================================
201
209
 
@@ -252,20 +260,20 @@ export const getOpenAuction = (
252
260
  if (progression < initialProgression) {
253
261
  progression = initialProgression // don't go backwards
254
262
  }
255
-
263
+
256
264
 
257
265
  // {1} = {1} / {1}
258
266
  const relativeProgression = initialProgression.eq(ONE)
259
- ? ONE
260
- : progression.sub(initialProgression).div(ONE.sub(initialProgression))
261
-
267
+ ? ONE
268
+ : progression.sub(initialProgression).div(ONE.sub(initialProgression))
269
+
262
270
  let rebalanceTarget = ONE
263
271
  let round: AuctionRound = AuctionRound.FINAL
264
-
272
+
265
273
  // make it an eject auction if there is 1 bps or more of value to eject
266
274
  if (portionBeingEjected.gte(1e-4)) {
267
275
  round = AuctionRound.EJECT
268
-
276
+
269
277
  rebalanceTarget = progression.add(portionBeingEjected.mul(1.2)) // set rebalanceTarget to 20% more than needed to ensure ejection completes
270
278
  if (rebalanceTarget.gt(ONE)) {
271
279
  rebalanceTarget = ONE
@@ -273,12 +281,21 @@ export const getOpenAuction = (
273
281
  } else if (relativeProgression.lt(finalStageAt.sub(0.02))) {
274
282
  // wiggle room to prevent having to re-run an auction at the same stage after price movement
275
283
  round = AuctionRound.PROGRESS
276
-
284
+
277
285
  rebalanceTarget = initialProgression.add(ONE.sub(initialProgression).mul(finalStageAt))
278
286
  if (rebalanceTarget.gte(ONE)) {
279
287
  throw new Error('something has gone very wrong')
280
288
  }
281
289
  }
290
+
291
+ if (logging) {
292
+ console.log('rebalanceTarget', rebalanceTarget.toString())
293
+ console.log('initialProgression', initialProgression.toString())
294
+ console.log('progression', progression.toString())
295
+ console.log('relativeProgression', relativeProgression.toString())
296
+ console.log('portionBeingEjected', portionBeingEjected.toString())
297
+ console.log('finalStageAt', finalStageAt.toString())
298
+ }
282
299
 
283
300
  // {1}
284
301
  const delta = ONE.sub(rebalanceTarget)
@@ -322,6 +339,10 @@ export const getOpenAuction = (
322
339
  newLimits.high = rebalance.limits.high
323
340
  }
324
341
 
342
+ if (logging) {
343
+ console.log('newLimits', newLimits)
344
+ }
345
+
325
346
  // ================================================================
326
347
 
327
348
  // get new weights, constrained by extremes
@@ -381,6 +402,10 @@ export const getOpenAuction = (
381
402
  return newWeightsD27
382
403
  })
383
404
 
405
+ if (logging) {
406
+ console.log('newWeights', newWeights)
407
+ }
408
+
384
409
  // ================================================================
385
410
 
386
411
  // get new prices, constrained by extremes
@@ -432,6 +457,10 @@ export const getOpenAuction = (
432
457
  return pricesD27
433
458
  })
434
459
 
460
+ if (logging) {
461
+ console.log('newPrices', newPrices)
462
+ }
463
+
435
464
  // ================================================================
436
465
 
437
466
  // calculate metrics
@@ -36,8 +36,13 @@ export const getStartRebalance = (
36
36
  _prices: number[],
37
37
  _priceError: number[],
38
38
  _dtfPrice: number,
39
- weightControl: boolean
39
+ weightControl: boolean,
40
+ logging: boolean = false
40
41
  ): StartRebalanceArgsPartial => {
42
+ if (logging) {
43
+ console.log('getStartRebalance', _supply, tokens, decimals, _targetBasket, _prices, _priceError, _dtfPrice, weightControl)
44
+ }
45
+
41
46
  // convert price number inputs to bigints
42
47
 
43
48
  // {USD/wholeTok}
@@ -86,6 +91,10 @@ export const getStartRebalance = (
86
91
  // D27{tok/share}{wholeShare/wholeTok} = D27 * {tok/wholeTok} / {share/wholeShare}
87
92
  const limitMultiplier = D27d.mul(new Decimal(`1e${decimals[i]}`)).div(D18d)
88
93
 
94
+ if (logging) {
95
+ console.log('limitMultiplier', limitMultiplier.toString())
96
+ }
97
+
89
98
  if (!weightControl) {
90
99
  // D27{tok/BU} = {wholeTok/wholeShare} * D27{tok/share}{wholeShare/wholeTok} / {BU/share}
91
100
  newWeights.push({
@@ -125,22 +134,28 @@ export const getStartRebalance = (
125
134
  high: bn(highPrice.mul(priceMultiplier)),
126
135
  })
127
136
  }
128
-
137
+
129
138
  // update low/high for tracking DTFs
130
139
  if (!weightControl) {
131
140
  // sum of dot product of targetBasket and priceError
132
141
  const totalPortion = targetBasket
133
- .map((portion: Decimal, i: number) => portion.mul(priceError[i]))
134
- .reduce((a: Decimal, b: Decimal) => a.add(b))
135
-
142
+ .map((portion: Decimal, i: number) => portion.mul(priceError[i]))
143
+ .reduce((a: Decimal, b: Decimal) => a.add(b))
144
+
136
145
  if (totalPortion.gte(ONE)) {
137
146
  throw new Error('totalPortion > 1')
138
147
  }
139
-
148
+
140
149
  // D18{BU/share} = {1} * D18 * {BU/share}
141
150
  newLimits.low = bn(ONE.div(ONE.add(totalPortion)).mul(D18d))
142
151
  newLimits.high = bn(ONE.add(totalPortion).mul(D18d))
143
152
  }
153
+
154
+ if (logging) {
155
+ console.log("newWeights", newWeights)
156
+ console.log('newPrices', newPrices)
157
+ console.log('newLimits', newLimits)
158
+ }
144
159
 
145
160
  return {
146
161
  weights: newWeights,