impermax-sdk 2.1.134 → 2.1.136

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.
@@ -30,16 +30,22 @@ export default class UniswapV3Position {
30
30
  private getInitialRealY;
31
31
  private getValueGivenPriceAndAmounts;
32
32
  private getValueGivenAmounts;
33
+ private getAmountXGivenValue;
34
+ private getAmountYGivenValue;
33
35
  private getDebtValueGivenPrice;
34
36
  private getCollateralValueGivenPrice;
35
37
  private getEquityValueGivenPrice;
36
38
  private getDebtValue;
37
39
  private getCollateralValue;
38
40
  private getEquityValue;
41
+ private getLiquidityPostLiquidationValueGivenPriceAndDebt;
42
+ private isLiquidatableGivenPriceAndDebt;
43
+ private isLiquidatableGivenDebt;
39
44
  private isLiquidatableGivenPrice;
40
45
  private isUnderwaterGivenPrice;
41
46
  private getLiquidityGivenAmounts;
42
47
  private getLiquidationPriceInRange;
48
+ private getMaxDeltaDebtInRange;
43
49
  /**
44
50
  * PUBLIC SETTERS
45
51
  */
@@ -70,10 +76,18 @@ export default class UniswapV3Position {
70
76
  priceA: number;
71
77
  priceB: number;
72
78
  } | undefined;
73
- leverage(): number;
79
+ getLeverage(): number;
74
80
  getOptimalLiquidity(amountX: number, amountY: number): {
75
81
  liquidity: number;
76
82
  amountX: number;
77
83
  amountY: number;
78
84
  };
85
+ getMaxLeverage(): number;
86
+ getMinLeverage(): number;
87
+ getMaxWithdrawable(): {
88
+ amountX: number;
89
+ amountY: number;
90
+ };
91
+ getMaxBorrowableX(): number;
92
+ getMaxBorrowableY(): number;
79
93
  }
@@ -72,6 +72,12 @@ class UniswapV3Position {
72
72
  getValueGivenAmounts(amountX, amountY) {
73
73
  return amountX * this.marketPrice + amountY;
74
74
  }
75
+ getAmountXGivenValue(value) {
76
+ return value / this.marketPrice;
77
+ }
78
+ getAmountYGivenValue(value) {
79
+ return value;
80
+ }
75
81
  getDebtValueGivenPrice(price) {
76
82
  return this.getValueGivenPriceAndAmounts(price, this.debtX, this.debtY);
77
83
  }
@@ -90,11 +96,24 @@ class UniswapV3Position {
90
96
  getEquityValue() {
91
97
  return this.getEquityValueGivenPrice(this.marketPrice);
92
98
  }
99
+ getLiquidityPostLiquidationValueGivenPriceAndDebt(price, debtX, debtY) {
100
+ const debtValue = this.getValueGivenPriceAndAmounts(price, debtX, debtY);
101
+ const collateralValue = this.getCollateralValueGivenPrice(price);
102
+ const collateralNeeded = debtValue * this.liquidationPenalty;
103
+ return collateralValue - collateralNeeded;
104
+ }
105
+ isLiquidatableGivenPriceAndDebt(price, debtX, debtY) {
106
+ return this.getLiquidityPostLiquidationValueGivenPriceAndDebt(price / this.safetyMargin, debtX, debtY) < 0
107
+ || this.getLiquidityPostLiquidationValueGivenPriceAndDebt(price * this.safetyMargin, debtX, debtY) < 0;
108
+ }
109
+ isLiquidatableGivenDebt(debtX, debtY) {
110
+ return this.isLiquidatableGivenPriceAndDebt(this.marketPrice, debtX, debtY);
111
+ }
93
112
  isLiquidatableGivenPrice(price) {
94
- return this.getEquityValueGivenPrice(price / this.safetyMargin) < 0 || this.getEquityValueGivenPrice(price * this.safetyMargin) < 0;
113
+ return this.isLiquidatableGivenPriceAndDebt(price, this.debtX, this.debtY);
95
114
  }
96
115
  isUnderwaterGivenPrice(price) {
97
- return this.getEquityValueGivenPrice(price) < 0;
116
+ return this.getLiquidityPostLiquidationValueGivenPriceAndDebt(price, this.debtX, this.debtY) < 0;
98
117
  }
99
118
  getLiquidityGivenAmounts(amountX, amountY) {
100
119
  if (amountX == 0) {
@@ -105,6 +124,7 @@ class UniswapV3Position {
105
124
  }
106
125
  return amountX / (1 / Math.sqrt(this.marketPrice) - 1 / Math.sqrt(this.priceB));
107
126
  }
127
+ // divide and conquer implementation
108
128
  getLiquidationPriceInRange(lowPrice, highPrice, lowIsLiquidatable, highIsLiquidatable) {
109
129
  var _a;
110
130
  if (lowIsLiquidatable == highIsLiquidatable)
@@ -115,6 +135,17 @@ class UniswapV3Position {
115
135
  const avgIsLiquidatable = this.isLiquidatableGivenPrice(avgPrice);
116
136
  return (_a = this.getLiquidationPriceInRange(lowPrice, avgPrice, lowIsLiquidatable, avgIsLiquidatable)) !== null && _a !== void 0 ? _a : this.getLiquidationPriceInRange(avgPrice, highPrice, avgIsLiquidatable, highIsLiquidatable);
117
137
  }
138
+ // divide and conquer implementation
139
+ getMaxDeltaDebtInRange(lowDeltaX, lowDeltaY, highDeltaX, highDeltaY) {
140
+ if (Math.abs(highDeltaX / lowDeltaX) < 1.001 || Math.abs(highDeltaY / lowDeltaY) < 1.001)
141
+ return [lowDeltaX, lowDeltaY];
142
+ const avgDeltaX = (lowDeltaX + highDeltaX) / 2;
143
+ const avgDeltaY = (lowDeltaY + highDeltaY) / 2;
144
+ if (this.isLiquidatableGivenDebt(this.debtX + avgDeltaX, this.debtY + avgDeltaY))
145
+ return this.getMaxDeltaDebtInRange(lowDeltaX, lowDeltaY, avgDeltaX, avgDeltaY);
146
+ else
147
+ return this.getMaxDeltaDebtInRange(avgDeltaX, avgDeltaY, highDeltaX, highDeltaY);
148
+ }
118
149
  /**
119
150
  * PUBLIC SETTERS
120
151
  */
@@ -205,7 +236,7 @@ class UniswapV3Position {
205
236
  priceB: priceB ? priceB : Infinity,
206
237
  };
207
238
  }
208
- leverage() {
239
+ getLeverage() {
209
240
  return this.getCollateralValue() / this.getEquityValue();
210
241
  }
211
242
  // amountX and amountY are expected to have the same sign
@@ -227,5 +258,66 @@ class UniswapV3Position {
227
258
  const liquidity = this.getLiquidityGivenAmounts(amountX, amountY);
228
259
  return { liquidity, amountX, amountY };
229
260
  }
261
+ getMaxLeverage() {
262
+ const currentLeverage = this.getLeverage();
263
+ if (this.isLiquidatable())
264
+ return currentLeverage;
265
+ const highLeverage = 100; // we assume the position is liquidatable with this leverage
266
+ const realX = this.getRealX();
267
+ const realY = this.getRealY();
268
+ const projectedRealX = realX * highLeverage / currentLeverage;
269
+ const projectedRealY = realY * highLeverage / currentLeverage;
270
+ const projectedDebtX = this.debtX + projectedRealX - realX;
271
+ const projectedDebtY = this.debtY + projectedRealY - realY;
272
+ const normalizedDeltaDebtX = projectedDebtX * currentLeverage / highLeverage - this.debtX;
273
+ const normalizedDeltaDebtY = projectedDebtY * currentLeverage / highLeverage - this.debtY;
274
+ const [deltaDebtX, deltaDebtY] = this.getMaxDeltaDebtInRange(0, 0, normalizedDeltaDebtX, normalizedDeltaDebtY);
275
+ const ratio = realX > 0 ? deltaDebtX / normalizedDeltaDebtX : deltaDebtY / normalizedDeltaDebtY;
276
+ return currentLeverage + (highLeverage - currentLeverage) * ratio;
277
+ }
278
+ getMinLeverage() {
279
+ const realX = this.getRealX();
280
+ const realY = this.getRealY();
281
+ if (realX > this.debtX && realY > this.debtY)
282
+ return 0;
283
+ // TODO binary search
284
+ return 0;
285
+ }
286
+ getMaxWithdrawable() {
287
+ const realX = this.getRealX();
288
+ const realY = this.getRealY();
289
+ let percentage = 0;
290
+ if (!this.isLiquidatable()) {
291
+ if (this.debtX == 0 && this.debtY == 0) {
292
+ percentage = 1;
293
+ }
294
+ else {
295
+ const highRatio = this.getEquityValue() / this.getDebtValue();
296
+ const [deltaDebtX, deltaDebtY] = this.getMaxDeltaDebtInRange(0, 0, this.debtX * highRatio, this.debtY * highRatio);
297
+ const ratio = this.debtX > 0 ? deltaDebtX / this.debtX : deltaDebtY / this.debtY;
298
+ percentage = 1 / (1 / ratio + 1);
299
+ }
300
+ }
301
+ return {
302
+ amountX: realX * percentage,
303
+ amountY: realY * percentage,
304
+ };
305
+ }
306
+ getMaxBorrowableX() {
307
+ if (this.isLiquidatable())
308
+ return 0;
309
+ const equityValue = this.getEquityValue();
310
+ const highDebtX = this.getAmountXGivenValue(equityValue);
311
+ const [debtX,] = this.getMaxDeltaDebtInRange(0, 0, highDebtX, 0);
312
+ return debtX;
313
+ }
314
+ getMaxBorrowableY() {
315
+ if (this.isLiquidatable())
316
+ return 0;
317
+ const equityValue = this.getEquityValue();
318
+ const highDebtY = this.getAmountYGivenValue(equityValue);
319
+ const [, debtY] = this.getMaxDeltaDebtInRange(0, 0, 0, highDebtY);
320
+ return debtY;
321
+ }
230
322
  }
231
323
  exports.default = UniswapV3Position;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.134",
3
+ "version": "2.1.136",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",