@curvefi/llamalend-api 1.0.21 → 1.0.22-beta.2

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.
package/lib/utils.js CHANGED
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { ethers } from "ethers";
11
11
  import BigNumber from 'bignumber.js';
12
12
  import { _getUsdPricesFromApi } from "./external-api.js";
13
- import { llamalend } from "./llamalend.js";
14
13
  import { L2Networks } from "./constants/L2Networks.js";
15
14
  import memoize from "memoizee";
16
15
  export const MAX_ALLOWANCE = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); // 2**256 - 1
@@ -102,8 +101,8 @@ export const gasSum = (gas, currentGas) => {
102
101
  }
103
102
  return gas;
104
103
  };
105
- export const _getAddress = (address) => {
106
- address = address || llamalend.signerAddress;
104
+ export const _getAddress = function (address) {
105
+ address = address || this.signerAddress;
107
106
  if (!address)
108
107
  throw Error("Need to connect wallet or pass address into args");
109
108
  return address;
@@ -117,290 +116,325 @@ export const handleMultiCallResponse = (callsMap, response) => {
117
116
  return result;
118
117
  };
119
118
  // coins can be either addresses or symbols
120
- export const _getCoinAddressesNoCheck = (...coins) => {
119
+ export const _getCoinAddressesNoCheck = function (...coins) {
121
120
  if (coins.length == 1 && Array.isArray(coins[0]))
122
121
  coins = coins[0];
123
122
  coins = coins;
124
- return coins.map((c) => c.toLowerCase()).map((c) => llamalend.constants.COINS[c] || c);
123
+ return coins.map((c) => c.toLowerCase()).map((c) => this.constants.COINS[c] || c);
125
124
  };
126
- export const _getCoinAddresses = (coins) => {
127
- const coinAddresses = _getCoinAddressesNoCheck(coins);
128
- const availableAddresses = Object.keys(llamalend.constants.DECIMALS);
125
+ export const _getCoinAddresses = function (coins) {
126
+ const coinAddresses = _getCoinAddressesNoCheck.call(this, coins);
127
+ const availableAddresses = Object.keys(this.constants.DECIMALS);
129
128
  for (const coinAddr of coinAddresses) {
130
129
  if (!availableAddresses.includes(coinAddr))
131
130
  throw Error(`Coin with address '${coinAddr}' is not available`);
132
131
  }
133
132
  return coinAddresses;
134
133
  };
135
- export const _getCoinDecimals = (coinAddresses) => {
136
- return coinAddresses.map((coinAddr) => { var _a; return (_a = llamalend.constants.DECIMALS[coinAddr.toLowerCase()]) !== null && _a !== void 0 ? _a : 18; });
134
+ export const _getCoinDecimals = function (coinAddresses) {
135
+ return coinAddresses.map((coinAddr) => { var _a; return (_a = this.constants.DECIMALS[coinAddr.toLowerCase()]) !== null && _a !== void 0 ? _a : 18; });
137
136
  };
138
137
  // --- BALANCES ---
139
- export const _getBalances = (coinAddresses_1, ...args_1) => __awaiter(void 0, [coinAddresses_1, ...args_1], void 0, function* (coinAddresses, address = "") {
140
- address = _getAddress(address);
141
- const _coinAddresses = [...coinAddresses];
142
- const ethIndex = getEthIndex(_coinAddresses);
143
- if (ethIndex !== -1) {
144
- _coinAddresses.splice(ethIndex, 1);
145
- }
146
- const contractCalls = [];
147
- for (const coinAddr of _coinAddresses) {
148
- contractCalls.push(llamalend.contracts[coinAddr].multicallContract.balanceOf(address));
149
- }
150
- const _balances = yield llamalend.multicallProvider.all(contractCalls);
151
- if (ethIndex !== -1) {
152
- const ethBalance = yield llamalend.provider.getBalance(address);
153
- _balances.splice(ethIndex, 0, ethBalance);
154
- }
155
- return _balances;
156
- });
157
- export const getBalances = (coins_1, ...args_1) => __awaiter(void 0, [coins_1, ...args_1], void 0, function* (coins, address = "") {
158
- const coinAddresses = _getCoinAddresses(coins);
159
- const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
160
- const _balances = yield _getBalances(coinAddresses, address);
161
- return _balances.map((_b, i) => formatUnits(_b, decimals[i]));
162
- });
163
- export const _getAllowance = memoize((coins, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
164
- const _coins = [...coins];
165
- const ethIndex = getEthIndex(_coins);
166
- if (ethIndex !== -1) {
167
- _coins.splice(ethIndex, 1);
168
- }
169
- let allowance;
170
- if (_coins.length === 1) {
171
- allowance = [yield llamalend.contracts[_coins[0]].contract.allowance(address, spender, llamalend.constantOptions)];
172
- }
173
- else {
174
- const contractCalls = _coins.map((coinAddr) => llamalend.contracts[coinAddr].multicallContract.allowance(address, spender));
175
- allowance = yield llamalend.multicallProvider.all(contractCalls);
176
- }
177
- if (ethIndex !== -1) {
178
- allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
179
- }
180
- return allowance;
181
- }), {
138
+ export const _getBalances = function (coinAddresses_1) {
139
+ return __awaiter(this, arguments, void 0, function* (coinAddresses, address = "") {
140
+ address = _getAddress.call(this, address);
141
+ const _coinAddresses = [...coinAddresses];
142
+ const ethIndex = getEthIndex(_coinAddresses);
143
+ if (ethIndex !== -1) {
144
+ _coinAddresses.splice(ethIndex, 1);
145
+ }
146
+ const contractCalls = [];
147
+ for (const coinAddr of _coinAddresses) {
148
+ contractCalls.push(this.contracts[coinAddr].multicallContract.balanceOf(address));
149
+ }
150
+ const _balances = yield this.multicallProvider.all(contractCalls);
151
+ if (ethIndex !== -1) {
152
+ const ethBalance = yield this.provider.getBalance(address);
153
+ _balances.splice(ethIndex, 0, ethBalance);
154
+ }
155
+ return _balances;
156
+ });
157
+ };
158
+ export const getBalances = function (coins_1) {
159
+ return __awaiter(this, arguments, void 0, function* (coins, address = "") {
160
+ const coinAddresses = _getCoinAddresses.call(this, coins);
161
+ const decimals = _getCoinDecimals.call(this, coinAddresses).map((item) => Number(item));
162
+ const _balances = yield _getBalances.call(this, coinAddresses, address);
163
+ return _balances.map((_b, i) => formatUnits(_b, decimals[i]));
164
+ });
165
+ };
166
+ const _getAllowanceMemoized = memoize(function (coins, address, spender, contracts, multicallProvider, constantOptions) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ const _coins = [...coins];
169
+ const ethIndex = getEthIndex(_coins);
170
+ if (ethIndex !== -1) {
171
+ _coins.splice(ethIndex, 1);
172
+ }
173
+ let allowance;
174
+ if (_coins.length === 1) {
175
+ allowance = [yield contracts[_coins[0]].contract.allowance(address, spender, constantOptions)];
176
+ }
177
+ else {
178
+ const contractCalls = _coins.map((coinAddr) => contracts[coinAddr].multicallContract.allowance(address, spender));
179
+ allowance = yield multicallProvider.all(contractCalls);
180
+ }
181
+ if (ethIndex !== -1) {
182
+ allowance.splice(ethIndex, 0, MAX_ALLOWANCE);
183
+ }
184
+ return allowance;
185
+ });
186
+ }, {
182
187
  promise: true,
183
188
  maxAge: 5 * 1000, // 5s
184
189
  primitive: true,
185
- length: 3,
190
+ length: 6,
186
191
  });
192
+ export function _getAllowance(coins, address, spender) {
193
+ return __awaiter(this, void 0, void 0, function* () {
194
+ return _getAllowanceMemoized(coins, address, spender, this.contracts, this.multicallProvider, this.constantOptions);
195
+ });
196
+ }
187
197
  // coins can be either addresses or symbols
188
- export const getAllowance = (coins, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
189
- const coinAddresses = _getCoinAddresses(coins);
190
- const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
191
- const _allowance = yield _getAllowance(coinAddresses, address, spender);
192
- return _allowance.map((a, i) => llamalend.formatUnits(a, decimals[i]));
193
- });
198
+ export const getAllowance = function (coins, address, spender) {
199
+ return __awaiter(this, void 0, void 0, function* () {
200
+ const coinAddresses = _getCoinAddresses.call(this, coins);
201
+ const decimals = _getCoinDecimals.call(this, coinAddresses).map((item) => Number(item));
202
+ const _allowance = yield _getAllowance.call(this, coinAddresses, address, spender);
203
+ return _allowance.map((a, i) => this.formatUnits(a, decimals[i]));
204
+ });
205
+ };
194
206
  // coins can be either addresses or symbols
195
- export const hasAllowance = (coins, amounts, address, spender) => __awaiter(void 0, void 0, void 0, function* () {
196
- const coinAddresses = _getCoinAddresses(coins);
197
- const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
198
- const _allowance = yield _getAllowance(coinAddresses, address, spender);
199
- const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
200
- return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
201
- });
202
- export const _ensureAllowance = (coins_1, _amounts_1, spender_1, ...args_1) => __awaiter(void 0, [coins_1, _amounts_1, spender_1, ...args_1], void 0, function* (coins, _amounts, spender, isMax = true) {
203
- const address = llamalend.signerAddress;
204
- const _allowance = yield _getAllowance(coins, address, spender);
205
- const txHashes = [];
206
- for (let i = 0; i < _allowance.length; i++) {
207
- if (_allowance[i] < _amounts[i]) {
208
- const contract = llamalend.contracts[coins[i]].contract;
209
- const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
210
- yield llamalend.updateFeeData();
211
- const gasLimit = _mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, _approveAmount, llamalend.constantOptions)));
212
- txHashes.push((yield contract.approve(spender, _approveAmount, Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash);
207
+ export const hasAllowance = function (coins, amounts, address, spender) {
208
+ return __awaiter(this, void 0, void 0, function* () {
209
+ const coinAddresses = _getCoinAddresses.call(this, coins);
210
+ const decimals = _getCoinDecimals.call(this, coinAddresses).map((item) => Number(item));
211
+ const _allowance = yield _getAllowance.call(this, coinAddresses, address, spender);
212
+ const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
213
+ return _allowance.map((a, i) => a >= _amounts[i]).reduce((a, b) => a && b);
214
+ });
215
+ };
216
+ export const _ensureAllowance = function (coins_1, _amounts_1, spender_1) {
217
+ return __awaiter(this, arguments, void 0, function* (coins, _amounts, spender, isMax = true) {
218
+ const address = this.signerAddress;
219
+ const _allowance = yield _getAllowance.call(this, coins, address, spender);
220
+ const txHashes = [];
221
+ for (let i = 0; i < _allowance.length; i++) {
222
+ if (_allowance[i] < _amounts[i]) {
223
+ const contract = this.contracts[coins[i]].contract;
224
+ const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
225
+ yield this.updateFeeData();
226
+ const gasLimit = _mulBy1_3(DIGas(yield contract.approve.estimateGas(spender, _approveAmount, this.constantOptions)));
227
+ txHashes.push((yield contract.approve(spender, _approveAmount, Object.assign(Object.assign({}, this.options), { gasLimit }))).hash);
228
+ }
213
229
  }
214
- }
215
- return txHashes;
216
- });
230
+ return txHashes;
231
+ });
232
+ };
217
233
  // coins can be either addresses or symbols
218
- export const ensureAllowanceEstimateGas = (coins_1, amounts_1, spender_1, ...args_1) => __awaiter(void 0, [coins_1, amounts_1, spender_1, ...args_1], void 0, function* (coins, amounts, spender, isMax = true) {
219
- const coinAddresses = _getCoinAddresses(coins);
220
- const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
221
- const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
222
- const _allowance = yield _getAllowance(coinAddresses, llamalend.signerAddress, spender);
223
- let gas = [0, 0];
224
- for (let i = 0; i < _allowance.length; i++) {
225
- if (_allowance[i] < _amounts[i]) {
226
- const contract = llamalend.contracts[coinAddresses[i]].contract;
227
- const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
228
- const currentGas = smartNumber(yield contract.approve.estimateGas(spender, _approveAmount, llamalend.constantOptions));
229
- gas = gasSum(gas, currentGas);
234
+ export const ensureAllowanceEstimateGas = function (coins_1, amounts_1, spender_1) {
235
+ return __awaiter(this, arguments, void 0, function* (coins, amounts, spender, isMax = true) {
236
+ const coinAddresses = _getCoinAddresses.call(this, coins);
237
+ const decimals = _getCoinDecimals.call(this, coinAddresses).map((item) => Number(item));
238
+ const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
239
+ const _allowance = yield _getAllowance.call(this, coinAddresses, this.signerAddress, spender);
240
+ let gas = [0, 0];
241
+ for (let i = 0; i < _allowance.length; i++) {
242
+ if (_allowance[i] < _amounts[i]) {
243
+ const contract = this.contracts[coinAddresses[i]].contract;
244
+ const _approveAmount = isMax ? MAX_ALLOWANCE : _amounts[i];
245
+ const currentGas = smartNumber(yield contract.approve.estimateGas(spender, _approveAmount, this.constantOptions));
246
+ gas = gasSum(gas, currentGas);
247
+ }
230
248
  }
231
- }
232
- return getGasFromArray(gas);
233
- });
249
+ return getGasFromArray(gas);
250
+ });
251
+ };
234
252
  // coins can be either addresses or symbols
235
- export const ensureAllowance = (coins_1, amounts_1, spender_1, ...args_1) => __awaiter(void 0, [coins_1, amounts_1, spender_1, ...args_1], void 0, function* (coins, amounts, spender, isMax = true) {
236
- const coinAddresses = _getCoinAddresses(coins);
237
- const decimals = _getCoinDecimals(coinAddresses).map((item) => Number(item));
238
- const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
239
- return yield _ensureAllowance(coinAddresses, _amounts, spender, isMax);
240
- });
253
+ export const ensureAllowance = function (coins_1, amounts_1, spender_1) {
254
+ return __awaiter(this, arguments, void 0, function* (coins, amounts, spender, isMax = true) {
255
+ const coinAddresses = _getCoinAddresses.call(this, coins);
256
+ const decimals = _getCoinDecimals.call(this, coinAddresses).map((item) => Number(item));
257
+ const _amounts = amounts.map((a, i) => parseUnits(a, decimals[i]));
258
+ return yield _ensureAllowance.call(this, coinAddresses, _amounts, spender, isMax);
259
+ });
260
+ };
241
261
  const _usdRatesCache = {};
242
- export const _getUsdRate = (assetId) => __awaiter(void 0, void 0, void 0, function* () {
243
- var _a, _c;
244
- if (llamalend.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
245
- return 0; // RSR
246
- const pricesFromApi = yield _getUsdPricesFromApi();
247
- if (assetId.toLowerCase() in pricesFromApi)
248
- return pricesFromApi[assetId.toLowerCase()];
249
- if (assetId === 'USD' || (llamalend.chainId === 137 && (assetId.toLowerCase() === llamalend.constants.COINS.am3crv.toLowerCase())))
250
- return 1;
251
- let chainName = {
252
- 1: 'ethereum',
253
- 10: 'optimistic-ethereum',
254
- 56: "binance-smart-chain",
255
- 100: 'xdai',
256
- 137: 'polygon-pos',
257
- 146: 'sonic',
258
- 196: 'x-layer',
259
- 250: 'fantom',
260
- 252: 'fraxtal',
261
- 324: 'zksync',
262
- 1284: 'moonbeam',
263
- 2222: 'kava',
264
- 5000: 'mantle',
265
- 8453: 'base',
266
- 42220: 'celo',
267
- 43114: 'avalanche',
268
- 42161: 'arbitrum-one',
269
- 1313161554: 'aurora',
270
- }[llamalend.chainId];
271
- const nativeTokenName = {
272
- 1: 'ethereum',
273
- 10: 'ethereum',
274
- 56: 'binancecoin',
275
- 100: 'xdai',
276
- 137: 'matic-network',
277
- 146: 'sonic-3',
278
- 196: 'okb',
279
- 250: 'fantom',
280
- 252: 'frax-ether',
281
- 324: 'ethereum',
282
- 1284: 'moonbeam',
283
- 2222: 'kava',
284
- 5000: 'mantle',
285
- 8453: 'ethereum',
286
- 42220: 'celo',
287
- 43114: 'avalanche-2',
288
- 42161: 'ethereum',
289
- 1313161554: 'ethereum',
290
- }[llamalend.chainId];
291
- if (chainName === undefined) {
292
- throw Error('curve object is not initialized');
293
- }
294
- assetId = {
295
- 'CRV': 'curve-dao-token',
296
- 'EUR': 'stasis-eurs',
297
- 'BTC': 'bitcoin',
298
- 'ETH': 'ethereum',
299
- 'LINK': 'link',
300
- }[assetId.toUpperCase()] || assetId;
301
- assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
302
- // No EURT on Coingecko Polygon
303
- if (llamalend.chainId === 137 && assetId.toLowerCase() === llamalend.constants.COINS.eurt) {
304
- chainName = 'ethereum';
305
- assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
306
- }
307
- // CRV
308
- if (assetId.toLowerCase() === llamalend.constants.ALIASES.crv) {
309
- assetId = 'curve-dao-token';
310
- }
311
- if ((((_a = _usdRatesCache[assetId]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 600000 < Date.now()) {
312
- const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
313
- `https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
314
- `https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;
315
- const response = yield fetch(url);
316
- const data = yield response.json();
317
- try {
318
- _usdRatesCache[assetId] = { 'rate': (_c = data[assetId]['usd']) !== null && _c !== void 0 ? _c : 0, 'time': Date.now() };
262
+ export const _getUsdRate = function (assetId) {
263
+ return __awaiter(this, void 0, void 0, function* () {
264
+ var _a, _c;
265
+ if (this.chainId === 1 && assetId.toLowerCase() === '0x8762db106b2c2a0bccb3a80d1ed41273552616e8')
266
+ return 0; // RSR
267
+ const pricesFromApi = yield _getUsdPricesFromApi.call(this);
268
+ if (assetId.toLowerCase() in pricesFromApi)
269
+ return pricesFromApi[assetId.toLowerCase()];
270
+ if (assetId === 'USD' || (this.chainId === 137 && (assetId.toLowerCase() === this.constants.COINS.am3crv.toLowerCase())))
271
+ return 1;
272
+ let chainName = {
273
+ 1: 'ethereum',
274
+ 10: 'optimistic-ethereum',
275
+ 56: "binance-smart-chain",
276
+ 100: 'xdai',
277
+ 137: 'polygon-pos',
278
+ 146: 'sonic',
279
+ 196: 'x-layer',
280
+ 250: 'fantom',
281
+ 252: 'fraxtal',
282
+ 324: 'zksync',
283
+ 1284: 'moonbeam',
284
+ 2222: 'kava',
285
+ 5000: 'mantle',
286
+ 8453: 'base',
287
+ 42220: 'celo',
288
+ 43114: 'avalanche',
289
+ 42161: 'arbitrum-one',
290
+ 1313161554: 'aurora',
291
+ }[this.chainId];
292
+ const nativeTokenName = {
293
+ 1: 'ethereum',
294
+ 10: 'ethereum',
295
+ 56: 'binancecoin',
296
+ 100: 'xdai',
297
+ 137: 'matic-network',
298
+ 146: 'sonic-3',
299
+ 196: 'okb',
300
+ 250: 'fantom',
301
+ 252: 'frax-ether',
302
+ 324: 'ethereum',
303
+ 1284: 'moonbeam',
304
+ 2222: 'kava',
305
+ 5000: 'mantle',
306
+ 8453: 'ethereum',
307
+ 42220: 'celo',
308
+ 43114: 'avalanche-2',
309
+ 42161: 'ethereum',
310
+ 1313161554: 'ethereum',
311
+ }[this.chainId];
312
+ if (chainName === undefined) {
313
+ throw Error('curve object is not initialized');
319
314
  }
320
- catch ( // TODO pay attention!
321
- _d) { // TODO pay attention!
322
- _usdRatesCache[assetId] = { 'rate': 0, 'time': Date.now() };
315
+ assetId = {
316
+ 'CRV': 'curve-dao-token',
317
+ 'EUR': 'stasis-eurs',
318
+ 'BTC': 'bitcoin',
319
+ 'ETH': 'ethereum',
320
+ 'LINK': 'link',
321
+ }[assetId.toUpperCase()] || assetId;
322
+ assetId = isEth(assetId) ? nativeTokenName : assetId.toLowerCase();
323
+ // No EURT on Coingecko Polygon
324
+ if (this.chainId === 137 && assetId.toLowerCase() === this.constants.COINS.eurt) {
325
+ chainName = 'ethereum';
326
+ assetId = '0xC581b735A1688071A1746c968e0798D642EDE491'.toLowerCase(); // EURT Ethereum
323
327
  }
324
- }
325
- return _usdRatesCache[assetId]['rate'];
326
- });
327
- export const getUsdRate = (coin) => __awaiter(void 0, void 0, void 0, function* () {
328
- const [coinAddress] = _getCoinAddressesNoCheck(coin);
329
- return yield _getUsdRate(coinAddress);
330
- });
331
- export const getBaseFeeByLastBlock = () => __awaiter(void 0, void 0, void 0, function* () {
332
- const provider = llamalend.provider;
333
- try {
334
- const block = yield provider.getBlock('latest');
335
- if (!block) {
336
- return 0.01;
328
+ // CRV
329
+ if (assetId.toLowerCase() === this.constants.ALIASES.crv) {
330
+ assetId = 'curve-dao-token';
337
331
  }
338
- return Number(block.baseFeePerGas) / (Math.pow(10, 9));
339
- }
340
- catch (error) {
341
- throw new Error(error);
342
- }
343
- });
344
- export const getGasPriceFromL1 = () => __awaiter(void 0, void 0, void 0, function* () {
345
- if (L2Networks.includes(llamalend.chainId) && llamalend.L1WeightedGasPrice) {
346
- return llamalend.L1WeightedGasPrice + 1e9; // + 1 gwei
347
- }
348
- else {
349
- throw Error("This method exists only for L2 networks");
350
- }
351
- });
352
- export const getGasPriceFromL2 = () => __awaiter(void 0, void 0, void 0, function* () {
353
- if (llamalend.chainId === 42161) {
332
+ if ((((_a = _usdRatesCache[assetId]) === null || _a === void 0 ? void 0 : _a.time) || 0) + 600000 < Date.now()) {
333
+ const url = [nativeTokenName, 'ethereum', 'bitcoin', 'link', 'curve-dao-token', 'stasis-eurs'].includes(assetId.toLowerCase()) ?
334
+ `https://api.coingecko.com/api/v3/simple/price?ids=${assetId}&vs_currencies=usd` :
335
+ `https://api.coingecko.com/api/v3/simple/token_price/${chainName}?contract_addresses=${assetId}&vs_currencies=usd`;
336
+ const response = yield fetch(url);
337
+ const data = yield response.json();
338
+ try {
339
+ _usdRatesCache[assetId] = { 'rate': (_c = data[assetId]['usd']) !== null && _c !== void 0 ? _c : 0, 'time': Date.now() };
340
+ }
341
+ catch ( // TODO pay attention!
342
+ _d) { // TODO pay attention!
343
+ _usdRatesCache[assetId] = { 'rate': 0, 'time': Date.now() };
344
+ }
345
+ }
346
+ return _usdRatesCache[assetId]['rate'];
347
+ });
348
+ };
349
+ export const getUsdRate = function (coin) {
350
+ return __awaiter(this, void 0, void 0, function* () {
351
+ const [coinAddress] = _getCoinAddressesNoCheck.call(this, coin);
352
+ return yield _getUsdRate.call(this, coinAddress);
353
+ });
354
+ };
355
+ export const getBaseFeeByLastBlock = function () {
356
+ return __awaiter(this, void 0, void 0, function* () {
357
+ const provider = this.provider;
354
358
  try {
355
- return yield getBaseFeeByLastBlock();
359
+ const block = yield provider.getBlock('latest');
360
+ if (!block) {
361
+ return 0.01;
362
+ }
363
+ return Number(block.baseFeePerGas) / (Math.pow(10, 9));
356
364
  }
357
- catch (e) {
358
- throw Error(e);
365
+ catch (error) {
366
+ throw new Error(error);
359
367
  }
360
- }
361
- else {
362
- throw Error("This method exists only for ARBITRUM network");
363
- }
364
- });
365
- export const getGasInfoForL2 = () => __awaiter(void 0, void 0, void 0, function* () {
366
- if (llamalend.chainId === 42161) {
367
- try {
368
- const baseFee = yield getBaseFeeByLastBlock();
369
- return {
370
- maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
371
- maxPriorityFeePerGas: 0.01,
372
- };
368
+ });
369
+ };
370
+ export const getGasPriceFromL1 = function () {
371
+ return __awaiter(this, void 0, void 0, function* () {
372
+ if (L2Networks.includes(this.chainId) && this.L1WeightedGasPrice) {
373
+ return this.L1WeightedGasPrice + 1e9; // + 1 gwei
373
374
  }
374
- catch (e) {
375
- throw Error(e);
375
+ else {
376
+ throw Error("This method exists only for L2 networks");
376
377
  }
377
- }
378
- else {
379
- throw Error("This method exists only for ARBITRUM network");
380
- }
381
- });
382
- export const totalSupply = () => __awaiter(void 0, void 0, void 0, function* () {
383
- const calls = [];
384
- for (const llammaId of llamalend.getMintMarketList()) {
385
- const controllerAddress = llamalend.constants.LLAMMAS[llammaId].controller_address;
386
- const controllerContract = llamalend.contracts[controllerAddress].multicallContract;
387
- calls.push(controllerContract.minted(), controllerContract.redeemed());
388
- }
389
- for (const pegKeeper of llamalend.constants.PEG_KEEPERS) {
390
- calls.push(llamalend.contracts[pegKeeper].multicallContract.debt());
391
- }
392
- const res = yield llamalend.multicallProvider.all(calls);
393
- let mintedBN = BN(0);
394
- for (let i = 0; i < llamalend.getMintMarketList().length; i++) {
395
- const [_minted, _redeemed] = res.splice(0, 2);
396
- mintedBN = toBN(_minted).minus(toBN(_redeemed)).plus(mintedBN);
397
- }
398
- let pegKeepersBN = BN(0);
399
- for (const _pegKeeperDebt of res) {
400
- pegKeepersBN = pegKeepersBN.plus(toBN(_pegKeeperDebt));
401
- }
402
- return { total: mintedBN.plus(pegKeepersBN).toString(), minted: mintedBN.toString(), pegKeepersDebt: pegKeepersBN.toString() };
403
- });
378
+ });
379
+ };
380
+ export function getGasPriceFromL2() {
381
+ return __awaiter(this, void 0, void 0, function* () {
382
+ if (this.chainId === 42161) {
383
+ try {
384
+ return yield getBaseFeeByLastBlock.call(this);
385
+ }
386
+ catch (e) {
387
+ throw Error(e);
388
+ }
389
+ }
390
+ else {
391
+ throw Error("This method exists only for ARBITRUM network");
392
+ }
393
+ });
394
+ }
395
+ export const getGasInfoForL2 = function () {
396
+ return __awaiter(this, void 0, void 0, function* () {
397
+ if (this.chainId === 42161) {
398
+ try {
399
+ const baseFee = yield getBaseFeeByLastBlock.call(this);
400
+ return {
401
+ maxFeePerGas: Number(((baseFee * 1.1) + 0.01).toFixed(2)),
402
+ maxPriorityFeePerGas: 0.01,
403
+ };
404
+ }
405
+ catch (e) {
406
+ throw Error(e);
407
+ }
408
+ }
409
+ else {
410
+ throw Error("This method exists only for ARBITRUM network");
411
+ }
412
+ });
413
+ };
414
+ export const totalSupply = function () {
415
+ return __awaiter(this, void 0, void 0, function* () {
416
+ const calls = [];
417
+ for (const llammaId of this.getMintMarketList()) {
418
+ const controllerAddress = this.constants.LLAMMAS[llammaId].controller_address;
419
+ const controllerContract = this.contracts[controllerAddress].multicallContract;
420
+ calls.push(controllerContract.minted(), controllerContract.redeemed());
421
+ }
422
+ for (const pegKeeper of this.constants.PEG_KEEPERS) {
423
+ calls.push(this.contracts[pegKeeper].multicallContract.debt());
424
+ }
425
+ const res = yield this.multicallProvider.all(calls);
426
+ let mintedBN = BN(0);
427
+ for (let i = 0; i < this.getMintMarketList().length; i++) {
428
+ const [_minted, _redeemed] = res.splice(0, 2);
429
+ mintedBN = toBN(_minted).minus(toBN(_redeemed)).plus(mintedBN);
430
+ }
431
+ let pegKeepersBN = BN(0);
432
+ for (const _pegKeeperDebt of res) {
433
+ pegKeepersBN = pegKeepersBN.plus(toBN(_pegKeeperDebt));
434
+ }
435
+ return { total: mintedBN.plus(pegKeepersBN).toString(), minted: mintedBN.toString(), pegKeepersDebt: pegKeepersBN.toString() };
436
+ });
437
+ };
404
438
  export const getLsdApy = memoize((name) => __awaiter(void 0, void 0, void 0, function* () {
405
439
  const response = yield fetch('https://yields.llama.fi/pools');
406
440
  const { data } = yield response.json();