@reserve-protocol/dtf-rebalance-lib 0.2.1 → 0.2.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.
- package/dist/open-auction.js +43 -18
- package/package.json +1 -1
package/dist/open-auction.js
CHANGED
@@ -106,25 +106,35 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
106
106
|
// {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
|
107
107
|
const shareValue = folio
|
108
108
|
.map((f, i) => {
|
109
|
+
if (!rebalance.inRebalance[i]) {
|
110
|
+
return numbers_1.ZERO;
|
111
|
+
}
|
109
112
|
return f.mul(prices[i]);
|
110
113
|
})
|
111
114
|
.reduce((a, b) => a.add(b));
|
112
115
|
// {USD/wholeBU} = {wholeTok/wholeBU} * {USD/wholeTok}
|
113
|
-
const buValue = weightRanges
|
116
|
+
const buValue = weightRanges
|
117
|
+
.map((weightRange, i) => {
|
118
|
+
if (!rebalance.inRebalance[i]) {
|
119
|
+
return numbers_1.ZERO;
|
120
|
+
}
|
121
|
+
return weightRange.spot.mul(prices[i]);
|
122
|
+
})
|
123
|
+
.reduce((a, b) => a.add(b));
|
114
124
|
const buPriceChange = buValue.sub(shareValue).abs().div(shareValue);
|
115
|
-
console.log(` 🧺 ${buPriceChange.mul(100).toFixed(2)}% price
|
125
|
+
console.log(` 🧺 ${buPriceChange.mul(100).toFixed(2)}% basket price difference`);
|
116
126
|
if (debug) {
|
117
127
|
console.log("shareValue", shareValue.toString());
|
118
128
|
console.log("buValue", buValue.toString());
|
119
129
|
}
|
120
130
|
if (buValue.div(shareValue).gt(10) || shareValue.div(buValue).gt(10)) {
|
121
|
-
throw new Error("buValue and shareValue are too different");
|
131
|
+
throw new Error("buValue and shareValue are too different, something probably went wrong");
|
122
132
|
}
|
123
133
|
// ================================================================
|
124
134
|
// calculate rebalanceTarget
|
125
135
|
const ejectionIndices = [];
|
126
136
|
for (let i = 0; i < rebalance.weights.length; i++) {
|
127
|
-
if (rebalance.weights[i].spot == 0n) {
|
137
|
+
if (rebalance.inRebalance[i] && rebalance.weights[i].spot == 0n) {
|
128
138
|
ejectionIndices.push(i);
|
129
139
|
}
|
130
140
|
}
|
@@ -138,24 +148,31 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
138
148
|
// {1} = {USD/wholeShare} / {USD/wholeShare}
|
139
149
|
let progression = folio
|
140
150
|
.map((actualBalance, i) => {
|
151
|
+
if (!rebalance.inRebalance[i]) {
|
152
|
+
return numbers_1.ZERO;
|
153
|
+
}
|
141
154
|
// {wholeTok/wholeShare} = {USD/wholeShare} * {1} / {USD/wholeTok}
|
142
155
|
const balanceExpected = shareValue.mul(targetBasket[i]).div(prices[i]);
|
143
156
|
// {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
|
144
|
-
const
|
157
|
+
const balanceInBasket = balanceExpected.gt(actualBalance) ? actualBalance : balanceExpected;
|
145
158
|
// {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
|
146
|
-
return
|
159
|
+
return balanceInBasket.mul(prices[i]);
|
147
160
|
})
|
148
161
|
.reduce((a, b) => a.add(b))
|
149
162
|
.div(shareValue);
|
150
163
|
// {1} = {USD/wholeShare} / {USD/wholeShare}
|
151
164
|
const initialProgression = initialFolio
|
152
165
|
.map((initialBalance, i) => {
|
166
|
+
// make sure to include tokens that were already ejected
|
167
|
+
if (!rebalance.inRebalance[i] && rebalance.weights[i].spot > 0n) {
|
168
|
+
return numbers_1.ZERO;
|
169
|
+
}
|
153
170
|
// {wholeTok/wholeShare} = {USD/wholeShare} * {1} / {USD/wholeTok}
|
154
171
|
const balanceExpected = shareValue.mul(targetBasket[i]).div(prices[i]);
|
155
172
|
// {wholeTok/wholeShare} = {wholeTok/wholeBU} * {wholeBU/wholeShare}
|
156
|
-
const
|
173
|
+
const balanceInBasket = balanceExpected.gt(initialBalance) ? initialBalance : balanceExpected;
|
157
174
|
// {USD/wholeShare} = {wholeTok/wholeShare} * {USD/wholeTok}
|
158
|
-
return
|
175
|
+
return balanceInBasket.mul(prices[i]);
|
159
176
|
})
|
160
177
|
.reduce((a, b) => a.add(b))
|
161
178
|
.div(shareValue);
|
@@ -354,8 +371,6 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
354
371
|
}
|
355
372
|
// ================================================================
|
356
373
|
// calculate metrics
|
357
|
-
// {USD} = {1} * {USD/wholeShare} * {wholeShare}
|
358
|
-
let valueBeingTraded = rebalanceTarget.sub(progression).mul(shareValue).mul(supply);
|
359
374
|
const surplusTokens = [];
|
360
375
|
const deficitTokens = [];
|
361
376
|
// update Decimal weightRanges
|
@@ -367,6 +382,9 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
367
382
|
high: new decimal_js_light_1.default(range.high.toString()).mul(numbers_1.D18d).div(decimalScale[i]).div(numbers_1.D27d),
|
368
383
|
};
|
369
384
|
});
|
385
|
+
// {USD}
|
386
|
+
let surplusValue = numbers_1.ZERO;
|
387
|
+
let deficitValue = numbers_1.ZERO;
|
370
388
|
rebalance.tokens.forEach((token, i) => {
|
371
389
|
if (!rebalance.inRebalance[i]) {
|
372
390
|
return;
|
@@ -376,20 +394,27 @@ const getOpenAuction = (rebalance, _supply, _initialFolio = [], _targetBasket =
|
|
376
394
|
const sellDownTo = weightRanges[i].high.mul(actualLimits.high);
|
377
395
|
if (folio[i].lt(buyUpTo)) {
|
378
396
|
deficitTokens.push(token);
|
397
|
+
// {USD} += {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
|
398
|
+
deficitValue = deficitValue.add(buyUpTo.sub(folio[i]).mul(prices[i]).mul(supply));
|
379
399
|
}
|
380
400
|
else if (folio[i].gt(sellDownTo)) {
|
381
401
|
surplusTokens.push(token);
|
402
|
+
// {USD} += {wholeTok/wholeShare} * {USD/wholeTok} * {wholeShare}
|
403
|
+
surplusValue = surplusValue.add(folio[i].sub(sellDownTo).mul(prices[i]).mul(supply));
|
382
404
|
}
|
383
405
|
});
|
384
|
-
//
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
406
|
+
// {USD}
|
407
|
+
const valueBeingTraded = surplusValue.gt(deficitValue) ? deficitValue : surplusValue;
|
408
|
+
// // completed if nothing to trade
|
409
|
+
// if (valueBeingTraded.eq(ZERO)) {
|
410
|
+
// round = AuctionRound.FINAL;
|
411
|
+
// rebalanceTarget = ONE;
|
412
|
+
// relativeProgression = ONE;
|
413
|
+
// progression = ONE;
|
414
|
+
// }
|
415
|
+
// // TODO maybe add back
|
391
416
|
// ================================================================
|
392
|
-
//
|
417
|
+
// filter tokens that are not in the rebalance
|
393
418
|
const returnTokens = [];
|
394
419
|
const returnWeights = [];
|
395
420
|
const returnPrices = [];
|