impermax-sdk 1.2.59 → 1.2.60

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,6 +1,7 @@
1
1
  import OffchainLendingPool from "./lendingPool";
2
2
  import * as lendingPool from './lendingPool';
3
3
  import OffchainPriceHelper from "./offchainPriceHelper";
4
+ import OffchainAPRHelper from "./offchainAPRHelper";
4
5
  import OffchainSolidexHelper from "./offchainSolidexHelper";
5
6
  import OffchainAccount from "./account";
6
7
  import * as account from "./account";
@@ -9,5 +10,5 @@ import OffchainLendingVault from './offchainLendingVault';
9
10
  import OffchainPoolToken from "./offchainPoolToken";
10
11
  import Offchain from './offchain';
11
12
  import OffchainConfigManager from "./configManager";
12
- export { account, lendingPool, configManager, OffchainAccount, OffchainLendingPool, OffchainConfigManager, OffchainPoolToken, OffchainLendingVault, OffchainSolidexHelper, OffchainPriceHelper, };
13
+ export { account, lendingPool, configManager, OffchainAccount, OffchainLendingPool, OffchainConfigManager, OffchainPoolToken, OffchainLendingVault, OffchainSolidexHelper, OffchainPriceHelper, OffchainAPRHelper };
13
14
  export default Offchain;
@@ -26,13 +26,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.OffchainPriceHelper = exports.OffchainSolidexHelper = exports.OffchainLendingVault = exports.OffchainPoolToken = exports.OffchainConfigManager = exports.OffchainLendingPool = exports.OffchainAccount = exports.configManager = exports.lendingPool = exports.account = void 0;
29
+ exports.OffchainAPRHelper = exports.OffchainPriceHelper = exports.OffchainSolidexHelper = exports.OffchainLendingVault = exports.OffchainPoolToken = exports.OffchainConfigManager = exports.OffchainLendingPool = exports.OffchainAccount = exports.configManager = exports.lendingPool = exports.account = void 0;
30
30
  const lendingPool_1 = __importDefault(require("./lendingPool"));
31
31
  exports.OffchainLendingPool = lendingPool_1.default;
32
32
  const lendingPool = __importStar(require("./lendingPool"));
33
33
  exports.lendingPool = lendingPool;
34
34
  const offchainPriceHelper_1 = __importDefault(require("./offchainPriceHelper"));
35
35
  exports.OffchainPriceHelper = offchainPriceHelper_1.default;
36
+ const offchainAPRHelper_1 = __importDefault(require("./offchainAPRHelper"));
37
+ exports.OffchainAPRHelper = offchainAPRHelper_1.default;
36
38
  const offchainSolidexHelper_1 = __importDefault(require("./offchainSolidexHelper"));
37
39
  exports.OffchainSolidexHelper = offchainSolidexHelper_1.default;
38
40
  const account_1 = __importDefault(require("./account"));
@@ -19,7 +19,6 @@ const farms_1 = require("../../config/farms");
19
19
  const utils_1 = require("../../utils");
20
20
  const amms_1 = require("../../config/amms");
21
21
  const factories_1 = require("../../config/factories");
22
- const offchainAPRHelper_1 = require("../offchainAPRHelper");
23
22
  class OffchainLendingPool {
24
23
  constructor(offchain, factory, pairAddress) {
25
24
  this.getOffchain = () => this.offchain;
@@ -216,12 +215,16 @@ class OffchainLendingPool {
216
215
  return totalBorrowsUSDA + totalBorrowsUSDB;
217
216
  });
218
217
  }
219
- // Try get APR from defillama
218
+ // Try get APR from defillama if the uniswapv2 pair is listed on `offchainAPRHelper`
220
219
  getTotalAPR() {
221
220
  return __awaiter(this, void 0, void 0, function* () {
222
- const uniswapV2Pair = (yield this.getLendingPoolData()).pair.uniswapV2PairAddress.toLowerCase();
223
- if (Object.keys(offchainAPRHelper_1.defillama_ids).includes(uniswapV2Pair)) {
224
- return yield (0, offchainAPRHelper_1.getDefiLlamaAPR)(offchainAPRHelper_1.defillama_ids[uniswapV2Pair]);
221
+ const uniswapV2Pair = (yield this.getLendingPoolData()).pair.uniswapV2PairAddress;
222
+ // Remove this condition for all chains
223
+ if (this.offchain.network == types_1.Networks.Fantom) {
224
+ // If llama fails/apr is 0 we get the uniswapAPR and stakingAPR through subgraph
225
+ const apr = yield this.offchain.getAPRHelper().getLlamaAPR(uniswapV2Pair, this.offchain.network);
226
+ if (apr > 0)
227
+ return apr;
225
228
  }
226
229
  const [uniswapAPR, stakingAPR] = yield Promise.all([this.getUniswapAPR(), this.getStakingAPR()]);
227
230
  return uniswapAPR + stakingAPR;
@@ -1,6 +1,7 @@
1
1
  import * as initializer from "./offchainInitializer";
2
2
  import OffchainLendingPool from "./lendingPool";
3
3
  import OffchainPriceHelper from "./offchainPriceHelper";
4
+ import OffchainAPRHelper from "./offchainAPRHelper";
4
5
  import OffchainSolidexHelper from "./offchainSolidexHelper";
5
6
  import OffchainAccount from "./account";
6
7
  import { Address, AddressIndex, Factory, FactoryIndex, LendingPoolIndex, Networks, WhitelistState } from '../config/types';
@@ -20,6 +21,7 @@ export default class Offchain {
20
21
  protected lendingVaults: AddressIndex<OffchainLendingVault>;
21
22
  protected accounts: AddressIndex<OffchainAccount>;
22
23
  protected priceHelper: OffchainPriceHelper;
24
+ protected aprHelper: OffchainAPRHelper;
23
25
  protected solidexHelper: OffchainSolidexHelper;
24
26
  protected configManager: OffchainConfigManager;
25
27
  protected lendingPoolsData: Promise<LendingPoolIndex<LendingPoolData>> | null;
@@ -41,6 +43,7 @@ export default class Offchain {
41
43
  getPriceHelper: () => OffchainPriceHelper;
42
44
  getSolidexHelper: () => OffchainSolidexHelper;
43
45
  getConfigManager: () => OffchainConfigManager;
46
+ getAPRHelper: () => OffchainAPRHelper;
44
47
  /**
45
48
  * TODO
46
49
  * Potrei organizzare in:
@@ -37,6 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
38
  const initializer = __importStar(require("./offchainInitializer"));
39
39
  const offchainPriceHelper_1 = __importDefault(require("./offchainPriceHelper"));
40
+ const offchainAPRHelper_1 = __importDefault(require("./offchainAPRHelper"));
40
41
  const offchainSolidexHelper_1 = __importDefault(require("./offchainSolidexHelper"));
41
42
  const account_1 = __importDefault(require("./account"));
42
43
  const offchainLendingVault_1 = __importDefault(require("./offchainLendingVault"));
@@ -46,6 +47,7 @@ class Offchain {
46
47
  this.getPriceHelper = () => this.priceHelper;
47
48
  this.getSolidexHelper = () => this.solidexHelper;
48
49
  this.getConfigManager = () => this.configManager;
50
+ this.getAPRHelper = () => this.aprHelper;
49
51
  /**
50
52
  * TODO
51
53
  * Potrei organizzare in:
@@ -92,6 +94,7 @@ class Offchain {
92
94
  this.priceHelper = new offchainPriceHelper_1.default(this);
93
95
  this.solidexHelper = new offchainSolidexHelper_1.default(this);
94
96
  this.configManager = new configManager_1.default(this);
97
+ this.aprHelper = new offchainAPRHelper_1.default(this);
95
98
  this.accounts = {};
96
99
  this.usersData = {};
97
100
  this.lendingVaultsUsersData = {};
@@ -107,6 +110,7 @@ class Offchain {
107
110
  this.priceHelper.cleanCache();
108
111
  this.solidexHelper.cleanCache();
109
112
  this.configManager.cleanCache();
113
+ this.aprHelper.cleanCache();
110
114
  }
111
115
  getLendingPool(factory, pairAddress) {
112
116
  return __awaiter(this, void 0, void 0, function* () {
@@ -1,4 +1,12 @@
1
- export declare const defillama_ids: {
2
- [k: string]: string;
3
- };
4
- export declare const getDefiLlamaAPR: (poolId: any) => Promise<number>;
1
+ import { Address, Networks } from '../config/types';
2
+ import Offchain from "./index";
3
+ export default class OffchainAPRHelper {
4
+ private llamaAllPools;
5
+ private initialized;
6
+ offchain: Offchain;
7
+ constructor(offchain: Offchain);
8
+ cleanCache(): void;
9
+ private initializeLlamaAllPools;
10
+ private getLlamaPools;
11
+ getLlamaAPR(pool: Address, chain: Networks): Promise<number>;
12
+ }
@@ -9,42 +9,86 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getDefiLlamaAPR = exports.defillama_ids = void 0;
13
- const ids = {
14
- // -------------------------------------- FANTOM ------------------------------------
15
- // FTM/PLUS
16
- "0x964cc3a9dbad0ddddc25601abc662b6e193f92f3": "1277ae6c-58cf-43a1-a130-55c9f80e9316",
17
- // FSONIC/WFTM
18
- "0x767520fA98e1E24b3326fD42B24c9DCFCe8BcE14": "d3e4b430-f2e5-44b0-a9f1-196d3bf54a2d",
19
- // WFTM/TANGO
20
- "0x997fBfb238125dB4e90a62b423bbED2e202edd5A": "c8523f56-c4bb-4213-acdf-6799f20c9ec1",
21
- // WFTM/SGOAT
22
- "0xAcB5B7A37310854A6E74dD9889f6a98dA0Ef9975": "dc1f2a39-7952-4460-98eb-ec2e5d9f181c",
23
- // WFTM/BAY
24
- "0xA169AF5669A1d5b0a57260918CB035E152Aa3269": "bd208a0f-d9ee-4692-930e-6bcf6e50228c",
25
- // EQUAL/HOOPS
26
- "0xfe02fd5b3F8904e2f4108505bEE7cf91E1D179E4": "bc0ada32-62b3-44b4-963d-f09a6484b366",
27
- // WFTM/ECO
28
- "0xC2D4eA5547AB3d642f61A2aDadA89c9E85299e69": "48faaded-4501-4a3d-bd60-6ca9b7233dc7",
29
- // WFTM/EQUAL
30
- "0x3d6c56f6855b7Cc746fb80848755B0a9c3770122": "2bae2b5d-e3c9-4a3b-ad4e-923d19bb9a2f",
31
- // MCLB/FBOMB
32
- "0x68ca64CABbB3d40dBB0de37101A8C0b701f66fD6": "e0d0bad2-1664-49c0-9a67-345b25ec03f8",
12
+ const types_1 = require("../config/types");
13
+ // Map of OUR chains to the chain names used by llama in their `poolsOld`
14
+ const LlamaChains = {
15
+ [types_1.Networks.Ropsten]: 'ropsten',
16
+ [types_1.Networks.Mainnet]: 'ethereum',
17
+ [types_1.Networks.Polygon]: 'polygon',
18
+ [types_1.Networks.Arbitrum]: 'arbitrum',
19
+ [types_1.Networks.Avalanche]: 'avalanche',
20
+ [types_1.Networks.Moonriver]: 'moonriver',
21
+ [types_1.Networks.Aurora]: 'aurora',
22
+ [types_1.Networks.Cronos]: 'cronos',
23
+ [types_1.Networks.Fantom]: 'fantom',
24
+ [types_1.Networks.Harmony]: 'harmony',
25
+ [types_1.Networks.Moonbeam]: 'moonbeam',
26
+ [types_1.Networks.Sxnetwork]: 'sxnetwork',
27
+ [types_1.Networks.Canto]: 'canto',
28
+ [types_1.Networks.ZksyncEra]: 'zksync era',
29
+ [types_1.Networks.Base]: 'base',
30
+ [types_1.Networks.Mantle]: 'mantle',
31
+ [types_1.Networks.Scroll]: 'scroll'
33
32
  };
34
- // https://stackoverflow.com/questions/12539574/whats-the-best-way-most-efficient-to-turn-all-the-keys-of-an-object-to-lower
35
- // Convert all keys to lowercase just in case and export this
36
- exports.defillama_ids = Object.fromEntries(Object.entries(ids).map(([k, v]) => [k.toLowerCase(), v]));
37
- // Get the DefiLlama APR for `poolID`
38
- const getDefiLlamaAPR = (poolId) => __awaiter(void 0, void 0, void 0, function* () {
39
- let apy;
40
- try {
41
- const res = yield fetch(`https://yields.llama.fi/chart/${poolId}`).then(i => i.json());
42
- apy = res.data[res.data.length - 1].apy;
33
+ // Class to get the APR from DefiLlama directly, only makes 1 call to api and caches all pools
34
+ class OffchainAPRHelper {
35
+ constructor(offchain) {
36
+ this.llamaAllPools = {};
37
+ this.initialized = null;
38
+ this.offchain = offchain;
43
39
  }
44
- catch (err) {
45
- apy = 0;
40
+ cleanCache() {
41
+ this.llamaAllPools = {};
42
+ this.initialized = null;
46
43
  }
47
- // in percentage to match uniswapAPR and stakingAPR
48
- return apy / 100;
49
- });
50
- exports.getDefiLlamaAPR = getDefiLlamaAPR;
44
+ // Initializes all llama pools
45
+ initializeLlamaAllPools() {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ try {
48
+ const response = yield fetch('https://yields.llama.fi/poolsOld').then(i => i.json());
49
+ const pools = response.data;
50
+ // `i.pool_old` is the pool address with sometimes `-{chain}` separator if contracts exists on diff chains
51
+ this.llamaAllPools = pools.reduce((acc, i) => {
52
+ const pool = i.pool_old.split("-")[0];
53
+ const llamaChain = i.chain;
54
+ const llamaId = pool.toLowerCase() + "-" + llamaChain.toLowerCase();
55
+ acc[llamaId] = i;
56
+ return acc;
57
+ }, {});
58
+ }
59
+ catch (err) {
60
+ console.warn("The llamas are down?");
61
+ this.llamaAllPools = {};
62
+ }
63
+ });
64
+ }
65
+ getLlamaPools() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ // First call initializes
68
+ if (!this.initialized)
69
+ this.initialized = this.initializeLlamaAllPools();
70
+ // async mutex to only call llama api once
71
+ yield this.initialized;
72
+ return this.llamaAllPools;
73
+ });
74
+ }
75
+ // Given a DefiLlama ID get the latest APR
76
+ getLlamaAPR(pool, chain) {
77
+ var _a;
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ // Get or initialize the pools
80
+ const llamaAllPools = yield this.getLlamaPools();
81
+ // Map to llama chain names, if not supported, then fetch native way
82
+ const llamaChain = LlamaChains[chain];
83
+ if (!llamaChain)
84
+ return 0;
85
+ // Make pool unique ({poolAddress}-{chain})
86
+ const llamaId = pool.toLowerCase() + "-" + llamaChain.toLowerCase();
87
+ // Convert to decimals to match `getUniswapAPR` and `getStakingAPR`
88
+ const apy = ((_a = llamaAllPools[llamaId]) === null || _a === void 0 ? void 0 : _a.apy) / 100;
89
+ // If 0 then should fetch native way to make sure
90
+ return apy || 0;
91
+ });
92
+ }
93
+ }
94
+ exports.default = OffchainAPRHelper;
@@ -125,6 +125,7 @@ class OffchainPriceHelper {
125
125
  offchainTokenPrice["0xea38f1ccf77bf43f352636241b05dd8f6f5f52b2"] = 0;
126
126
  offchainTokenPrice["0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83"] = 0;
127
127
  offchainTokenPrice["0xc19669a405067927865b40ea045a2baabbbe57f5"] = 0;
128
+ offchainTokenPrice["0x3fd3a0c85b70754efc07ac9ac0cbbdce664865a6"] = 0;
128
129
  return offchainTokenPrice;
129
130
  });
130
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "1.2.59",
3
+ "version": "1.2.60",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",