@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/.github/workflows/publish.yml +1 -0
- package/lib/external-api.d.ts +6 -4
- package/lib/external-api.js +116 -98
- package/lib/index.d.ts +138 -41
- package/lib/index.js +80 -75
- package/lib/lendMarkets/LendMarketTemplate.d.ts +3 -1
- package/lib/lendMarkets/LendMarketTemplate.js +333 -333
- package/lib/lendMarkets/lendMarketConstructor.d.ts +2 -1
- package/lib/lendMarkets/lendMarketConstructor.js +3 -4
- package/lib/llamalend.d.ts +2 -2
- package/lib/llamalend.js +12 -20
- package/lib/mintMarkets/MintMarketTemplate.d.ts +3 -1
- package/lib/mintMarkets/MintMarketTemplate.js +204 -204
- package/lib/mintMarkets/mintMarketConstructor.d.ts +2 -1
- package/lib/mintMarkets/mintMarketConstructor.js +2 -2
- package/lib/st-crvUSD.d.ts +29 -28
- package/lib/st-crvUSD.js +237 -173
- package/lib/utils.d.ts +20 -19
- package/lib/utils.js +290 -256
- package/package.json +14 -14
- package/src/external-api.ts +25 -11
- package/src/index.ts +88 -76
- package/src/lendMarkets/LendMarketTemplate.ts +348 -346
- package/src/lendMarkets/lendMarketConstructor.ts +4 -4
- package/src/llamalend.ts +42 -58
- package/src/mintMarkets/MintMarketTemplate.ts +206 -204
- package/src/mintMarkets/mintMarketConstructor.ts +3 -2
- package/src/st-crvUSD.ts +97 -96
- package/src/utils.ts +85 -82
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 ||
|
|
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) =>
|
|
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(
|
|
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 =
|
|
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 =
|
|
140
|
-
address =
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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:
|
|
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)
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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)
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
-
|
|
216
|
-
}
|
|
230
|
+
return txHashes;
|
|
231
|
+
});
|
|
232
|
+
};
|
|
217
233
|
// coins can be either addresses or symbols
|
|
218
|
-
export const ensureAllowanceEstimateGas =
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
-
|
|
233
|
-
}
|
|
249
|
+
return getGasFromArray(gas);
|
|
250
|
+
});
|
|
251
|
+
};
|
|
234
252
|
// coins can be either addresses or symbols
|
|
235
|
-
export const ensureAllowance =
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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 =
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
-
|
|
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
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
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 (
|
|
358
|
-
throw Error(
|
|
365
|
+
catch (error) {
|
|
366
|
+
throw new Error(error);
|
|
359
367
|
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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
|
-
|
|
375
|
-
throw Error(
|
|
375
|
+
else {
|
|
376
|
+
throw Error("This method exists only for L2 networks");
|
|
376
377
|
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
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();
|