@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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import memoize from "memoizee";
|
|
2
2
|
import BigNumber from "bignumber.js";
|
|
3
|
-
import {
|
|
3
|
+
import type { Llamalend } from "../llamalend.js";
|
|
4
4
|
import {
|
|
5
5
|
_getAddress,
|
|
6
6
|
parseUnits,
|
|
@@ -25,6 +25,7 @@ import {_getUserCollateralCrvUsd} from "../external-api.js";
|
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
export class MintMarketTemplate {
|
|
28
|
+
private llamalend: Llamalend;
|
|
28
29
|
id: string;
|
|
29
30
|
address: string;
|
|
30
31
|
controller: string;
|
|
@@ -121,8 +122,9 @@ export class MintMarketTemplate {
|
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
|
|
124
|
-
constructor(id: string) {
|
|
125
|
-
|
|
125
|
+
constructor(id: string, llamalend: Llamalend) {
|
|
126
|
+
this.llamalend = llamalend;
|
|
127
|
+
const llammaData = this.llamalend.constants.LLAMMAS[id];
|
|
126
128
|
|
|
127
129
|
this.id = id;
|
|
128
130
|
this.address = llammaData.amm_address;
|
|
@@ -135,7 +137,7 @@ export class MintMarketTemplate {
|
|
|
135
137
|
this.collateralSymbol = llammaData.collateral_symbol;
|
|
136
138
|
this.collateralDecimals = Number(llammaData.collateral_decimals);
|
|
137
139
|
this.coins = ["crvUSD", llammaData.collateral_symbol];
|
|
138
|
-
this.coinAddresses = [llamalend.crvUsdAddress, llammaData.collateral_address];
|
|
140
|
+
this.coinAddresses = [this.llamalend.crvUsdAddress, llammaData.collateral_address];
|
|
139
141
|
this.coinDecimals = [18, Number(llammaData.collateral_decimals)];
|
|
140
142
|
this.minBands = llammaData.min_bands;
|
|
141
143
|
this.maxBands = llammaData.max_bands;
|
|
@@ -225,20 +227,20 @@ export class MintMarketTemplate {
|
|
|
225
227
|
liquidation_discount: string, // %
|
|
226
228
|
loan_discount: string, // %
|
|
227
229
|
}> => {
|
|
228
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
229
|
-
const controllerContract = llamalend.contracts[this.controller].multicallContract;
|
|
230
|
-
const monetaryPolicyContract = llamalend.contracts[this.monetaryPolicy].multicallContract;
|
|
230
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
231
|
+
const controllerContract = this.llamalend.contracts[this.controller].multicallContract;
|
|
232
|
+
const monetaryPolicyContract = this.llamalend.contracts[this.monetaryPolicy].multicallContract;
|
|
231
233
|
|
|
232
234
|
const calls = [
|
|
233
235
|
llammaContract.fee(),
|
|
234
236
|
llammaContract.admin_fee(),
|
|
235
237
|
llammaContract.rate(),
|
|
236
|
-
"rate(address)" in llamalend.contracts[this.monetaryPolicy].contract ? monetaryPolicyContract.rate(this.controller) : monetaryPolicyContract.rate(),
|
|
238
|
+
"rate(address)" in this.llamalend.contracts[this.monetaryPolicy].contract ? monetaryPolicyContract.rate(this.controller) : monetaryPolicyContract.rate(),
|
|
237
239
|
controllerContract.liquidation_discount(),
|
|
238
240
|
controllerContract.loan_discount(),
|
|
239
241
|
]
|
|
240
242
|
|
|
241
|
-
const [_fee, _admin_fee, _rate, _mp_rate, _liquidation_discount, _loan_discount]: bigint[] = await llamalend.multicallProvider.all(calls) as bigint[];
|
|
243
|
+
const [_fee, _admin_fee, _rate, _mp_rate, _liquidation_discount, _loan_discount]: bigint[] = await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
242
244
|
const [fee, admin_fee, liquidation_discount, loan_discount] = [_fee, _admin_fee, _liquidation_discount, _loan_discount]
|
|
243
245
|
.map((x) => formatUnits(x * BigInt(100)));
|
|
244
246
|
|
|
@@ -254,16 +256,16 @@ export class MintMarketTemplate {
|
|
|
254
256
|
});
|
|
255
257
|
|
|
256
258
|
private async statsBalances(): Promise<[string, string]> {
|
|
257
|
-
const crvusdContract = llamalend.contracts[llamalend.crvUsdAddress].multicallContract;
|
|
258
|
-
const collateralContract = llamalend.contracts[isEth(this.collateral) ? llamalend.constants.WETH : this.collateral].multicallContract;
|
|
259
|
-
const contract = llamalend.contracts[this.address].multicallContract;
|
|
259
|
+
const crvusdContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
|
260
|
+
const collateralContract = this.llamalend.contracts[isEth(this.collateral) ? this.llamalend.constants.WETH : this.collateral].multicallContract;
|
|
261
|
+
const contract = this.llamalend.contracts[this.address].multicallContract;
|
|
260
262
|
const calls = [
|
|
261
263
|
crvusdContract.balanceOf(this.address),
|
|
262
264
|
collateralContract.balanceOf(this.address),
|
|
263
265
|
contract.admin_fees_x(),
|
|
264
266
|
contract.admin_fees_y(),
|
|
265
267
|
]
|
|
266
|
-
const [_crvusdBalance, _collateralBalance, _crvusdAdminFees, _collateralAdminFees]: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
268
|
+
const [_crvusdBalance, _collateralBalance, _crvusdAdminFees, _collateralAdminFees]: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
267
269
|
|
|
268
270
|
return [
|
|
269
271
|
formatUnits(_crvusdBalance - _crvusdAdminFees),
|
|
@@ -272,14 +274,14 @@ export class MintMarketTemplate {
|
|
|
272
274
|
}
|
|
273
275
|
|
|
274
276
|
private statsMaxMinBands = memoize(async (): Promise<[number, number]> => {
|
|
275
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
277
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
276
278
|
|
|
277
279
|
const calls1 = [
|
|
278
280
|
llammaContract.max_band(),
|
|
279
281
|
llammaContract.min_band(),
|
|
280
282
|
]
|
|
281
283
|
|
|
282
|
-
return (await llamalend.multicallProvider.all(calls1) as BigNumber[]).map((_b) => Number(_b)) as [number, number];
|
|
284
|
+
return (await this.llamalend.multicallProvider.all(calls1) as BigNumber[]).map((_b) => Number(_b)) as [number, number];
|
|
283
285
|
},
|
|
284
286
|
{
|
|
285
287
|
promise: true,
|
|
@@ -287,7 +289,7 @@ export class MintMarketTemplate {
|
|
|
287
289
|
});
|
|
288
290
|
|
|
289
291
|
private statsActiveBand = memoize(async (): Promise<number> => {
|
|
290
|
-
return Number((await llamalend.contracts[this.address].contract.active_band_with_skip()))
|
|
292
|
+
return Number((await this.llamalend.contracts[this.address].contract.active_band_with_skip()))
|
|
291
293
|
},
|
|
292
294
|
{
|
|
293
295
|
promise: true,
|
|
@@ -302,11 +304,11 @@ export class MintMarketTemplate {
|
|
|
302
304
|
}
|
|
303
305
|
|
|
304
306
|
private async statsBandBalances(n: number): Promise<{ stablecoin: string, collateral: string }> {
|
|
305
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
307
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
306
308
|
const calls = [];
|
|
307
309
|
calls.push(llammaContract.bands_x(n), llammaContract.bands_y(n));
|
|
308
310
|
|
|
309
|
-
const _balances: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
311
|
+
const _balances: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
310
312
|
|
|
311
313
|
return {
|
|
312
314
|
stablecoin: formatUnits(_balances[0]),
|
|
@@ -317,13 +319,13 @@ export class MintMarketTemplate {
|
|
|
317
319
|
private async statsBandsBalances(): Promise<{ [index: number]: { stablecoin: string, collateral: string } }> {
|
|
318
320
|
const [max_band, min_band]: number[] = await this.statsMaxMinBands();
|
|
319
321
|
|
|
320
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
322
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
321
323
|
const calls = [];
|
|
322
324
|
for (let i = min_band; i <= max_band; i++) {
|
|
323
325
|
calls.push(llammaContract.bands_x(i), llammaContract.bands_y(i));
|
|
324
326
|
}
|
|
325
327
|
|
|
326
|
-
const _bands: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
328
|
+
const _bands: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
327
329
|
|
|
328
330
|
const bands: { [index: number]: { stablecoin: string, collateral: string } } = {};
|
|
329
331
|
for (let i = min_band; i <= max_band; i++) {
|
|
@@ -341,9 +343,9 @@ export class MintMarketTemplate {
|
|
|
341
343
|
}
|
|
342
344
|
|
|
343
345
|
private statsTotalSupply = memoize(async (): Promise<string> => {
|
|
344
|
-
const controllerContract = llamalend.contracts[this.controller].multicallContract;
|
|
346
|
+
const controllerContract = this.llamalend.contracts[this.controller].multicallContract;
|
|
345
347
|
const calls = [controllerContract.minted(), controllerContract.redeemed()]
|
|
346
|
-
const [_minted, _redeemed]: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
348
|
+
const [_minted, _redeemed]: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
347
349
|
|
|
348
350
|
return toBN(_minted).minus(toBN(_redeemed)).toString();
|
|
349
351
|
},
|
|
@@ -353,7 +355,7 @@ export class MintMarketTemplate {
|
|
|
353
355
|
});
|
|
354
356
|
|
|
355
357
|
private statsTotalDebt = memoize(async (): Promise<string> => {
|
|
356
|
-
const debt = await llamalend.contracts[this.controller].contract.total_debt(llamalend.constantOptions);
|
|
358
|
+
const debt = await this.llamalend.contracts[this.controller].contract.total_debt(this.llamalend.constantOptions);
|
|
357
359
|
|
|
358
360
|
return formatUnits(debt);
|
|
359
361
|
},
|
|
@@ -363,10 +365,10 @@ export class MintMarketTemplate {
|
|
|
363
365
|
});
|
|
364
366
|
|
|
365
367
|
private statsTotalStablecoin = memoize(async (): Promise<string> => {
|
|
366
|
-
const stablecoinContract = llamalend.contracts[llamalend.crvUsdAddress].multicallContract;
|
|
367
|
-
const ammContract = llamalend.contracts[this.address].multicallContract;
|
|
368
|
+
const stablecoinContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
|
369
|
+
const ammContract = this.llamalend.contracts[this.address].multicallContract;
|
|
368
370
|
|
|
369
|
-
const [_balance, _fee]: bigint[] = await llamalend.multicallProvider.all([
|
|
371
|
+
const [_balance, _fee]: bigint[] = await this.llamalend.multicallProvider.all([
|
|
370
372
|
stablecoinContract.balanceOf(this.address),
|
|
371
373
|
ammContract.admin_fees_x(),
|
|
372
374
|
]);
|
|
@@ -379,10 +381,10 @@ export class MintMarketTemplate {
|
|
|
379
381
|
});
|
|
380
382
|
|
|
381
383
|
private statsTotalCollateral = memoize(async (): Promise<string> => {
|
|
382
|
-
const collateralContract = llamalend.contracts[isEth(this.collateral) ? llamalend.constants.WETH : this.collateral].multicallContract;
|
|
383
|
-
const ammContract = llamalend.contracts[this.address].multicallContract;
|
|
384
|
+
const collateralContract = this.llamalend.contracts[isEth(this.collateral) ? this.llamalend.constants.WETH : this.collateral].multicallContract;
|
|
385
|
+
const ammContract = this.llamalend.contracts[this.address].multicallContract;
|
|
384
386
|
|
|
385
|
-
const [_balance, _fee]: bigint[] = await llamalend.multicallProvider.all([
|
|
387
|
+
const [_balance, _fee]: bigint[] = await this.llamalend.multicallProvider.all([
|
|
386
388
|
collateralContract.balanceOf(this.address),
|
|
387
389
|
ammContract.admin_fees_y(),
|
|
388
390
|
]);
|
|
@@ -395,15 +397,15 @@ export class MintMarketTemplate {
|
|
|
395
397
|
});
|
|
396
398
|
|
|
397
399
|
private statsCapAndAvailable = memoize(async (): Promise<{ "cap": string, "available": string }> => {
|
|
398
|
-
const factoryContract = llamalend.contracts[llamalend.constants.FACTORY].multicallContract;
|
|
399
|
-
const crvusdContract = llamalend.contracts[llamalend.crvUsdAddress].multicallContract;
|
|
400
|
+
const factoryContract = this.llamalend.contracts[this.llamalend.constants.FACTORY].multicallContract;
|
|
401
|
+
const crvusdContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
|
400
402
|
|
|
401
|
-
const [_cap, _available]: bigint[] = await llamalend.multicallProvider.all([
|
|
403
|
+
const [_cap, _available]: bigint[] = await this.llamalend.multicallProvider.all([
|
|
402
404
|
factoryContract.debt_ceiling(this.controller),
|
|
403
405
|
crvusdContract.balanceOf(this.controller),
|
|
404
406
|
]);
|
|
405
407
|
|
|
406
|
-
return { "cap": llamalend.formatUnits(_cap), "available": llamalend.formatUnits(_available) }
|
|
408
|
+
return { "cap": this.llamalend.formatUnits(_cap), "available": this.llamalend.formatUnits(_available) }
|
|
407
409
|
},
|
|
408
410
|
{
|
|
409
411
|
promise: true,
|
|
@@ -413,28 +415,28 @@ export class MintMarketTemplate {
|
|
|
413
415
|
// ---------------------------------------
|
|
414
416
|
|
|
415
417
|
public async loanExists(address = ""): Promise<boolean> {
|
|
416
|
-
address = _getAddress(address);
|
|
417
|
-
return await llamalend.contracts[this.controller].contract.loan_exists(address, llamalend.constantOptions);
|
|
418
|
+
address = _getAddress.call(this.llamalend, address);
|
|
419
|
+
return await this.llamalend.contracts[this.controller].contract.loan_exists(address, this.llamalend.constantOptions);
|
|
418
420
|
}
|
|
419
421
|
|
|
420
422
|
public async userDebt(address = ""): Promise<string> {
|
|
421
|
-
address = _getAddress(address);
|
|
422
|
-
const debt = await llamalend.contracts[this.controller].contract.debt(address, llamalend.constantOptions);
|
|
423
|
+
address = _getAddress.call(this.llamalend, address);
|
|
424
|
+
const debt = await this.llamalend.contracts[this.controller].contract.debt(address, this.llamalend.constantOptions);
|
|
423
425
|
|
|
424
426
|
return formatUnits(debt);
|
|
425
427
|
}
|
|
426
428
|
|
|
427
429
|
public async userHealth(full = true, address = ""): Promise<string> {
|
|
428
|
-
address = _getAddress(address);
|
|
429
|
-
let _health = await llamalend.contracts[this.controller].contract.health(address, full, llamalend.constantOptions) as bigint;
|
|
430
|
+
address = _getAddress.call(this.llamalend, address);
|
|
431
|
+
let _health = await this.llamalend.contracts[this.controller].contract.health(address, full, this.llamalend.constantOptions) as bigint;
|
|
430
432
|
_health = _health * BigInt(100);
|
|
431
433
|
|
|
432
434
|
return formatUnits(_health);
|
|
433
435
|
}
|
|
434
436
|
|
|
435
437
|
public async userBands(address = ""): Promise<number[]> {
|
|
436
|
-
address = _getAddress(address);
|
|
437
|
-
const _bands = await llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions) as BigNumber[];
|
|
438
|
+
address = _getAddress.call(this.llamalend, address);
|
|
439
|
+
const _bands = await this.llamalend.contracts[this.address].contract.read_user_tick_numbers(address, this.llamalend.constantOptions) as BigNumber[];
|
|
438
440
|
|
|
439
441
|
return _bands.map((_t) => Number(_t)).reverse();
|
|
440
442
|
}
|
|
@@ -446,16 +448,16 @@ export class MintMarketTemplate {
|
|
|
446
448
|
}
|
|
447
449
|
|
|
448
450
|
public async userPrices(address = ""): Promise<string[]> {
|
|
449
|
-
address = _getAddress(address);
|
|
450
|
-
const _prices = await llamalend.contracts[this.controller].contract.user_prices(address, llamalend.constantOptions) as bigint[];
|
|
451
|
+
address = _getAddress.call(this.llamalend, address);
|
|
452
|
+
const _prices = await this.llamalend.contracts[this.controller].contract.user_prices(address, this.llamalend.constantOptions) as bigint[];
|
|
451
453
|
|
|
452
454
|
return _prices.map((_p) =>formatUnits(_p)).reverse();
|
|
453
455
|
}
|
|
454
456
|
|
|
455
457
|
public async _userState(address = ""): Promise<{ _collateral: bigint, _stablecoin: bigint, _debt: bigint }> {
|
|
456
|
-
address = _getAddress(address);
|
|
457
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
458
|
-
const [_collateral, _stablecoin, _debt] = await contract.user_state(address, llamalend.constantOptions) as bigint[];
|
|
458
|
+
address = _getAddress.call(this.llamalend, address);
|
|
459
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
460
|
+
const [_collateral, _stablecoin, _debt] = await contract.user_state(address, this.llamalend.constantOptions) as bigint[];
|
|
459
461
|
|
|
460
462
|
return { _collateral, _stablecoin, _debt }
|
|
461
463
|
}
|
|
@@ -471,12 +473,12 @@ export class MintMarketTemplate {
|
|
|
471
473
|
}
|
|
472
474
|
|
|
473
475
|
public async userLoss(userAddress = ""): Promise<{ deposited_collateral: string, current_collateral_estimation: string, loss: string, loss_pct: string }> {
|
|
474
|
-
userAddress = _getAddress(userAddress);
|
|
476
|
+
userAddress = _getAddress.call(this.llamalend, userAddress);
|
|
475
477
|
const [deposited_collateral, _current_collateral_estimation] = await Promise.all([
|
|
476
|
-
_getUserCollateralCrvUsd(llamalend.constants.NETWORK_NAME, this.controller, userAddress),
|
|
477
|
-
llamalend.contracts[this.address].contract.get_y_up(userAddress),
|
|
478
|
+
_getUserCollateralCrvUsd(this.llamalend.constants.NETWORK_NAME, this.controller, userAddress),
|
|
479
|
+
this.llamalend.contracts[this.address].contract.get_y_up(userAddress),
|
|
478
480
|
]);
|
|
479
|
-
const current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateralDecimals);
|
|
481
|
+
const current_collateral_estimation = this.llamalend.formatUnits(_current_collateral_estimation, this.collateralDecimals);
|
|
480
482
|
|
|
481
483
|
if (BN(deposited_collateral).lte(0)) {
|
|
482
484
|
return {
|
|
@@ -501,9 +503,9 @@ export class MintMarketTemplate {
|
|
|
501
503
|
const [n2, n1] = await this.userBands(address);
|
|
502
504
|
if (n1 == 0 && n2 == 0) return {};
|
|
503
505
|
|
|
504
|
-
address = _getAddress(address);
|
|
505
|
-
const contract = llamalend.contracts[this.address].contract;
|
|
506
|
-
const [_stablecoins, _collaterals] = await contract.get_xy(address, llamalend.constantOptions) as [bigint[], bigint[]];
|
|
506
|
+
address = _getAddress.call(this.llamalend, address);
|
|
507
|
+
const contract = this.llamalend.contracts[this.address].contract;
|
|
508
|
+
const [_stablecoins, _collaterals] = await contract.get_xy(address, this.llamalend.constantOptions) as [bigint[], bigint[]];
|
|
507
509
|
|
|
508
510
|
const res: IDict<{ stablecoin: string, collateral: string }> = {};
|
|
509
511
|
for (let i = n1; i <= n2; i++) {
|
|
@@ -517,7 +519,7 @@ export class MintMarketTemplate {
|
|
|
517
519
|
}
|
|
518
520
|
|
|
519
521
|
public async oraclePrice(): Promise<string> {
|
|
520
|
-
const _price = await llamalend.contracts[this.address].contract.price_oracle(llamalend.constantOptions) as bigint;
|
|
522
|
+
const _price = await this.llamalend.contracts[this.address].contract.price_oracle(this.llamalend.constantOptions) as bigint;
|
|
521
523
|
return formatUnits(_price);
|
|
522
524
|
}
|
|
523
525
|
|
|
@@ -542,12 +544,12 @@ export class MintMarketTemplate {
|
|
|
542
544
|
}
|
|
543
545
|
|
|
544
546
|
public async price(): Promise<string> {
|
|
545
|
-
const _price = await llamalend.contracts[this.address].contract.get_p(llamalend.constantOptions) as bigint;
|
|
547
|
+
const _price = await this.llamalend.contracts[this.address].contract.get_p(this.llamalend.constantOptions) as bigint;
|
|
546
548
|
return formatUnits(_price);
|
|
547
549
|
}
|
|
548
550
|
|
|
549
551
|
public basePrice = memoize(async(): Promise<string> => {
|
|
550
|
-
const _price = await llamalend.contracts[this.address].contract.get_base_price(llamalend.constantOptions) as bigint;
|
|
552
|
+
const _price = await this.llamalend.contracts[this.address].contract.get_base_price(this.llamalend.constantOptions) as bigint;
|
|
551
553
|
return formatUnits(_price);
|
|
552
554
|
},
|
|
553
555
|
{
|
|
@@ -583,7 +585,7 @@ export class MintMarketTemplate {
|
|
|
583
585
|
// ---------------- WALLET BALANCES ----------------
|
|
584
586
|
|
|
585
587
|
private async walletBalances(address = ""): Promise<{ collateral: string, stablecoin: string }> {
|
|
586
|
-
const [collateral, stablecoin] = await getBalances([this.collateral, llamalend.crvUsdAddress], address);
|
|
588
|
+
const [collateral, stablecoin] = await getBalances.call(this.llamalend, [this.collateral, this.llamalend.crvUsdAddress], address);
|
|
587
589
|
return { stablecoin, collateral }
|
|
588
590
|
}
|
|
589
591
|
|
|
@@ -598,7 +600,7 @@ export class MintMarketTemplate {
|
|
|
598
600
|
this._checkRange(range);
|
|
599
601
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
600
602
|
|
|
601
|
-
return formatUnits(await llamalend.contracts[this.controller].contract.max_borrowable(_collateral, range, llamalend.constantOptions));
|
|
603
|
+
return formatUnits(await this.llamalend.contracts[this.controller].contract.max_borrowable(_collateral, range, this.llamalend.constantOptions));
|
|
602
604
|
}
|
|
603
605
|
|
|
604
606
|
public createLoanMaxRecvAllRanges = memoize(async (collateral: number | string): Promise<{ [index: number]: string }> => {
|
|
@@ -606,9 +608,9 @@ export class MintMarketTemplate {
|
|
|
606
608
|
|
|
607
609
|
const calls = [];
|
|
608
610
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
609
|
-
calls.push(llamalend.contracts[this.controller].multicallContract.max_borrowable(_collateral, N));
|
|
611
|
+
calls.push(this.llamalend.contracts[this.controller].multicallContract.max_borrowable(_collateral, N));
|
|
610
612
|
}
|
|
611
|
-
const _amounts = await llamalend.multicallProvider.all(calls) as bigint[];
|
|
613
|
+
const _amounts = await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
612
614
|
|
|
613
615
|
const res: { [index: number]: string } = {};
|
|
614
616
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
@@ -633,20 +635,20 @@ export class MintMarketTemplate {
|
|
|
633
635
|
|
|
634
636
|
private async _calcN1(_collateral: bigint, _debt: bigint, range: number): Promise<bigint> {
|
|
635
637
|
this._checkRange(range);
|
|
636
|
-
return await llamalend.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, range, llamalend.constantOptions);
|
|
638
|
+
return await this.llamalend.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, range, this.llamalend.constantOptions);
|
|
637
639
|
}
|
|
638
640
|
|
|
639
641
|
private async _calcN1AllRanges(_collateral: bigint, _debt: bigint, maxN: number): Promise<bigint[]> {
|
|
640
642
|
const calls = [];
|
|
641
643
|
for (let N = this.minBands; N <= maxN; N++) {
|
|
642
|
-
calls.push(llamalend.contracts[this.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
644
|
+
calls.push(this.llamalend.contracts[this.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
643
645
|
}
|
|
644
|
-
return await llamalend.multicallProvider.all(calls) as bigint[];
|
|
646
|
+
return await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
645
647
|
}
|
|
646
648
|
|
|
647
649
|
private async _getPrices(_n2: bigint, _n1: bigint): Promise<string[]> {
|
|
648
|
-
const contract = llamalend.contracts[this.address].multicallContract;
|
|
649
|
-
return (await llamalend.multicallProvider.all([
|
|
650
|
+
const contract = this.llamalend.contracts[this.address].multicallContract;
|
|
651
|
+
return (await this.llamalend.multicallProvider.all([
|
|
650
652
|
contract.p_oracle_down(_n2),
|
|
651
653
|
contract.p_oracle_up(_n1),
|
|
652
654
|
]) as bigint[]).map((_p) => formatUnits(_p));
|
|
@@ -722,27 +724,27 @@ export class MintMarketTemplate {
|
|
|
722
724
|
}
|
|
723
725
|
|
|
724
726
|
public async createLoanHealth(collateral: number | string, debt: number | string, range: number, full = true, address = ""): Promise<string> {
|
|
725
|
-
address = _getAddress(address);
|
|
727
|
+
address = _getAddress.call(this.llamalend, address);
|
|
726
728
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
727
729
|
const _debt = parseUnits(debt);
|
|
728
730
|
|
|
729
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
730
|
-
let _health = await contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions) as bigint;
|
|
731
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
732
|
+
let _health = await contract.health_calculator(address, _collateral, _debt, full, range, this.llamalend.constantOptions) as bigint;
|
|
731
733
|
_health = _health * BigInt(100);
|
|
732
734
|
|
|
733
735
|
return formatUnits(_health);
|
|
734
736
|
}
|
|
735
737
|
|
|
736
738
|
public async createLoanIsApproved(collateral: number | string): Promise<boolean> {
|
|
737
|
-
return await hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
739
|
+
return await hasAllowance.call(this.llamalend, [this.collateral], [collateral], this.llamalend.signerAddress, this.controller);
|
|
738
740
|
}
|
|
739
741
|
|
|
740
742
|
private async createLoanApproveEstimateGas (collateral: number | string): Promise<TGas> {
|
|
741
|
-
return await ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
743
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
742
744
|
}
|
|
743
745
|
|
|
744
746
|
public async createLoanApprove(collateral: number | string): Promise<string[]> {
|
|
745
|
-
return await ensureAllowance([this.collateral], [collateral], this.controller);
|
|
747
|
+
return await ensureAllowance.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
746
748
|
}
|
|
747
749
|
|
|
748
750
|
private async _createLoan(collateral: number | string, debt: number | string, range: number, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -751,14 +753,14 @@ export class MintMarketTemplate {
|
|
|
751
753
|
|
|
752
754
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
753
755
|
const _debt = parseUnits(debt);
|
|
754
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
755
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
756
|
-
const gas = await contract.create_loan.estimateGas(_collateral, _debt, range, { ...llamalend.constantOptions, value });
|
|
756
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
757
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
758
|
+
const gas = await contract.create_loan.estimateGas(_collateral, _debt, range, { ...this.llamalend.constantOptions, value });
|
|
757
759
|
if (estimateGas) return smartNumber(gas);
|
|
758
760
|
|
|
759
|
-
await llamalend.updateFeeData();
|
|
761
|
+
await this.llamalend.updateFeeData();
|
|
760
762
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
761
|
-
return (await contract.create_loan(_collateral, _debt, range, { ...llamalend.options, gasLimit, value })).hash
|
|
763
|
+
return (await contract.create_loan(_collateral, _debt, range, { ...this.llamalend.options, gasLimit, value })).hash
|
|
762
764
|
}
|
|
763
765
|
|
|
764
766
|
public async createLoanEstimateGas(collateral: number | string, debt: number | string, range: number): Promise<number> {
|
|
@@ -778,15 +780,15 @@ export class MintMarketTemplate {
|
|
|
778
780
|
const N = await this.userRange();
|
|
779
781
|
const _collateral = _currentCollateral + parseUnits(collateralAmount, this.collateralDecimals);
|
|
780
782
|
|
|
781
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
782
|
-
const _debt: bigint = await contract.max_borrowable(_collateral, N, llamalend.constantOptions);
|
|
783
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
784
|
+
const _debt: bigint = await contract.max_borrowable(_collateral, N, this.llamalend.constantOptions);
|
|
783
785
|
|
|
784
786
|
return formatUnits(_debt - _currentDebt);
|
|
785
787
|
}
|
|
786
788
|
|
|
787
789
|
private async _borrowMoreBands(collateral: number | string, debt: number | string): Promise<[bigint, bigint]> {
|
|
788
790
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = await this._userState();
|
|
789
|
-
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
791
|
+
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
790
792
|
|
|
791
793
|
const N = await this.userRange();
|
|
792
794
|
const _collateral = _currentCollateral + parseUnits(collateral, this.collateralDecimals);
|
|
@@ -811,44 +813,44 @@ export class MintMarketTemplate {
|
|
|
811
813
|
}
|
|
812
814
|
|
|
813
815
|
public async borrowMoreHealth(collateral: number | string, debt: number | string, full = true, address = ""): Promise<string> {
|
|
814
|
-
address = _getAddress(address);
|
|
816
|
+
address = _getAddress.call(this.llamalend, address);
|
|
815
817
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
816
818
|
const _debt = parseUnits(debt);
|
|
817
819
|
|
|
818
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
819
|
-
let _health = await contract.health_calculator(address, _collateral, _debt, full, 0, llamalend.constantOptions) as bigint;
|
|
820
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
821
|
+
let _health = await contract.health_calculator(address, _collateral, _debt, full, 0, this.llamalend.constantOptions) as bigint;
|
|
820
822
|
_health = _health * BigInt(100);
|
|
821
823
|
|
|
822
824
|
return formatUnits(_health);
|
|
823
825
|
}
|
|
824
826
|
|
|
825
827
|
public async borrowMoreIsApproved(collateral: number | string): Promise<boolean> {
|
|
826
|
-
return await hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
828
|
+
return await hasAllowance.call(this.llamalend, [this.collateral], [collateral], this.llamalend.signerAddress, this.controller);
|
|
827
829
|
}
|
|
828
830
|
|
|
829
831
|
private async borrowMoreApproveEstimateGas (collateral: number | string): Promise<TGas> {
|
|
830
|
-
return await ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
832
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
831
833
|
}
|
|
832
834
|
|
|
833
835
|
public async borrowMoreApprove(collateral: number | string): Promise<string[]> {
|
|
834
|
-
return await ensureAllowance([this.collateral], [collateral], this.controller);
|
|
836
|
+
return await ensureAllowance.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
835
837
|
}
|
|
836
838
|
|
|
837
839
|
private async _borrowMore(collateral: number | string, debt: number | string, estimateGas: boolean): Promise<string | TGas> {
|
|
838
840
|
const { stablecoin, debt: currentDebt } = await this.userState();
|
|
839
|
-
if (Number(currentDebt) === 0) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
840
|
-
if (Number(stablecoin) > 0) throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
841
|
+
if (Number(currentDebt) === 0) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
842
|
+
if (Number(stablecoin) > 0) throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
|
|
841
843
|
|
|
842
844
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
843
845
|
const _debt = parseUnits(debt);
|
|
844
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
845
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
846
|
-
const gas = await contract.borrow_more.estimateGas(_collateral, _debt, { ...llamalend.constantOptions, value });
|
|
846
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
847
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
848
|
+
const gas = await contract.borrow_more.estimateGas(_collateral, _debt, { ...this.llamalend.constantOptions, value });
|
|
847
849
|
if (estimateGas) return smartNumber(gas);
|
|
848
850
|
|
|
849
|
-
await llamalend.updateFeeData();
|
|
851
|
+
await this.llamalend.updateFeeData();
|
|
850
852
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
851
|
-
return (await contract.borrow_more(_collateral, _debt, { ...llamalend.options, gasLimit, value })).hash
|
|
853
|
+
return (await contract.borrow_more(_collateral, _debt, { ...this.llamalend.options, gasLimit, value })).hash
|
|
852
854
|
}
|
|
853
855
|
|
|
854
856
|
public async borrowMoreEstimateGas(collateral: number | string, debt: number | string): Promise<number> {
|
|
@@ -864,7 +866,7 @@ export class MintMarketTemplate {
|
|
|
864
866
|
// ---------------- ADD COLLATERAL ----------------
|
|
865
867
|
|
|
866
868
|
private async _addCollateralBands(collateral: number | string, address = ""): Promise<[bigint, bigint]> {
|
|
867
|
-
address = _getAddress(address);
|
|
869
|
+
address = _getAddress.call(this.llamalend, address);
|
|
868
870
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = await this._userState(address);
|
|
869
871
|
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${address} does not exist`);
|
|
870
872
|
|
|
@@ -889,26 +891,26 @@ export class MintMarketTemplate {
|
|
|
889
891
|
}
|
|
890
892
|
|
|
891
893
|
public async addCollateralHealth(collateral: number | string, full = true, address = ""): Promise<string> {
|
|
892
|
-
address = _getAddress(address);
|
|
894
|
+
address = _getAddress.call(this.llamalend, address);
|
|
893
895
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
894
896
|
|
|
895
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
896
|
-
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions) as bigint;
|
|
897
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
898
|
+
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions) as bigint;
|
|
897
899
|
_health = _health * BigInt(100);
|
|
898
900
|
|
|
899
901
|
return formatUnits(_health);
|
|
900
902
|
}
|
|
901
903
|
|
|
902
904
|
public async addCollateralIsApproved(collateral: number | string): Promise<boolean> {
|
|
903
|
-
return await hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
905
|
+
return await hasAllowance.call(this.llamalend, [this.collateral], [collateral], this.llamalend.signerAddress, this.controller);
|
|
904
906
|
}
|
|
905
907
|
|
|
906
908
|
private async addCollateralApproveEstimateGas (collateral: number | string): Promise<TGas> {
|
|
907
|
-
return await ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
909
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
908
910
|
}
|
|
909
911
|
|
|
910
912
|
public async addCollateralApprove(collateral: number | string): Promise<string[]> {
|
|
911
|
-
return await ensureAllowance([this.collateral], [collateral], this.controller);
|
|
913
|
+
return await ensureAllowance.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
912
914
|
}
|
|
913
915
|
|
|
914
916
|
private async _addCollateral(collateral: number | string, address: string, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -917,24 +919,24 @@ export class MintMarketTemplate {
|
|
|
917
919
|
if (Number(stablecoin) > 0) throw Error(`User ${address} is already in liquidation mode`);
|
|
918
920
|
|
|
919
921
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
920
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
921
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
922
|
-
const gas = await contract.add_collateral.estimateGas(_collateral, address, { ...llamalend.constantOptions, value });
|
|
922
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
923
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
924
|
+
const gas = await contract.add_collateral.estimateGas(_collateral, address, { ...this.llamalend.constantOptions, value });
|
|
923
925
|
if (estimateGas) return smartNumber(gas);
|
|
924
926
|
|
|
925
|
-
await llamalend.updateFeeData();
|
|
927
|
+
await this.llamalend.updateFeeData();
|
|
926
928
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
927
|
-
return (await contract.add_collateral(_collateral, address, { ...llamalend.options, gasLimit, value })).hash
|
|
929
|
+
return (await contract.add_collateral(_collateral, address, { ...this.llamalend.options, gasLimit, value })).hash
|
|
928
930
|
}
|
|
929
931
|
|
|
930
932
|
public async addCollateralEstimateGas(collateral: number | string, address = ""): Promise<number> {
|
|
931
|
-
address = _getAddress(address);
|
|
933
|
+
address = _getAddress.call(this.llamalend, address);
|
|
932
934
|
if (!(await this.addCollateralIsApproved(collateral))) throw Error("Approval is needed for gas estimation");
|
|
933
935
|
return await this._addCollateral(collateral, address, true) as number;
|
|
934
936
|
}
|
|
935
937
|
|
|
936
938
|
public async addCollateral(collateral: number | string, address = ""): Promise<string> {
|
|
937
|
-
address = _getAddress(address);
|
|
939
|
+
address = _getAddress.call(this.llamalend, address);
|
|
938
940
|
await this.addCollateralApprove(collateral);
|
|
939
941
|
return await this._addCollateral(collateral, address, false) as string;
|
|
940
942
|
}
|
|
@@ -944,14 +946,14 @@ export class MintMarketTemplate {
|
|
|
944
946
|
public async maxRemovable(): Promise<string> {
|
|
945
947
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = await this._userState();
|
|
946
948
|
const N = await this.userRange();
|
|
947
|
-
const _requiredCollateral = await llamalend.contracts[this.controller].contract.min_collateral(_currentDebt, N, llamalend.constantOptions)
|
|
949
|
+
const _requiredCollateral = await this.llamalend.contracts[this.controller].contract.min_collateral(_currentDebt, N, this.llamalend.constantOptions)
|
|
948
950
|
|
|
949
951
|
return formatUnits(_currentCollateral - _requiredCollateral, this.collateralDecimals);
|
|
950
952
|
}
|
|
951
953
|
|
|
952
954
|
private async _removeCollateralBands(collateral: number | string): Promise<[bigint, bigint]> {
|
|
953
955
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = await this._userState();
|
|
954
|
-
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
956
|
+
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
955
957
|
|
|
956
958
|
const N = await this.userRange();
|
|
957
959
|
const _collateral = _currentCollateral - parseUnits(collateral, this.collateralDecimals);
|
|
@@ -974,11 +976,11 @@ export class MintMarketTemplate {
|
|
|
974
976
|
}
|
|
975
977
|
|
|
976
978
|
public async removeCollateralHealth(collateral: number | string, full = true, address = ""): Promise<string> {
|
|
977
|
-
address = _getAddress(address);
|
|
979
|
+
address = _getAddress.call(this.llamalend, address);
|
|
978
980
|
const _collateral = parseUnits(collateral, this.collateralDecimals) * BigInt(-1);
|
|
979
981
|
|
|
980
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
981
|
-
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions) as bigint;
|
|
982
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
983
|
+
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions) as bigint;
|
|
982
984
|
_health = _health * BigInt(100);
|
|
983
985
|
|
|
984
986
|
return formatUnits(_health);
|
|
@@ -986,17 +988,17 @@ export class MintMarketTemplate {
|
|
|
986
988
|
|
|
987
989
|
private async _removeCollateral(collateral: number | string, estimateGas: boolean): Promise<string | TGas> {
|
|
988
990
|
const { stablecoin, debt: currentDebt } = await this.userState();
|
|
989
|
-
if (Number(currentDebt) === 0) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
990
|
-
if (Number(stablecoin) > 0) throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
991
|
+
if (Number(currentDebt) === 0) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
992
|
+
if (Number(stablecoin) > 0) throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
|
|
991
993
|
|
|
992
994
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
993
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
994
|
-
const gas = this.isDeleverageSupported ? await contract.remove_collateral.estimateGas(_collateral, llamalend.constantOptions) : await contract.remove_collateral.estimateGas(_collateral, isEth(this.collateral), llamalend.constantOptions);
|
|
995
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
996
|
+
const gas = this.isDeleverageSupported ? await contract.remove_collateral.estimateGas(_collateral, this.llamalend.constantOptions) : await contract.remove_collateral.estimateGas(_collateral, isEth(this.collateral), this.llamalend.constantOptions);
|
|
995
997
|
if (estimateGas) return smartNumber(gas);
|
|
996
998
|
|
|
997
|
-
await llamalend.updateFeeData();
|
|
999
|
+
await this.llamalend.updateFeeData();
|
|
998
1000
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
999
|
-
return (this.isDeleverageSupported ? await contract.remove_collateral(_collateral, { ...llamalend.options, gasLimit }) : await contract.remove_collateral(_collateral, isEth(this.collateral), { ...llamalend.options, gasLimit })).hash
|
|
1001
|
+
return (this.isDeleverageSupported ? await contract.remove_collateral(_collateral, { ...this.llamalend.options, gasLimit }) : await contract.remove_collateral(_collateral, isEth(this.collateral), { ...this.llamalend.options, gasLimit })).hash
|
|
1000
1002
|
}
|
|
1001
1003
|
|
|
1002
1004
|
public async removeCollateralEstimateGas(collateral: number | string): Promise<number> {
|
|
@@ -1015,7 +1017,7 @@ export class MintMarketTemplate {
|
|
|
1015
1017
|
|
|
1016
1018
|
const N = await this.userRange(address);
|
|
1017
1019
|
const _debt = _currentDebt - parseUnits(debt);
|
|
1018
|
-
const _n1 = _currentStablecoin === BigInt(0) ? await this._calcN1(_currentCollateral, _debt, N) : (await llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions) as bigint[])[0];
|
|
1020
|
+
const _n1 = _currentStablecoin === BigInt(0) ? await this._calcN1(_currentCollateral, _debt, N) : (await this.llamalend.contracts[this.address].contract.read_user_tick_numbers(address, this.llamalend.constantOptions) as bigint[])[0];
|
|
1019
1021
|
const _n2 = _n1 + BigInt(N - 1);
|
|
1020
1022
|
|
|
1021
1023
|
return [_n2, _n1];
|
|
@@ -1034,44 +1036,44 @@ export class MintMarketTemplate {
|
|
|
1034
1036
|
}
|
|
1035
1037
|
|
|
1036
1038
|
public async repayIsApproved(debt: number | string): Promise<boolean> {
|
|
1037
|
-
return await hasAllowance([llamalend.crvUsdAddress], [debt], llamalend.signerAddress, this.controller);
|
|
1039
|
+
return await hasAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [debt], this.llamalend.signerAddress, this.controller);
|
|
1038
1040
|
}
|
|
1039
1041
|
|
|
1040
1042
|
private async repayApproveEstimateGas (debt: number | string): Promise<TGas> {
|
|
1041
|
-
return await ensureAllowanceEstimateGas([llamalend.crvUsdAddress], [debt], this.controller);
|
|
1043
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.llamalend.crvUsdAddress], [debt], this.controller);
|
|
1042
1044
|
}
|
|
1043
1045
|
|
|
1044
1046
|
public async repayApprove(debt: number | string): Promise<string[]> {
|
|
1045
|
-
return await ensureAllowance([llamalend.crvUsdAddress], [debt], this.controller);
|
|
1047
|
+
return await ensureAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [debt], this.controller);
|
|
1046
1048
|
}
|
|
1047
1049
|
|
|
1048
1050
|
public async repayHealth(debt: number | string, full = true, address = ""): Promise<string> {
|
|
1049
|
-
address = _getAddress(address);
|
|
1051
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1050
1052
|
const _debt = parseUnits(debt) * BigInt(-1);
|
|
1051
1053
|
|
|
1052
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
1053
|
-
let _health = await contract.health_calculator(address, 0, _debt, full, 0, llamalend.constantOptions) as bigint;
|
|
1054
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
1055
|
+
let _health = await contract.health_calculator(address, 0, _debt, full, 0, this.llamalend.constantOptions) as bigint;
|
|
1054
1056
|
_health = _health * BigInt(100);
|
|
1055
1057
|
|
|
1056
1058
|
return formatUnits(_health);
|
|
1057
1059
|
}
|
|
1058
1060
|
|
|
1059
1061
|
private async _repay(debt: number | string, address: string, estimateGas: boolean): Promise<string | TGas> {
|
|
1060
|
-
address = _getAddress(address);
|
|
1062
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1061
1063
|
const { debt: currentDebt } = await this.userState(address);
|
|
1062
1064
|
if (Number(currentDebt) === 0) throw Error(`Loan for ${address} does not exist`);
|
|
1063
1065
|
|
|
1064
1066
|
const _debt = parseUnits(debt);
|
|
1065
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1067
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1066
1068
|
const [, n1] = await this.userBands(address);
|
|
1067
1069
|
const { stablecoin } = await this.userState(address);
|
|
1068
1070
|
const n = (BN(stablecoin).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1; // In liquidation mode it doesn't matter if active band moves
|
|
1069
|
-
const gas = this.isDeleverageSupported ? await contract.repay.estimateGas(_debt, address, n, llamalend.constantOptions) : await contract.repay.estimateGas(_debt, address, n, isEth(this.collateral), llamalend.constantOptions);
|
|
1071
|
+
const gas = this.isDeleverageSupported ? await contract.repay.estimateGas(_debt, address, n, this.llamalend.constantOptions) : await contract.repay.estimateGas(_debt, address, n, isEth(this.collateral), this.llamalend.constantOptions);
|
|
1070
1072
|
if (estimateGas) return smartNumber(gas);
|
|
1071
1073
|
|
|
1072
|
-
await llamalend.updateFeeData();
|
|
1074
|
+
await this.llamalend.updateFeeData();
|
|
1073
1075
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1074
|
-
return (this.isDeleverageSupported ? await contract.repay(_debt, address, n, { ...llamalend.options, gasLimit }) : await contract.repay(_debt, address, n, isEth(this.collateral), { ...llamalend.options, gasLimit })).hash
|
|
1076
|
+
return (this.isDeleverageSupported ? await contract.repay(_debt, address, n, { ...this.llamalend.options, gasLimit }) : await contract.repay(_debt, address, n, isEth(this.collateral), { ...this.llamalend.options, gasLimit })).hash
|
|
1075
1077
|
}
|
|
1076
1078
|
|
|
1077
1079
|
public async repayEstimateGas(debt: number | string, address = ""): Promise<number> {
|
|
@@ -1087,38 +1089,38 @@ export class MintMarketTemplate {
|
|
|
1087
1089
|
// ---------------- FULL REPAY ----------------
|
|
1088
1090
|
|
|
1089
1091
|
private async _fullRepayAmount(address = ""): Promise<string> {
|
|
1090
|
-
address = _getAddress(address);
|
|
1092
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1091
1093
|
const debt = await this.userDebt(address);
|
|
1092
1094
|
return BN(debt).times(1.0001).toString();
|
|
1093
1095
|
}
|
|
1094
1096
|
|
|
1095
1097
|
public async fullRepayIsApproved(address = ""): Promise<boolean> {
|
|
1096
|
-
address = _getAddress(address);
|
|
1098
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1097
1099
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1098
1100
|
return await this.repayIsApproved(fullRepayAmount);
|
|
1099
1101
|
}
|
|
1100
1102
|
|
|
1101
1103
|
private async fullRepayApproveEstimateGas (address = ""): Promise<TGas> {
|
|
1102
|
-
address = _getAddress(address);
|
|
1104
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1103
1105
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1104
1106
|
return await this.repayApproveEstimateGas(fullRepayAmount);
|
|
1105
1107
|
}
|
|
1106
1108
|
|
|
1107
1109
|
public async fullRepayApprove(address = ""): Promise<string[]> {
|
|
1108
|
-
address = _getAddress(address);
|
|
1110
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1109
1111
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1110
1112
|
return await this.repayApprove(fullRepayAmount);
|
|
1111
1113
|
}
|
|
1112
1114
|
|
|
1113
1115
|
public async fullRepayEstimateGas(address = ""): Promise<number> {
|
|
1114
|
-
address = _getAddress(address);
|
|
1116
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1115
1117
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1116
1118
|
if (!(await this.repayIsApproved(fullRepayAmount))) throw Error("Approval is needed for gas estimation");
|
|
1117
1119
|
return await this._repay(fullRepayAmount, address, true) as number;
|
|
1118
1120
|
}
|
|
1119
1121
|
|
|
1120
1122
|
public async fullRepay(address = ""): Promise<string> {
|
|
1121
|
-
address = _getAddress(address);
|
|
1123
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1122
1124
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1123
1125
|
await this.repayApprove(fullRepayAmount);
|
|
1124
1126
|
return await this._repay(fullRepayAmount, address, false) as string;
|
|
@@ -1129,15 +1131,15 @@ export class MintMarketTemplate {
|
|
|
1129
1131
|
public async maxSwappable(i: number, j: number): Promise<string> {
|
|
1130
1132
|
if (!(i === 0 && j === 1) && !(i === 1 && j === 0)) throw Error("Wrong index");
|
|
1131
1133
|
const inDecimals = this.coinDecimals[i];
|
|
1132
|
-
const contract = llamalend.contracts[this.address].contract;
|
|
1133
|
-
const [_inAmount, _outAmount] = await contract.get_dxdy(i, j, MAX_ALLOWANCE, llamalend.constantOptions) as bigint[];
|
|
1134
|
+
const contract = this.llamalend.contracts[this.address].contract;
|
|
1135
|
+
const [_inAmount, _outAmount] = await contract.get_dxdy(i, j, MAX_ALLOWANCE, this.llamalend.constantOptions) as bigint[];
|
|
1134
1136
|
if (_outAmount === BigInt(0)) return "0";
|
|
1135
1137
|
|
|
1136
1138
|
return formatUnits(_inAmount, inDecimals)
|
|
1137
1139
|
}
|
|
1138
1140
|
|
|
1139
1141
|
private async _swapExpected(i: number, j: number, _amount: bigint): Promise<bigint> {
|
|
1140
|
-
return await llamalend.contracts[this.address].contract.get_dy(i, j, _amount, llamalend.constantOptions) as bigint;
|
|
1142
|
+
return await this.llamalend.contracts[this.address].contract.get_dy(i, j, _amount, this.llamalend.constantOptions) as bigint;
|
|
1141
1143
|
}
|
|
1142
1144
|
|
|
1143
1145
|
public async swapExpected(i: number, j: number, amount: number | string): Promise<string> {
|
|
@@ -1153,7 +1155,7 @@ export class MintMarketTemplate {
|
|
|
1153
1155
|
if (!(i === 0 && j === 1) && !(i === 1 && j === 0)) throw Error("Wrong index");
|
|
1154
1156
|
const [inDecimals, outDecimals] = this.coinDecimals;
|
|
1155
1157
|
const _amount = parseUnits(outAmount, outDecimals);
|
|
1156
|
-
const _expected = await llamalend.contracts[this.address].contract.get_dx(i, j, _amount, llamalend.constantOptions) as bigint;
|
|
1158
|
+
const _expected = await this.llamalend.contracts[this.address].contract.get_dx(i, j, _amount, this.llamalend.constantOptions) as bigint;
|
|
1157
1159
|
|
|
1158
1160
|
return formatUnits(_expected, inDecimals)
|
|
1159
1161
|
}
|
|
@@ -1194,19 +1196,19 @@ export class MintMarketTemplate {
|
|
|
1194
1196
|
public async swapIsApproved(i: number, amount: number | string): Promise<boolean> {
|
|
1195
1197
|
if (i !== 0 && i !== 1) throw Error("Wrong index");
|
|
1196
1198
|
|
|
1197
|
-
return await hasAllowance([this.coinAddresses[i]], [amount], llamalend.signerAddress, this.address);
|
|
1199
|
+
return await hasAllowance.call(this.llamalend, [this.coinAddresses[i]], [amount], this.llamalend.signerAddress, this.address);
|
|
1198
1200
|
}
|
|
1199
1201
|
|
|
1200
1202
|
private async swapApproveEstimateGas (i: number, amount: number | string): Promise<TGas> {
|
|
1201
1203
|
if (i !== 0 && i !== 1) throw Error("Wrong index");
|
|
1202
1204
|
|
|
1203
|
-
return await ensureAllowanceEstimateGas([this.coinAddresses[i]], [amount], this.address);
|
|
1205
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.coinAddresses[i]], [amount], this.address);
|
|
1204
1206
|
}
|
|
1205
1207
|
|
|
1206
1208
|
public async swapApprove(i: number, amount: number | string): Promise<string[]> {
|
|
1207
1209
|
if (i !== 0 && i !== 1) throw Error("Wrong index");
|
|
1208
1210
|
|
|
1209
|
-
return await ensureAllowance([this.coinAddresses[i]], [amount], this.address);
|
|
1211
|
+
return await ensureAllowance.call(this.llamalend, [this.coinAddresses[i]], [amount], this.address);
|
|
1210
1212
|
}
|
|
1211
1213
|
|
|
1212
1214
|
private async _swap(i: number, j: number, amount: number | string, slippage: number, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -1217,13 +1219,13 @@ export class MintMarketTemplate {
|
|
|
1217
1219
|
const _expected = await this._swapExpected(i, j, _amount);
|
|
1218
1220
|
const minRecvAmountBN: BigNumber = toBN(_expected, outDecimals).times(100 - slippage).div(100);
|
|
1219
1221
|
const _minRecvAmount = fromBN(minRecvAmountBN, outDecimals);
|
|
1220
|
-
const contract = llamalend.contracts[this.address].contract;
|
|
1221
|
-
const gas = await contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, llamalend.constantOptions);
|
|
1222
|
+
const contract = this.llamalend.contracts[this.address].contract;
|
|
1223
|
+
const gas = await contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, this.llamalend.constantOptions);
|
|
1222
1224
|
if (estimateGas) return smartNumber(gas);
|
|
1223
1225
|
|
|
1224
|
-
await llamalend.updateFeeData();
|
|
1226
|
+
await this.llamalend.updateFeeData();
|
|
1225
1227
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1226
|
-
return (await contract.exchange(i, j, _amount, _minRecvAmount, { ...llamalend.options, gasLimit })).hash
|
|
1228
|
+
return (await contract.exchange(i, j, _amount, _minRecvAmount, { ...this.llamalend.options, gasLimit })).hash
|
|
1227
1229
|
}
|
|
1228
1230
|
|
|
1229
1231
|
public async swapEstimateGas(i: number, j: number, amount: number | string, slippage = 0.1): Promise<number> {
|
|
@@ -1239,25 +1241,25 @@ export class MintMarketTemplate {
|
|
|
1239
1241
|
// ---------------- LIQUIDATE ----------------
|
|
1240
1242
|
|
|
1241
1243
|
public async tokensToLiquidate(address = ""): Promise<string> {
|
|
1242
|
-
address = _getAddress(address);
|
|
1243
|
-
const _tokens = await llamalend.contracts[this.controller].contract.tokens_to_liquidate(address, llamalend.constantOptions) as bigint;
|
|
1244
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1245
|
+
const _tokens = await this.llamalend.contracts[this.controller].contract.tokens_to_liquidate(address, this.llamalend.constantOptions) as bigint;
|
|
1244
1246
|
|
|
1245
1247
|
return formatUnits(_tokens)
|
|
1246
1248
|
}
|
|
1247
1249
|
|
|
1248
1250
|
public async liquidateIsApproved(address = ""): Promise<boolean> {
|
|
1249
1251
|
const tokensToLiquidate = await this.tokensToLiquidate(address);
|
|
1250
|
-
return await hasAllowance([llamalend.crvUsdAddress], [tokensToLiquidate], llamalend.signerAddress, this.controller);
|
|
1252
|
+
return await hasAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [tokensToLiquidate], this.llamalend.signerAddress, this.controller);
|
|
1251
1253
|
}
|
|
1252
1254
|
|
|
1253
1255
|
private async liquidateApproveEstimateGas (address = ""): Promise<TGas> {
|
|
1254
1256
|
const tokensToLiquidate = await this.tokensToLiquidate(address);
|
|
1255
|
-
return await ensureAllowanceEstimateGas([llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1257
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1256
1258
|
}
|
|
1257
1259
|
|
|
1258
1260
|
public async liquidateApprove(address = ""): Promise<string[]> {
|
|
1259
1261
|
const tokensToLiquidate = await this.tokensToLiquidate(address);
|
|
1260
|
-
return await ensureAllowance([llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1262
|
+
return await ensureAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1261
1263
|
}
|
|
1262
1264
|
|
|
1263
1265
|
private async _liquidate(address: string, slippage: number, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -1269,13 +1271,13 @@ export class MintMarketTemplate {
|
|
|
1269
1271
|
|
|
1270
1272
|
const minAmountBN: BigNumber = BN(stablecoin).times(100 - slippage).div(100);
|
|
1271
1273
|
const _minAmount = fromBN(minAmountBN);
|
|
1272
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1273
|
-
const gas = this.isDeleverageSupported ? (await contract.liquidate.estimateGas(address, _minAmount, llamalend.constantOptions)) : (await contract.liquidate.estimateGas(address, _minAmount, isEth(this.collateral), llamalend.constantOptions))
|
|
1274
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1275
|
+
const gas = this.isDeleverageSupported ? (await contract.liquidate.estimateGas(address, _minAmount, this.llamalend.constantOptions)) : (await contract.liquidate.estimateGas(address, _minAmount, isEth(this.collateral), this.llamalend.constantOptions))
|
|
1274
1276
|
if (estimateGas) return smartNumber(gas);
|
|
1275
1277
|
|
|
1276
|
-
await llamalend.updateFeeData();
|
|
1278
|
+
await this.llamalend.updateFeeData();
|
|
1277
1279
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1278
|
-
return (this.isDeleverageSupported ? await contract.liquidate(address, _minAmount, { ...llamalend.options, gasLimit }) : await contract.liquidate(address, _minAmount, isEth(this.collateral), { ...llamalend.options, gasLimit })).hash
|
|
1280
|
+
return (this.isDeleverageSupported ? await contract.liquidate(address, _minAmount, { ...this.llamalend.options, gasLimit }) : await contract.liquidate(address, _minAmount, isEth(this.collateral), { ...this.llamalend.options, gasLimit })).hash
|
|
1279
1281
|
}
|
|
1280
1282
|
|
|
1281
1283
|
public async liquidateEstimateGas(address: string, slippage = 0.1): Promise<number> {
|
|
@@ -1304,12 +1306,12 @@ export class MintMarketTemplate {
|
|
|
1304
1306
|
|
|
1305
1307
|
public async selfLiquidateEstimateGas(slippage = 0.1): Promise<number> {
|
|
1306
1308
|
if (!(await this.selfLiquidateIsApproved())) throw Error("Approval is needed for gas estimation");
|
|
1307
|
-
return await this._liquidate(llamalend.signerAddress, slippage, true) as number;
|
|
1309
|
+
return await this._liquidate(this.llamalend.signerAddress, slippage, true) as number;
|
|
1308
1310
|
}
|
|
1309
1311
|
|
|
1310
1312
|
public async selfLiquidate(slippage = 0.1): Promise<string> {
|
|
1311
1313
|
await this.selfLiquidateApprove();
|
|
1312
|
-
return await this._liquidate(llamalend.signerAddress, slippage, false) as string;
|
|
1314
|
+
return await this._liquidate(this.llamalend.signerAddress, slippage, false) as string;
|
|
1313
1315
|
}
|
|
1314
1316
|
|
|
1315
1317
|
// ---------------- CREATE LOAN WITH LEVERAGE ----------------
|
|
@@ -1334,15 +1336,15 @@ export class MintMarketTemplate {
|
|
|
1334
1336
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1335
1337
|
const calls = [];
|
|
1336
1338
|
for (let i = 0; i < 5; i++) {
|
|
1337
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, range, i));
|
|
1339
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, range, i));
|
|
1338
1340
|
}
|
|
1339
|
-
const _res: bigint[][] = await llamalend.multicallProvider.all(calls);
|
|
1341
|
+
const _res: bigint[][] = await this.llamalend.multicallProvider.all(calls);
|
|
1340
1342
|
const _maxBorrowable = _res.map((r) => r[0] * BigInt(999) / BigInt(1000));
|
|
1341
1343
|
const _maxCollateral = _res.map((r) => r[1] * BigInt(999) / BigInt(1000));
|
|
1342
1344
|
const routeIdx = this._getBestIdx(_maxCollateral);
|
|
1343
1345
|
|
|
1344
|
-
const maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1345
|
-
const maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1346
|
+
const maxBorrowable = this.llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1347
|
+
const maxCollateral = this.llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1346
1348
|
return {
|
|
1347
1349
|
maxBorrowable,
|
|
1348
1350
|
maxCollateral,
|
|
@@ -1359,10 +1361,10 @@ export class MintMarketTemplate {
|
|
|
1359
1361
|
const calls = [];
|
|
1360
1362
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1361
1363
|
for (let i = 0; i < 5; i++) {
|
|
1362
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, i));
|
|
1364
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, i));
|
|
1363
1365
|
}
|
|
1364
1366
|
}
|
|
1365
|
-
const _rawRes: bigint[][] = await llamalend.multicallProvider.all(calls);
|
|
1367
|
+
const _rawRes: bigint[][] = await this.llamalend.multicallProvider.all(calls);
|
|
1366
1368
|
|
|
1367
1369
|
const res: IDict<{ maxBorrowable: string, maxCollateral: string, leverage: string, routeIdx: number }> = {};
|
|
1368
1370
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
@@ -1370,8 +1372,8 @@ export class MintMarketTemplate {
|
|
|
1370
1372
|
const _maxBorrowable = _res.map((r) => r[0] * BigInt(999) / BigInt(1000));
|
|
1371
1373
|
const _maxCollateral = _res.map((r) => r[1] * BigInt(999) / BigInt(1000));
|
|
1372
1374
|
const routeIdx = this._getBestIdx(_maxCollateral);
|
|
1373
|
-
const maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1374
|
-
const maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1375
|
+
const maxBorrowable = this.llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1376
|
+
const maxCollateral = this.llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1375
1377
|
res[N] = {
|
|
1376
1378
|
maxBorrowable,
|
|
1377
1379
|
maxCollateral,
|
|
@@ -1393,14 +1395,14 @@ export class MintMarketTemplate {
|
|
|
1393
1395
|
|
|
1394
1396
|
const calls = [];
|
|
1395
1397
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1396
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, routeIdx));
|
|
1398
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, routeIdx));
|
|
1397
1399
|
}
|
|
1398
|
-
const _res: bigint[][] = await llamalend.multicallProvider.all(calls);
|
|
1400
|
+
const _res: bigint[][] = await this.llamalend.multicallProvider.all(calls);
|
|
1399
1401
|
|
|
1400
1402
|
const res: IDict<{ maxBorrowable: string, maxCollateral: string, leverage: string }> = {};
|
|
1401
1403
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1402
|
-
const maxBorrowable = llamalend.formatUnits(_res[N - this.minBands][0] * BigInt(999) / BigInt(1000));
|
|
1403
|
-
const maxCollateral = llamalend.formatUnits(_res[N - this.minBands][1]* BigInt(999) / BigInt(1000), this.collateralDecimals);
|
|
1404
|
+
const maxBorrowable = this.llamalend.formatUnits(_res[N - this.minBands][0] * BigInt(999) / BigInt(1000));
|
|
1405
|
+
const maxCollateral = this.llamalend.formatUnits(_res[N - this.minBands][1]* BigInt(999) / BigInt(1000), this.collateralDecimals);
|
|
1404
1406
|
res[N] = {
|
|
1405
1407
|
maxBorrowable,
|
|
1406
1408
|
maxCollateral,
|
|
@@ -1421,9 +1423,9 @@ export class MintMarketTemplate {
|
|
|
1421
1423
|
const _debt = parseUnits(debt);
|
|
1422
1424
|
const calls = [];
|
|
1423
1425
|
for (let i = 0; i < 5; i++) {
|
|
1424
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.get_collateral(_debt, i));
|
|
1426
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.get_collateral(_debt, i));
|
|
1425
1427
|
}
|
|
1426
|
-
const _leverageCollateral: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
1428
|
+
const _leverageCollateral: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
1427
1429
|
const routeIdx = this._getBestIdx(_leverageCollateral);
|
|
1428
1430
|
|
|
1429
1431
|
return { _collateral: _userCollateral + _leverageCollateral[routeIdx], routeIdx }
|
|
@@ -1443,14 +1445,14 @@ export class MintMarketTemplate {
|
|
|
1443
1445
|
Promise<{ collateral: string, leverage: string, routeIdx: number }> {
|
|
1444
1446
|
this._checkLeverageZap();
|
|
1445
1447
|
const { _collateral, routeIdx } = await this._leverageCreateLoanCollateral(userCollateral, debt);
|
|
1446
|
-
const collateral = llamalend.formatUnits(_collateral, this.collateralDecimals);
|
|
1448
|
+
const collateral = this.llamalend.formatUnits(_collateral, this.collateralDecimals);
|
|
1447
1449
|
|
|
1448
1450
|
return { collateral, leverage: BN(collateral).div(userCollateral).toFixed(4), routeIdx };
|
|
1449
1451
|
}
|
|
1450
1452
|
|
|
1451
1453
|
private async leverageGetRouteName(routeIdx: number): Promise<string> {
|
|
1452
1454
|
this._checkLeverageZap();
|
|
1453
|
-
return await llamalend.contracts[this.leverageZap].contract.route_names(routeIdx);
|
|
1455
|
+
return await this.llamalend.contracts[this.leverageZap].contract.route_names(routeIdx);
|
|
1454
1456
|
}
|
|
1455
1457
|
|
|
1456
1458
|
private async leverageGetMaxRange(collateral: number | string, debt: number | string): Promise<number> {
|
|
@@ -1469,7 +1471,7 @@ export class MintMarketTemplate {
|
|
|
1469
1471
|
const routeIdx = await this._getRouteIdx(collateral, debt);
|
|
1470
1472
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1471
1473
|
const _debt = parseUnits(debt);
|
|
1472
|
-
return await llamalend.contracts[this.leverageZap].contract.calculate_debt_n1(_collateral, _debt, range, routeIdx, llamalend.constantOptions);
|
|
1474
|
+
return await this.llamalend.contracts[this.leverageZap].contract.calculate_debt_n1(_collateral, _debt, range, routeIdx, this.llamalend.constantOptions);
|
|
1473
1475
|
}
|
|
1474
1476
|
|
|
1475
1477
|
private async _leverageCalcN1AllRanges(collateral: number | string, debt: number | string, maxN: number): Promise<bigint[]> {
|
|
@@ -1478,9 +1480,9 @@ export class MintMarketTemplate {
|
|
|
1478
1480
|
const _debt = parseUnits(debt);
|
|
1479
1481
|
const calls = [];
|
|
1480
1482
|
for (let N = this.minBands; N <= maxN; N++) {
|
|
1481
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.calculate_debt_n1(_collateral, _debt, N, routeIdx));
|
|
1483
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.calculate_debt_n1(_collateral, _debt, N, routeIdx));
|
|
1482
1484
|
}
|
|
1483
|
-
return await llamalend.multicallProvider.all(calls) as bigint[];
|
|
1485
|
+
return await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
1484
1486
|
}
|
|
1485
1487
|
|
|
1486
1488
|
private async _leverageCreateLoanBands(collateral: number | string, debt: number | string, range: number): Promise<[bigint, bigint]> {
|
|
@@ -1558,8 +1560,8 @@ export class MintMarketTemplate {
|
|
|
1558
1560
|
const { _collateral } = await this._leverageCreateLoanCollateral(collateral, debt);
|
|
1559
1561
|
const _debt = parseUnits(debt);
|
|
1560
1562
|
|
|
1561
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
1562
|
-
let _health = await contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions) as bigint;
|
|
1563
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
1564
|
+
let _health = await contract.health_calculator(address, _collateral, _debt, full, range, this.llamalend.constantOptions) as bigint;
|
|
1563
1565
|
_health = _health * BigInt(100);
|
|
1564
1566
|
|
|
1565
1567
|
return formatUnits(_health);
|
|
@@ -1570,7 +1572,7 @@ export class MintMarketTemplate {
|
|
|
1570
1572
|
const small_x_BN = BN(100);
|
|
1571
1573
|
const { _collateral, routeIdx } = await this._leverageCreateLoanCollateral(collateral, debt);
|
|
1572
1574
|
const _y = _collateral - parseUnits(collateral, this.collateralDecimals);
|
|
1573
|
-
const _small_y = await llamalend.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx);
|
|
1575
|
+
const _small_y = await this.llamalend.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx);
|
|
1574
1576
|
const y_BN = toBN(_y, this.collateralDecimals);
|
|
1575
1577
|
const small_y_BN = toBN(_small_y, this.collateralDecimals);
|
|
1576
1578
|
const rateBN = y_BN.div(x_BN);
|
|
@@ -1586,24 +1588,24 @@ export class MintMarketTemplate {
|
|
|
1586
1588
|
|
|
1587
1589
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1588
1590
|
const _debt = parseUnits(debt);
|
|
1589
|
-
const leverageContract = llamalend.contracts[this.leverageZap].contract;
|
|
1591
|
+
const leverageContract = this.llamalend.contracts[this.leverageZap].contract;
|
|
1590
1592
|
const routeIdx = await this._getRouteIdx(collateral, debt);
|
|
1591
|
-
const _expected = await leverageContract.get_collateral_underlying(_debt, routeIdx, llamalend.constantOptions);
|
|
1593
|
+
const _expected = await leverageContract.get_collateral_underlying(_debt, routeIdx, this.llamalend.constantOptions);
|
|
1592
1594
|
const minRecvBN = toBN(_expected, this.collateralDecimals).times(100 - slippage).div(100);
|
|
1593
1595
|
const _minRecv = fromBN(minRecvBN, this.collateralDecimals);
|
|
1594
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1595
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
1596
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1597
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
1596
1598
|
const gas = await contract.create_loan_extended.estimateGas(
|
|
1597
1599
|
_collateral,
|
|
1598
1600
|
_debt,
|
|
1599
1601
|
range,
|
|
1600
1602
|
this.leverageZap,
|
|
1601
1603
|
[routeIdx, _minRecv],
|
|
1602
|
-
{ ...llamalend.constantOptions, value }
|
|
1604
|
+
{ ...this.llamalend.constantOptions, value }
|
|
1603
1605
|
);
|
|
1604
1606
|
if (estimateGas) return smartNumber(gas);
|
|
1605
1607
|
|
|
1606
|
-
await llamalend.updateFeeData();
|
|
1608
|
+
await this.llamalend.updateFeeData();
|
|
1607
1609
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1608
1610
|
return (await contract.create_loan_extended(
|
|
1609
1611
|
_collateral,
|
|
@@ -1611,7 +1613,7 @@ export class MintMarketTemplate {
|
|
|
1611
1613
|
range,
|
|
1612
1614
|
this.leverageZap,
|
|
1613
1615
|
[routeIdx, _minRecv],
|
|
1614
|
-
{ ...llamalend.options, gasLimit, value }
|
|
1616
|
+
{ ...this.llamalend.options, gasLimit, value }
|
|
1615
1617
|
)).hash
|
|
1616
1618
|
}
|
|
1617
1619
|
|
|
@@ -1638,11 +1640,11 @@ export class MintMarketTemplate {
|
|
|
1638
1640
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1639
1641
|
const calls = [];
|
|
1640
1642
|
for (let i = 0; i < 5; i++) {
|
|
1641
|
-
calls.push(llamalend.contracts[this.deleverageZap].multicallContract.get_stablecoins(_collateral, i));
|
|
1643
|
+
calls.push(this.llamalend.contracts[this.deleverageZap].multicallContract.get_stablecoins(_collateral, i));
|
|
1642
1644
|
}
|
|
1643
|
-
const _stablecoins_arr: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
1645
|
+
const _stablecoins_arr: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
1644
1646
|
const routeIdx = this._getBestIdx(_stablecoins_arr);
|
|
1645
|
-
const stablecoins = llamalend.formatUnits(_stablecoins_arr[routeIdx]);
|
|
1647
|
+
const stablecoins = this.llamalend.formatUnits(_stablecoins_arr[routeIdx]);
|
|
1646
1648
|
|
|
1647
1649
|
return { stablecoins, routeIdx };
|
|
1648
1650
|
},
|
|
@@ -1653,11 +1655,11 @@ export class MintMarketTemplate {
|
|
|
1653
1655
|
|
|
1654
1656
|
private async deleverageGetRouteName(routeIdx: number): Promise<string> {
|
|
1655
1657
|
this._checkDeleverageZap();
|
|
1656
|
-
return await llamalend.contracts[this.deleverageZap].contract.route_names(routeIdx);
|
|
1658
|
+
return await this.llamalend.contracts[this.deleverageZap].contract.route_names(routeIdx);
|
|
1657
1659
|
}
|
|
1658
1660
|
|
|
1659
1661
|
private async deleverageIsFullRepayment(deleverageCollateral: number | string, address = ""): Promise<boolean> {
|
|
1660
|
-
address = _getAddress(address);
|
|
1662
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1661
1663
|
const { stablecoin, debt } = await this.userState(address);
|
|
1662
1664
|
const { stablecoins: deleverageStablecoins } = await this.deleverageRepayStablecoins(deleverageCollateral);
|
|
1663
1665
|
|
|
@@ -1673,7 +1675,7 @@ export class MintMarketTemplate {
|
|
|
1673
1675
|
// There is no deleverage zap
|
|
1674
1676
|
if (this.deleverageZap === "0x0000000000000000000000000000000000000000") return false;
|
|
1675
1677
|
|
|
1676
|
-
address = _getAddress(address);
|
|
1678
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1677
1679
|
const { collateral, stablecoin, debt } = await this.userState(address);
|
|
1678
1680
|
// Loan does not exist
|
|
1679
1681
|
if (BN(debt).eq(0)) return false;
|
|
@@ -1686,7 +1688,7 @@ export class MintMarketTemplate {
|
|
|
1686
1688
|
}
|
|
1687
1689
|
|
|
1688
1690
|
private _deleverageRepayBands = memoize( async (collateral: number | string, address: string): Promise<[bigint, bigint]> => {
|
|
1689
|
-
address = _getAddress(address);
|
|
1691
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1690
1692
|
if (!(await this.deleverageIsAvailable(collateral, address))) return [parseUnits(0, 0), parseUnits(0, 0)];
|
|
1691
1693
|
const { routeIdx } = await this.deleverageRepayStablecoins(collateral);
|
|
1692
1694
|
const { _debt: _currentDebt } = await this._userState(address);
|
|
@@ -1697,7 +1699,7 @@ export class MintMarketTemplate {
|
|
|
1697
1699
|
let _n1 = parseUnits(0, 0);
|
|
1698
1700
|
let _n2 = parseUnits(0, 0);
|
|
1699
1701
|
try {
|
|
1700
|
-
_n1 = await llamalend.contracts[this.deleverageZap].contract.calculate_debt_n1(_collateral, routeIdx, address);
|
|
1702
|
+
_n1 = await this.llamalend.contracts[this.deleverageZap].contract.calculate_debt_n1(_collateral, routeIdx, address);
|
|
1701
1703
|
_n2 = _n1 + BigInt(N - 1);
|
|
1702
1704
|
} catch {
|
|
1703
1705
|
console.log("Full repayment");
|
|
@@ -1726,7 +1728,7 @@ export class MintMarketTemplate {
|
|
|
1726
1728
|
|
|
1727
1729
|
private async deleverageRepayHealth(collateral: number | string, full = true, address = ""): Promise<string> {
|
|
1728
1730
|
this._checkDeleverageZap();
|
|
1729
|
-
address = _getAddress(address);
|
|
1731
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1730
1732
|
if (!(await this.deleverageIsAvailable(collateral, address))) return "0.0";
|
|
1731
1733
|
const { _stablecoin, _debt } = await this._userState(address);
|
|
1732
1734
|
const { stablecoins: deleverageStablecoins } = await this.deleverageRepayStablecoins(collateral);
|
|
@@ -1735,8 +1737,8 @@ export class MintMarketTemplate {
|
|
|
1735
1737
|
const N = await this.userRange(address);
|
|
1736
1738
|
|
|
1737
1739
|
if ((_debt + _d_debt) < 0) return "0.0";
|
|
1738
|
-
const contract = llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
1739
|
-
let _health = await contract.health_calculator(address, _d_collateral, _d_debt, full, N, llamalend.constantOptions) as bigint;
|
|
1740
|
+
const contract = this.llamalend.contracts[this.healthCalculator ?? this.controller].contract;
|
|
1741
|
+
let _health = await contract.health_calculator(address, _d_collateral, _d_debt, full, N, this.llamalend.constantOptions) as bigint;
|
|
1740
1742
|
_health = _health * BigInt(100);
|
|
1741
1743
|
|
|
1742
1744
|
return formatUnits(_health);
|
|
@@ -1747,7 +1749,7 @@ export class MintMarketTemplate {
|
|
|
1747
1749
|
const small_x_BN = BN(0.001);
|
|
1748
1750
|
const { stablecoins, routeIdx } = await this.deleverageRepayStablecoins(collateral);
|
|
1749
1751
|
const _y = parseUnits(stablecoins);
|
|
1750
|
-
const _small_y = await llamalend.contracts[this.deleverageZap].contract.get_stablecoins(fromBN(small_x_BN, this.collateralDecimals), routeIdx);
|
|
1752
|
+
const _small_y = await this.llamalend.contracts[this.deleverageZap].contract.get_stablecoins(fromBN(small_x_BN, this.collateralDecimals), routeIdx);
|
|
1751
1753
|
const y_BN = toBN(_y);
|
|
1752
1754
|
const small_y_BN = toBN(_small_y);
|
|
1753
1755
|
const rateBN = y_BN.div(x_BN);
|
|
@@ -1758,21 +1760,21 @@ export class MintMarketTemplate {
|
|
|
1758
1760
|
}
|
|
1759
1761
|
|
|
1760
1762
|
private async _deleverageRepay(collateral: number | string, slippage: number, estimateGas: boolean): Promise<string | TGas> {
|
|
1761
|
-
const { debt: currentDebt } = await this.userState(llamalend.signerAddress);
|
|
1762
|
-
if (Number(currentDebt) === 0) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1763
|
+
const { debt: currentDebt } = await this.userState(this.llamalend.signerAddress);
|
|
1764
|
+
if (Number(currentDebt) === 0) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
1763
1765
|
|
|
1764
1766
|
const { stablecoins, routeIdx } = await this.deleverageRepayStablecoins(collateral);
|
|
1765
1767
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1766
1768
|
const _debt = parseUnits(stablecoins);
|
|
1767
1769
|
const minRecvBN = toBN(_debt).times(100 - slippage).div(100);
|
|
1768
1770
|
const _minRecv = fromBN(minRecvBN);
|
|
1769
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1770
|
-
const gas = await contract.repay_extended.estimateGas(this.deleverageZap, [routeIdx, _collateral, _minRecv], llamalend.constantOptions);
|
|
1771
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1772
|
+
const gas = await contract.repay_extended.estimateGas(this.deleverageZap, [routeIdx, _collateral, _minRecv], this.llamalend.constantOptions);
|
|
1771
1773
|
if (estimateGas) return smartNumber(gas);
|
|
1772
1774
|
|
|
1773
|
-
await llamalend.updateFeeData();
|
|
1775
|
+
await this.llamalend.updateFeeData();
|
|
1774
1776
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1775
|
-
return (await contract.repay_extended(this.deleverageZap, [routeIdx, _collateral, _minRecv], { ...llamalend.options, gasLimit })).hash
|
|
1777
|
+
return (await contract.repay_extended(this.deleverageZap, [routeIdx, _collateral, _minRecv], { ...this.llamalend.options, gasLimit })).hash
|
|
1776
1778
|
}
|
|
1777
1779
|
|
|
1778
1780
|
private async deleverageRepayEstimateGas(collateral: number | string, slippage = 0.1): Promise<number> {
|