impermax-sdk 2.1.231 → 2.1.232
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.d.ts +4 -4
- package/lib/offchain/account/lendingPool/nftlp/offchainAccountNftlpUniswapV3.js +22 -10
- package/lib/onchain/account/lendingPool/nftlp/onchainAccountNftlpUniswapV3.js +33 -5
- package/lib/onchain/impermaxFactory/lendingPool/onchainBorrowable.d.ts +4 -0
- package/lib/onchain/impermaxFactory/lendingPool/onchainBorrowable.js +6 -0
- package/lib/utils/position/uniswapV3/index.d.ts +21 -3
- package/lib/utils/position/uniswapV3/index.js +20 -21
- package/package.json +1 -1
|
@@ -17,8 +17,8 @@ export type UniswapV3PositionData = NftlpPosition & {
|
|
|
17
17
|
nftlp: PositionData;
|
|
18
18
|
};
|
|
19
19
|
interface PositionFees {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
unclaimedFees0: number;
|
|
21
|
+
unclaimedFees1: number;
|
|
22
22
|
}
|
|
23
23
|
export default class OffchainAccountNftlpUniswapV3 extends OffchainAccountNftlp {
|
|
24
24
|
protected cache: {
|
|
@@ -41,9 +41,9 @@ export default class OffchainAccountNftlpUniswapV3 extends OffchainAccountNftlp
|
|
|
41
41
|
getPriceA(tokenId: number): Promise<number>;
|
|
42
42
|
getPriceB(tokenId: number): Promise<number>;
|
|
43
43
|
getLiquidity(tokenId: number): Promise<number>;
|
|
44
|
-
createPositionObject(tokenId: number
|
|
44
|
+
createPositionObject(tokenId: number): Promise<UniswapV3Position>;
|
|
45
45
|
getPositionObject(tokenId: number): Promise<UniswapV3Position>;
|
|
46
|
-
|
|
46
|
+
getUnclaimedFees(tokenId: number): Promise<PositionFees>;
|
|
47
47
|
getTotalAccruedFees(tokenId: number): Promise<{
|
|
48
48
|
accruedFees0: number;
|
|
49
49
|
accruedFees1: number;
|
|
@@ -46,9 +46,21 @@ class OffchainAccountNftlpUniswapV3 extends offchainAccountNftlp_1.default {
|
|
|
46
46
|
const liquidity = (await this.getPositionData(tokenId)).liquidity;
|
|
47
47
|
return (0, uniswapV3General_1.formatUniV3Liquidity)(liquidity, decimalsA, decimalsB);
|
|
48
48
|
}
|
|
49
|
-
async createPositionObject(tokenId
|
|
50
|
-
const {
|
|
51
|
-
return new uniswapV3_1.default(
|
|
49
|
+
async createPositionObject(tokenId) {
|
|
50
|
+
const { unclaimedFees0, unclaimedFees1 } = await this.getUnclaimedFees(tokenId);
|
|
51
|
+
return new uniswapV3_1.default({
|
|
52
|
+
liquidity: await this.getLiquidity(tokenId),
|
|
53
|
+
unclaimedFeesX: unclaimedFees0,
|
|
54
|
+
unclaimedFeesY: unclaimedFees1,
|
|
55
|
+
debtX: await this.getLendingPool().getBorrowableA().getBorrowedAmount(tokenId),
|
|
56
|
+
debtY: await this.getLendingPool().getBorrowableB().getBorrowedAmount(tokenId),
|
|
57
|
+
marketPrice: await this.getNftlp().getMarketPrice(),
|
|
58
|
+
oraclePrice: await this.getNftlp().getOraclePrice(),
|
|
59
|
+
priceA: await this.getPriceA(tokenId),
|
|
60
|
+
priceB: await this.getPriceB(tokenId),
|
|
61
|
+
safetyMargin: await this.getLendingPool().getSafetyMargin(),
|
|
62
|
+
liquidationPenalty: await this.getLendingPool().getLiquidationPenalty(),
|
|
63
|
+
});
|
|
52
64
|
}
|
|
53
65
|
async getPositionObject(tokenId) {
|
|
54
66
|
if (!this.cache.positionsObject)
|
|
@@ -57,7 +69,7 @@ class OffchainAccountNftlpUniswapV3 extends offchainAccountNftlp_1.default {
|
|
|
57
69
|
this.cache.positionsObject[tokenId] = this.createPositionObject(tokenId);
|
|
58
70
|
return this.cache.positionsObject[tokenId];
|
|
59
71
|
}
|
|
60
|
-
async
|
|
72
|
+
async getUnclaimedFees(tokenId) {
|
|
61
73
|
if (!this.cache.pendingFees)
|
|
62
74
|
this.cache.pendingFees = {};
|
|
63
75
|
if (tokenId in this.cache.pendingFees)
|
|
@@ -68,24 +80,24 @@ class OffchainAccountNftlpUniswapV3 extends offchainAccountNftlp_1.default {
|
|
|
68
80
|
const api = private_api_1.NFTLP_FEE_API[network][factory];
|
|
69
81
|
if (!api) {
|
|
70
82
|
console.log(`Missing Fees API for ${lendingPoolId} on ${network}?`);
|
|
71
|
-
return {
|
|
83
|
+
return { unclaimedFees0: 0, unclaimedFees1: 0 };
|
|
72
84
|
}
|
|
73
85
|
try {
|
|
74
86
|
const positionFees = await fetch(api.concat(`collect/${lendingPoolId}/${tokenId}`)).then(i => i.json());
|
|
75
|
-
this.cache.pendingFees[tokenId] = {
|
|
87
|
+
this.cache.pendingFees[tokenId] = { unclaimedFees0: Number(positionFees.feesEarned0), unclaimedFees1: Number(positionFees.feesEarned1) };
|
|
76
88
|
return this.cache.pendingFees[tokenId];
|
|
77
89
|
}
|
|
78
90
|
catch (err) {
|
|
79
91
|
console.log(`No pending fees data for ${lendingPoolId} on ${network}`);
|
|
80
|
-
return {
|
|
92
|
+
return { unclaimedFees0: 0, unclaimedFees1: 0 };
|
|
81
93
|
}
|
|
82
94
|
}
|
|
83
95
|
async getTotalAccruedFees(tokenId) {
|
|
84
96
|
const { storedTotalFeesEarned0, storedTotalFeesEarned1 } = (await this.getPositionData(tokenId));
|
|
85
|
-
const {
|
|
97
|
+
const { unclaimedFees0, unclaimedFees1 } = await this.getUnclaimedFees(tokenId);
|
|
86
98
|
return {
|
|
87
|
-
accruedFees0: Number(storedTotalFeesEarned0) +
|
|
88
|
-
accruedFees1: Number(storedTotalFeesEarned1) +
|
|
99
|
+
accruedFees0: Number(storedTotalFeesEarned0) + unclaimedFees0,
|
|
100
|
+
accruedFees1: Number(storedTotalFeesEarned1) + unclaimedFees1,
|
|
89
101
|
};
|
|
90
102
|
}
|
|
91
103
|
}
|
|
@@ -86,13 +86,41 @@ class OnchainAccountNftlpUniswapV3 extends onchainAccountNftlp_1.default {
|
|
|
86
86
|
};
|
|
87
87
|
}
|
|
88
88
|
async createPositionObject(tokenId, lockStateChange = true) {
|
|
89
|
-
const
|
|
90
|
-
return new uniswapV3_1.default(
|
|
89
|
+
const { unclaimedFees0, unclaimedFees1 } = await this.getCurrentUnclaimedFees(tokenId);
|
|
90
|
+
return new uniswapV3_1.default({
|
|
91
|
+
liquidity: await this.getLiquidity(tokenId),
|
|
92
|
+
unclaimedFeesX: unclaimedFees0,
|
|
93
|
+
unclaimedFeesY: unclaimedFees1,
|
|
94
|
+
debtX: await this.getLendingPool().getBorrowableA().getBorrowed(tokenId),
|
|
95
|
+
debtY: await this.getLendingPool().getBorrowableB().getBorrowed(tokenId),
|
|
96
|
+
marketPrice: await this.getNftlp().getMarketPrice(),
|
|
97
|
+
oraclePrice: await this.getNftlp().getOraclePrice(),
|
|
98
|
+
priceA: await this.getPriceA(tokenId),
|
|
99
|
+
priceB: await this.getPriceB(tokenId),
|
|
100
|
+
safetyMargin: await this.getLendingPool().getSafetyMargin(),
|
|
101
|
+
liquidationPenalty: await this.getLendingPool().getLiquidationPenalty(),
|
|
102
|
+
availableToBorrowX: await this.getNftlp().getLendingPool().getBorrowableA().getAvailableToBorrow(),
|
|
103
|
+
availableToBorrowY: await this.getNftlp().getLendingPool().getBorrowableB().getAvailableToBorrow(),
|
|
104
|
+
lockStateChange,
|
|
105
|
+
});
|
|
91
106
|
}
|
|
92
107
|
async createNewPositionObject(fee, priceA, priceB, lockStateChange = false) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
108
|
+
return new uniswapV3_1.default({
|
|
109
|
+
liquidity: 0,
|
|
110
|
+
unclaimedFeesX: 0,
|
|
111
|
+
unclaimedFeesY: 0,
|
|
112
|
+
debtX: 0,
|
|
113
|
+
debtY: 0,
|
|
114
|
+
marketPrice: await this.getNftlp().getMarketPrice(),
|
|
115
|
+
oraclePrice: await this.getNftlp().getOraclePrice(),
|
|
116
|
+
priceA: await this.getNftlp().nearestUsableTickPrice(priceA, fee),
|
|
117
|
+
priceB: await this.getNftlp().nearestUsableTickPrice(priceB, fee),
|
|
118
|
+
safetyMargin: await this.getLendingPool().getSafetyMargin(),
|
|
119
|
+
liquidationPenalty: await this.getLendingPool().getLiquidationPenalty(),
|
|
120
|
+
availableToBorrowX: await this.getNftlp().getLendingPool().getBorrowableA().getAvailableToBorrow(),
|
|
121
|
+
availableToBorrowY: await this.getNftlp().getLendingPool().getBorrowableB().getAvailableToBorrow(),
|
|
122
|
+
lockStateChange,
|
|
123
|
+
});
|
|
96
124
|
}
|
|
97
125
|
async getPositionObject(tokenId) {
|
|
98
126
|
if (!this.cache.positionsObject)
|
|
@@ -24,6 +24,10 @@ export default abstract class OnchainBorrowable extends OnchainPoolToken {
|
|
|
24
24
|
protected abstract initializePoolToken(): Promise<Contract>;
|
|
25
25
|
protected initializeToken(): Promise<Contract>;
|
|
26
26
|
isETH(): Promise<boolean>;
|
|
27
|
+
getAvailableToBorrow(): Promise<number>;
|
|
28
|
+
/***
|
|
29
|
+
* TODO remove this part
|
|
30
|
+
**/
|
|
27
31
|
private initializeBorrowTracker;
|
|
28
32
|
getBorrowTracker(): Promise<string>;
|
|
29
33
|
hasFarming(): Promise<boolean>;
|
|
@@ -37,6 +37,12 @@ class OnchainBorrowable extends onchainPoolToken_1.default {
|
|
|
37
37
|
const underlying = (await this.getToken())._address;
|
|
38
38
|
return (underlying.toLowerCase() === weths_1.WETH[this.getLendingPool().getImpermaxFactory().getOnchain().network].toLowerCase());
|
|
39
39
|
}
|
|
40
|
+
async getAvailableToBorrow() {
|
|
41
|
+
return await this.getTotalBalance();
|
|
42
|
+
}
|
|
43
|
+
/***
|
|
44
|
+
* TODO remove this part
|
|
45
|
+
**/
|
|
40
46
|
// Farming Pool
|
|
41
47
|
async initializeBorrowTracker() {
|
|
42
48
|
const borrowable = await this.getPoolToken();
|
|
@@ -2,6 +2,22 @@ import type { Position } from "../interface";
|
|
|
2
2
|
/**
|
|
3
3
|
* TODO: Cache for initialRealX, intialCollateralValue, etc...
|
|
4
4
|
*/
|
|
5
|
+
export interface UniswapV3PositionParams {
|
|
6
|
+
liquidity: number;
|
|
7
|
+
unclaimedFeesX: number;
|
|
8
|
+
unclaimedFeesY: number;
|
|
9
|
+
debtX: number;
|
|
10
|
+
debtY: number;
|
|
11
|
+
priceA: number;
|
|
12
|
+
priceB: number;
|
|
13
|
+
marketPrice: number;
|
|
14
|
+
oraclePrice: number;
|
|
15
|
+
safetyMargin: number;
|
|
16
|
+
liquidationPenalty: number;
|
|
17
|
+
availableToBorrowX?: number;
|
|
18
|
+
availableToBorrowY?: number;
|
|
19
|
+
lockStateChange?: boolean;
|
|
20
|
+
}
|
|
5
21
|
export declare class UniswapV3Position implements Position {
|
|
6
22
|
lockStateChange: boolean;
|
|
7
23
|
state: number;
|
|
@@ -18,13 +34,15 @@ export declare class UniswapV3Position implements Position {
|
|
|
18
34
|
initialUnclaimedFeesY: number;
|
|
19
35
|
initialDebtX: number;
|
|
20
36
|
initialDebtY: number;
|
|
21
|
-
marketPrice: number;
|
|
22
|
-
oraclePrice: number;
|
|
23
37
|
priceA: number;
|
|
24
38
|
priceB: number;
|
|
39
|
+
marketPrice: number;
|
|
40
|
+
oraclePrice: number;
|
|
25
41
|
safetyMargin: number;
|
|
26
42
|
liquidationPenalty: number;
|
|
27
|
-
|
|
43
|
+
availableToBorrowX: number;
|
|
44
|
+
availableToBorrowY: number;
|
|
45
|
+
constructor(params: UniswapV3PositionParams);
|
|
28
46
|
private checkLock;
|
|
29
47
|
/**
|
|
30
48
|
* PRIVATE SETTERS
|
|
@@ -20,28 +20,27 @@ function getRealY(liquidity, price, priceA, priceB) {
|
|
|
20
20
|
const LOWEST_PRICE = 1 / 1e18;
|
|
21
21
|
const HIGHEST_PRICE = 1e18;
|
|
22
22
|
const FEE_COLLECTED_WEIGHT = 0.95;
|
|
23
|
-
/**
|
|
24
|
-
* TODO: Cache for initialRealX, intialCollateralValue, etc...
|
|
25
|
-
*/
|
|
26
23
|
class UniswapV3Position {
|
|
27
|
-
constructor(
|
|
28
|
-
this.liquidity =
|
|
29
|
-
this.unclaimedFeesX =
|
|
30
|
-
this.unclaimedFeesY =
|
|
31
|
-
this.debtX =
|
|
32
|
-
this.debtY =
|
|
33
|
-
this.initialLiquidity =
|
|
34
|
-
this.initialUnclaimedFeesX =
|
|
35
|
-
this.initialUnclaimedFeesY =
|
|
36
|
-
this.initialDebtX =
|
|
37
|
-
this.initialDebtY =
|
|
38
|
-
this.marketPrice =
|
|
39
|
-
this.oraclePrice =
|
|
40
|
-
this.priceA =
|
|
41
|
-
this.priceB =
|
|
42
|
-
this.safetyMargin =
|
|
43
|
-
this.liquidationPenalty =
|
|
44
|
-
this.
|
|
24
|
+
constructor(params) {
|
|
25
|
+
this.liquidity = params.liquidity;
|
|
26
|
+
this.unclaimedFeesX = params.unclaimedFeesX;
|
|
27
|
+
this.unclaimedFeesY = params.unclaimedFeesY;
|
|
28
|
+
this.debtX = params.debtX;
|
|
29
|
+
this.debtY = params.debtY;
|
|
30
|
+
this.initialLiquidity = params.liquidity;
|
|
31
|
+
this.initialUnclaimedFeesX = params.unclaimedFeesX;
|
|
32
|
+
this.initialUnclaimedFeesY = params.unclaimedFeesY;
|
|
33
|
+
this.initialDebtX = params.debtX;
|
|
34
|
+
this.initialDebtY = params.debtY;
|
|
35
|
+
this.marketPrice = params.marketPrice;
|
|
36
|
+
this.oraclePrice = params.oraclePrice;
|
|
37
|
+
this.priceA = params.priceA;
|
|
38
|
+
this.priceB = params.priceB;
|
|
39
|
+
this.safetyMargin = params.safetyMargin;
|
|
40
|
+
this.liquidationPenalty = params.liquidationPenalty;
|
|
41
|
+
this.availableToBorrowX = params.availableToBorrowX ?? 0;
|
|
42
|
+
this.availableToBorrowY = params.availableToBorrowY ?? 0;
|
|
43
|
+
this.lockStateChange = params.lockStateChange ?? true;
|
|
45
44
|
this.state = 0;
|
|
46
45
|
}
|
|
47
46
|
checkLock() {
|