impermax-sdk 2.1.169 → 2.1.170
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/lib/offchain/account/lendingPool/nftlp/offchainAccountNftlpUniswapV3.js +3 -2
- package/lib/onchain/account/lendingPool/nftlp/onchainAccountNftlpUniswapV3.js +3 -2
- package/lib/utils/position/interface.d.ts +2 -7
- package/lib/utils/position/uniswapV3/index.d.ts +24 -9
- package/lib/utils/position/uniswapV3/index.js +110 -70
- package/package.json +1 -1
|
@@ -46,11 +46,12 @@ class OffchainAccountNftlpUniswapV3 extends offchainAccountNftlp_1.default {
|
|
|
46
46
|
return (0, uniswapV3General_1.formatUniV3Liquidity)(liquidity, decimalsA, decimalsB);
|
|
47
47
|
}
|
|
48
48
|
async createPositionObject(tokenId, lockStateChange = true) {
|
|
49
|
-
return new uniswapV3_1.default(await this.getLiquidity(tokenId),
|
|
49
|
+
return new uniswapV3_1.default(await this.getLiquidity(tokenId), 0, // TODO uncollectedFees
|
|
50
|
+
0, await this.getLendingPool().getBorrowableA().getBorrowedAmount(tokenId), await this.getLendingPool().getBorrowableB().getBorrowedAmount(tokenId), await this.getNftlp().getMarketPrice(), await this.getNftlp().getOraclePrice(), await this.getPriceA(tokenId), await this.getPriceB(tokenId), await this.getLendingPool().getSafetyMargin(), await this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
50
51
|
}
|
|
51
52
|
async createNewPositionObject(fee, priceA, priceB, lockStateChange = false) {
|
|
52
53
|
// TODO check for priceA/priceB sanity
|
|
53
|
-
return new uniswapV3_1.default(0, 0, 0, await this.getNftlp().getMarketPrice(), await this.getNftlp().getOraclePrice(), priceA, priceB, await this.getLendingPool().getSafetyMargin(), await this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
54
|
+
return new uniswapV3_1.default(0, 0, 0, 0, 0, await this.getNftlp().getMarketPrice(), await this.getNftlp().getOraclePrice(), priceA, priceB, await this.getLendingPool().getSafetyMargin(), await this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
54
55
|
}
|
|
55
56
|
async getPositionObject(tokenId) {
|
|
56
57
|
if (!this.cache.positionsObject)
|
|
@@ -45,11 +45,12 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
|
|
|
45
45
|
return (0, uniswapV3General_1.formatUniV3Liquidity)(liquidity, decimalsA, decimalsB);
|
|
46
46
|
}
|
|
47
47
|
async createPositionObject(tokenId, lockStateChange = true) {
|
|
48
|
-
return new uniswapV3_1.default(await this.getLiquidity(tokenId),
|
|
48
|
+
return new uniswapV3_1.default(await this.getLiquidity(tokenId), 0, // TODO uncollectedFees
|
|
49
|
+
0, await this.getLendingPool().getBorrowableA().getBorrowed(tokenId), await this.getLendingPool().getBorrowableB().getBorrowed(tokenId), await this.getNftlp().getMarketPrice(), await this.getNftlp().getOraclePrice(), await this.getPriceA(tokenId), await this.getPriceB(tokenId), await this.getLendingPool().getSafetyMargin(), await this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
49
50
|
}
|
|
50
51
|
async createNewPositionObject(fee, priceA, priceB, lockStateChange = false) {
|
|
51
52
|
// TODO check for priceA/priceB sanity
|
|
52
|
-
return new uniswapV3_1.default(0, 0, 0, await this.getNftlp().getMarketPrice(), await this.getNftlp().getOraclePrice(), priceA, priceB, await this.getLendingPool().getSafetyMargin(), await this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
53
|
+
return new uniswapV3_1.default(0, 0, 0, 0, 0, await this.getNftlp().getMarketPrice(), await this.getNftlp().getOraclePrice(), priceA, priceB, await this.getLendingPool().getSafetyMargin(), await this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
53
54
|
}
|
|
54
55
|
async getPositionObject(tokenId) {
|
|
55
56
|
if (!this.cache.positionsObject)
|
|
@@ -12,11 +12,6 @@ export interface Position {
|
|
|
12
12
|
readonly liquidationPenalty: number;
|
|
13
13
|
debtX: number;
|
|
14
14
|
debtY: number;
|
|
15
|
-
setRealX(amountX: number): void;
|
|
16
|
-
setRealY(amountY: number): void;
|
|
17
|
-
setDebtX(debtX: number): void;
|
|
18
|
-
setDebtY(debtY: number): void;
|
|
19
|
-
setRealXRealY(amountX: number, amountY: number): void;
|
|
20
15
|
depositX(amount: number): void;
|
|
21
16
|
depositY(amount: number): void;
|
|
22
17
|
withdrawX(amount: number): void;
|
|
@@ -27,8 +22,8 @@ export interface Position {
|
|
|
27
22
|
repayY(amount: number): void;
|
|
28
23
|
isUnderwater(): boolean;
|
|
29
24
|
isLiquidatable(): boolean;
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
getDepositedX(): number;
|
|
26
|
+
getDepositedY(): number;
|
|
32
27
|
getNetX(): number;
|
|
33
28
|
getNetY(): number;
|
|
34
29
|
getLeverage(): number;
|
|
@@ -6,9 +6,13 @@ export declare class UniswapV3Position implements Position {
|
|
|
6
6
|
* NOTICE all values inside this class are normalized (no decimals or mantissa to take into account)
|
|
7
7
|
*/
|
|
8
8
|
liquidity: number;
|
|
9
|
+
uncollectedFeesX: number;
|
|
10
|
+
uncollectedFeesY: number;
|
|
9
11
|
debtX: number;
|
|
10
12
|
debtY: number;
|
|
11
13
|
initialLiquidity: number;
|
|
14
|
+
initialUncollectedFeesX: number;
|
|
15
|
+
initialUncollectedFeesY: number;
|
|
12
16
|
initialDebtX: number;
|
|
13
17
|
initialDebtY: number;
|
|
14
18
|
marketPrice: number;
|
|
@@ -17,10 +21,18 @@ export declare class UniswapV3Position implements Position {
|
|
|
17
21
|
priceB: number;
|
|
18
22
|
safetyMargin: number;
|
|
19
23
|
liquidationPenalty: number;
|
|
20
|
-
constructor(_liquidity: number, _debtX: number, _debtY: number, _marketPrice: number, _oraclePrice: number, _priceA: number, _priceB: number, _safetyMargin: number, _liquidationPenalty: number, _lockStateChange: boolean);
|
|
24
|
+
constructor(_liquidity: number, _uncollectedFeesX: number, _uncollectedFeesY: number, _debtX: number, _debtY: number, _marketPrice: number, _oraclePrice: number, _priceA: number, _priceB: number, _safetyMargin: number, _liquidationPenalty: number, _lockStateChange: boolean);
|
|
21
25
|
private checkLock;
|
|
22
26
|
/**
|
|
23
|
-
* PRIVATE
|
|
27
|
+
* PRIVATE SETTERS
|
|
28
|
+
*/
|
|
29
|
+
private setRealXRealY;
|
|
30
|
+
private setRealX;
|
|
31
|
+
private setRealY;
|
|
32
|
+
private setDebtX;
|
|
33
|
+
private setDebtY;
|
|
34
|
+
/**
|
|
35
|
+
* PRIVATE GETTERS
|
|
24
36
|
*/
|
|
25
37
|
private getRealXGivenLiquidityAndPrice;
|
|
26
38
|
private getRealYGivenLiquidityAndPrice;
|
|
@@ -28,6 +40,8 @@ export declare class UniswapV3Position implements Position {
|
|
|
28
40
|
private getRealYGivenLiquidity;
|
|
29
41
|
private getRealXGivenPrice;
|
|
30
42
|
private getRealYGivenPrice;
|
|
43
|
+
private getRealX;
|
|
44
|
+
private getRealY;
|
|
31
45
|
private getInitialRealX;
|
|
32
46
|
private getInitialRealY;
|
|
33
47
|
private getValueGivenPriceAndAmounts;
|
|
@@ -36,26 +50,27 @@ export declare class UniswapV3Position implements Position {
|
|
|
36
50
|
private getAmountYGivenValue;
|
|
37
51
|
private getDebtValueGivenPrice;
|
|
38
52
|
private getCollateralValueGivenPrice;
|
|
53
|
+
private getWeightedCollateralValueGivenPrice;
|
|
39
54
|
private getEquityValueGivenPrice;
|
|
40
55
|
private getDebtValue;
|
|
41
56
|
private getCollateralValue;
|
|
42
57
|
private getEquityValue;
|
|
43
58
|
private getLiquidityPostLiquidationValueGivenPriceAndDebt;
|
|
59
|
+
private getWeightedLiquidityPostLiquidationValueGivenPriceAndDebt;
|
|
60
|
+
private getWeightedLiquidityPostLiquidationValueGivenPriceAndDeltaLeverage;
|
|
44
61
|
private isLiquidatableGivenPriceAndDebt;
|
|
62
|
+
private isLiquidatableGivenPriceAndDeltaLeverage;
|
|
45
63
|
private isLiquidatableGivenDebt;
|
|
64
|
+
private isLiquidatableGivenDeltaLeverage;
|
|
46
65
|
private isLiquidatableGivenPrice;
|
|
47
66
|
private isUnderwaterGivenPrice;
|
|
48
67
|
private getLiquidityGivenAmounts;
|
|
49
68
|
private getLiquidationPriceInRange;
|
|
50
69
|
private getMaxDeltaDebtInRange;
|
|
70
|
+
private getMaxDeltaLeverageInRange;
|
|
51
71
|
/**
|
|
52
72
|
* PUBLIC SETTERS
|
|
53
73
|
*/
|
|
54
|
-
setRealXRealY(amountX: number, amountY: number): void;
|
|
55
|
-
setRealX(amountX: number): void;
|
|
56
|
-
setRealY(amountY: number): void;
|
|
57
|
-
setDebtX(_debtX: number): void;
|
|
58
|
-
setDebtY(_debtY: number): void;
|
|
59
74
|
depositX(amount: number): void;
|
|
60
75
|
depositY(amount: number): void;
|
|
61
76
|
withdrawX(amount: number): void;
|
|
@@ -67,8 +82,8 @@ export declare class UniswapV3Position implements Position {
|
|
|
67
82
|
/**
|
|
68
83
|
* PUBLIC GETTERS
|
|
69
84
|
*/
|
|
70
|
-
|
|
71
|
-
|
|
85
|
+
getDepositedX(): number;
|
|
86
|
+
getDepositedY(): number;
|
|
72
87
|
getNetX(): number;
|
|
73
88
|
getNetY(): number;
|
|
74
89
|
isLiquidatable(): boolean;
|
|
@@ -19,13 +19,18 @@ function getRealY(liquidity, price, priceA, priceB) {
|
|
|
19
19
|
}
|
|
20
20
|
const LOWEST_PRICE = 1 / 1e18;
|
|
21
21
|
const HIGHEST_PRICE = 1e18;
|
|
22
|
+
const FEE_COLLECTED_WEIGHT = 0.95;
|
|
22
23
|
class UniswapV3Position {
|
|
23
24
|
// TODO add fee logic
|
|
24
|
-
constructor(_liquidity, _debtX, _debtY, _marketPrice, _oraclePrice, _priceA, _priceB, _safetyMargin, _liquidationPenalty, _lockStateChange) {
|
|
25
|
+
constructor(_liquidity, _uncollectedFeesX, _uncollectedFeesY, _debtX, _debtY, _marketPrice, _oraclePrice, _priceA, _priceB, _safetyMargin, _liquidationPenalty, _lockStateChange) {
|
|
25
26
|
this.liquidity = _liquidity;
|
|
27
|
+
this.uncollectedFeesX = _uncollectedFeesX;
|
|
28
|
+
this.uncollectedFeesY = _uncollectedFeesY;
|
|
26
29
|
this.debtX = _debtX;
|
|
27
30
|
this.debtY = _debtY;
|
|
28
31
|
this.initialLiquidity = _liquidity;
|
|
32
|
+
this.initialUncollectedFeesX = _uncollectedFeesX;
|
|
33
|
+
this.initialUncollectedFeesY = _uncollectedFeesY;
|
|
29
34
|
this.initialDebtX = _debtX;
|
|
30
35
|
this.initialDebtY = _debtY;
|
|
31
36
|
this.marketPrice = _marketPrice;
|
|
@@ -44,7 +49,45 @@ class UniswapV3Position {
|
|
|
44
49
|
this.state++;
|
|
45
50
|
}
|
|
46
51
|
/**
|
|
47
|
-
* PRIVATE
|
|
52
|
+
* PRIVATE SETTERS
|
|
53
|
+
*/
|
|
54
|
+
setRealXRealY(amountX, amountY) {
|
|
55
|
+
if (Number.isNaN(amountX))
|
|
56
|
+
return;
|
|
57
|
+
if (Number.isNaN(amountY))
|
|
58
|
+
return;
|
|
59
|
+
this.checkLock();
|
|
60
|
+
const { liquidity } = this.getOptimalLiquidity(amountX, amountY);
|
|
61
|
+
this.liquidity = liquidity;
|
|
62
|
+
}
|
|
63
|
+
setRealX(amountX) {
|
|
64
|
+
if (Number.isNaN(amountX))
|
|
65
|
+
return;
|
|
66
|
+
this.checkLock();
|
|
67
|
+
const { liquidity } = this.getOptimalLiquidity(amountX, Infinity);
|
|
68
|
+
this.liquidity = liquidity;
|
|
69
|
+
}
|
|
70
|
+
setRealY(amountY) {
|
|
71
|
+
if (Number.isNaN(amountY))
|
|
72
|
+
return;
|
|
73
|
+
this.checkLock();
|
|
74
|
+
const { liquidity } = this.getOptimalLiquidity(Infinity, amountY);
|
|
75
|
+
this.liquidity = liquidity;
|
|
76
|
+
}
|
|
77
|
+
setDebtX(_debtX) {
|
|
78
|
+
if (Number.isNaN(_debtX))
|
|
79
|
+
return;
|
|
80
|
+
this.checkLock();
|
|
81
|
+
this.debtX = _debtX;
|
|
82
|
+
}
|
|
83
|
+
setDebtY(_debtY) {
|
|
84
|
+
if (Number.isNaN(_debtY))
|
|
85
|
+
return;
|
|
86
|
+
this.checkLock();
|
|
87
|
+
this.debtY = _debtY;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* PRIVATE GETTERS
|
|
48
91
|
*/
|
|
49
92
|
getRealXGivenLiquidityAndPrice(liquidity, price) {
|
|
50
93
|
return getRealX(liquidity, price, this.priceA, this.priceB);
|
|
@@ -64,6 +107,12 @@ class UniswapV3Position {
|
|
|
64
107
|
getRealYGivenPrice(price) {
|
|
65
108
|
return this.getRealYGivenLiquidityAndPrice(this.liquidity, price);
|
|
66
109
|
}
|
|
110
|
+
getRealX() {
|
|
111
|
+
return this.getRealXGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
|
|
112
|
+
}
|
|
113
|
+
getRealY() {
|
|
114
|
+
return this.getRealYGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
|
|
115
|
+
}
|
|
67
116
|
getInitialRealX() {
|
|
68
117
|
return this.getRealXGivenLiquidityAndPrice(this.initialLiquidity, this.marketPrice);
|
|
69
118
|
}
|
|
@@ -86,7 +135,10 @@ class UniswapV3Position {
|
|
|
86
135
|
return this.getValueGivenPriceAndAmounts(price, this.debtX, this.debtY);
|
|
87
136
|
}
|
|
88
137
|
getCollateralValueGivenPrice(price) {
|
|
89
|
-
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price), this.getRealYGivenPrice(price));
|
|
138
|
+
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.uncollectedFeesX, this.getRealYGivenPrice(price) + this.uncollectedFeesY);
|
|
139
|
+
}
|
|
140
|
+
getWeightedCollateralValueGivenPrice(price) {
|
|
141
|
+
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.uncollectedFeesX * FEE_COLLECTED_WEIGHT, this.getRealYGivenPrice(price) + this.uncollectedFeesY * FEE_COLLECTED_WEIGHT);
|
|
90
142
|
}
|
|
91
143
|
getEquityValueGivenPrice(price) {
|
|
92
144
|
return this.getCollateralValueGivenPrice(price) - this.getDebtValueGivenPrice(price);
|
|
@@ -106,13 +158,33 @@ class UniswapV3Position {
|
|
|
106
158
|
const collateralNeeded = debtValue * this.liquidationPenalty;
|
|
107
159
|
return collateralValue - collateralNeeded;
|
|
108
160
|
}
|
|
161
|
+
getWeightedLiquidityPostLiquidationValueGivenPriceAndDebt(price, debtX, debtY) {
|
|
162
|
+
const debtValue = this.getValueGivenPriceAndAmounts(price, debtX, debtY);
|
|
163
|
+
const collateralValue = this.getWeightedCollateralValueGivenPrice(price);
|
|
164
|
+
const collateralNeeded = debtValue * this.liquidationPenalty;
|
|
165
|
+
return collateralValue - collateralNeeded;
|
|
166
|
+
}
|
|
167
|
+
getWeightedLiquidityPostLiquidationValueGivenPriceAndDeltaLeverage(price, deltaX, deltaY) {
|
|
168
|
+
const deltaValue = this.getValueGivenPriceAndAmounts(price, deltaX, deltaY);
|
|
169
|
+
const debtValue = this.getDebtValueGivenPrice(price) + deltaValue;
|
|
170
|
+
const collateralValue = this.getWeightedCollateralValueGivenPrice(price) + deltaValue;
|
|
171
|
+
const collateralNeeded = debtValue * this.liquidationPenalty;
|
|
172
|
+
return collateralValue - collateralNeeded;
|
|
173
|
+
}
|
|
109
174
|
isLiquidatableGivenPriceAndDebt(price, debtX, debtY) {
|
|
110
|
-
return this.
|
|
111
|
-
|| this.
|
|
175
|
+
return this.getWeightedLiquidityPostLiquidationValueGivenPriceAndDebt(price / this.safetyMargin, debtX, debtY) < 0
|
|
176
|
+
|| this.getWeightedLiquidityPostLiquidationValueGivenPriceAndDebt(price * this.safetyMargin, debtX, debtY) < 0;
|
|
177
|
+
}
|
|
178
|
+
isLiquidatableGivenPriceAndDeltaLeverage(price, deltaX, deltaY) {
|
|
179
|
+
return this.getWeightedLiquidityPostLiquidationValueGivenPriceAndDeltaLeverage(price / this.safetyMargin, deltaX, deltaY) < 0
|
|
180
|
+
|| this.getWeightedLiquidityPostLiquidationValueGivenPriceAndDeltaLeverage(price * this.safetyMargin, deltaX, deltaY) < 0;
|
|
112
181
|
}
|
|
113
182
|
isLiquidatableGivenDebt(debtX, debtY) {
|
|
114
183
|
return this.isLiquidatableGivenPriceAndDebt(this.marketPrice, debtX, debtY);
|
|
115
184
|
}
|
|
185
|
+
isLiquidatableGivenDeltaLeverage(deltaX, deltaY) {
|
|
186
|
+
return this.isLiquidatableGivenPriceAndDeltaLeverage(this.marketPrice, deltaX, deltaY);
|
|
187
|
+
}
|
|
116
188
|
isLiquidatableGivenPrice(price) {
|
|
117
189
|
return this.isLiquidatableGivenPriceAndDebt(price, this.debtX, this.debtY);
|
|
118
190
|
}
|
|
@@ -138,7 +210,6 @@ class UniswapV3Position {
|
|
|
138
210
|
return this.getLiquidationPriceInRange(lowPrice, avgPrice, lowIsLiquidatable, avgIsLiquidatable)
|
|
139
211
|
?? this.getLiquidationPriceInRange(avgPrice, highPrice, avgIsLiquidatable, highIsLiquidatable);
|
|
140
212
|
}
|
|
141
|
-
// divide and conquer implementation
|
|
142
213
|
getMaxDeltaDebtInRange(lowDeltaX, lowDeltaY, highDeltaX, highDeltaY) {
|
|
143
214
|
if (Number.isNaN(lowDeltaX) || Number.isNaN(lowDeltaY) || Number.isNaN(highDeltaX) || Number.isNaN(highDeltaY))
|
|
144
215
|
return [0, 0];
|
|
@@ -153,50 +224,23 @@ class UniswapV3Position {
|
|
|
153
224
|
else
|
|
154
225
|
return this.getMaxDeltaDebtInRange(avgDeltaX, avgDeltaY, highDeltaX, highDeltaY);
|
|
155
226
|
}
|
|
227
|
+
getMaxDeltaLeverageInRange(lowDeltaX, lowDeltaY, highDeltaX, highDeltaY) {
|
|
228
|
+
if (Number.isNaN(lowDeltaX) || Number.isNaN(lowDeltaY) || Number.isNaN(highDeltaX) || Number.isNaN(highDeltaY))
|
|
229
|
+
return [0, 0];
|
|
230
|
+
if (highDeltaX == 0 && highDeltaY == 0)
|
|
231
|
+
return [0, 0];
|
|
232
|
+
if (Math.abs(highDeltaX / lowDeltaX) < 1.001 || Math.abs(highDeltaY / lowDeltaY) < 1.001)
|
|
233
|
+
return [lowDeltaX, lowDeltaY];
|
|
234
|
+
const avgDeltaX = (lowDeltaX + highDeltaX) / 2;
|
|
235
|
+
const avgDeltaY = (lowDeltaY + highDeltaY) / 2;
|
|
236
|
+
if (this.isLiquidatableGivenDeltaLeverage(this.debtX + avgDeltaX, this.debtY + avgDeltaY))
|
|
237
|
+
return this.getMaxDeltaLeverageInRange(lowDeltaX, lowDeltaY, avgDeltaX, avgDeltaY);
|
|
238
|
+
else
|
|
239
|
+
return this.getMaxDeltaLeverageInRange(avgDeltaX, avgDeltaY, highDeltaX, highDeltaY);
|
|
240
|
+
}
|
|
156
241
|
/**
|
|
157
242
|
* PUBLIC SETTERS
|
|
158
243
|
*/
|
|
159
|
-
// Setters
|
|
160
|
-
/*public setLiquidity(_liquidity: number) {
|
|
161
|
-
this.checkLock();
|
|
162
|
-
this.liquidity = _liquidity;
|
|
163
|
-
}*/
|
|
164
|
-
setRealXRealY(amountX, amountY) {
|
|
165
|
-
if (Number.isNaN(amountX))
|
|
166
|
-
return;
|
|
167
|
-
if (Number.isNaN(amountY))
|
|
168
|
-
return;
|
|
169
|
-
this.checkLock();
|
|
170
|
-
const { liquidity } = this.getOptimalLiquidity(amountX, amountY);
|
|
171
|
-
this.liquidity = liquidity;
|
|
172
|
-
}
|
|
173
|
-
setRealX(amountX) {
|
|
174
|
-
if (Number.isNaN(amountX))
|
|
175
|
-
return;
|
|
176
|
-
this.checkLock();
|
|
177
|
-
const { liquidity } = this.getOptimalLiquidity(amountX, Infinity);
|
|
178
|
-
this.liquidity = liquidity;
|
|
179
|
-
}
|
|
180
|
-
setRealY(amountY) {
|
|
181
|
-
if (Number.isNaN(amountY))
|
|
182
|
-
return;
|
|
183
|
-
this.checkLock();
|
|
184
|
-
const { liquidity } = this.getOptimalLiquidity(Infinity, amountY);
|
|
185
|
-
this.liquidity = liquidity;
|
|
186
|
-
}
|
|
187
|
-
setDebtX(_debtX) {
|
|
188
|
-
if (Number.isNaN(_debtX))
|
|
189
|
-
return;
|
|
190
|
-
this.checkLock();
|
|
191
|
-
this.debtX = _debtX;
|
|
192
|
-
}
|
|
193
|
-
setDebtY(_debtY) {
|
|
194
|
-
if (Number.isNaN(_debtY))
|
|
195
|
-
return;
|
|
196
|
-
this.checkLock();
|
|
197
|
-
this.debtY = _debtY;
|
|
198
|
-
}
|
|
199
|
-
// Actions
|
|
200
244
|
depositX(amount) {
|
|
201
245
|
this.setRealX(this.getInitialRealX() + amount);
|
|
202
246
|
}
|
|
@@ -205,6 +249,7 @@ class UniswapV3Position {
|
|
|
205
249
|
}
|
|
206
250
|
withdrawX(amount) {
|
|
207
251
|
this.setRealX(Math.max(this.getInitialRealX() - amount, 0));
|
|
252
|
+
// TODO set uncollected fees in proportion
|
|
208
253
|
}
|
|
209
254
|
withdrawY(amount) {
|
|
210
255
|
this.setRealY(Math.max(this.getInitialRealY() - amount, 0));
|
|
@@ -224,17 +269,17 @@ class UniswapV3Position {
|
|
|
224
269
|
/**
|
|
225
270
|
* PUBLIC GETTERS
|
|
226
271
|
*/
|
|
227
|
-
|
|
228
|
-
return this.
|
|
272
|
+
getDepositedX() {
|
|
273
|
+
return this.getRealX() + this.uncollectedFeesX;
|
|
229
274
|
}
|
|
230
|
-
|
|
231
|
-
return this.
|
|
275
|
+
getDepositedY() {
|
|
276
|
+
return this.getRealY() + this.uncollectedFeesY;
|
|
232
277
|
}
|
|
233
278
|
getNetX() {
|
|
234
|
-
return this.
|
|
279
|
+
return this.getDepositedX() - this.debtX;
|
|
235
280
|
}
|
|
236
281
|
getNetY() {
|
|
237
|
-
return this.
|
|
282
|
+
return this.getDepositedY() - this.debtY;
|
|
238
283
|
}
|
|
239
284
|
isLiquidatable() {
|
|
240
285
|
return this.isLiquidatableGivenPrice(this.oraclePrice);
|
|
@@ -300,31 +345,26 @@ class UniswapV3Position {
|
|
|
300
345
|
}
|
|
301
346
|
const realX = this.getRealX();
|
|
302
347
|
const realY = this.getRealY();
|
|
303
|
-
const
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
const projectedDebtY = this.debtY + projectedRealY - realY;
|
|
307
|
-
const normalizedDeltaDebtX = projectedDebtX * currentLeverage / highLeverage - this.debtX;
|
|
308
|
-
const normalizedDeltaDebtY = projectedDebtY * currentLeverage / highLeverage - this.debtY;
|
|
309
|
-
const [deltaDebtX, deltaDebtY] = this.getMaxDeltaDebtInRange(0, 0, normalizedDeltaDebtX, normalizedDeltaDebtY);
|
|
348
|
+
const maxDeltaX = realX * highLeverage / currentLeverage;
|
|
349
|
+
const maxDeltaY = realY * highLeverage / currentLeverage;
|
|
350
|
+
const [deltaX, deltaY] = this.getMaxDeltaLeverageInRange(0, 0, maxDeltaX, maxDeltaY);
|
|
310
351
|
const collateralValue = this.getCollateralValue();
|
|
311
|
-
const
|
|
312
|
-
const equityValue = collateralValue - debtValue;
|
|
352
|
+
const additionalValue = this.getValueGivenAmounts(deltaX, deltaY);
|
|
313
353
|
if (setLiquidityToZero)
|
|
314
354
|
this.liquidity = 0;
|
|
315
|
-
return collateralValue /
|
|
355
|
+
return (collateralValue + additionalValue) / collateralValue * currentLeverage;
|
|
316
356
|
}
|
|
317
357
|
getMinLeverage() {
|
|
318
|
-
const
|
|
319
|
-
const
|
|
320
|
-
if (
|
|
358
|
+
const depositedX = this.getDepositedX();
|
|
359
|
+
const depositedY = this.getDepositedY();
|
|
360
|
+
if (depositedX >= this.debtX && depositedY >= this.debtY)
|
|
321
361
|
return 0;
|
|
322
362
|
// TODO binary search
|
|
323
363
|
return 0;
|
|
324
364
|
}
|
|
325
365
|
getMaxWithdrawable() {
|
|
326
|
-
const
|
|
327
|
-
const
|
|
366
|
+
const depositedX = this.getDepositedX();
|
|
367
|
+
const depositedY = this.getDepositedY();
|
|
328
368
|
let percentage = 0;
|
|
329
369
|
if (!this.isLiquidatable()) {
|
|
330
370
|
if (this.debtX == 0 && this.debtY == 0) {
|
|
@@ -338,8 +378,8 @@ class UniswapV3Position {
|
|
|
338
378
|
}
|
|
339
379
|
}
|
|
340
380
|
return {
|
|
341
|
-
amountX:
|
|
342
|
-
amountY:
|
|
381
|
+
amountX: depositedX * percentage,
|
|
382
|
+
amountY: depositedY * percentage,
|
|
343
383
|
};
|
|
344
384
|
}
|
|
345
385
|
getMaxBorrowableX() {
|