impermax-sdk 1.2.102 → 1.2.103

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.
@@ -1,5 +1,5 @@
1
1
  import { Address, NetworkFactoryIndex, NetworkIndex } from './types';
2
- export declare const IMPERMAX_SUBGRAPH_URL: NetworkFactoryIndex<Address>;
2
+ export declare const IMPERMAX_SUBGRAPH_URL: NetworkFactoryIndex<Address[]>;
3
3
  export declare const IMPERMAX_CHEF_SUBGRAPH_URL: NetworkIndex<string>;
4
4
  export declare const LENDING_VAULT_SUBGRAPH_URL: NetworkIndex<string>;
5
5
  export declare const BLOCKS_SUBGRAPH_URL: NetworkIndex<string>;
@@ -5,65 +5,77 @@ const types_1 = require("./types");
5
5
  exports.IMPERMAX_SUBGRAPH_URL = {
6
6
  [types_1.Networks.Ropsten]: {},
7
7
  [types_1.Networks.Mainnet]: {
8
- [types_1.Factory.V2V1]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/Dn5WZv53V1K8LrKqqYn29hhuhpwfuFfmsrsTqMgFjrD3'
8
+ [types_1.Factory.V2V1]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/Dn5WZv53V1K8LrKqqYn29hhuhpwfuFfmsrsTqMgFjrD3']
9
9
  },
10
10
  [types_1.Networks.Polygon]: {
11
- [types_1.Factory.V2V1_1]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/F3BfecWo2by5QKrwhkXwuXjyLZnQqJ1wqoejwe67KuyG',
12
- [types_1.Factory.V2V2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/5JbuAaWwyRm78yWpRCJJS217hXXft59g9MbsjTTZnYQa',
13
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/Giwg5N4SCehmUzNwXtAQZnZvUuTjcp7cr3nzUZosRDFU',
14
- [types_1.Factory.SOL_STABLE]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/EWckMdMiZeDSGHxq2V8gZBDF549Xjr9iKkPvisqckF11',
11
+ [types_1.Factory.V2V1_1]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/F3BfecWo2by5QKrwhkXwuXjyLZnQqJ1wqoejwe67KuyG'],
12
+ [types_1.Factory.V2V2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/5JbuAaWwyRm78yWpRCJJS217hXXft59g9MbsjTTZnYQa'],
13
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/Giwg5N4SCehmUzNwXtAQZnZvUuTjcp7cr3nzUZosRDFU'],
14
+ [types_1.Factory.SOL_STABLE]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/EWckMdMiZeDSGHxq2V8gZBDF549Xjr9iKkPvisqckF11'],
15
15
  },
16
16
  [types_1.Networks.Arbitrum]: {
17
- [types_1.Factory.V2V1_1]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/3PMyaq269mDMwhJ7E285RYD1r43x996b6xZmJSCGvtkm',
18
- [types_1.Factory.V2V2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/BkMRb8mR5zgCbjFSgau6s27pnANhR6k64RGVKUbWaZdY',
19
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/AdLbcSfG6JnpZ7RDdHNHq3bJ21waGnRi7AJ5x3YFGYFo',
17
+ [types_1.Factory.V2V1_1]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/3PMyaq269mDMwhJ7E285RYD1r43x996b6xZmJSCGvtkm'],
18
+ [types_1.Factory.V2V2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/BkMRb8mR5zgCbjFSgau6s27pnANhR6k64RGVKUbWaZdY'],
19
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/AdLbcSfG6JnpZ7RDdHNHq3bJ21waGnRi7AJ5x3YFGYFo'],
20
20
  },
21
21
  [types_1.Networks.Avalanche]: {
22
- [types_1.Factory.V2V1_2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/HXhHppWXhFqgLfdmTahFj2x7F5Xq8BwADxGY3nbCUAwr',
23
- [types_1.Factory.V2V2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/8Gz5ZGyRkSA7WBrSteeyez39AgZQsoEbtXe8GtHD7JMQ',
24
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/78e6cZp11r2coLx8YYo5y4AqzEhQXYT6sw3Bg5Tiy5XU',
22
+ [types_1.Factory.V2V1_2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/HXhHppWXhFqgLfdmTahFj2x7F5Xq8BwADxGY3nbCUAwr'],
23
+ [types_1.Factory.V2V2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/8Gz5ZGyRkSA7WBrSteeyez39AgZQsoEbtXe8GtHD7JMQ'],
24
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/78e6cZp11r2coLx8YYo5y4AqzEhQXYT6sw3Bg5Tiy5XU'],
25
25
  },
26
26
  [types_1.Networks.Mantle]: {
27
- [types_1.Factory.V2V2]: 'https://subgraph-api.mantle.xyz/subgraphs/name/impermax-finance/impermax-mantle-v2'
27
+ [types_1.Factory.V2V2]: ['https://subgraph-api.mantle.xyz/subgraphs/name/impermax-finance/impermax-mantle-v2']
28
28
  },
29
29
  [types_1.Networks.Moonriver]: {
30
- [types_1.Factory.V2V1_2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/6NqkDBwiybbB3BjErF6QGGYJCqzyhVFKpNYHQTN2j8Jn'
30
+ [types_1.Factory.V2V1_2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/6NqkDBwiybbB3BjErF6QGGYJCqzyhVFKpNYHQTN2j8Jn']
31
31
  },
32
32
  [types_1.Networks.Aurora]: {},
33
33
  [types_1.Networks.Cronos]: {},
34
34
  [types_1.Networks.Fantom]: {
35
- [types_1.Factory.SOLV1_2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/8vssJypWas5oenzsjjQq1yLqMe7FRt2evcqZdzCNEWt7',
36
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/DJ2bTPinDji7Kwev3WjyfxRo487tJHiEEMEo8kmTMT8F',
37
- [types_1.Factory.V2V2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/DPk9E2c9fLKF8N6rN3jcyvSrEq4VEgjdeeJcnaEKAngG'
35
+ [types_1.Factory.SOLV1_2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/8vssJypWas5oenzsjjQq1yLqMe7FRt2evcqZdzCNEWt7'],
36
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/DJ2bTPinDji7Kwev3WjyfxRo487tJHiEEMEo8kmTMT8F'],
37
+ [types_1.Factory.V2V2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/DPk9E2c9fLKF8N6rN3jcyvSrEq4VEgjdeeJcnaEKAngG']
38
38
  },
39
39
  [types_1.Networks.Canto]: {
40
- [types_1.Factory.SOLV2]: 'https://api.impermax.finance/subgraphs/name/impermax-finance/impermax-canto-solv2',
40
+ [types_1.Factory.SOLV2]: ['https://api.impermax.finance/subgraphs/name/impermax-finance/impermax-canto-solv2'],
41
41
  },
42
42
  [types_1.Networks.ZksyncEra]: {
43
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/8YSS88X8ChDw1QvuAMtxjezSXNiYer9k9d3Uu8XFLHzX',
43
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/8YSS88X8ChDw1QvuAMtxjezSXNiYer9k9d3Uu8XFLHzX'],
44
44
  },
45
45
  [types_1.Networks.Harmony]: {},
46
46
  [types_1.Networks.Moonbeam]: {},
47
47
  [types_1.Networks.Sxnetwork]: {},
48
48
  [types_1.Networks.Blast]: {
49
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/4FDikpVjE2XnDJ5QcpQ1YJXWcUDiAgE7x8ErtsHh33UB',
50
- [types_1.Factory.V2V2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/6xkrXFPJ4wqh9cy9ayP19WGr2wZVSMP3FniMjcW2LUG4'
49
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/4FDikpVjE2XnDJ5QcpQ1YJXWcUDiAgE7x8ErtsHh33UB'],
50
+ [types_1.Factory.V2V2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/6xkrXFPJ4wqh9cy9ayP19WGr2wZVSMP3FniMjcW2LUG4']
51
51
  },
52
52
  [types_1.Networks.Base]: {
53
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/EjB7Dsb7WnfFf1NPPr5rbGnGmk2Jqs42nWPVHjv9997u',
54
- [types_1.Factory.SOL_STABLE]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/9wN7xinUXf8RP8dbwpkzSmCNQKfz31FDpvPEvTQyT8SL',
55
- [types_1.Factory.V2V2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/B7xsBeef3FohKaFPcsAsviyLKaWXTqcSELjAsPj2B7iW',
53
+ [types_1.Factory.SOLV2]: [
54
+ 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/EjB7Dsb7WnfFf1NPPr5rbGnGmk2Jqs42nWPVHjv9997u',
55
+ 'https://base-start-production.up.railway.app/'
56
+ ],
57
+ [types_1.Factory.SOL_STABLE]: [
58
+ 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/9wN7xinUXf8RP8dbwpkzSmCNQKfz31FDpvPEvTQyT8SL',
59
+ 'https://base-start-production.up.railway.app/'
60
+ ],
61
+ [types_1.Factory.V2V2]: [
62
+ 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/B7xsBeef3FohKaFPcsAsviyLKaWXTqcSELjAsPj2B7iW',
63
+ 'https://base-start-production.up.railway.app/'
64
+ ],
56
65
  },
57
66
  [types_1.Networks.Scroll]: {
58
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/9HL6kLRt3AbQsprTvk7KZdaHunJ5GGsLdYWd27nNfCFC',
59
- [types_1.Factory.SOL_STABLE]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/C42ymmkDArE1T5wdYKwao6mntqNSjP2E34TeGusiF3sk'
67
+ [types_1.Factory.SOLV2]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/9HL6kLRt3AbQsprTvk7KZdaHunJ5GGsLdYWd27nNfCFC'],
68
+ [types_1.Factory.SOL_STABLE]: ['https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/C42ymmkDArE1T5wdYKwao6mntqNSjP2E34TeGusiF3sk']
60
69
  },
61
70
  [types_1.Networks.Optimism]: {
62
- [types_1.Factory.SOLV2]: 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/74C2pMzH3wNKf38PNheGZfSiY4FzmJoKiGtHLYa7oD2s'
71
+ [types_1.Factory.SOLV2]: [
72
+ 'https://gateway.thegraph.com/api/1350441d268f171aeb0934412dfadf3b/subgraphs/id/74C2pMzH3wNKf38PNheGZfSiY4FzmJoKiGtHLYa7oD2s',
73
+ 'https://optimism-factory-production.up.railway.app/'
74
+ ]
63
75
  },
64
76
  [types_1.Networks.Real]: {
65
- [types_1.Factory.SOL_STABLE]: 'https://api.goldsky.com/api/public/project_cm2d5q4l4w31601vz4swb3vmi/subgraphs/impermax-finance/impermax-real-v2-stable/gn',
66
- [types_1.Factory.SOLV2]: 'https://api.goldsky.com/api/public/project_cm2rhb30ot9wu01to8c9h9e37/subgraphs/impermax-real-solv2/3.0/gn'
77
+ [types_1.Factory.SOL_STABLE]: ['https://api.goldsky.com/api/public/project_cm2d5q4l4w31601vz4swb3vmi/subgraphs/impermax-finance/impermax-real-v2-stable/gn'],
78
+ [types_1.Factory.SOLV2]: ['https://api.goldsky.com/api/public/project_cm2rhb30ot9wu01to8c9h9e37/subgraphs/impermax-real-solv2/3.0/gn']
67
79
  },
68
80
  };
69
81
  exports.IMPERMAX_CHEF_SUBGRAPH_URL = {
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -33,11 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
33
10
  };
34
11
  Object.defineProperty(exports, "__esModule", { value: true });
35
12
  exports.getProposalData = exports.getProposalsMetadata = exports.fetchProposalData = exports.fetchProposalsMetadata = void 0;
36
- const query = __importStar(require("../query"));
13
+ const queries_1 = require("../queries");
37
14
  const subgraphs_1 = require("../../config/subgraphs");
38
15
  function fetchProposalsMetadata(rangeBegin, rangeLength, descending) {
39
16
  return __awaiter(this, void 0, void 0, function* () {
40
17
  const subgraph = subgraphs_1.CONFIG_MANAGER_SUBGRAPH_URL[this.getOffchain().network];
18
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
41
19
  const result = yield this.getOffchain().apolloFetcher(subgraph, query.proposalsMetaQuery(rangeBegin, rangeLength, descending));
42
20
  return result.data.proposals;
43
21
  });
@@ -46,6 +24,7 @@ exports.fetchProposalsMetadata = fetchProposalsMetadata;
46
24
  function fetchProposalData(id) {
47
25
  return __awaiter(this, void 0, void 0, function* () {
48
26
  const subgraph = subgraphs_1.CONFIG_MANAGER_SUBGRAPH_URL[this.getOffchain().network];
27
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
49
28
  const result = yield this.getOffchain().apolloFetcher(subgraph, query.proposalQuery(id));
50
29
  return result.data.proposal;
51
30
  });
@@ -4,6 +4,7 @@ import OffchainPriceHelper from "./offchainPriceHelper";
4
4
  import OffchainAPRHelper from "./offchainAPRHelper";
5
5
  import OffchainSolidexHelper from "./offchainSolidexHelper";
6
6
  import OffchainAccount from "./account";
7
+ import OffchainEndpointManager from './offchainEndpointManager';
7
8
  import { Address, AddressIndex, Factory, FactoryIndex, LendingPoolIndex, Networks, WhitelistState } from '../config/types';
8
9
  import { LendingPoolData, LendingVaultData, LendingVaultPosition, TvlData, UserData, XimxData } from './offchainTypes';
9
10
  import OffchainLendingVault from './offchainLendingVault';
@@ -35,6 +36,7 @@ export default class Offchain {
35
36
  protected tvlData: Promise<TvlData> | null;
36
37
  protected ximxData: Promise<XimxData> | null;
37
38
  protected whitelistData: Promise<LendingPoolIndex<WhitelistState>> | null;
39
+ protected endpointManager: OffchainEndpointManager;
38
40
  constructor(cfg: OffchainCfg);
39
41
  cleanCache(): void;
40
42
  getLendingPool(factory: Factory, pairAddress: Address): Promise<OffchainLendingPool>;
@@ -40,6 +40,7 @@ const offchainPriceHelper_1 = __importDefault(require("./offchainPriceHelper"));
40
40
  const offchainAPRHelper_1 = __importDefault(require("./offchainAPRHelper"));
41
41
  const offchainSolidexHelper_1 = __importDefault(require("./offchainSolidexHelper"));
42
42
  const account_1 = __importDefault(require("./account"));
43
+ const offchainEndpointManager_1 = __importDefault(require("./offchainEndpointManager"));
43
44
  const offchainLendingVault_1 = __importDefault(require("./offchainLendingVault"));
44
45
  const configManager_1 = __importDefault(require("./configManager"));
45
46
  class Offchain {
@@ -95,6 +96,7 @@ class Offchain {
95
96
  this.solidexHelper = new offchainSolidexHelper_1.default(this);
96
97
  this.configManager = new configManager_1.default(this);
97
98
  this.aprHelper = new offchainAPRHelper_1.default(this);
99
+ this.endpointManager = new offchainEndpointManager_1.default();
98
100
  this.accounts = {};
99
101
  this.usersData = {};
100
102
  this.lendingVaultsUsersData = {};
@@ -0,0 +1,25 @@
1
+ import { DocumentNode } from "graphql";
2
+ import { IQueryBuilder } from "./queries/interfaces/query-builder";
3
+ import { Networks } from "../config/types";
4
+ export default class OffchainEndpointManager {
5
+ private endpoint;
6
+ constructor();
7
+ /**
8
+ * Main fetcher function to query from multiple endpoints. The query MUST have _meta included.
9
+ *
10
+ * @param endpoints - Array of endpoints
11
+ * @param network - The network of the request (needed for the block due to ponder's `_meta` result)
12
+ * @param queryBuilder - The query requested from IQueryBuilder interface (ie. `lendingPoolQuery`, `tvlQuery`, etc.)
13
+ */
14
+ fetch(endpoints: string[], network: Networks, queryBuilder: (builder: IQueryBuilder) => DocumentNode): Promise<any>;
15
+ /**
16
+ * Fetch a query from an endpoint (ponder, thegraph, etc.)
17
+ */
18
+ private fetchFromEndpoint;
19
+ /**
20
+ * Given an array of responses from endpoint query, get the response wiht the latest block number
21
+ */
22
+ private bestEndpointBlock;
23
+ private filterEndpoints;
24
+ private getPreferredEndpoint;
25
+ }
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const apollo_client_1 = __importDefault(require("apollo-client"));
16
+ const apollo_link_http_1 = require("apollo-link-http");
17
+ const apollo_cache_inmemory_1 = require("apollo-cache-inmemory");
18
+ const queries_1 = require("./queries");
19
+ class OffchainEndpointManager {
20
+ constructor() {
21
+ this.endpoint = null;
22
+ // Get the param:
23
+ // 1. ?endpoint=thegraph for thegraph
24
+ // 2. ?endpoint=ibexer for ponder
25
+ // 3. none for both and picks the best of the 2
26
+ this.endpoint = this.getPreferredEndpoint(window.location.search);
27
+ }
28
+ // ---------------- Getter ---------------- //
29
+ /**
30
+ * Main fetcher function to query from multiple endpoints. The query MUST have _meta included.
31
+ *
32
+ * @param endpoints - Array of endpoints
33
+ * @param network - The network of the request (needed for the block due to ponder's `_meta` result)
34
+ * @param queryBuilder - The query requested from IQueryBuilder interface (ie. `lendingPoolQuery`, `tvlQuery`, etc.)
35
+ */
36
+ fetch(endpoints, network, queryBuilder) {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ // This is a test as we roll this out, to filter pass URL search param with thegraph or ibexer
39
+ const filteredEndpoints = this.filterEndpoints(endpoints);
40
+ if (filteredEndpoints.length === 0) {
41
+ console.warn("No endpoints available after filtering");
42
+ return null;
43
+ }
44
+ const endpointQueries = filteredEndpoints.map(endpoint => ({
45
+ endpoint,
46
+ query: queryBuilder(queries_1.QueryBuilderFactory.getQuery(endpoint)),
47
+ }));
48
+ const endpointResults = yield Promise.allSettled(endpointQueries.map(eq => this.fetchFromEndpoint(eq)));
49
+ // If one endpoint breaks then it shouldnt break, filter it out and report
50
+ const responses = endpointResults
51
+ .filter((result, index) => {
52
+ if (result.status === "rejected") {
53
+ console.log(`${endpointQueries[index].endpoint} is down?`);
54
+ }
55
+ return result.status === 'fulfilled';
56
+ })
57
+ .map(result => result.value);
58
+ const latestResponse = this.bestEndpointBlock(responses, network);
59
+ if (!latestResponse) {
60
+ console.error("Cant find endpoint block");
61
+ return null;
62
+ }
63
+ // Transform the data if needed in case of ponder or different api responses
64
+ return queries_1.QueryBuilderFactory.transformData(latestResponse.result);
65
+ });
66
+ }
67
+ // ---------------- Private ---------------- //
68
+ /**
69
+ * Fetch a query from an endpoint (ponder, thegraph, etc.)
70
+ */
71
+ fetchFromEndpoint({ endpoint, query }) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ const client = new apollo_client_1.default({
74
+ link: new apollo_link_http_1.HttpLink({ uri: endpoint }),
75
+ cache: new apollo_cache_inmemory_1.InMemoryCache()
76
+ });
77
+ const result = yield client.query({
78
+ query,
79
+ fetchPolicy: 'cache-first'
80
+ });
81
+ return { endpoint, result };
82
+ });
83
+ }
84
+ /**
85
+ * Given an array of responses from endpoint query, get the response wiht the latest block number
86
+ */
87
+ bestEndpointBlock(responses, network) {
88
+ if (!responses.length)
89
+ return null;
90
+ let bestEndpoint = responses[0];
91
+ let maxBlockNumber = 0;
92
+ for (const response of responses) {
93
+ const queryBuilder = queries_1.QueryBuilderFactory.getQuery(response.endpoint);
94
+ const blockNumber = queryBuilder.getBlockNumber(response.result, network);
95
+ if (blockNumber > maxBlockNumber && response.result.data) {
96
+ maxBlockNumber = blockNumber;
97
+ bestEndpoint = response;
98
+ }
99
+ }
100
+ return Object.assign(Object.assign({}, bestEndpoint), { maxBlockNumber });
101
+ }
102
+ // ---------------- Monitoring ---------------- //
103
+ filterEndpoints(endpoints) {
104
+ // No endpoint passed, ignore
105
+ if (!this.endpoint)
106
+ return endpoints;
107
+ const preferredEndpoint = this.endpoint === "thegraph" ? "thegraph" : "railway";
108
+ return endpoints.filter(endpoint => endpoint.includes(preferredEndpoint));
109
+ }
110
+ getPreferredEndpoint(searchParams) {
111
+ const urlParams = new URLSearchParams(searchParams);
112
+ const endpoint = urlParams.get("endpoint");
113
+ if (!endpoint)
114
+ return null;
115
+ switch (endpoint.toLowerCase()) {
116
+ case "thegraph":
117
+ return "thegraph";
118
+ case "ibexer":
119
+ return "ibexer";
120
+ default:
121
+ return null;
122
+ }
123
+ }
124
+ }
125
+ exports.default = OffchainEndpointManager;
@@ -1,27 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -39,7 +16,7 @@ exports.getLendingVaultsUserData = exports.initializeLendingVaultsUserData = exp
39
16
  const apollo_client_1 = __importDefault(require("apollo-client"));
40
17
  const apollo_link_http_1 = require("apollo-link-http");
41
18
  const apollo_cache_inmemory_1 = require("apollo-cache-inmemory");
42
- const query = __importStar(require("./query"));
19
+ const queries_1 = require("./queries");
43
20
  const lendingPool_1 = __importDefault(require("./lendingPool"));
44
21
  const subgraphs_1 = require("../config/subgraphs");
45
22
  const types_1 = require("../config/types");
@@ -55,10 +32,11 @@ function apolloFetcher(subgraphUrl, query) {
55
32
  }),
56
33
  cache: new apollo_cache_inmemory_1.InMemoryCache(),
57
34
  });
58
- return client.query({
35
+ const result = yield client.query({
59
36
  query: query,
60
37
  fetchPolicy: 'cache-first',
61
38
  });
39
+ return queries_1.QueryBuilderFactory.transformData(result);
62
40
  });
63
41
  }
64
42
  exports.apolloFetcher = apolloFetcher;
@@ -67,7 +45,10 @@ function fetchBlockByTimestamp(timestamp) {
67
45
  try {
68
46
  if (subgraphs_1.BLOCKS_SUBGRAPH_URL[this.network].length == 0)
69
47
  return null;
70
- const result = yield this.apolloFetcher(subgraphs_1.BLOCKS_SUBGRAPH_URL[this.network], query.blockQuery(timestamp));
48
+ const subgraph = subgraphs_1.BLOCKS_SUBGRAPH_URL[this.network];
49
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
50
+ const result = yield this.apolloFetcher(subgraph, query.blockQuery(timestamp));
51
+ // TODO: This is wrong in case of different endpoints?
71
52
  console.log(result.data._meta.block.number);
72
53
  if (!result.data.blocks[0])
73
54
  return null;
@@ -86,8 +67,9 @@ function fetchLendingPools() {
86
67
  const calls = [];
87
68
  let factory;
88
69
  for (factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
89
- const subgraph = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
90
- calls.push(this.apolloFetcher(subgraph, query.lendingPoolsQuery(factory, [])));
70
+ // Multiple endpoints
71
+ const subgraphs = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
72
+ calls.push(this.endpointManager.fetch(subgraphs, this.network, (queryBuilder) => queryBuilder.lendingPoolsQuery(factory, [], this.network)));
91
73
  }
92
74
  const results = yield Promise.all(calls);
93
75
  const lendingPools = {};
@@ -109,8 +91,14 @@ function fetchLendingPoolsPast(seconds) {
109
91
  const calls = [];
110
92
  let factory;
111
93
  for (factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
112
- const subgraph = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
113
- calls.push(this.apolloFetcher(subgraph, query.lendingPoolsPastQuery(blockNumber, this.whitelistedPairs[factory])));
94
+ const endpoints = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
95
+ // Not all endpoints might support this
96
+ const supportedEndpoints = endpoints.filter(endpoint => queries_1.QueryBuilderFactory.getQuery(endpoint).lendingPoolsPastQuery);
97
+ // Skip if no endpoints support past queries for this factory
98
+ if (supportedEndpoints.length === 0)
99
+ continue;
100
+ // Query only from supported endpoints
101
+ calls.push(this.endpointManager.fetch(supportedEndpoints, this.network, (queryBuilder) => queryBuilder.lendingPoolsPastQuery(blockNumber, this.whitelistedPairs[factory])));
114
102
  }
115
103
  const results = yield Promise.all(calls);
116
104
  const lendingPools = {};
@@ -129,9 +117,13 @@ exports.fetchLendingPoolsPast = fetchLendingPoolsPast;
129
117
  // Farming APR
130
118
  function fetchImpermaxChefRewardRates() {
131
119
  return __awaiter(this, void 0, void 0, function* () {
132
- if (!subgraphs_1.IMPERMAX_CHEF_SUBGRAPH_URL[this.network])
120
+ const subgraph = subgraphs_1.IMPERMAX_CHEF_SUBGRAPH_URL[this.network];
121
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
122
+ // `impermaxChefQuery` is optional, see `offchain/queries/interfaces/query-builder.ts`
123
+ // Since it's not part of core, one endpoint should suffice
124
+ if (!subgraph || !query.impermaxChefQuery)
133
125
  return null;
134
- const result = yield this.apolloFetcher(subgraphs_1.IMPERMAX_CHEF_SUBGRAPH_URL[this.network], query.impermaxChefQuery());
126
+ const result = yield this.apolloFetcher(subgraph, query.impermaxChefQuery());
135
127
  const rewards = {};
136
128
  let rewardToken = result.data.impermaxChefs[0].rewardToken;
137
129
  let rewardPerSec = parseFloat(result.data.impermaxChefs[0].rewardPerSec);
@@ -171,7 +163,12 @@ function fetchPastVolume(amm, lendingPools, seconds) {
171
163
  let result;
172
164
  const pastVolume = {};
173
165
  try {
174
- result = yield this.apolloFetcher(amms_1.AMM_SUBGRAPH_URLS[this.network][amm], query.pastVolumeQuery(blockNumber, lendingPools.map(lendingPool => lendingPool.pair.uniswapV2PairAddress)));
166
+ const subgraph = amms_1.AMM_SUBGRAPH_URLS[this.network][amm];
167
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
168
+ // Since it's not part of core, one endpoint should suffice
169
+ if (!query.pastVolumeQuery)
170
+ return {};
171
+ result = yield this.apolloFetcher(subgraph, query.pastVolumeQuery(blockNumber, lendingPools.map(lendingPool => lendingPool.pair.uniswapV2PairAddress)));
175
172
  for (const pair of result.data.pairs) {
176
173
  pastVolume[pair.id] = parseInt(pair.volumeUSD);
177
174
  }
@@ -189,7 +186,12 @@ function fetchCurrentVolumeAndReserves(amm, lendingPools) {
189
186
  const currentVolume = {};
190
187
  const currentReserve = {};
191
188
  try {
192
- result = yield this.apolloFetcher(amms_1.AMM_SUBGRAPH_URLS[this.network][amm], query.currentVolumeAndReservesQuery(lendingPools.map(lendingPool => lendingPool.pair.uniswapV2PairAddress)));
189
+ const subgraph = amms_1.AMM_SUBGRAPH_URLS[this.network][amm];
190
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
191
+ // Since it's not part of core, one endpoint should suffice
192
+ if (!query.currentVolumeAndReservesQuery)
193
+ return { currentReserve, currentVolume };
194
+ result = yield this.apolloFetcher(subgraph, query.currentVolumeAndReservesQuery(lendingPools.map(lendingPool => lendingPool.pair.uniswapV2PairAddress)));
193
195
  for (const pair of result.data.pairs) {
194
196
  currentVolume[pair.id] = parseInt(pair.volumeUSD);
195
197
  currentReserve[pair.id] = parseInt(pair.reserveUSD);
@@ -314,15 +316,15 @@ function initializeLendingPoolsData() {
314
316
  }
315
317
  // Quickfix for MIM and SPELL
316
318
  if (this.network === types_1.Networks.Arbitrum) {
317
- //const ETH_MIM = '0xa6e69dd0c36f3b59539e2c7afd274ef91b5c70f9';
318
- //const ETH_SPELL = '0x35b44b303eacb71114aa62fe8dae6f9c5de0f680';
319
+ // const ETH_MIM = '0xa6e69dd0c36f3b59539e2c7afd274ef91b5c70f9';
320
+ // const ETH_SPELL = '0x35b44b303eacb71114aa62fe8dae6f9c5de0f680';
319
321
  const ETH_gGOHM = '0x6d94f7e67c6ae0b0257c35754e059fdfb249d998';
320
322
  const ETH_MAGIC = '0x88a0b00bbe918ee00e3482f71962070dc052984b';
321
323
  const ETH_RDNT = '0xfb468ad210d427d65c9a765a480649d5754ca325';
322
- const LIZ_LIZ_ETH = '0x699c5e23357ab1bf658bd0b38070d746e37671f3';
323
- const LIZ_ETH_USDC = '0x1229aac120ae75301ab1c3a2a04ec14183493584';
324
- const LIZ_ETH_BTC = '0xce009c44e1645bc6d19c90ba440c618a42ae581d';
325
- const LIZ_LIZ_USDC = '0x463ec151258370a07431068bb3bdc98b5a6ba18f';
324
+ // const LIZ_LIZ_ETH = '0x699c5e23357ab1bf658bd0b38070d746e37671f3';
325
+ // const LIZ_ETH_USDC = '0x1229aac120ae75301ab1c3a2a04ec14183493584';
326
+ // const LIZ_ETH_BTC = '0xce009c44e1645bc6d19c90ba440c618a42ae581d';
327
+ // const LIZ_LIZ_USDC = '0x463ec151258370a07431068bb3bdc98b5a6ba18f';
326
328
  const lendingPoolsDataV2V1_1 = lendingPoolsData[types_1.Factory.V2V1_1];
327
329
  if (lendingPoolsDataV2V1_1) {
328
330
  if (lendingPoolsDataV2V1_1[ETH_gGOHM]) {
@@ -475,9 +477,9 @@ function initializeTvlData() {
475
477
  const calls = [];
476
478
  for (const network of networks) {
477
479
  for (const factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[network]) {
478
- const subgraph = subgraphs_1.IMPERMAX_SUBGRAPH_URL[network][factory];
479
- if (subgraph && subgraph.length > 0)
480
- calls.push(this.apolloFetcher(subgraph, query.tvlQuery()));
480
+ // Multiple endpoints
481
+ const subgraphs = subgraphs_1.IMPERMAX_SUBGRAPH_URL[network][factory];
482
+ calls.push(this.endpointManager.fetch(subgraphs, this.network, (queryBuilder) => queryBuilder.tvlQuery()));
481
483
  }
482
484
  }
483
485
  let results;
@@ -503,7 +505,7 @@ function initializeTvlData() {
503
505
  let thisChainBorrowUSD = 0;
504
506
  let i = 0;
505
507
  for (const network of networks) {
506
- for (const factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[network]) {
508
+ for (const _ in subgraphs_1.IMPERMAX_SUBGRAPH_URL[network]) {
507
509
  const data = results[i].data.impermaxFactories[0];
508
510
  if (data != null) {
509
511
  crossChainTVLUSD += parseFloat(data.totalBalanceUSD);
@@ -521,12 +523,13 @@ function initializeTvlData() {
521
523
  if (!networks.includes(this.network)) {
522
524
  const calls = [];
523
525
  for (const factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
524
- const subgraph = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
525
- calls.push(this.apolloFetcher(subgraph, query.tvlQuery()));
526
+ // Multiple endpoints
527
+ const subgraphs = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
528
+ calls.push(this.endpointManager.fetch(subgraphs, this.network, (queryBuilder) => queryBuilder.tvlQuery()));
526
529
  }
527
530
  const results = yield Promise.all(calls);
528
531
  i = 0;
529
- for (const factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
532
+ for (const _ in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
530
533
  const data = results[i].data.impermaxFactories[0];
531
534
  if (data != null) {
532
535
  thisChainTVLUSD += parseFloat(data.totalBalanceUSD);
@@ -560,8 +563,9 @@ function fetchUserData(account) {
560
563
  return __awaiter(this, void 0, void 0, function* () {
561
564
  const positions = {};
562
565
  for (const factory in subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network]) {
563
- const subgraph = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
564
- const result = yield this.apolloFetcher(subgraph, query.userQuery(account));
566
+ const subgraphs = subgraphs_1.IMPERMAX_SUBGRAPH_URL[this.network][factory];
567
+ // We pass network + factory to get the actual factory address in case we use multiple factories in 1 api
568
+ const result = yield this.endpointManager.fetch(subgraphs, this.network, (queryBuilder) => queryBuilder.userQuery(account, this.network, factory));
565
569
  const user = result.data.user;
566
570
  if (!user)
567
571
  continue;
@@ -632,7 +636,9 @@ exports.getUserData = getUserData;
632
636
  // IMX Staking
633
637
  function initializeXimxData() {
634
638
  return __awaiter(this, void 0, void 0, function* () {
635
- const result = yield apolloFetcher(subgraphs_1.XIMX_SUBGRAPH_URL, query.ximxQuery());
639
+ const subgraph = subgraphs_1.XIMX_SUBGRAPH_URL;
640
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
641
+ const result = yield apolloFetcher(subgraph, query.ximxQuery());
636
642
  return result.data.ximxes[0];
637
643
  });
638
644
  }
@@ -651,6 +657,7 @@ exports.getXimxData = getXimxData;
651
657
  function fetchLendingVaults() {
652
658
  return __awaiter(this, void 0, void 0, function* () {
653
659
  const subgraph = subgraphs_1.LENDING_VAULT_SUBGRAPH_URL[this.network];
660
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
654
661
  const result = yield this.apolloFetcher(subgraph, query.lendingVaultsQuery([]));
655
662
  return result.data.lendingVaults;
656
663
  });
@@ -697,6 +704,9 @@ function fetchWhitelist() {
697
704
  const subgraph = subgraphs_1.WHITELIST_SUBGRAPH_URL[this.network];
698
705
  if (!subgraph)
699
706
  return [];
707
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
708
+ if (!query.whitelistQuery)
709
+ return [];
700
710
  const result = yield this.apolloFetcher(subgraph, query.whitelistQuery());
701
711
  if (!result || !result.data.whitelistStates)
702
712
  return [];
@@ -748,6 +758,7 @@ exports.getWhitelistState = getWhitelistState;
748
758
  function fetchLendingVaultsUserData(account) {
749
759
  return __awaiter(this, void 0, void 0, function* () {
750
760
  const subgraph = subgraphs_1.LENDING_VAULT_SUBGRAPH_URL[this.network];
761
+ const query = queries_1.QueryBuilderFactory.getQuery(subgraph);
751
762
  const result = yield this.apolloFetcher(subgraph, query.lendingVaultsUserQuery(account));
752
763
  const user = result.data.user;
753
764
  if (!user)
@@ -0,0 +1,14 @@
1
+ import { Address, Factory, Networks } from "../../../../config/types";
2
+ import { IQueryBuilder } from "../../interfaces/query-builder";
3
+ export declare class PonderQueryBuilder implements IQueryBuilder {
4
+ blockQuery(): import("graphql").DocumentNode;
5
+ getBlockNumber(response: any, network: Networks): number;
6
+ lendingPoolsQuery(factory: Factory, addressesFilter: Address[], network: Networks): import("graphql").DocumentNode;
7
+ userQuery(account: Address, network: Networks, factory: Factory): import("graphql").DocumentNode;
8
+ tvlQuery(): import("graphql").DocumentNode;
9
+ lendingVaultsUserQuery(account: Address): import("graphql").DocumentNode;
10
+ lendingVaultsQuery(addressesFilter: Address[]): import("graphql").DocumentNode;
11
+ proposalsMetaQuery(rangeBegin: any, rangeLength: any, descending: any): import("graphql").DocumentNode;
12
+ proposalQuery(id: number): import("graphql").DocumentNode;
13
+ ximxQuery(): import("graphql").DocumentNode;
14
+ }