impermax-sdk 2.1.185 → 2.1.187

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.
@@ -0,0 +1,6 @@
1
+ import { Address, FactoryIndex } from '../../../config/types';
2
+ import Offchain from '../../offchain';
3
+ import OffchainAccount from '../../account';
4
+ export declare function getBorrowersList(this: Offchain): Promise<OffchainAccount[]>;
5
+ export declare function initializeBorrowersList(this: Offchain): Promise<OffchainAccount[]>;
6
+ export declare function fetchBorrowersList(this: Offchain): Promise<FactoryIndex<Address[]>>;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchBorrowersList = exports.initializeBorrowersList = exports.getBorrowersList = void 0;
4
+ const types_1 = require("../../../config/types");
5
+ const subgraphs_1 = require("../../../config/subgraphs");
6
+ /*--------------------------------------------------------------------------*
7
+ * Getters *
8
+ *--------------------------------------------------------------------------*/
9
+ async function getBorrowersList() {
10
+ if (!this.borrowersList)
11
+ this.borrowersList = this.initializeBorrowersList();
12
+ return this.borrowersList;
13
+ }
14
+ exports.getBorrowersList = getBorrowersList;
15
+ /*--------------------------------------------------------------------------*
16
+ * Initializers *
17
+ *--------------------------------------------------------------------------*/
18
+ async function initializeBorrowersList() {
19
+ const borrowersData = await this.fetchBorrowersList();
20
+ const uniqueBorrowers = new Set();
21
+ // Make borrowers unique across all lending pools
22
+ for (const factory in borrowersData) {
23
+ borrowersData[factory].forEach((b) => uniqueBorrowers.add(b.toLowerCase()));
24
+ }
25
+ return Array.from(uniqueBorrowers).map(address => this.getAccount(address));
26
+ }
27
+ exports.initializeBorrowersList = initializeBorrowersList;
28
+ /*--------------------------------------------------------------------------*
29
+ * Fetchers *
30
+ *--------------------------------------------------------------------------*/
31
+ async function fetchBorrowersList() {
32
+ const calls = [];
33
+ const factories = [];
34
+ let factory;
35
+ for (factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
36
+ if (factory === types_1.Factory.V3)
37
+ continue;
38
+ const subgraphs = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
39
+ calls.push(this.getEndpointManager().fetch(subgraphs, this.network, (queryBuilder) => queryBuilder.borrowersListQueryV2()));
40
+ factories.push(factory);
41
+ }
42
+ const results = await Promise.all(calls);
43
+ const borrowers = {};
44
+ results.forEach((result, index) => {
45
+ if (!result)
46
+ return;
47
+ // Get the factory for this result
48
+ const factory = factories[index];
49
+ const borrowPositions = result.data?.borrowPositions;
50
+ if (!borrowPositions)
51
+ return;
52
+ // Borrower addresses for this factory
53
+ borrowers[factory] = borrowPositions.map((i) => i.user.id);
54
+ });
55
+ return borrowers;
56
+ }
57
+ exports.fetchBorrowersList = fetchBorrowersList;
@@ -10,3 +10,4 @@ export * from './staking';
10
10
  export * from './farming';
11
11
  export * from './tvl';
12
12
  export * from './private-api';
13
+ export * from './borrowers';
@@ -26,3 +26,4 @@ __exportStar(require("./staking"), exports);
26
26
  __exportStar(require("./farming"), exports);
27
27
  __exportStar(require("./tvl"), exports);
28
28
  __exportStar(require("./private-api"), exports);
29
+ __exportStar(require("./borrowers"), exports);
@@ -45,6 +45,7 @@ export default class Offchain {
45
45
  [key: string]: NftlpPosition[];
46
46
  }>>;
47
47
  };
48
+ protected borrowersList: Promise<OffchainAccount[]> | null;
48
49
  constructor(offchainMultichain: OffchainMultichain, network: Networks, whitelist?: FactoryIndex<Address[]>);
49
50
  cleanCache(): void;
50
51
  getLendingPool(factory: Factory, pairAddress: Address): Promise<OffchainLendingPool>;
@@ -89,6 +90,9 @@ export default class Offchain {
89
90
  protected fetchAllV3Positions: typeof initializer.fetchAllV3Positions;
90
91
  protected initializeAllV3Positions: typeof initializer.initializeAllV3Positions;
91
92
  getAllV3PositionsData: typeof initializer.getAllV3PositionsData;
93
+ protected fetchBorrowersList: typeof initializer.fetchBorrowersList;
94
+ protected initializeBorrowersList: typeof initializer.initializeBorrowersList;
95
+ getBorrowersList: typeof initializer.getBorrowersList;
92
96
  /**
93
97
  * DATA GETTERS
94
98
  */
@@ -84,6 +84,9 @@ class Offchain {
84
84
  this.fetchAllV3Positions = initializer.fetchAllV3Positions;
85
85
  this.initializeAllV3Positions = initializer.initializeAllV3Positions;
86
86
  this.getAllV3PositionsData = initializer.getAllV3PositionsData;
87
+ this.fetchBorrowersList = initializer.fetchBorrowersList;
88
+ this.initializeBorrowersList = initializer.initializeBorrowersList;
89
+ this.getBorrowersList = initializer.getBorrowersList;
87
90
  // TODO fix conflicting lines (commented)
88
91
  //this.network = cfg.network;
89
92
  //this.chainId = cfg.chainId;
@@ -114,6 +117,7 @@ class Offchain {
114
117
  this.getPriceHelper().cleanCache();
115
118
  this.getAPRHelper().cleanCache();
116
119
  this.allV3PositionsData = {};
120
+ this.borrowersList = null;
117
121
  }
118
122
  async getLendingPool(factory, pairAddress) {
119
123
  await this.getLendingPoolsData(); // make sure that lending pools are initialized
@@ -11,6 +11,7 @@ import OffchainPriceHelper from "./offchainPriceHelper";
11
11
  import OffchainEndpointManager from './offchainEndpointManager';
12
12
  import OffchainMultichainAccount from "./account/offchainMultichainAccount";
13
13
  import { AddressIndex } from "../config/types";
14
+ import OffchainAccount from "./account/offchainAccount";
14
15
  export declare enum SortDirection {
15
16
  ASC = "asc",
16
17
  DESC = "desc"
@@ -31,6 +32,9 @@ export interface VaultListParams {
31
32
  stable?: boolean;
32
33
  highTvl?: boolean;
33
34
  }
35
+ export interface BorrowersListParams {
36
+ networks?: Array<Networks>;
37
+ }
34
38
  export declare enum LendingPoolListOrderBy {
35
39
  TVL = 0,
36
40
  TOTAL_SUPPLY = 1,
@@ -66,4 +70,5 @@ export default class OffchainMultichain {
66
70
  networkHasVault(network: Networks): boolean;
67
71
  getLendingPoolList(params?: LendingPoolListParams): Promise<Array<OffchainLendingPool>>;
68
72
  getMultichainAccount(accountAddress: Address): OffchainMultichainAccount;
73
+ getMultichainBorrowersList(params?: BorrowersListParams): Promise<OffchainAccount[]>;
69
74
  }
@@ -130,6 +130,20 @@ class OffchainMultichain {
130
130
  }
131
131
  return this.multichainAccounts[accountAddress];
132
132
  }
133
+ /*--------------------------------------------------------*
134
+ * Borrowers List
135
+ *--------------------------------------------------------*/
136
+ async getMultichainBorrowersList(params = {}) {
137
+ const allNetworks = params.networks || Object.values(types_1.Networks);
138
+ // Networks
139
+ const networkPromises = allNetworks.map(async (network) => {
140
+ const offchain = this.getOffchain(network);
141
+ const chainBorrowers = await offchain.getBorrowersList();
142
+ return chainBorrowers;
143
+ });
144
+ const allBorrowers = await Promise.all(networkPromises);
145
+ return [...new Set(allBorrowers.flat())];
146
+ }
133
147
  }
134
148
  exports.default = OffchainMultichain;
135
149
  _a = OffchainMultichain;
@@ -15,4 +15,5 @@ export declare class PonderQueryBuilder implements IQueryBuilder {
15
15
  proposalQuery(id: number): import("graphql").DocumentNode;
16
16
  ximxQuery(): import("graphql").DocumentNode;
17
17
  nftlpPositionQuery(lendingPoolId: string, tokenId: string): import("graphql").DocumentNode;
18
+ borrowersListQueryV2(): import("graphql").DocumentNode;
18
19
  }
@@ -482,5 +482,22 @@ class PonderQueryBuilder {
482
482
  }
483
483
  }`;
484
484
  }
485
+ /*-----------------------------*
486
+ * List of Borrowers V2 & V3
487
+ *-----------------------------*/
488
+ borrowersListQueryV2() {
489
+ return (0, graphql_tag_1.default) `{
490
+ borrowPositions(where: {borrowBalance_not: "0"}) {
491
+ items {
492
+ user {
493
+ id
494
+ }
495
+ }
496
+ }
497
+ _meta {
498
+ status
499
+ }
500
+ }`;
501
+ }
485
502
  }
486
503
  exports.PonderQueryBuilder = PonderQueryBuilder;
@@ -20,4 +20,5 @@ export declare class TheGraphQueryBuilder implements IQueryBuilder {
20
20
  pastVolumeQuery(blockNumber: number, addressesFilter: Address[]): import("graphql").DocumentNode;
21
21
  currentVolumeAndReservesQuery(addressesFilter: Address[]): import("graphql").DocumentNode;
22
22
  whitelistQuery(): import("graphql").DocumentNode;
23
+ borrowersListQueryV2(): import("graphql").DocumentNode;
23
24
  }
@@ -375,5 +375,14 @@ class TheGraphQueryBuilder {
375
375
  }
376
376
  }`;
377
377
  }
378
+ /*-----------------------------*
379
+ * List of Borrowers V2 & V3
380
+ *-----------------------------*/
381
+ borrowersListQueryV2() {
382
+ return (0, graphql_tag_1.default) `{
383
+ borrowPositions(where: {borrowBalance_gt: "0"}) { user { id } }
384
+ _meta { block { number } }
385
+ }`;
386
+ }
378
387
  }
379
388
  exports.TheGraphQueryBuilder = TheGraphQueryBuilder;
@@ -20,4 +20,5 @@ export interface IQueryBuilder {
20
20
  whitelistQuery?(): DocumentNode;
21
21
  currentVolumeAndReservesQuery?(addressesFilter: Address[]): DocumentNode;
22
22
  pastVolumeQuery?(blockNumber: number, addressesFilter: Address[]): DocumentNode;
23
+ borrowersListQueryV2(): DocumentNode;
23
24
  }
@@ -81,7 +81,6 @@ class OnchainInteractionsNftlpUniswapV3 extends onchainInteractionsNftlp_1.defau
81
81
  // Borrow and mint
82
82
  if ((borrowADelta > 0 && depositADelta > 0) || (borrowBDelta > 0 && depositBDelta > 0)) {
83
83
  // slightly increase borrowDelta in order to avoid amountUser to be dust
84
- // TODO what if one of these is negative?
85
84
  const actualBorrowADelta = Math.max(borrowADelta * this.getDust(), 0);
86
85
  const actualBorrowBDelta = Math.max(borrowBDelta * this.getDust(), 0);
87
86
  const amountAUser = Math.max(depositADelta - actualBorrowADelta, 0);
@@ -316,9 +316,9 @@ class UniswapV3Position {
316
316
  }
317
317
  // amountX and amountY are expected to have the same sign
318
318
  getOptimalLiquidity(amountX, amountY) {
319
- if (amountX != 0 && amountY != 0) {
320
- const sampleX = this.getRealXGivenLiquidity(1);
321
- const sampleY = this.getRealYGivenLiquidity(1);
319
+ const sampleX = this.getRealXGivenLiquidity(1);
320
+ const sampleY = this.getRealYGivenLiquidity(1);
321
+ if (sampleX != 0 && sampleY != 0) {
322
322
  if (sampleX == 0)
323
323
  amountX = 0;
324
324
  else if (sampleY == 0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "2.1.185",
3
+ "version": "2.1.187",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",