@curvefi/llamalend-api 2.0.22 → 2.0.23

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.
@@ -16,8 +16,19 @@ export declare const _getUserCollateralForce: (network: INetworkName, controller
16
16
  export declare const _getUserCollateralCrvUsd: ((network: INetworkName, controller: string, user: string) => Promise<string>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<string>>;
17
17
  export declare const _getUserCollateralCrvUsdFull: ((network: INetworkName, controller: string, user: string) => Promise<UserCollateral>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<UserCollateral>>;
18
18
  export declare const _getMarketsData: ((network: INetworkName) => Promise<IMarketData>) & memoize.Memoized<(network: INetworkName) => Promise<IMarketData>>;
19
+ export interface ICrvUsdMarketAPI {
20
+ address: string;
21
+ llamma: string;
22
+ amm_a: number;
23
+ monetary_policy_address: string;
24
+ collateral_token: {
25
+ symbol: string;
26
+ address: string;
27
+ decimals: number;
28
+ };
29
+ }
30
+ export declare const _getCrvUsdMarketsData: (() => Promise<ICrvUsdMarketAPI[]>) & memoize.Memoized<() => Promise<ICrvUsdMarketAPI[]>>;
19
31
  export declare function _getQuoteOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string, pathVizImage: boolean, slippage?: number): Promise<IQuoteOdos>;
20
32
  export declare function _getExpectedOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string): Promise<string>;
21
33
  export declare function _assembleTxOdos(this: Llamalend, pathId: string): Promise<string>;
22
- export declare const _getHiddenPools: (() => Promise<any>) & memoize.Memoized<() => Promise<any>>;
23
34
  export {};
@@ -138,6 +138,18 @@ export const _getMarketsData = memoize((network) => __awaiter(void 0, void 0, vo
138
138
  promise: true,
139
139
  maxAge: 10 * 1000, // 10s
140
140
  });
141
+ export const _getCrvUsdMarketsData = memoize(() => __awaiter(void 0, void 0, void 0, function* () {
142
+ const url = 'https://prices.curve.finance/v1/crvusd/markets/ethereum';
143
+ const response = yield fetch(url, { headers: { "accept": "application/json" } });
144
+ if (response.status !== 200) {
145
+ throw Error(`Fetch error: ${response.status} ${response.statusText}`);
146
+ }
147
+ const { data } = yield response.json();
148
+ return data;
149
+ }), {
150
+ promise: true,
151
+ maxAge: 10 * 1000, // 10s
152
+ });
141
153
  // --- ODOS ---
142
154
  export function _getQuoteOdos(fromToken_1, toToken_1, _amount_1, blacklist_1, pathVizImage_1) {
143
155
  return __awaiter(this, arguments, void 0, function* (fromToken, toToken, _amount, blacklist, pathVizImage, slippage = 0.5) {
@@ -182,13 +194,6 @@ export function _assembleTxOdos(pathId) {
182
194
  return _assembleTxOdosMemoized(this.constants.ALIASES.leverage_zap, pathId);
183
195
  });
184
196
  }
185
- export const _getHiddenPools = memoize(() => __awaiter(void 0, void 0, void 0, function* () {
186
- const response = yield fetch(`https://api.curve.finance/api/getHiddenPools`);
187
- return (yield response.json()).data;
188
- }), {
189
- promise: true,
190
- maxAge: 5 * 60 * 1000, // 5m
191
- });
192
197
  function fetchJson(url) {
193
198
  return __awaiter(this, void 0, void 0, function* () {
194
199
  var _a;
package/lib/index.d.ts CHANGED
@@ -50,9 +50,11 @@ export declare function createLlamalend(): {
50
50
  getGasPriceFromL1: () => Promise<number>;
51
51
  getGasPriceFromL2: () => Promise<number>;
52
52
  getGasInfoForL2: () => Promise<Record<string, number>>;
53
- fetchStats: (amms: string[], controllers: string[], vaults: string[], borrowed_tokens: string[], collateral_tokens: string[], version?: "v1" | "v2") => Promise<void>;
54
53
  mintMarkets: {
55
54
  getMarketList: () => string[];
55
+ fetchMintMarkets: ({ useApi }?: {
56
+ useApi?: boolean;
57
+ }) => Promise<void>;
56
58
  };
57
59
  lendMarkets: {
58
60
  fetchMarkets: ({ useApi, version }?: {
@@ -152,9 +154,11 @@ declare const _default: {
152
154
  getGasPriceFromL1: () => Promise<number>;
153
155
  getGasPriceFromL2: () => Promise<number>;
154
156
  getGasInfoForL2: () => Promise<Record<string, number>>;
155
- fetchStats: (amms: string[], controllers: string[], vaults: string[], borrowed_tokens: string[], collateral_tokens: string[], version?: "v1" | "v2") => Promise<void>;
156
157
  mintMarkets: {
157
158
  getMarketList: () => string[];
159
+ fetchMintMarkets: ({ useApi }?: {
160
+ useApi?: boolean;
161
+ }) => Promise<void>;
158
162
  };
159
163
  lendMarkets: {
160
164
  fetchMarkets: ({ useApi, version }?: {
package/lib/index.js CHANGED
@@ -43,11 +43,10 @@ export function createLlamalend() {
43
43
  getGasPriceFromL1: getGasPriceFromL1.bind(llamalend),
44
44
  getGasPriceFromL2: getGasPriceFromL2.bind(llamalend),
45
45
  getGasInfoForL2: getGasInfoForL2.bind(llamalend),
46
- // Core methods
47
- fetchStats: llamalend.fetchStats.bind(llamalend),
48
46
  // Market lists
49
47
  mintMarkets: {
50
48
  getMarketList: llamalend.getMintMarketList.bind(llamalend),
49
+ fetchMintMarkets: llamalend.fetchMintMarkets.bind(llamalend),
51
50
  },
52
51
  lendMarkets: {
53
52
  fetchMarkets: llamalend.fetchLendMarkets.bind(llamalend),
@@ -75,13 +75,6 @@ export const fetchOneWayMarketsByBlockchain = (llamalend_1, ...args_1) => __awai
75
75
  llamalend.constants.DECIMALS[c] = COIN_DATA[c].decimals;
76
76
  }
77
77
  registerMarkets(llamalend, names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges, COIN_DATA, version);
78
- if (version === 'v2') {
79
- llamalend.constants.ONE_WAY_MARKETS_V2 = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS_V2);
80
- }
81
- else {
82
- llamalend.constants.ONE_WAY_MARKETS = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS);
83
- }
84
- yield llamalend.fetchStats(amms, controllers, vaults, borrowed_tokens, collateral_tokens, version);
85
78
  });
86
79
  export const fetchOneWayMarketsByAPI = (llamalend_1, ...args_1) => __awaiter(void 0, [llamalend_1, ...args_1], void 0, function* (llamalend, version = 'v1') {
87
80
  const { names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges } = yield getFactoryMarketDataByAPI(llamalend);
@@ -90,10 +83,4 @@ export const fetchOneWayMarketsByAPI = (llamalend_1, ...args_1) => __awaiter(voi
90
83
  llamalend.constants.DECIMALS[c] = COIN_DATA[c].decimals;
91
84
  }
92
85
  registerMarkets(llamalend, names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges, COIN_DATA, version);
93
- if (version === 'v2') {
94
- llamalend.constants.ONE_WAY_MARKETS_V2 = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS_V2);
95
- }
96
- else {
97
- llamalend.constants.ONE_WAY_MARKETS = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS);
98
- }
99
86
  });
@@ -1,5 +1,5 @@
1
1
  import { ethers, Networkish, BigNumberish, Numeric } from "ethers";
2
- import { Provider as MulticallProvider } from '@curvefi/ethcall';
2
+ import { Provider as MulticallProvider, Contract as MulticallContract } from '@curvefi/ethcall';
3
3
  import { IChainId, ILlamalend, ILlamma, IDict, INetworkName, ICurveContract, IOneWayMarket, ICoin } from "./interfaces.js";
4
4
  import { MintMarketTemplate } from "./mintMarkets";
5
5
  import { LendMarketTemplate } from "./lendMarkets";
@@ -67,21 +67,25 @@ declare class Llamalend implements ILlamalend {
67
67
  maxPriorityFeePerGas?: number;
68
68
  chainId?: number;
69
69
  }): Promise<void>;
70
- setContract(address: string, abi: any): void;
70
+ initContract: (address: string, abi: any, provider: ethers.BrowserProvider | ethers.JsonRpcProvider | ethers.Signer) => ethers.Contract;
71
+ initMulticallContract: (address: string, abi: any) => MulticallContract;
72
+ setContract(address: string | undefined, abi: any): void;
71
73
  setCustomFeeData(customFeeData: {
72
74
  gasPrice?: number;
73
75
  maxFeePerGas?: number;
74
76
  maxPriorityFeePerGas?: number;
75
77
  }): void;
76
- _filterHiddenMarkets(markets: IDict<IOneWayMarket>): Promise<IDict<IOneWayMarket>>;
78
+ _setupMintMarketContracts(useApi?: boolean): Promise<void>;
77
79
  getLendMarketList: () => string[];
78
80
  getMintMarketList: () => string[];
79
81
  fetchLendMarkets: ({ useApi, version }?: {
80
82
  useApi?: boolean;
81
83
  version?: "v1" | "v2";
82
84
  }) => Promise<void>;
85
+ fetchMintMarkets: ({ useApi }?: {
86
+ useApi?: boolean;
87
+ }) => Promise<void>;
83
88
  getCoins: (collateral_tokens: string[], borrowed_tokens: string[], useApi?: boolean) => Promise<IDict<ICoin>>;
84
- fetchStats: (amms: string[], controllers: string[], vaults: string[], borrowed_tokens: string[], collateral_tokens: string[], version?: "v1" | "v2") => Promise<void>;
85
89
  formatUnits(value: BigNumberish, unit?: string | Numeric): string;
86
90
  parseUnits(value: string, unit?: string | Numeric): bigint;
87
91
  updateFeeData(): Promise<void>;
package/lib/llamalend.js CHANGED
@@ -24,10 +24,7 @@ import gasOracleBlobABI from './constants/abis/gas_oracle_optimism_blob.json' wi
24
24
  // crvUSD ABIs
25
25
  import llammaABI from "./constants/abis/crvUSD/llamma.json" with { type: 'json' };
26
26
  import controllerABI from "./constants/abis/crvUSD/controller.json" with { type: 'json' };
27
- import controllerV2ABI from "./constants/abis/crvUSD/controller_v2.json";
28
27
  import PegKeeper from "./constants/abis/crvUSD/PegKeeper.json" with { type: 'json' };
29
- import FactoryABI from "./constants/abis/crvUSD/Factory.json" with { type: 'json' };
30
- import MonetaryPolicy2ABI from "./constants/abis/crvUSD/MonetaryPolicy2.json" with { type: 'json' };
31
28
  import HealthCalculatorZapABI from "./constants/abis/crvUSD/HealthCalculatorZap.json" with { type: 'json' };
32
29
  import LeverageZapCrvUSDABI from "./constants/abis/crvUSD/LeverageZap.json" with { type: 'json' };
33
30
  import DeleverageZapABI from "./constants/abis/crvUSD/DeleverageZap.json" with { type: 'json' };
@@ -36,10 +33,25 @@ import { COINS_ETHEREUM, COINS_OPTIMISM, COINS_ARBITRUM, COINS_FRAXTAL, COINS_SO
36
33
  import { LLAMMAS } from "./constants/llammas.js";
37
34
  import { L2Networks } from "./constants/L2Networks.js";
38
35
  import { createCall, handleMultiCallResponse } from "./utils.js";
39
- import { cacheKey, cacheStats } from "./cache/index.js";
40
- import { _getMarketsData, _getHiddenPools } from "./external-api.js";
36
+ import { _getMarketsData, _getCrvUsdMarketsData } from "./external-api.js";
41
37
  import { extractDecimals } from "./constants/utils.js";
42
38
  import { fetchOneWayMarketsByBlockchain, fetchOneWayMarketsByAPI } from "./lendMarkets/fetch/fetchLendMarkets.js";
39
+ import { fetchMintMarketsByBlockchain, fetchMintMarketsByAPI } from "./mintMarkets/fetch/fetchMintMarkets.js";
40
+ const memoizeByAddress = (factory) => {
41
+ return () => {
42
+ const cache = {};
43
+ return (address, abi, ...args) => {
44
+ if (address in cache) {
45
+ return cache[address];
46
+ }
47
+ const result = factory(address, abi, ...args);
48
+ cache[address] = result;
49
+ return result;
50
+ };
51
+ };
52
+ };
53
+ const memoizedContract = memoizeByAddress((address, abi, provider) => new Contract(address, abi, provider));
54
+ const memoizedMulticallContract = memoizeByAddress((address, abi) => new MulticallContract(address, abi));
43
55
  export const NETWORK_CONSTANTS = {
44
56
  1: {
45
57
  NAME: 'ethereum',
@@ -71,6 +83,8 @@ export const NETWORK_CONSTANTS = {
71
83
  };
72
84
  class Llamalend {
73
85
  constructor() {
86
+ this.initContract = memoizedContract();
87
+ this.initMulticallContract = memoizedMulticallContract();
74
88
  this.getLendMarketList = () => Object.keys(Object.assign(Object.assign({}, this.constants.ONE_WAY_MARKETS), this.constants.ONE_WAY_MARKETS_V2));
75
89
  this.getMintMarketList = () => Object.keys(this.constants.LLAMMAS);
76
90
  this.fetchLendMarkets = (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ useApi = true, version = 'v1' } = {}) {
@@ -84,6 +98,12 @@ class Llamalend {
84
98
  yield fetchOneWayMarketsByBlockchain(this, version);
85
99
  }
86
100
  });
101
+ this.fetchMintMarkets = (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ useApi = true } = {}) {
102
+ yield Promise.all([
103
+ this._setupMintMarketContracts(useApi),
104
+ useApi ? fetchMintMarketsByAPI(this) : fetchMintMarketsByBlockchain(this),
105
+ ]);
106
+ });
87
107
  this.getCoins = (collateral_tokens_1, borrowed_tokens_1, ...args_1) => __awaiter(this, [collateral_tokens_1, borrowed_tokens_1, ...args_1], void 0, function* (collateral_tokens, borrowed_tokens, useApi = false) {
88
108
  const coins = new Set([...collateral_tokens, ...borrowed_tokens]);
89
109
  const COINS_DATA = {};
@@ -134,46 +154,6 @@ class Llamalend {
134
154
  }
135
155
  return COINS_DATA;
136
156
  });
137
- this.fetchStats = (amms_1, controllers_1, vaults_1, borrowed_tokens_1, collateral_tokens_1, ...args_1) => __awaiter(this, [amms_1, controllers_1, vaults_1, borrowed_tokens_1, collateral_tokens_1, ...args_1], void 0, function* (amms, controllers, vaults, borrowed_tokens, collateral_tokens, version = 'v1') {
138
- cacheStats.clear();
139
- const marketCount = controllers.length;
140
- const calls = [];
141
- for (let i = 0; i < marketCount; i++) {
142
- calls.push(createCall(this.contracts[controllers[i]], 'total_debt', []));
143
- calls.push(createCall(this.contracts[vaults[i]], 'totalAssets', []));
144
- calls.push(createCall(this.contracts[borrowed_tokens[i]], 'balanceOf', [controllers[i]]));
145
- calls.push(createCall(this.contracts[amms[i]], 'rate', []));
146
- calls.push(createCall(this.contracts[borrowed_tokens[i]], 'balanceOf', [amms[i]]));
147
- if (version === 'v1') {
148
- calls.push(createCall(this.contracts[amms[i]], 'admin_fees_x', []));
149
- calls.push(createCall(this.contracts[amms[i]], 'admin_fees_y', []));
150
- }
151
- calls.push(createCall(this.contracts[collateral_tokens[i]], 'balanceOf', [amms[i]]));
152
- }
153
- const res = yield this.multicallProvider.all(calls);
154
- for (let i = 0; i < marketCount; i++) {
155
- if (version === 'v1') {
156
- cacheStats.set(cacheKey(controllers[i], 'total_debt'), res[(i * 8) + 0]);
157
- cacheStats.set(cacheKey(vaults[i], 'totalAssets', controllers[i]), res[(i * 8) + 1]);
158
- cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', controllers[i]), res[(i * 8) + 2]);
159
- cacheStats.set(cacheKey(amms[i], 'rate'), res[(i * 8) + 3]);
160
- cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', amms[i]), res[(i * 8) + 4]);
161
- cacheStats.set(cacheKey(amms[i], 'admin_fees_x'), res[(i * 8) + 5]);
162
- cacheStats.set(cacheKey(amms[i], 'admin_fees_y'), res[(i * 8) + 6]);
163
- cacheStats.set(cacheKey(collateral_tokens[i], 'balanceOf', amms[i]), res[(i * 8) + 7]);
164
- }
165
- else {
166
- cacheStats.set(cacheKey(controllers[i], 'total_debt'), res[(i * 6) + 0]);
167
- cacheStats.set(cacheKey(vaults[i], 'totalAssets', controllers[i]), res[(i * 6) + 1]);
168
- cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', controllers[i]), res[(i * 6) + 2]);
169
- cacheStats.set(cacheKey(amms[i], 'rate'), res[(i * 6) + 3]);
170
- cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', amms[i]), res[(i * 6) + 4]);
171
- cacheStats.set(cacheKey(amms[i], 'admin_fees_x'), BigInt(0)); // Always 0 for v2
172
- cacheStats.set(cacheKey(amms[i], 'admin_fees_y'), BigInt(0)); // Always 0 for v2
173
- cacheStats.set(cacheKey(collateral_tokens[i], 'balanceOf', amms[i]), res[(i * 6) + 5]);
174
- }
175
- }
176
- });
177
157
  this.address = '00000';
178
158
  this.crvUsdAddress = COINS_ETHEREUM.crvusd;
179
159
  this.provider = null;
@@ -241,55 +221,47 @@ class Llamalend {
241
221
  ],
242
222
  WETH: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".toLowerCase(),
243
223
  };
244
- // JsonRpc provider
224
+ let signerPromise;
245
225
  if (providerType.toLowerCase() === 'JsonRpc'.toLowerCase()) {
246
226
  providerSettings = providerSettings;
247
227
  let jsonRpcApiProviderOptions;
248
228
  if (providerSettings.batchMaxCount) {
249
- jsonRpcApiProviderOptions = {
250
- batchMaxCount: providerSettings.batchMaxCount,
251
- };
252
- }
253
- if (providerSettings.url) {
254
- this.provider = new ethers.JsonRpcProvider(providerSettings.url, undefined, jsonRpcApiProviderOptions);
255
- }
256
- else {
257
- this.provider = new ethers.JsonRpcProvider('http://localhost:8545/', undefined, jsonRpcApiProviderOptions);
229
+ jsonRpcApiProviderOptions = { batchMaxCount: providerSettings.batchMaxCount };
258
230
  }
231
+ this.provider = new ethers.JsonRpcProvider(providerSettings.url || 'http://localhost:8545/', undefined, jsonRpcApiProviderOptions);
259
232
  if (providerSettings.privateKey) {
260
- this.signer = new ethers.Wallet(providerSettings.privateKey, this.provider);
233
+ signerPromise = Promise.resolve(new ethers.Wallet(providerSettings.privateKey, this.provider));
261
234
  }
262
235
  else if (!((_a = providerSettings.url) === null || _a === void 0 ? void 0 : _a.startsWith("https://rpc.gnosischain.com"))) {
263
- try {
264
- this.signer = yield this.provider.getSigner();
265
- }
266
- catch (_b) {
267
- this.signer = null;
268
- }
236
+ signerPromise = this.provider.getSigner().catch(() => null);
237
+ }
238
+ else {
239
+ signerPromise = Promise.resolve(null);
269
240
  }
270
- // Web3 provider
271
241
  }
272
242
  else if (providerType.toLowerCase() === 'Web3'.toLowerCase()) {
273
243
  providerSettings = providerSettings;
274
244
  this.provider = new ethers.BrowserProvider(providerSettings.externalProvider);
275
- this.signer = yield this.provider.getSigner();
276
- // Infura provider
245
+ signerPromise = this.provider.getSigner();
277
246
  }
278
247
  else if (providerType.toLowerCase() === 'Infura'.toLowerCase()) {
279
248
  providerSettings = providerSettings;
280
249
  this.provider = new ethers.InfuraProvider(providerSettings.network, providerSettings.apiKey);
281
- this.signer = null;
282
- // Alchemy provider
250
+ signerPromise = Promise.resolve(null);
283
251
  }
284
252
  else if (providerType.toLowerCase() === 'Alchemy'.toLowerCase()) {
285
253
  providerSettings = providerSettings;
286
254
  this.provider = new ethers.AlchemyProvider(providerSettings.network, providerSettings.apiKey);
287
- this.signer = null;
255
+ signerPromise = Promise.resolve(null);
288
256
  }
289
257
  else {
290
258
  throw Error('Wrong providerType');
291
259
  }
292
- const network = yield this.provider.getNetwork();
260
+ const [signer, network] = yield Promise.all([
261
+ signerPromise,
262
+ this.provider.getNetwork(),
263
+ ]);
264
+ this.signer = signer;
293
265
  this.chainId = Number(network.chainId) === 133 || Number(network.chainId) === 31337 ? 1 : Number(network.chainId);
294
266
  console.log("CURVE-LLAMALEND-JS IS CONNECTED TO NETWORK:", { name: network.name.toUpperCase(), chainId: Number(this.chainId) });
295
267
  if (this.chainId === 42161) {
@@ -307,7 +279,7 @@ class Llamalend {
307
279
  try {
308
280
  this.signerAddress = yield this.signer.getAddress();
309
281
  }
310
- catch (_c) {
282
+ catch (_b) {
311
283
  this.signer = null;
312
284
  }
313
285
  }
@@ -315,7 +287,6 @@ class Llamalend {
315
287
  this.signerAddress = '';
316
288
  }
317
289
  this.feeData = { gasPrice: options.gasPrice, maxFeePerGas: options.maxFeePerGas, maxPriorityFeePerGas: options.maxPriorityFeePerGas };
318
- yield this.updateFeeData();
319
290
  // oneWayMarkets contracts
320
291
  this.setContract(this.constants.ALIASES['one_way_factory'], OneWayLendingFactoryABI);
321
292
  if (this.constants.ALIASES['one_way_factory_v2'] && this.constants.ALIASES['one_way_factory_v2'] !== this.constants.ZERO_ADDRESS) {
@@ -342,103 +313,6 @@ class Llamalend {
342
313
  this.setContract(this.constants.ALIASES.gauge_factory, GaugeFactorySidechainABI);
343
314
  }
344
315
  }
345
- // crvUSD contracts
346
- this.setContract(this.crvUsdAddress, ERC20ABI);
347
- if (this.chainId === 1) {
348
- this.setContract(this.constants.COINS.crvusd.toLowerCase(), ERC20ABI);
349
- for (const llamma of Object.values(this.constants.LLAMMAS)) {
350
- this.setContract(llamma.amm_address, llammaABI);
351
- this.setContract(llamma.controller_address, controllerABI);
352
- const monetary_policy_address = yield this.contracts[llamma.controller_address].contract.monetary_policy(this.constantOptions);
353
- llamma.monetary_policy_address = monetary_policy_address.toLowerCase();
354
- this.setContract(llamma.monetary_policy_address, llamma.monetary_policy_abi);
355
- if (llamma.collateral_address === "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
356
- this.setContract(this.constants.WETH, ERC20ABI);
357
- }
358
- else {
359
- this.setContract(llamma.collateral_address, ERC20ABI);
360
- }
361
- this.setContract(llamma.leverage_zap, LeverageZapCrvUSDABI);
362
- this.setContract(llamma.deleverage_zap, DeleverageZapABI);
363
- if (llamma.health_calculator_zap)
364
- this.setContract(llamma.health_calculator_zap, HealthCalculatorZapABI);
365
- }
366
- for (const pegKeeper of this.constants.PEG_KEEPERS) {
367
- this.setContract(pegKeeper, PegKeeper);
368
- }
369
- }
370
- // TODO Put it in a separate method
371
- // Fetch new llammas
372
- if (this.chainId === 1) {
373
- this.setContract(this.constants.FACTORY, FactoryABI);
374
- const factoryContract = this.contracts[this.constants.FACTORY].contract;
375
- const factoryMulticallContract = this.contracts[this.constants.FACTORY].multicallContract;
376
- const N1 = Object.keys(this.constants.LLAMMAS).length;
377
- const N2 = yield factoryContract.n_collaterals(this.constantOptions);
378
- let calls = [];
379
- for (let i = N1; i < N2; i++) {
380
- calls.push(factoryMulticallContract.collaterals(i), factoryMulticallContract.amms(i), factoryMulticallContract.controllers(i));
381
- }
382
- const res = (yield this.multicallProvider.all(calls)).map((c) => c.toLowerCase());
383
- const collaterals = res.filter((a, i) => i % 3 == 0);
384
- const amms = res.filter((a, i) => i % 3 == 1);
385
- const controllers = res.filter((a, i) => i % 3 == 2);
386
- if (collaterals.length > 0) {
387
- for (const collateral of collaterals)
388
- this.setContract(collateral, ERC20ABI);
389
- calls = [];
390
- for (const collateral of collaterals) {
391
- calls.push(this.contracts[collateral].multicallContract.symbol(), this.contracts[collateral].multicallContract.decimals());
392
- }
393
- const res = (yield this.multicallProvider.all(calls)).map((x) => {
394
- if (typeof x === "string")
395
- return x.toLowerCase();
396
- return x;
397
- });
398
- calls = [];
399
- for (const amm of amms) {
400
- this.setContract(amm, llammaABI);
401
- calls.push(this.contracts[amm].multicallContract.A());
402
- }
403
- const AParams = (yield this.multicallProvider.all(calls)).map((x) => {
404
- return Number(x);
405
- });
406
- for (let i = 0; i < collaterals.length; i++) {
407
- const is_eth = collaterals[i] === this.constants.WETH;
408
- const [collateral_symbol, collateral_decimals] = res.splice(0, 2);
409
- if (i >= collaterals.length - 3) {
410
- this.setContract(controllers[i], controllerV2ABI);
411
- }
412
- else {
413
- this.setContract(controllers[i], controllerABI);
414
- }
415
- const monetary_policy_address = (yield this.contracts[controllers[i]].contract.monetary_policy(this.constantOptions)).toLowerCase();
416
- this.setContract(monetary_policy_address, MonetaryPolicy2ABI);
417
- const _llammaId = is_eth ? "eth" : collateral_symbol.toLowerCase();
418
- let llammaId = _llammaId;
419
- let j = 2;
420
- while (llammaId in this.constants.LLAMMAS)
421
- llammaId = _llammaId + j++;
422
- this.constants.LLAMMAS[llammaId] = {
423
- amm_address: amms[i],
424
- controller_address: controllers[i],
425
- monetary_policy_address,
426
- collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collaterals[i],
427
- leverage_zap: this.constants.ALIASES.leverage_zap,
428
- deleverage_zap: "0x0000000000000000000000000000000000000000",
429
- collateral_symbol: is_eth ? "ETH" : collateral_symbol,
430
- collateral_decimals,
431
- min_bands: 4,
432
- max_bands: 50,
433
- default_bands: 10,
434
- A: AParams[i],
435
- monetary_policy_abi: MonetaryPolicy2ABI,
436
- is_deleverage_supported: true,
437
- index: N1 + i,
438
- };
439
- }
440
- }
441
- }
442
316
  this.constants.DECIMALS = Object.assign(Object.assign({}, extractDecimals(this.constants.LLAMMAS)), { [this.crvUsdAddress]: 18, [this.constants.ALIASES.crv]: 18, [this.constants.ALIASES.crvUSD]: 18, [this.constants.ALIASES.st_crvUSD]: 18 });
443
317
  if (L2Networks.includes(this.chainId)) {
444
318
  // eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -479,20 +353,70 @@ class Llamalend {
479
353
  setContract(address, abi) {
480
354
  if (address === this.constants.ZERO_ADDRESS || address === undefined)
481
355
  return;
482
- this.contracts[address] = {
483
- contract: new Contract(address, abi, this.signer || this.provider),
484
- multicallContract: new MulticallContract(address, abi),
485
- address: address,
486
- abi: abi,
356
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
357
+ const llamalendInstance = this;
358
+ const proxyHandler = {
359
+ get: function (target, name) {
360
+ if (name === 'contract') {
361
+ return llamalendInstance.initContract(target['address'], target['abi'], llamalendInstance.signer || llamalendInstance.provider);
362
+ }
363
+ else if (name === 'multicallContract') {
364
+ return llamalendInstance.initMulticallContract(target['address'], target['abi']);
365
+ }
366
+ else {
367
+ return target[name];
368
+ }
369
+ },
370
+ };
371
+ const coreContract = {
372
+ address,
373
+ abi,
487
374
  };
375
+ this.contracts[address] = new Proxy(coreContract, proxyHandler);
488
376
  }
489
377
  setCustomFeeData(customFeeData) {
490
378
  this.feeData = Object.assign(Object.assign({}, this.feeData), customFeeData);
491
379
  }
492
- _filterHiddenMarkets(markets) {
493
- return __awaiter(this, void 0, void 0, function* () {
494
- const hiddenMarkets = (yield _getHiddenPools())[this.constants.NETWORK_NAME] || [];
495
- return Object.fromEntries(Object.entries(markets).filter(([id]) => !hiddenMarkets.includes(id)));
380
+ _setupMintMarketContracts() {
381
+ return __awaiter(this, arguments, void 0, function* (useApi = true) {
382
+ this.setContract(this.crvUsdAddress, ERC20ABI);
383
+ if (this.chainId !== 1)
384
+ return;
385
+ this.setContract(this.constants.COINS.crvusd.toLowerCase(), ERC20ABI);
386
+ const llammas = Object.values(this.constants.LLAMMAS);
387
+ for (const llamma of llammas) {
388
+ this.setContract(llamma.amm_address, llammaABI);
389
+ this.setContract(llamma.controller_address, controllerABI);
390
+ }
391
+ if (useApi) {
392
+ const apiData = yield _getCrvUsdMarketsData();
393
+ const monetaryPolicyMap = new Map(apiData.map((m) => [m.address.toLowerCase(), m.monetary_policy_address.toLowerCase()]));
394
+ for (const llamma of llammas) {
395
+ const fresh = monetaryPolicyMap.get(llamma.controller_address);
396
+ if (fresh)
397
+ llamma.monetary_policy_address = fresh;
398
+ }
399
+ }
400
+ else {
401
+ const monetaryPolicies = (yield this.multicallProvider.all(llammas.map((l) => this.contracts[l.controller_address].multicallContract.monetary_policy()))).map((a) => a.toLowerCase());
402
+ llammas.forEach((llamma, i) => { llamma.monetary_policy_address = monetaryPolicies[i]; });
403
+ }
404
+ for (const llamma of llammas) {
405
+ this.setContract(llamma.monetary_policy_address, llamma.monetary_policy_abi);
406
+ if (llamma.collateral_address === "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
407
+ this.setContract(this.constants.WETH, ERC20ABI);
408
+ }
409
+ else {
410
+ this.setContract(llamma.collateral_address, ERC20ABI);
411
+ }
412
+ this.setContract(llamma.leverage_zap, LeverageZapCrvUSDABI);
413
+ this.setContract(llamma.deleverage_zap, DeleverageZapABI);
414
+ if (llamma.health_calculator_zap)
415
+ this.setContract(llamma.health_calculator_zap, HealthCalculatorZapABI);
416
+ }
417
+ for (const pegKeeper of this.constants.PEG_KEEPERS) {
418
+ this.setContract(pegKeeper, PegKeeper);
419
+ }
496
420
  });
497
421
  }
498
422
  formatUnits(value, unit) {
@@ -0,0 +1,3 @@
1
+ import type { Llamalend } from "../../llamalend.js";
2
+ export declare const fetchMintMarketsByAPI: (llamalend: Llamalend) => Promise<void>;
3
+ export declare const fetchMintMarketsByBlockchain: (llamalend: Llamalend) => Promise<void>;