impermax-sdk 2.1.293 → 2.1.295

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.
@@ -8,17 +8,17 @@ interface PositionData {
8
8
  liquidity: number;
9
9
  feeGrowthInside0LastX128: number;
10
10
  feeGrowthInside1LastX128: number;
11
- unclaimedFees0: number;
12
- unclaimedFees1: number;
11
+ storedUnclaimedFees0: number;
12
+ storedUnclaimedFees1: number;
13
13
  storedTotalFeesEarned0: number;
14
14
  storedTotalFeesEarned1: number;
15
15
  }
16
16
  export type UniswapV3PositionData = NftlpPosition & {
17
17
  nftlp: PositionData;
18
18
  };
19
- interface PositionFees {
20
- unclaimedFees0: number;
21
- unclaimedFees1: number;
19
+ interface FeeAmounts {
20
+ amountX: number;
21
+ amountY: number;
22
22
  }
23
23
  export default class OffchainAccountNftlpUniswapV3 extends OffchainAccountNftlp {
24
24
  protected cache: {
@@ -28,8 +28,8 @@ export default class OffchainAccountNftlpUniswapV3 extends OffchainAccountNftlp
28
28
  positionsObject?: {
29
29
  [keys in number]: Promise<UniswapV3Position>;
30
30
  };
31
- pendingFees?: {
32
- [keys in number]: PositionFees;
31
+ newlyEarnedFees?: {
32
+ [keys in number]: FeeAmounts;
33
33
  };
34
34
  };
35
35
  getNftlp: () => any;
@@ -43,10 +43,10 @@ export default class OffchainAccountNftlpUniswapV3 extends OffchainAccountNftlp
43
43
  getLiquidity(tokenId: number): Promise<number>;
44
44
  createPositionObject(tokenId: number): Promise<UniswapV3Position>;
45
45
  getPositionObject(tokenId: number): Promise<UniswapV3Position>;
46
- getUnclaimedFees(tokenId: number): Promise<PositionFees>;
47
- getTotalAccruedFees(tokenId: number): Promise<{
48
- accruedFees0: number;
49
- accruedFees1: number;
50
- }>;
46
+ getNewlyEarnedFees(tokenId: number): Promise<FeeAmounts>;
47
+ getStoredUnclaimedFees(tokenId: number): Promise<FeeAmounts>;
48
+ getStoredTotalFeesEarned(tokenId: number): Promise<FeeAmounts>;
49
+ getUnclaimedFees(tokenId: number): Promise<FeeAmounts>;
50
+ getTotalFeesEarned(tokenId: number): Promise<FeeAmounts>;
51
51
  }
52
52
  export {};
@@ -47,11 +47,11 @@ class OffchainAccountNftlpUniswapV3 extends offchainAccountNftlp_1.default {
47
47
  return (0, uniswapV3General_1.formatUniV3Liquidity)(liquidity, decimalsA, decimalsB);
48
48
  }
49
49
  async createPositionObject(tokenId) {
50
- const { unclaimedFees0, unclaimedFees1 } = await this.getUnclaimedFees(tokenId);
50
+ const unclaimedFees = await this.getUnclaimedFees(tokenId);
51
51
  return new uniswapV3_1.default({
52
52
  liquidity: await this.getLiquidity(tokenId),
53
- unclaimedFeesX: unclaimedFees0,
54
- unclaimedFeesY: unclaimedFees1,
53
+ unclaimedFeesX: unclaimedFees.amountX,
54
+ unclaimedFeesY: unclaimedFees.amountY,
55
55
  debtX: await this.getLendingPool().getBorrowableA().getBorrowedAmount(tokenId),
56
56
  debtY: await this.getLendingPool().getBorrowableB().getBorrowedAmount(tokenId),
57
57
  marketPrice: await this.getNftlp().getMarketPrice(),
@@ -72,35 +72,51 @@ class OffchainAccountNftlpUniswapV3 extends offchainAccountNftlp_1.default {
72
72
  this.cache.positionsObject[tokenId] = this.createPositionObject(tokenId);
73
73
  return this.cache.positionsObject[tokenId];
74
74
  }
75
- async getUnclaimedFees(tokenId) {
76
- if (!this.cache.pendingFees)
77
- this.cache.pendingFees = {};
78
- if (tokenId in this.cache.pendingFees)
79
- return this.cache.pendingFees[tokenId];
75
+ async getNewlyEarnedFees(tokenId) {
76
+ if (!this.cache.newlyEarnedFees)
77
+ this.cache.newlyEarnedFees = {};
78
+ if (tokenId in this.cache.newlyEarnedFees)
79
+ return this.cache.newlyEarnedFees[tokenId];
80
80
  const network = this.getLendingPool().getLendingPool().getOffchain().network;
81
81
  const factory = this.getLendingPool().getLendingPool().getFactory();
82
82
  const lendingPoolId = this.getLendingPool().getLendingPool().getPairAddress();
83
83
  const api = private_api_1.NFTLP_FEE_API[network][factory];
84
84
  if (!api) {
85
- console.log(`Missing Fees API for ${lendingPoolId} on ${network}?`);
86
- return { unclaimedFees0: 0, unclaimedFees1: 0 };
85
+ console.log(`Missing newly earned fees API for ${lendingPoolId} on ${network}?`);
86
+ return { amountX: 0, amountY: 0 };
87
87
  }
88
88
  try {
89
89
  const positionFees = await fetch(api.concat(`collect/${lendingPoolId}/${tokenId}`)).then(i => i.json());
90
- this.cache.pendingFees[tokenId] = { unclaimedFees0: Number(positionFees.feesEarned0), unclaimedFees1: Number(positionFees.feesEarned1) };
91
- return this.cache.pendingFees[tokenId];
90
+ this.cache.newlyEarnedFees[tokenId] = { amountX: Number(positionFees.newlyEarnedFees0), amountY: Number(positionFees.newlyEarnedFees1) };
91
+ return this.cache.newlyEarnedFees[tokenId];
92
92
  }
93
93
  catch (err) {
94
- console.log(`No pending fees data for ${lendingPoolId} on ${network}`);
95
- return { unclaimedFees0: 0, unclaimedFees1: 0 };
94
+ console.log(`No newly earned fees data for ${lendingPoolId} on ${network}`);
95
+ return { amountX: 0, amountY: 0 };
96
96
  }
97
97
  }
98
- async getTotalAccruedFees(tokenId) {
98
+ async getStoredUnclaimedFees(tokenId) {
99
+ const { storedUnclaimedFees0, storedUnclaimedFees1 } = (await this.getPositionData(tokenId));
100
+ return { amountX: Number(storedUnclaimedFees0), amountY: Number(storedUnclaimedFees1) };
101
+ }
102
+ async getStoredTotalFeesEarned(tokenId) {
99
103
  const { storedTotalFeesEarned0, storedTotalFeesEarned1 } = (await this.getPositionData(tokenId));
100
- const { unclaimedFees0, unclaimedFees1 } = await this.getUnclaimedFees(tokenId);
104
+ return { amountX: Number(storedTotalFeesEarned0), amountY: Number(storedTotalFeesEarned1) };
105
+ }
106
+ async getUnclaimedFees(tokenId) {
107
+ const storedUnclaimedFees = await this.getStoredUnclaimedFees(tokenId);
108
+ const newlyEarnedFees = await this.getNewlyEarnedFees(tokenId);
109
+ return {
110
+ amountX: storedUnclaimedFees.amountX + newlyEarnedFees.amountX,
111
+ amountY: storedUnclaimedFees.amountY + newlyEarnedFees.amountY,
112
+ };
113
+ }
114
+ async getTotalFeesEarned(tokenId) {
115
+ const storedTotalFeesEarned = await this.getStoredTotalFeesEarned(tokenId);
116
+ const newlyEarnedFees = await this.getNewlyEarnedFees(tokenId);
101
117
  return {
102
- accruedFees0: Number(storedTotalFeesEarned0) + unclaimedFees0,
103
- accruedFees1: Number(storedTotalFeesEarned1) + unclaimedFees1,
118
+ amountX: storedTotalFeesEarned.amountX + newlyEarnedFees.amountX,
119
+ amountY: storedTotalFeesEarned.amountY + newlyEarnedFees.amountY,
104
120
  };
105
121
  }
106
122
  }
@@ -43,13 +43,9 @@ class OffchainNftlpUniswapV3 extends offchainNftlp_1.default {
43
43
  const feeRaw = fee * 1000000;
44
44
  const nftlpUniswapV3Data = await this.getNftlpData();
45
45
  const pools = nftlpUniswapV3Data.uniswapV3Pools;
46
- console.log("feeRaw", feeRaw);
47
- console.log("pools", pools);
48
46
  for (let pool of pools) {
49
47
  if (parseInt(pool.fee) == feeRaw) {
50
- console.log("initializeUniswapV3Pool");
51
48
  const liquidity = parseInt(pool.liquidity);
52
- console.log("liquidity", liquidity);
53
49
  const decimalsA = await this.getLendingPool().getBorrowableA().getDecimals();
54
50
  const decimalsB = await this.getLendingPool().getBorrowableB().getDecimals();
55
51
  return {
@@ -83,10 +79,6 @@ class OffchainNftlpUniswapV3 extends offchainNftlp_1.default {
83
79
  const llamaApyBase = apyBase / 100;
84
80
  const apr1dFees = volumeUsd1d * fee * 365 / tvlUsd;
85
81
  const apr7dFees = volumeUsd7d * fee / 7 * 365 / tvlUsd;
86
- console.log("llamaApyBase", llamaApyBase);
87
- console.log("apr1dFees", apr1dFees);
88
- console.log("apr7dFees", apr7dFees);
89
- console.log("tvlUsd", tvlUsd);
90
82
  // convert llama apy to rate
91
83
  const avgRate = apr7dFees / utils_1.SECONDS_IN_YEAR;
92
84
  // calculate the usd price of 1 unit of formatted liquidity
@@ -97,11 +89,6 @@ class OffchainNftlpUniswapV3 extends offchainNftlp_1.default {
97
89
  const baseLiquidityUsd = pool.liquidity * baseLiquidityPrice;
98
90
  const avgConcentrationFactor = baseLiquidityUsd / tvlUsd;
99
91
  const fullRangeRate = avgRate / avgConcentrationFactor;
100
- console.log("avgRate", avgRate);
101
- console.log("baseLiquidityPrice", baseLiquidityPrice);
102
- console.log("baseLiquidityUsd", baseLiquidityUsd);
103
- console.log("avgConcentrationFactor", avgConcentrationFactor);
104
- console.log("fullRangeRate", fullRangeRate);
105
92
  return { fullRangeRate, tvlUsd };
106
93
  }
107
94
  async getFullRangeRate(fee) {
@@ -116,15 +103,11 @@ class OffchainNftlpUniswapV3 extends offchainNftlp_1.default {
116
103
  totalTvl += tvlUsd;
117
104
  totalCompoundedRate += fullRangeRate * tvlUsd;
118
105
  }
119
- console.log("totalTvl", totalTvl);
120
- console.log("totalCompoundedRate", totalCompoundedRate);
121
106
  return totalTvl > 0 ? totalCompoundedRate / totalTvl : 0;
122
107
  }
123
108
  // calculate the pair APR assuming a +-30% price range
124
109
  async getTotalAPR() {
125
110
  const fullRangeAPR = await this.getWeightedAvgFullRangeRate() * utils_1.SECONDS_IN_YEAR;
126
- console.log("fullRangeAPR", fullRangeAPR);
127
- console.log("uniV3ConcentrationFactor", (0, uniswapV3General_1.uniV3ConcentrationFactor)(1 / 1.3, 1.3));
128
111
  return fullRangeAPR * (0, uniswapV3General_1.uniV3ConcentrationFactor)(1 / 1.3, 1.3);
129
112
  }
130
113
  }
@@ -494,6 +494,8 @@ class PonderQueryBuilder {
494
494
  feeGrowthInside1LastX128
495
495
  storedTotalFeesEarned0
496
496
  storedTotalFeesEarned1
497
+ storedUnclaimedFees0
498
+ storedUnclaimedFees1
497
499
  id
498
500
  nftlp {
499
501
  factory {
@@ -91,7 +91,6 @@ function uniV3ConcentrationFactor(priceA, priceB) {
91
91
  if (priceB > exports.MAX_PRICE)
92
92
  priceB = exports.MAX_PRICE;
93
93
  const deviationFromAvg = Math.sqrt(priceB / priceA);
94
- console.log("deviationFromAvg", deviationFromAvg);
95
94
  return 1 / (1 - 1 / Math.sqrt(deviationFromAvg));
96
95
  }
97
96
  exports.uniV3ConcentrationFactor = uniV3ConcentrationFactor;
@@ -80,9 +80,11 @@ class UniswapV3Position extends genericPosition_1.default {
80
80
  return this.getInitialRealY() + this.initialUnclaimedFeesY;
81
81
  }
82
82
  getDepositedX() {
83
+ console.log("getDepositedX", this.getRealX() + this.unclaimedFeesX, this.getRealX(), this.unclaimedFeesX);
83
84
  return this.getRealX() + this.unclaimedFeesX;
84
85
  }
85
86
  getDepositedY() {
87
+ console.log("getDepositedY", this.getRealY() + this.unclaimedFeesY, this.getRealY(), this.unclaimedFeesY);
86
88
  return this.getRealY() + this.unclaimedFeesY;
87
89
  }
88
90
  getBaseRate() {
@@ -92,8 +94,6 @@ class UniswapV3Position extends genericPosition_1.default {
92
94
  if (this.priceB < this.marketPrice)
93
95
  return 0;
94
96
  // otherwise calculate the rate based on the concentration factor
95
- console.log("this.fullRangeRate", this.fullRangeRate);
96
- console.log("getBaseRate", this.fullRangeRate * (0, uniswapV3General_1.uniV3ConcentrationFactor)(this.priceA, this.priceB));
97
97
  return this.fullRangeRate * (0, uniswapV3General_1.uniV3ConcentrationFactor)(this.priceA, this.priceB);
98
98
  }
99
99
  // maxAmountX and maxAmountY are expected to have the same sign
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.293",
3
+ "version": "2.1.295",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",