impermax-sdk 2.1.199 → 2.1.201
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.d.ts +11 -3
- package/lib/onchain/account/lendingPool/nftlp/onchainAccountNftlpUniswapV3.js +33 -14
- package/lib/utils/position/uniswapV3/index.d.ts +5 -5
- package/lib/utils/position/uniswapV3/index.js +10 -10
- package/package.json +1 -1
|
@@ -28,12 +28,20 @@ export default class OnchainAccountNftlpUniswapV3 extends OnchainAccountNftlp {
|
|
|
28
28
|
getPriceA(tokenId: number): Promise<number>;
|
|
29
29
|
getPriceB(tokenId: number): Promise<number>;
|
|
30
30
|
getLiquidity(tokenId: number): Promise<number>;
|
|
31
|
-
createPositionObject(tokenId: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
|
|
32
|
-
createNewPositionObject(fee: number, priceA: number, priceB: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
|
|
33
|
-
getPositionObject(tokenId: number): Promise<UniswapV3Position>;
|
|
34
31
|
getCurrentFeeGrowthInsideX128(tokenId: number): Promise<{
|
|
35
32
|
currentFeeGrowthInside0X128: number;
|
|
36
33
|
currentFeeGrowthInside1X128: number;
|
|
37
34
|
}>;
|
|
35
|
+
getCurrentUnclaimedFeesRaw(tokenId: number): Promise<{
|
|
36
|
+
unclaimedFees0: number;
|
|
37
|
+
unclaimedFees1: number;
|
|
38
|
+
}>;
|
|
39
|
+
getCurrentUnclaimedFees(tokenId: number): Promise<{
|
|
40
|
+
unclaimedFees0: number;
|
|
41
|
+
unclaimedFees1: number;
|
|
42
|
+
}>;
|
|
43
|
+
createPositionObject(tokenId: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
|
|
44
|
+
createNewPositionObject(fee: number, priceA: number, priceB: number, lockStateChange?: boolean): Promise<UniswapV3Position>;
|
|
45
|
+
getPositionObject(tokenId: number): Promise<UniswapV3Position>;
|
|
38
46
|
}
|
|
39
47
|
export {};
|
|
@@ -7,6 +7,7 @@ const onchainAccountNftlp_1 = __importDefault(require("./onchainAccountNftlp"));
|
|
|
7
7
|
const uniswapV3_1 = __importDefault(require("../../../../utils/position/uniswapV3"));
|
|
8
8
|
const uniswapV3General_1 = require("../../../../utils/nftlpMath/uniswapV3General");
|
|
9
9
|
const REINVESTOR = '0x000000000000000000000000000000000000dead';
|
|
10
|
+
const Q128 = 2 * 128;
|
|
10
11
|
class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
|
|
11
12
|
constructor() {
|
|
12
13
|
super(...arguments);
|
|
@@ -45,9 +46,39 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
|
|
|
45
46
|
const liquidity = (await this.getPositionData(tokenId)).liquidity;
|
|
46
47
|
return (0, uniswapV3General_1.formatUniV3Liquidity)(liquidity, decimalsA, decimalsB);
|
|
47
48
|
}
|
|
49
|
+
async getCurrentFeeGrowthInsideX128(tokenId) {
|
|
50
|
+
const multicall = this.getLendingPool().getAccount().getOnchain().multicall;
|
|
51
|
+
const nftlp = this.getNftlp().getNftlp();
|
|
52
|
+
const [_, position] = await multicall.aggregate([
|
|
53
|
+
nftlp.methods.reinvest(tokenId, REINVESTOR),
|
|
54
|
+
nftlp.methods.positions(tokenId)
|
|
55
|
+
]);
|
|
56
|
+
return {
|
|
57
|
+
currentFeeGrowthInside0X128: position[4],
|
|
58
|
+
currentFeeGrowthInside1X128: position[5],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
async getCurrentUnclaimedFeesRaw(tokenId) {
|
|
62
|
+
const { currentFeeGrowthInside0X128, currentFeeGrowthInside1X128 } = await this.getCurrentFeeGrowthInsideX128(tokenId);
|
|
63
|
+
const positionData = await this.getPositionData(tokenId);
|
|
64
|
+
const delta0 = currentFeeGrowthInside0X128 - positionData.feeGrowthInside0LastX128;
|
|
65
|
+
const delta1 = currentFeeGrowthInside1X128 - positionData.feeGrowthInside1LastX128;
|
|
66
|
+
return {
|
|
67
|
+
unclaimedFees0: positionData.unclaimedFees0 + delta0 * positionData.liquidity / Q128,
|
|
68
|
+
unclaimedFees1: positionData.unclaimedFees1 + delta1 * positionData.liquidity / Q128,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async getCurrentUnclaimedFees(tokenId) {
|
|
72
|
+
const raw = await this.getCurrentUnclaimedFeesRaw(tokenId);
|
|
73
|
+
return {
|
|
74
|
+
unclaimedFees0: await this.getNftlp().getLendingPool().getBorrowableA().normalize(raw.unclaimedFees0),
|
|
75
|
+
unclaimedFees1: await this.getNftlp().getLendingPool().getBorrowableB().normalize(raw.unclaimedFees1),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
48
78
|
async createPositionObject(tokenId, lockStateChange = true) {
|
|
49
|
-
|
|
50
|
-
|
|
79
|
+
const unclaimedFees = await this.getCurrentUnclaimedFees(tokenId);
|
|
80
|
+
console.log("unclaimedFees", unclaimedFees);
|
|
81
|
+
return new uniswapV3_1.default(await this.getLiquidity(tokenId), unclaimedFees.unclaimedFees0, unclaimedFees.unclaimedFees1, 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);
|
|
51
82
|
}
|
|
52
83
|
async createNewPositionObject(fee, priceA, priceB, lockStateChange = false) {
|
|
53
84
|
priceA = await this.getNftlp().nearestUsableTickPrice(priceA, fee);
|
|
@@ -61,17 +92,5 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
|
|
|
61
92
|
this.cache.positionsObject[tokenId] = this.createPositionObject(tokenId);
|
|
62
93
|
return this.cache.positionsObject[tokenId];
|
|
63
94
|
}
|
|
64
|
-
async getCurrentFeeGrowthInsideX128(tokenId) {
|
|
65
|
-
const multicall = this.getLendingPool().getAccount().getOnchain().multicall;
|
|
66
|
-
const nftlp = this.getNftlp().getNftlp();
|
|
67
|
-
const [_, position] = await multicall.aggregate([
|
|
68
|
-
nftlp.methods.reinvest(tokenId, REINVESTOR),
|
|
69
|
-
nftlp.methods.positions(tokenId)
|
|
70
|
-
]);
|
|
71
|
-
return {
|
|
72
|
-
currentFeeGrowthInside0X128: position[4],
|
|
73
|
-
currentFeeGrowthInside1X128: position[5],
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
95
|
}
|
|
77
96
|
exports.default = OnchainAccountNftlpUniswapV3;
|
|
@@ -6,13 +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
|
-
|
|
10
|
-
|
|
9
|
+
unclaimedFeesX: number;
|
|
10
|
+
unclaimedFeesY: number;
|
|
11
11
|
debtX: number;
|
|
12
12
|
debtY: number;
|
|
13
13
|
initialLiquidity: number;
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
initialUnclaimedFeesX: number;
|
|
15
|
+
initialUnclaimedFeesY: number;
|
|
16
16
|
initialDebtX: number;
|
|
17
17
|
initialDebtY: number;
|
|
18
18
|
marketPrice: number;
|
|
@@ -21,7 +21,7 @@ export declare class UniswapV3Position implements Position {
|
|
|
21
21
|
priceB: number;
|
|
22
22
|
safetyMargin: number;
|
|
23
23
|
liquidationPenalty: number;
|
|
24
|
-
constructor(_liquidity: number,
|
|
24
|
+
constructor(_liquidity: number, _unclaimedFeesX: number, _unclaimedFeesY: number, _debtX: number, _debtY: number, _marketPrice: number, _oraclePrice: number, _priceA: number, _priceB: number, _safetyMargin: number, _liquidationPenalty: number, _lockStateChange: boolean);
|
|
25
25
|
private checkLock;
|
|
26
26
|
/**
|
|
27
27
|
* PRIVATE SETTERS
|
|
@@ -21,15 +21,15 @@ const LOWEST_PRICE = 1 / 1e18;
|
|
|
21
21
|
const HIGHEST_PRICE = 1e18;
|
|
22
22
|
const FEE_COLLECTED_WEIGHT = 0.95;
|
|
23
23
|
class UniswapV3Position {
|
|
24
|
-
constructor(_liquidity,
|
|
24
|
+
constructor(_liquidity, _unclaimedFeesX, _unclaimedFeesY, _debtX, _debtY, _marketPrice, _oraclePrice, _priceA, _priceB, _safetyMargin, _liquidationPenalty, _lockStateChange) {
|
|
25
25
|
this.liquidity = _liquidity;
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
26
|
+
this.unclaimedFeesX = _unclaimedFeesX;
|
|
27
|
+
this.unclaimedFeesY = _unclaimedFeesY;
|
|
28
28
|
this.debtX = _debtX;
|
|
29
29
|
this.debtY = _debtY;
|
|
30
30
|
this.initialLiquidity = _liquidity;
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
31
|
+
this.initialUnclaimedFeesX = _unclaimedFeesX;
|
|
32
|
+
this.initialUnclaimedFeesY = _unclaimedFeesY;
|
|
33
33
|
this.initialDebtX = _debtX;
|
|
34
34
|
this.initialDebtY = _debtY;
|
|
35
35
|
this.marketPrice = _marketPrice;
|
|
@@ -132,14 +132,14 @@ class UniswapV3Position {
|
|
|
132
132
|
return this.getValueGivenPriceAndAmounts(price, this.debtX, this.debtY);
|
|
133
133
|
}
|
|
134
134
|
getCollateralValueGivenPrice(price) {
|
|
135
|
-
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.
|
|
135
|
+
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.unclaimedFeesX, this.getRealYGivenPrice(price) + this.unclaimedFeesY);
|
|
136
136
|
}
|
|
137
137
|
getWeightedCollateralValueGivenPrice(price) {
|
|
138
|
-
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.
|
|
138
|
+
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenPrice(price) + this.unclaimedFeesX * FEE_COLLECTED_WEIGHT, this.getRealYGivenPrice(price) + this.unclaimedFeesY * FEE_COLLECTED_WEIGHT);
|
|
139
139
|
}
|
|
140
140
|
getWeightedCollateralValueGivenPriceAndDeltaLeverage(price, deltaX, deltaY) {
|
|
141
141
|
const liquidity = this.getLiquidityGivenAmounts(deltaX, deltaY);
|
|
142
|
-
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenLiquidityAndPrice(this.liquidity + liquidity, price) + this.
|
|
142
|
+
return this.getValueGivenPriceAndAmounts(price, this.getRealXGivenLiquidityAndPrice(this.liquidity + liquidity, price) + this.unclaimedFeesX * FEE_COLLECTED_WEIGHT, this.getRealYGivenLiquidityAndPrice(this.liquidity + liquidity, price) + this.unclaimedFeesY * FEE_COLLECTED_WEIGHT);
|
|
143
143
|
}
|
|
144
144
|
getEquityValueGivenPrice(price) {
|
|
145
145
|
return this.getCollateralValueGivenPrice(price) - this.getDebtValueGivenPrice(price);
|
|
@@ -270,10 +270,10 @@ class UniswapV3Position {
|
|
|
270
270
|
* PUBLIC GETTERS
|
|
271
271
|
*/
|
|
272
272
|
getDepositedX() {
|
|
273
|
-
return this.getRealX() + this.
|
|
273
|
+
return this.getRealX() + this.unclaimedFeesX;
|
|
274
274
|
}
|
|
275
275
|
getDepositedY() {
|
|
276
|
-
return this.getRealY() + this.
|
|
276
|
+
return this.getRealY() + this.unclaimedFeesY;
|
|
277
277
|
}
|
|
278
278
|
getNetX() {
|
|
279
279
|
return this.getDepositedX() - this.debtX;
|