impermax-sdk 2.1.471 → 2.1.472

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.
@@ -6,6 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const offchainAccountNftlpAeroCL_1 = __importDefault(require("../../account/lendingPool/nftlp/offchainAccountNftlpAeroCL"));
7
7
  const types_1 = require("../../../config/types");
8
8
  const offchainNftlpGenericCL_1 = __importDefault(require("./offchainNftlpGenericCL"));
9
+ const Q128 = 2 ** 128;
10
+ const Q256 = BigInt(2) ** BigInt(256);
9
11
  class OffchainNftlpAeroCL extends offchainNftlpGenericCL_1.default {
10
12
  getExtension() {
11
13
  return types_1.Extension.AeroCL;
@@ -14,19 +16,48 @@ class OffchainNftlpAeroCL extends offchainNftlpGenericCL_1.default {
14
16
  return new offchainAccountNftlpAeroCL_1.default(accountCollateral);
15
17
  }
16
18
  async initializeFullRangeRateAccurate(tickSpacing) {
19
+ // To calculate the apr, we compare the current rewardGrowthGlobalX128 with the rewardGrowthGlobalX128 of 24 hours ago.
20
+ // If less than 24 hours of data are available, then we use the oldest feeGrowthGlobal available
21
+ // If there was an epoch change in the last 24 hours, we take the beginning of the epoch as the oldest point
22
+ const OLDEST_USABLE_DATA = 24 * 60 * 60;
17
23
  const { address } = await this.getGenericCLPool(tickSpacing);
18
24
  if (!address)
19
25
  return 0;
20
- let feeGrowthData = [];
26
+ let rewardGrowthData = [];
21
27
  try {
22
- feeGrowthData = await this.getLendingPool().getOffchain().getGenericCLPoolChart(this.getExtension(), address.toLowerCase());
28
+ rewardGrowthData = await this.getLendingPool().getOffchain().getGenericCLPoolChart(this.getExtension(), address.toLowerCase());
23
29
  }
24
30
  catch (err) { }
25
- if (!feeGrowthData || feeGrowthData.length < 1) {
31
+ if (!rewardGrowthData || rewardGrowthData.length < 2) {
26
32
  console.log(`No historic fee growth data available for aeroCL pool: ${address}`);
27
33
  return 0;
28
34
  }
29
- const rewardRatePerUnitOfLiquidity = Number(feeGrowthData[0].rewardRate) / Number(feeGrowthData[0].stakedLiquidity);
35
+ // the endpoint returns an array ordered from most recent to least recent
36
+ const mostRecentTimestamp = rewardGrowthData[0].timestamp;
37
+ const currentRewardGrowthGlobalX128 = BigInt(rewardGrowthData[0].rewardGrowthGlobalX128);
38
+ const currentRewardRate = rewardGrowthData[0].rewardRate;
39
+ let leastRecentTimestamp = 0;
40
+ let pastRewardGrowthGlobalX128;
41
+ for (const data of rewardGrowthData) {
42
+ if (mostRecentTimestamp - data.timestamp > OLDEST_USABLE_DATA)
43
+ break;
44
+ if (data.rewardRate != currentRewardRate)
45
+ break;
46
+ leastRecentTimestamp = data.timestamp;
47
+ pastRewardGrowthGlobalX128 = BigInt(data.feeGrowthGlobal0X128);
48
+ }
49
+ let rewardRatePerUnitOfLiquidity;
50
+ if (leastRecentTimestamp === 0) {
51
+ // we don't have enough data to calculate the average
52
+ rewardRatePerUnitOfLiquidity = Number(rewardGrowthData[0].rewardRate) / Number(rewardGrowthData[0].stakedLiquidity);
53
+ }
54
+ else {
55
+ const deltaTime = mostRecentTimestamp - leastRecentTimestamp;
56
+ let delta = currentRewardGrowthGlobalX128 - pastRewardGrowthGlobalX128;
57
+ if (delta < 0)
58
+ delta += Q256;
59
+ rewardRatePerUnitOfLiquidity = Number(delta) / Q128 / deltaTime;
60
+ }
30
61
  const priceA = await this.getLendingPool().getBorrowableA().getTokenPriceAccurate();
31
62
  const priceB = await this.getLendingPool().getBorrowableB().getTokenPriceAccurate();
32
63
  const decimalA = await this.getLendingPool().getBorrowableA().getDecimals();
@@ -64,9 +64,9 @@ class OffchainNftlpUniswapV3 extends offchainNftlpGenericCL_1.default {
64
64
  // and a feeGrowthGlobal at a previous point in time,
65
65
  // but this currently requires a separate call for each fee tier
66
66
  async initializeFullRangeRateAccurate(tickSpacing) {
67
- // To calculate the apr, we compare the current feeGrowthGlobal with the feeGrowthGlobal of 7 days ago.
68
- // If less than 7 days of data are available, then we use the oldest feeGrowthGlobal available
69
- const OLDEST_USABLE_DATA = 7 * 24 * 60 * 60;
67
+ // To calculate the apr, we compare the current feeGrowthGlobal with the feeGrowthGlobal of 24 hours ago.
68
+ // If less than 24 hours of data are available, then we use the oldest feeGrowthGlobal available
69
+ const OLDEST_USABLE_DATA = 24 * 60 * 60;
70
70
  const { address } = await this.getGenericCLPool(tickSpacing);
71
71
  if (!address)
72
72
  return 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.471",
3
+ "version": "2.1.472",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",