impermax-sdk 2.1.129 → 2.1.130
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/onchain/account/lendingPool/nftlp/onchainAccountNftlpUniswapV3.js +2 -2
- package/lib/onchain/interactions/lendingPool/nftlp/onchainInteractionsNftlpUniswapV3.js +1 -1
- package/lib/utils/nftlpMath/uniswapV3Position.d.ts +28 -6
- package/lib/utils/nftlpMath/uniswapV3Position.js +83 -26
- package/package.json +1 -1
|
@@ -76,8 +76,8 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
|
|
|
76
76
|
}
|
|
77
77
|
createNewPositionObject(fee, priceA, priceB, lockStateChange = false) {
|
|
78
78
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
|
|
80
|
-
0, 0, yield this.getNftlp().getMarketPrice(), yield this.getNftlp().getOraclePrice(), priceA, priceB, yield this.getLendingPool().getSafetyMargin(), yield this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
79
|
+
// TODO check for priceA/priceB sanity
|
|
80
|
+
return new uniswapV3Position_1.default(0, 0, 0, yield this.getNftlp().getMarketPrice(), yield this.getNftlp().getOraclePrice(), priceA, priceB, yield this.getLendingPool().getSafetyMargin(), yield this.getLendingPool().getLiquidationPenalty(), lockStateChange);
|
|
81
81
|
});
|
|
82
82
|
}
|
|
83
83
|
getPositionObject(tokenId) {
|
|
@@ -72,7 +72,7 @@ class OnchainInteractionsNftlpUniswapV3 extends onchainInteractionsNftlp_1.defau
|
|
|
72
72
|
throw new Error("Opposite depositADelta and depositBDelta signs");
|
|
73
73
|
const router = this.getUniswapV3Router();
|
|
74
74
|
const actionsGetter = this.getActionsGetter();
|
|
75
|
-
const optimalLiquidity = positionObject.
|
|
75
|
+
const optimalLiquidity = positionObject.getOptimalLiquidity(depositADelta, depositBDelta);
|
|
76
76
|
depositADelta = optimalLiquidity.amountX;
|
|
77
77
|
depositBDelta = optimalLiquidity.amountY;
|
|
78
78
|
// slightly increase repayAmount if we are repaying 100%
|
|
@@ -14,15 +14,37 @@ export default class UniswapV3Position {
|
|
|
14
14
|
liquidationPenalty: number;
|
|
15
15
|
constructor(_liquidity: number, _debtX: number, _debtY: number, _marketPrice: number, _oraclePrice: number, _priceA: number, _priceB: number, _safetyMargin: number, _liquidationPenalty: number, _lockStateChange: boolean);
|
|
16
16
|
private checkLock;
|
|
17
|
+
/**
|
|
18
|
+
* PRIVATE
|
|
19
|
+
*/
|
|
20
|
+
private getRealXGivenLiquidityAndPrice;
|
|
21
|
+
private getRealYGivenLiquidityAndPrice;
|
|
22
|
+
private getRealXGivenLiquidity;
|
|
23
|
+
private getRealYGivenLiquidity;
|
|
24
|
+
private getRealXGivenPrice;
|
|
25
|
+
private getRealYGivenPrice;
|
|
26
|
+
private getLiquidity;
|
|
27
|
+
/**
|
|
28
|
+
* PUBLIC SETTERS
|
|
29
|
+
*/
|
|
17
30
|
setLiquidity(_liquidity: number): void;
|
|
31
|
+
setRealX(amountX: number): void;
|
|
32
|
+
setRealY(amountY: number): void;
|
|
18
33
|
setDebtX(_debtX: number): void;
|
|
19
34
|
setDebtY(_debtY: number): void;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
35
|
+
/**
|
|
36
|
+
* PUBLIC GETTERS
|
|
37
|
+
*/
|
|
38
|
+
getRealX(): number;
|
|
39
|
+
getRealY(): number;
|
|
40
|
+
getNetX(): number;
|
|
41
|
+
getNetY(): number;
|
|
42
|
+
getLiquidationRange(): {
|
|
43
|
+
priceA: number;
|
|
44
|
+
priceB: number;
|
|
45
|
+
};
|
|
46
|
+
leverage(): void;
|
|
47
|
+
getOptimalLiquidity(amountX: number, amountY: number): {
|
|
26
48
|
liquidity: number;
|
|
27
49
|
amountX: number;
|
|
28
50
|
amountY: number;
|
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function getVirtualX(liquidity, price) {
|
|
4
|
+
return liquidity / Math.sqrt(price);
|
|
5
|
+
}
|
|
6
|
+
function getVirtualY(liquidity, price) {
|
|
7
|
+
return liquidity * Math.sqrt(price);
|
|
8
|
+
}
|
|
9
|
+
function getRealX(liquidity, price, priceA, priceB) {
|
|
10
|
+
const surplusX = getVirtualX(liquidity, priceB);
|
|
11
|
+
const virtualX = getVirtualX(liquidity, Math.max(price, priceA));
|
|
12
|
+
return Math.max(virtualX - surplusX, 0);
|
|
13
|
+
}
|
|
14
|
+
function getRealY(liquidity, price, priceA, priceB) {
|
|
15
|
+
const surplusY = getVirtualY(liquidity, priceA);
|
|
16
|
+
const virtualY = getVirtualY(liquidity, Math.min(price, priceB));
|
|
17
|
+
return Math.max(virtualY - surplusY, 0);
|
|
18
|
+
}
|
|
3
19
|
class UniswapV3Position {
|
|
4
20
|
// TODO add fee logic
|
|
5
21
|
constructor(_liquidity, _debtX, _debtY, _marketPrice, _oraclePrice, _priceA, _priceB, _safetyMargin, _liquidationPenalty, _lockStateChange) {
|
|
@@ -18,10 +34,53 @@ class UniswapV3Position {
|
|
|
18
34
|
if (this.lockStateChange)
|
|
19
35
|
throw Error("Can't change state of original position object");
|
|
20
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* PRIVATE
|
|
39
|
+
*/
|
|
40
|
+
getRealXGivenLiquidityAndPrice(liquidity, price) {
|
|
41
|
+
return getRealX(liquidity, price, this.priceA, this.priceB);
|
|
42
|
+
}
|
|
43
|
+
getRealYGivenLiquidityAndPrice(liquidity, price) {
|
|
44
|
+
return getRealY(liquidity, price, this.priceA, this.priceB);
|
|
45
|
+
}
|
|
46
|
+
getRealXGivenLiquidity(liquidity) {
|
|
47
|
+
return this.getRealXGivenLiquidityAndPrice(liquidity, this.marketPrice);
|
|
48
|
+
}
|
|
49
|
+
getRealYGivenLiquidity(liquidity) {
|
|
50
|
+
return this.getRealYGivenLiquidityAndPrice(liquidity, this.marketPrice);
|
|
51
|
+
}
|
|
52
|
+
getRealXGivenPrice(price) {
|
|
53
|
+
return this.getRealXGivenLiquidityAndPrice(this.liquidity, price);
|
|
54
|
+
}
|
|
55
|
+
getRealYGivenPrice(price) {
|
|
56
|
+
return this.getRealYGivenLiquidityAndPrice(this.liquidity, price);
|
|
57
|
+
}
|
|
58
|
+
getLiquidity(price, amountX, amountY) {
|
|
59
|
+
if (amountX == 0) {
|
|
60
|
+
return amountY / Math.sqrt(price);
|
|
61
|
+
}
|
|
62
|
+
if (amountY == 0) {
|
|
63
|
+
return amountX * Math.sqrt(price);
|
|
64
|
+
}
|
|
65
|
+
return amountX / (1 / Math.sqrt(price) - 1 / Math.sqrt(this.priceB));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* PUBLIC SETTERS
|
|
69
|
+
*/
|
|
21
70
|
setLiquidity(_liquidity) {
|
|
22
71
|
this.checkLock();
|
|
23
72
|
this.liquidity = _liquidity;
|
|
24
73
|
}
|
|
74
|
+
setRealX(amountX) {
|
|
75
|
+
this.checkLock();
|
|
76
|
+
const { liquidity } = this.getOptimalLiquidity(amountX, Infinity);
|
|
77
|
+
this.liquidity = liquidity;
|
|
78
|
+
}
|
|
79
|
+
setRealY(amountY) {
|
|
80
|
+
this.checkLock();
|
|
81
|
+
const { liquidity } = this.getOptimalLiquidity(Infinity, amountY);
|
|
82
|
+
this.liquidity = liquidity;
|
|
83
|
+
}
|
|
25
84
|
setDebtX(_debtX) {
|
|
26
85
|
this.checkLock();
|
|
27
86
|
this.debtX = _debtX;
|
|
@@ -30,40 +89,38 @@ class UniswapV3Position {
|
|
|
30
89
|
this.checkLock();
|
|
31
90
|
this.debtY = _debtY;
|
|
32
91
|
}
|
|
33
|
-
|
|
34
|
-
|
|
92
|
+
/**
|
|
93
|
+
* PUBLIC GETTERS
|
|
94
|
+
*/
|
|
95
|
+
getRealX() {
|
|
96
|
+
return this.getRealXGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
|
|
35
97
|
}
|
|
36
|
-
|
|
37
|
-
return this.liquidity
|
|
98
|
+
getRealY() {
|
|
99
|
+
return this.getRealYGivenLiquidityAndPrice(this.liquidity, this.marketPrice);
|
|
38
100
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const virtualX = this.getVirtualX(Math.max(price, this.priceA));
|
|
42
|
-
return Math.max(virtualX - surplusX, 0);
|
|
101
|
+
getNetX() {
|
|
102
|
+
return this.getRealX() - this.debtX;
|
|
43
103
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const virtualY = this.getVirtualY(Math.min(price, this.priceB));
|
|
47
|
-
return Math.max(virtualY - surplusY, 0);
|
|
104
|
+
getNetY() {
|
|
105
|
+
return this.getRealY() - this.debtY;
|
|
48
106
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
107
|
+
getLiquidationRange() {
|
|
108
|
+
// TODO
|
|
109
|
+
return {
|
|
110
|
+
priceA: 0,
|
|
111
|
+
priceB: Infinity,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
leverage() {
|
|
57
115
|
}
|
|
58
116
|
// amountX and amountY are expected to have the same sign
|
|
59
|
-
|
|
60
|
-
let liquidity;
|
|
117
|
+
getOptimalLiquidity(amountX, amountY) {
|
|
61
118
|
if (amountX != 0 && amountY != 0) {
|
|
62
|
-
const sampleX = this.
|
|
63
|
-
const sampleY = this.
|
|
119
|
+
const sampleX = this.getRealXGivenLiquidity(1);
|
|
120
|
+
const sampleY = this.getRealYGivenLiquidity(1);
|
|
64
121
|
if (sampleX == 0)
|
|
65
122
|
amountX = 0;
|
|
66
|
-
else if (
|
|
123
|
+
else if (sampleY == 0)
|
|
67
124
|
amountY = 0;
|
|
68
125
|
else {
|
|
69
126
|
if (Math.abs(amountX) / sampleX > Math.abs(amountY) / sampleY)
|
|
@@ -72,7 +129,7 @@ class UniswapV3Position {
|
|
|
72
129
|
amountY = amountX / sampleX * sampleY;
|
|
73
130
|
}
|
|
74
131
|
}
|
|
75
|
-
liquidity = this.getLiquidity(this.marketPrice, amountX, amountY);
|
|
132
|
+
const liquidity = this.getLiquidity(this.marketPrice, amountX, amountY);
|
|
76
133
|
return { liquidity, amountX, amountY };
|
|
77
134
|
}
|
|
78
135
|
}
|