impermax-sdk 2.1.133 → 2.1.134

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.
@@ -6,6 +6,9 @@ export default class UniswapV3Position {
6
6
  liquidity: number;
7
7
  debtX: number;
8
8
  debtY: number;
9
+ initialLiquidity: number;
10
+ initialDebtX: number;
11
+ initialDebtY: number;
9
12
  marketPrice: number;
10
13
  oraclePrice: number;
11
14
  priceA: number;
@@ -23,11 +26,16 @@ export default class UniswapV3Position {
23
26
  private getRealYGivenLiquidity;
24
27
  private getRealXGivenPrice;
25
28
  private getRealYGivenPrice;
29
+ private getInitialRealX;
30
+ private getInitialRealY;
26
31
  private getValueGivenPriceAndAmounts;
27
32
  private getValueGivenAmounts;
28
33
  private getDebtValueGivenPrice;
29
34
  private getCollateralValueGivenPrice;
30
35
  private getEquityValueGivenPrice;
36
+ private getDebtValue;
37
+ private getCollateralValue;
38
+ private getEquityValue;
31
39
  private isLiquidatableGivenPrice;
32
40
  private isUnderwaterGivenPrice;
33
41
  private getLiquidityGivenAmounts;
@@ -41,6 +49,14 @@ export default class UniswapV3Position {
41
49
  setRealY(amountY: number): void;
42
50
  setDebtX(_debtX: number): void;
43
51
  setDebtY(_debtY: number): void;
52
+ depositX(amount: number): void;
53
+ depositY(amount: number): void;
54
+ withdrawX(amount: number): void;
55
+ withdrawY(amount: number): void;
56
+ borrowX(amount: number): void;
57
+ borrowY(amount: number): void;
58
+ repayX(amount: number): void;
59
+ repayY(amount: number): void;
44
60
  /**
45
61
  * PUBLIC GETTERS
46
62
  */
@@ -54,7 +70,7 @@ export default class UniswapV3Position {
54
70
  priceA: number;
55
71
  priceB: number;
56
72
  } | undefined;
57
- leverage(): void;
73
+ leverage(): number;
58
74
  getOptimalLiquidity(amountX: number, amountY: number): {
59
75
  liquidity: number;
60
76
  amountX: number;
@@ -16,12 +16,17 @@ function getRealY(liquidity, price, priceA, priceB) {
16
16
  const virtualY = getVirtualY(liquidity, Math.min(price, priceB));
17
17
  return Math.max(virtualY - surplusY, 0);
18
18
  }
19
+ const LOWEST_PRICE = 1 / 1e18;
20
+ const HIGHEST_PRICE = 1e18;
19
21
  class UniswapV3Position {
20
22
  // TODO add fee logic
21
23
  constructor(_liquidity, _debtX, _debtY, _marketPrice, _oraclePrice, _priceA, _priceB, _safetyMargin, _liquidationPenalty, _lockStateChange) {
22
24
  this.liquidity = _liquidity;
23
25
  this.debtX = _debtX;
24
26
  this.debtY = _debtY;
27
+ this.initialLiquidity = _liquidity;
28
+ this.initialDebtX = _debtX;
29
+ this.initialDebtY = _debtY;
25
30
  this.marketPrice = _marketPrice;
26
31
  this.oraclePrice = _oraclePrice;
27
32
  this.priceA = _priceA;
@@ -55,6 +60,12 @@ class UniswapV3Position {
55
60
  getRealYGivenPrice(price) {
56
61
  return this.getRealYGivenLiquidityAndPrice(this.liquidity, price);
57
62
  }
63
+ getInitialRealX() {
64
+ return this.getRealXGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
65
+ }
66
+ getInitialRealY() {
67
+ return this.getRealYGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
68
+ }
58
69
  getValueGivenPriceAndAmounts(price, amountX, amountY) {
59
70
  return amountX * price + amountY;
60
71
  }
@@ -70,6 +81,15 @@ class UniswapV3Position {
70
81
  getEquityValueGivenPrice(price) {
71
82
  return this.getCollateralValueGivenPrice(price) - this.getDebtValueGivenPrice(price);
72
83
  }
84
+ getDebtValue() {
85
+ return this.getDebtValueGivenPrice(this.marketPrice);
86
+ }
87
+ getCollateralValue() {
88
+ return this.getCollateralValueGivenPrice(this.marketPrice);
89
+ }
90
+ getEquityValue() {
91
+ return this.getEquityValueGivenPrice(this.marketPrice);
92
+ }
73
93
  isLiquidatableGivenPrice(price) {
74
94
  return this.getEquityValueGivenPrice(price / this.safetyMargin) < 0 || this.getEquityValueGivenPrice(price * this.safetyMargin) < 0;
75
95
  }
@@ -85,18 +105,20 @@ class UniswapV3Position {
85
105
  }
86
106
  return amountX / (1 / Math.sqrt(this.marketPrice) - 1 / Math.sqrt(this.priceB));
87
107
  }
88
- getLiquidationPriceInRange(lowPrice, highPrice) {
108
+ getLiquidationPriceInRange(lowPrice, highPrice, lowIsLiquidatable, highIsLiquidatable) {
89
109
  var _a;
90
- if (this.isLiquidatableGivenPrice(lowPrice) == this.isLiquidatableGivenPrice(highPrice))
110
+ if (lowIsLiquidatable == highIsLiquidatable)
91
111
  return undefined;
92
- const meanPrice = Math.sqrt(lowPrice * highPrice);
112
+ const avgPrice = Math.sqrt(lowPrice * highPrice);
93
113
  if (lowPrice / highPrice > 0.9999)
94
- return meanPrice;
95
- return (_a = this.getLiquidationPriceInRange(lowPrice, meanPrice)) !== null && _a !== void 0 ? _a : this.getLiquidationPriceInRange(meanPrice, highPrice);
114
+ return avgPrice;
115
+ const avgIsLiquidatable = this.isLiquidatableGivenPrice(avgPrice);
116
+ return (_a = this.getLiquidationPriceInRange(lowPrice, avgPrice, lowIsLiquidatable, avgIsLiquidatable)) !== null && _a !== void 0 ? _a : this.getLiquidationPriceInRange(avgPrice, highPrice, avgIsLiquidatable, highIsLiquidatable);
96
117
  }
97
118
  /**
98
119
  * PUBLIC SETTERS
99
120
  */
121
+ // Setters
100
122
  setLiquidity(_liquidity) {
101
123
  this.checkLock();
102
124
  this.liquidity = _liquidity;
@@ -124,6 +146,31 @@ class UniswapV3Position {
124
146
  this.checkLock();
125
147
  this.debtY = _debtY;
126
148
  }
149
+ // Actions
150
+ depositX(amount) {
151
+ this.setRealX(this.getInitialRealX() + amount);
152
+ }
153
+ depositY(amount) {
154
+ this.setRealY(this.getInitialRealY() + amount);
155
+ }
156
+ withdrawX(amount) {
157
+ this.setRealX(Math.max(this.getInitialRealX() - amount, 0));
158
+ }
159
+ withdrawY(amount) {
160
+ this.setRealY(Math.max(this.getInitialRealY() - amount, 0));
161
+ }
162
+ borrowX(amount) {
163
+ this.setDebtX(this.initialDebtX + amount);
164
+ }
165
+ borrowY(amount) {
166
+ this.setDebtY(this.initialDebtY + amount);
167
+ }
168
+ repayX(amount) {
169
+ this.setDebtX(Math.max(this.initialDebtX - amount, 0));
170
+ }
171
+ repayY(amount) {
172
+ this.setDebtY(Math.max(this.initialDebtY - amount, 0));
173
+ }
127
174
  /**
128
175
  * PUBLIC GETTERS
129
176
  */
@@ -146,16 +193,20 @@ class UniswapV3Position {
146
193
  return this.isUnderwaterGivenPrice(this.marketPrice);
147
194
  }
148
195
  getLiquidationRange() {
149
- if (this.isLiquidatable())
196
+ const isLiquidatable = this.isLiquidatable();
197
+ if (isLiquidatable)
150
198
  return undefined;
151
- const priceA = this.getLiquidationPriceInRange(1 / 1e18, this.marketPrice);
152
- const priceB = this.getLiquidationPriceInRange(this.marketPrice, 1e18);
199
+ const lowIsLiquidatable = this.isLiquidatableGivenPrice(LOWEST_PRICE);
200
+ const highIsLiquidatable = this.isLiquidatableGivenPrice(HIGHEST_PRICE);
201
+ const priceA = this.getLiquidationPriceInRange(LOWEST_PRICE, this.marketPrice, lowIsLiquidatable, isLiquidatable);
202
+ const priceB = this.getLiquidationPriceInRange(this.marketPrice, HIGHEST_PRICE, isLiquidatable, highIsLiquidatable);
153
203
  return {
154
204
  priceA: priceA ? priceA : 0,
155
205
  priceB: priceB ? priceB : Infinity,
156
206
  };
157
207
  }
158
208
  leverage() {
209
+ return this.getCollateralValue() / this.getEquityValue();
159
210
  }
160
211
  // amountX and amountY are expected to have the same sign
161
212
  getOptimalLiquidity(amountX, amountY) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.133",
3
+ "version": "2.1.134",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",