impermax-sdk 2.1.166 → 2.1.167

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.
@@ -20,7 +20,14 @@ export default class OffchainAccount {
20
20
  getBorrowPositions(): Promise<FactoryIndex<Address[]>>;
21
21
  getSupplyPositions(): Promise<FactoryIndex<Address[]>>;
22
22
  getVaultsSuppliedUSD(): Promise<number>;
23
+ getNftlpPositions(): Promise<FactoryIndex<Address[]>>;
23
24
  getVaultsEarningsUSD(): Promise<number>;
24
25
  getVaultsYearlyYieldUSD(): Promise<number>;
25
- getNftlpPositions(): Promise<FactoryIndex<Address[]>>;
26
+ getVaultsDailyYieldUSD(): Promise<number>;
27
+ getSupplyEarningsUSD(): Promise<number>;
28
+ getSupplyYearlyYieldUSD(): Promise<number>;
29
+ getSupplyDailyYieldUSD(): Promise<number>;
30
+ getTotalEarningsUSD(): Promise<number>;
31
+ getTotalYearlyYieldUSD(): Promise<number>;
32
+ getTotalDailyYieldUSD(): Promise<number>;
26
33
  }
@@ -79,6 +79,29 @@ class OffchainAccount {
79
79
  }
80
80
  return suppliedUSD;
81
81
  }
82
+ async getNftlpPositions() {
83
+ const result = {};
84
+ const userData = await this.getUserData();
85
+ if (userData === null)
86
+ return result;
87
+ for (const factory in userData) {
88
+ // Check for V3 explicitely, not necesary since `positions` doesn't exist in v2
89
+ if (!(0, offchainTypes_1.isV3Factory)(factory))
90
+ continue;
91
+ result[factory] = [];
92
+ const collateralPositions = userData[factory].positions;
93
+ for (const lendingPool in collateralPositions) {
94
+ result[factory].push(lendingPool);
95
+ }
96
+ }
97
+ return result;
98
+ }
99
+ // -------------------------------
100
+ // Earnings
101
+ // ------------------------------
102
+ //
103
+ // Vaults
104
+ //
82
105
  async getVaultsEarningsUSD() {
83
106
  let earningsUSD = 0;
84
107
  const vaultsUserData = await this.getVaultsUserData();
@@ -109,22 +132,83 @@ class OffchainAccount {
109
132
  }
110
133
  return yieldUSD;
111
134
  }
112
- async getNftlpPositions() {
113
- const result = {};
114
- const userData = await this.getUserData();
115
- if (userData === null)
116
- return result;
117
- for (const factory in userData) {
118
- // Check for V3 explicitely, not necesary since `positions` doesn't exist in v2
119
- if (!(0, offchainTypes_1.isV3Factory)(factory))
120
- continue;
121
- result[factory] = [];
122
- const collateralPositions = userData[factory].positions;
123
- for (const lendingPool in collateralPositions) {
124
- result[factory].push(lendingPool);
135
+ async getVaultsDailyYieldUSD() {
136
+ const yearlyYield = await this.getSupplyYearlyYieldUSD();
137
+ return yearlyYield / 365;
138
+ }
139
+ //
140
+ // Borrowables
141
+ //
142
+ async getSupplyEarningsUSD() {
143
+ let earningsUSD = 0;
144
+ const positionsUserData = await this.getSupplyPositions();
145
+ if (!positionsUserData)
146
+ return 0;
147
+ // Get positions by factory
148
+ for (const factory in positionsUserData) {
149
+ const poolsOfFactory = positionsUserData[factory];
150
+ // Get pools of this factory
151
+ for (const poolAddress of poolsOfFactory) {
152
+ const pool = await this.getLendingPool(factory, poolAddress);
153
+ // Get earnings from both borrowables
154
+ const [earningsA, earningsB] = await Promise.all([
155
+ pool.getBorrowableA().getEarningsUSD(),
156
+ pool.getBorrowableB().getEarningsUSD()
157
+ ]);
158
+ earningsUSD += earningsA + earningsB;
125
159
  }
126
160
  }
127
- return result;
161
+ return earningsUSD;
162
+ }
163
+ async getSupplyYearlyYieldUSD() {
164
+ let yieldUSD = 0;
165
+ const positionsUserData = await this.getSupplyPositions();
166
+ if (!positionsUserData)
167
+ return 0;
168
+ // Get positions by factory
169
+ for (const factory in positionsUserData) {
170
+ const poolsOfFactory = positionsUserData[factory];
171
+ // Get pools of this factory
172
+ for (const poolAddress of poolsOfFactory) {
173
+ const pool = await this.getLendingPool(factory, poolAddress);
174
+ // Get yearly yield = value * APR
175
+ const [valueA, valueB, aprA, aprB] = await Promise.all([
176
+ pool.getBorrowableA().getValue(),
177
+ pool.getBorrowableB().getValue(),
178
+ pool.getBorrowableA().getPoolToken().getSupplyAPR(),
179
+ pool.getBorrowableB().getPoolToken().getSupplyAPR()
180
+ ]);
181
+ yieldUSD += valueA * aprA + valueB * aprB;
182
+ }
183
+ }
184
+ return yieldUSD;
185
+ }
186
+ async getSupplyDailyYieldUSD() {
187
+ // Daily yield is yearly yield / 365
188
+ const yearlyYield = await this.getSupplyYearlyYieldUSD();
189
+ return yearlyYield / 365;
190
+ }
191
+ // TODO: Collaterals
192
+ //
193
+ // Overview
194
+ //
195
+ async getTotalEarningsUSD() {
196
+ const [supplyEarnings, vaultEarnings] = await Promise.all([
197
+ this.getSupplyEarningsUSD(),
198
+ this.getVaultsEarningsUSD()
199
+ ]);
200
+ return supplyEarnings + vaultEarnings;
201
+ }
202
+ async getTotalYearlyYieldUSD() {
203
+ const [supplyYield, vaultYield] = await Promise.all([
204
+ this.getSupplyYearlyYieldUSD(),
205
+ this.getVaultsYearlyYieldUSD()
206
+ ]);
207
+ return supplyYield + vaultYield;
208
+ }
209
+ async getTotalDailyYieldUSD() {
210
+ const yearlyYield = await this.getTotalYearlyYieldUSD();
211
+ return yearlyYield / 365;
128
212
  }
129
213
  }
130
214
  exports.default = OffchainAccount;
@@ -0,0 +1,23 @@
1
+ import { AddressIndex, Factory } from "../../config/types";
2
+ import { NftlpPosition, BorrowPosition } from "../offchainTypes";
3
+ import Offchain from "../../offchain";
4
+ type PositionV3 = {
5
+ id: string;
6
+ userId: string;
7
+ tokenId: string;
8
+ lendingPool: {
9
+ id: string;
10
+ nftlp: {
11
+ factory: string;
12
+ };
13
+ };
14
+ borrowPositions: BorrowPosition[];
15
+ };
16
+ export declare function getAllV3PositionsData(this: Offchain, factory: Factory): Promise<AddressIndex<{
17
+ [key: string]: NftlpPosition[];
18
+ }>>;
19
+ export declare function initializeAllV3Positions(this: Offchain, factory: Factory): Promise<AddressIndex<{
20
+ [key: string]: NftlpPosition[];
21
+ }>>;
22
+ export declare function fetchAllV3Positions(this: Offchain, factory: Factory): Promise<PositionV3[]>;
23
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchAllV3Positions = exports.initializeAllV3Positions = exports.getAllV3PositionsData = void 0;
4
+ const nftlp_1 = require("../../config/nftlp");
5
+ const subgraphs_1 = require("../../config/subgraphs");
6
+ /*--------------------------------------------------------------------------*
7
+ * Getters *
8
+ *--------------------------------------------------------------------------*/
9
+ async function getAllV3PositionsData(factory) {
10
+ if (!this.allV3PositionsData[factory])
11
+ this.allV3PositionsData[factory] = this.initializeAllV3Positions(factory);
12
+ return this.allV3PositionsData[factory];
13
+ }
14
+ exports.getAllV3PositionsData = getAllV3PositionsData;
15
+ /*--------------------------------------------------------------------------*
16
+ * Initializers *
17
+ *--------------------------------------------------------------------------*/
18
+ async function initializeAllV3Positions(factory) {
19
+ const globalPositions = await this.fetchAllV3Positions(factory);
20
+ const positions = {};
21
+ for (const position of globalPositions) {
22
+ const lendingPoolId = position.lendingPool.id;
23
+ const userAddress = position.userId;
24
+ const nftlpFactory = position.lendingPool.nftlp.factory;
25
+ // Initialize empty if need
26
+ if (!positions[lendingPoolId])
27
+ positions[lendingPoolId] = {};
28
+ if (!positions[lendingPoolId][userAddress])
29
+ positions[lendingPoolId][userAddress] = [];
30
+ const dex = (0, nftlp_1.getNftlpDex)(this.network, nftlpFactory);
31
+ if (!dex) {
32
+ console.warn(`Unknown DEX for NFTLP factory ${nftlpFactory} on ${this.network}`);
33
+ continue;
34
+ }
35
+ // Might not be needed for all positions??
36
+ const endpoints = (0, nftlp_1.getNftlpSubgraph)(this.network, dex);
37
+ if (!endpoints?.length) {
38
+ console.warn(`No NFTLP endpoints found for ${dex} on ${this.network}`);
39
+ continue;
40
+ }
41
+ const nftlpData = await this.getEndpointManager().fetch(endpoints, this.network, (queryBuilder) => queryBuilder.nftlpPositionQuery(lendingPoolId, position.tokenId));
42
+ // Create final position object
43
+ const nftlpPosition = { ...position, dex, nftlp: nftlpData.data.nftlpPositions?.[0] };
44
+ // Add to results
45
+ positions[lendingPoolId][userAddress].push(nftlpPosition);
46
+ }
47
+ return positions;
48
+ }
49
+ exports.initializeAllV3Positions = initializeAllV3Positions;
50
+ /*--------------------------------------------------------------------------*
51
+ * Fetchers *
52
+ *--------------------------------------------------------------------------*/
53
+ // Same as user/positionsV3
54
+ async function fetchAllV3Positions(factory) {
55
+ const subgraphs = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
56
+ if (!subgraphs?.length) {
57
+ console.warn(`No subgraphs found for factory ${factory} on ${this.network}`);
58
+ return [];
59
+ }
60
+ const allPositionsData = await this.getEndpointManager().fetch(subgraphs, this.network, (queryBuilder) => queryBuilder.allPositionsV3Query());
61
+ return allPositionsData?.data?.positions || [];
62
+ }
63
+ exports.fetchAllV3Positions = fetchAllV3Positions;
@@ -3,6 +3,7 @@ export * from './blocks';
3
3
  export * from './lendingPools';
4
4
  export * from './lendingPoolsPast';
5
5
  export * from './vaults';
6
+ export * from './allPositionsV3';
6
7
  export * from './user';
7
8
  export * from './whitelist';
8
9
  export * from './staking';
@@ -19,6 +19,7 @@ __exportStar(require("./blocks"), exports);
19
19
  __exportStar(require("./lendingPools"), exports);
20
20
  __exportStar(require("./lendingPoolsPast"), exports);
21
21
  __exportStar(require("./vaults"), exports);
22
+ __exportStar(require("./allPositionsV3"), exports);
22
23
  __exportStar(require("./user"), exports);
23
24
  __exportStar(require("./whitelist"), exports);
24
25
  __exportStar(require("./staking"), exports);
@@ -48,19 +48,7 @@ async function initializeV3Positions(offchain, factory, rawUserData) {
48
48
  }
49
49
  // 3. Fetch NFTLP Position from endpoint/endpoints
50
50
  const nftlpData = await offchain.getEndpointManager().fetch(endpoints, offchain.network, (queryBuilder) => queryBuilder.nftlpPositionQuery(lendingPoolId, position.tokenId));
51
- // NOTE: Is there a better way? Ordered by borrowable address, and UniswapV3
52
- // orders by token0 < token1
53
- // Sort borrow positions
54
- const sortedBorrowPositions = position.borrowPositions.sort((a, b) => BigInt(a.borrowable.underlying.id) < BigInt(b.borrowable.underlying.id)
55
- ? -1
56
- : 1);
57
- // Combine everything
58
- const nftlpPosition = {
59
- ...position,
60
- dex,
61
- borrowPositions: sortedBorrowPositions,
62
- nftlp: nftlpData.data.nftlpPositions[0] || undefined,
63
- };
51
+ const nftlpPosition = { ...position, dex, nftlp: nftlpData.data.nftlpPositions?.[0] };
64
52
  positions[lendingPoolId].push(nftlpPosition);
65
53
  }
66
54
  return { positions };
@@ -1,9 +1,6 @@
1
1
  import { Address, AddressIndex } from "../../../config/types";
2
2
  import { VaultPosition } from "../../offchainTypes";
3
3
  import Offchain from "../../offchain";
4
- /**
5
- * User Data
6
- */
7
- export declare function fetchVaultsUserData(this: Offchain, account: Address): Promise<AddressIndex<VaultPosition[]>>;
8
- export declare function initializeVaultsUserData(this: Offchain, account: Address): Promise<AddressIndex<AddressIndex<VaultPosition>>>;
9
4
  export declare function getVaultsUserData(this: Offchain, account: Address): Promise<AddressIndex<AddressIndex<VaultPosition>>>;
5
+ export declare function initializeVaultsUserData(this: Offchain, account: Address): Promise<AddressIndex<AddressIndex<VaultPosition>>>;
6
+ export declare function fetchVaultsUserData(this: Offchain, account: Address): Promise<AddressIndex<VaultPosition[]>>;
@@ -1,10 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getVaultsUserData = exports.initializeVaultsUserData = exports.fetchVaultsUserData = void 0;
3
+ exports.fetchVaultsUserData = exports.initializeVaultsUserData = exports.getVaultsUserData = void 0;
4
4
  const subgraphs_1 = require("../../../config/subgraphs");
5
- /**
6
- * User Data
7
- */
5
+ /*--------------------------------------------------------------------------*
6
+ * Getters *
7
+ *--------------------------------------------------------------------------*/
8
+ async function getVaultsUserData(account) {
9
+ if (!(account in this.vaultsUsersData))
10
+ this.vaultsUsersData[account] = this.initializeVaultsUserData(account);
11
+ return (await this.vaultsUsersData[account]) || {};
12
+ }
13
+ exports.getVaultsUserData = getVaultsUserData;
14
+ /*--------------------------------------------------------------------------*
15
+ * Initializers *
16
+ *--------------------------------------------------------------------------*/
17
+ async function initializeVaultsUserData(account) {
18
+ const userVaultData = await this.fetchVaultsUserData(account);
19
+ const result = {};
20
+ for (const vaultType in userVaultData) {
21
+ const positions = userVaultData[vaultType];
22
+ if (!positions)
23
+ continue;
24
+ result[vaultType] = {};
25
+ for (const position of positions) {
26
+ result[vaultType][position.lendingVault.id] = position;
27
+ }
28
+ }
29
+ return result;
30
+ }
31
+ exports.initializeVaultsUserData = initializeVaultsUserData;
32
+ /*--------------------------------------------------------------------------*
33
+ * Fetchers *
34
+ *--------------------------------------------------------------------------*/
8
35
  async function fetchVaultsUserData(account) {
9
36
  const calls = [];
10
37
  let vaultType;
@@ -36,24 +63,3 @@ async function fetchVaultsUserData(account) {
36
63
  return userVaultData;
37
64
  }
38
65
  exports.fetchVaultsUserData = fetchVaultsUserData;
39
- async function initializeVaultsUserData(account) {
40
- const userVaultData = await this.fetchVaultsUserData(account);
41
- const result = {};
42
- for (const vaultType in userVaultData) {
43
- const positions = userVaultData[vaultType];
44
- if (!positions)
45
- continue;
46
- result[vaultType] = {};
47
- for (const position of positions) {
48
- result[vaultType][position.lendingVault.id] = position;
49
- }
50
- }
51
- return result;
52
- }
53
- exports.initializeVaultsUserData = initializeVaultsUserData;
54
- async function getVaultsUserData(account) {
55
- if (!(account in this.vaultsUsersData))
56
- this.vaultsUsersData[account] = this.initializeVaultsUserData(account);
57
- return (await this.vaultsUsersData[account]) || {};
58
- }
59
- exports.getVaultsUserData = getVaultsUserData;
@@ -3,7 +3,7 @@ import OffchainLendingPool from './lendingPool';
3
3
  import OffchainSolidexHelper from './offchainSolidexHelper';
4
4
  import OffchainAccount from './account';
5
5
  import { Address, AddressIndex, Factory, FactoryIndex, LendingPoolIndex, Networks, WhitelistState, VaultType } from '../config/types';
6
- import { LendingPoolData, VaultData, VaultPosition, TvlData, UserData, XimxData, VaultBorrowable } from './offchainTypes';
6
+ import { LendingPoolData, VaultData, VaultPosition, TvlData, UserData, XimxData, VaultBorrowable, NftlpPosition } from './offchainTypes';
7
7
  import OffchainVault from './vault/offchainVault';
8
8
  import OffchainConfigManager from './configManager';
9
9
  import { LlamaTvlChart } from './offchainAPRHelper';
@@ -40,6 +40,11 @@ export default class Offchain {
40
40
  protected vaultBorrowablesData: {
41
41
  [key in VaultType]?: Promise<AddressIndex<VaultBorrowable[]>>;
42
42
  };
43
+ protected allV3PositionsData: {
44
+ [key in Factory]?: Promise<AddressIndex<{
45
+ [key: string]: NftlpPosition[];
46
+ }>>;
47
+ };
43
48
  constructor(offchainMultichain: OffchainMultichain, network: Networks, whitelist?: FactoryIndex<Address[]>);
44
49
  cleanCache(): void;
45
50
  getLendingPool(factory: Factory, pairAddress: Address): Promise<OffchainLendingPool>;
@@ -81,6 +86,9 @@ export default class Offchain {
81
86
  protected initializeVaultsUserData: typeof initializer.initializeVaultsUserData;
82
87
  protected fetchVaultBorrowables: typeof initializer.fetchVaultBorrowables;
83
88
  getVaultBorrowables: typeof initializer.getVaultBorrowables;
89
+ protected fetchAllV3Positions: typeof initializer.fetchAllV3Positions;
90
+ protected initializeAllV3Positions: typeof initializer.initializeAllV3Positions;
91
+ getAllV3PositionsData: typeof initializer.getAllV3PositionsData;
84
92
  /**
85
93
  * DATA GETTERS
86
94
  */
@@ -35,6 +35,7 @@ const chainId_1 = require("../config/chainId");
35
35
  class Offchain {
36
36
  constructor(offchainMultichain, network, whitelist) {
37
37
  this.whitelistedPairs = {};
38
+ this.allV3PositionsData = {};
38
39
  this.getSolidexHelper = () => this.solidexHelper;
39
40
  this.getConfigManager = () => this.configManager;
40
41
  // Multichain, 1 instance
@@ -80,6 +81,9 @@ class Offchain {
80
81
  this.initializeVaultsUserData = initializer.initializeVaultsUserData;
81
82
  this.fetchVaultBorrowables = initializer.fetchVaultBorrowables;
82
83
  this.getVaultBorrowables = initializer.getVaultBorrowables;
84
+ this.fetchAllV3Positions = initializer.fetchAllV3Positions;
85
+ this.initializeAllV3Positions = initializer.initializeAllV3Positions;
86
+ this.getAllV3PositionsData = initializer.getAllV3PositionsData;
83
87
  // TODO fix conflicting lines (commented)
84
88
  //this.network = cfg.network;
85
89
  //this.chainId = cfg.chainId;
@@ -109,6 +113,7 @@ class Offchain {
109
113
  this.configManager.cleanCache();
110
114
  this.getPriceHelper().cleanCache();
111
115
  this.getAPRHelper().cleanCache();
116
+ this.allV3PositionsData = {};
112
117
  }
113
118
  async getLendingPool(factory, pairAddress) {
114
119
  await this.getLendingPoolsData(); // make sure that lending pools are initialized
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.166",
3
+ "version": "2.1.167",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",