@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,
|
|
@@ -33,6 +33,7 @@ const WEEK = 7 * DAY;
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
export class LendMarketTemplate {
|
|
36
|
+
private llamalend: Llamalend;
|
|
36
37
|
id: string;
|
|
37
38
|
name: string
|
|
38
39
|
addresses: {
|
|
@@ -234,7 +235,8 @@ export class LendMarketTemplate {
|
|
|
234
235
|
}
|
|
235
236
|
};
|
|
236
237
|
|
|
237
|
-
constructor(id: string, marketData: IOneWayMarket) {
|
|
238
|
+
constructor(id: string, marketData: IOneWayMarket, llamalend: Llamalend) {
|
|
239
|
+
this.llamalend = llamalend;
|
|
238
240
|
this.id = id;
|
|
239
241
|
this.name = marketData.name;
|
|
240
242
|
this.addresses = marketData.addresses;
|
|
@@ -387,42 +389,42 @@ export class LendMarketTemplate {
|
|
|
387
389
|
// ---------------- VAULT ----------------
|
|
388
390
|
|
|
389
391
|
private async vaultMaxDeposit(address = ""): Promise<string> {
|
|
390
|
-
address = _getAddress(address);
|
|
391
|
-
// const _amount = await llamalend.contracts[this.addresses.vault].contract.maxDeposit(address); TODO use maxDeposit
|
|
392
|
-
const _amount = await llamalend.contracts[this.addresses.borrowed_token].contract.balanceOf(address);
|
|
392
|
+
address = _getAddress.call(this.llamalend, address);
|
|
393
|
+
// const _amount = await this.llamalend.contracts[this.addresses.vault].contract.maxDeposit(address); TODO use maxDeposit
|
|
394
|
+
const _amount = await this.llamalend.contracts[this.addresses.borrowed_token].contract.balanceOf(address);
|
|
393
395
|
|
|
394
396
|
return formatUnits(_amount, this.borrowed_token.decimals);
|
|
395
397
|
}
|
|
396
398
|
|
|
397
399
|
private async vaultPreviewDeposit(amount: TAmount): Promise<string> {
|
|
398
400
|
const _amount = parseUnits(amount, this.borrowed_token.decimals);
|
|
399
|
-
const _shares = await llamalend.contracts[this.addresses.vault].contract.previewDeposit(_amount);
|
|
401
|
+
const _shares = await this.llamalend.contracts[this.addresses.vault].contract.previewDeposit(_amount);
|
|
400
402
|
|
|
401
403
|
return formatUnits(_shares, 18);
|
|
402
404
|
}
|
|
403
405
|
|
|
404
406
|
private async vaultDepositIsApproved(borrowed: TAmount): Promise<boolean> {
|
|
405
|
-
return await hasAllowance([this.borrowed_token.address], [borrowed], llamalend.signerAddress, this.addresses.vault);
|
|
407
|
+
return await hasAllowance.call(this.llamalend, [this.borrowed_token.address], [borrowed], this.llamalend.signerAddress, this.addresses.vault);
|
|
406
408
|
}
|
|
407
409
|
|
|
408
410
|
private async vaultDepositApproveEstimateGas (borrowed: TAmount): Promise<TGas> {
|
|
409
|
-
return await ensureAllowanceEstimateGas([this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
411
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
410
412
|
}
|
|
411
413
|
|
|
412
414
|
private async vaultDepositApprove(borrowed: TAmount): Promise<string[]> {
|
|
413
|
-
return await ensureAllowance([this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
415
|
+
return await ensureAllowance.call(this.llamalend, [this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
414
416
|
}
|
|
415
417
|
|
|
416
418
|
private async _vaultDeposit(amount: TAmount, estimateGas = false): Promise<string | TGas> {
|
|
417
419
|
const _amount = parseUnits(amount, this.borrowed_token.decimals);
|
|
418
|
-
const gas = await llamalend.contracts[this.addresses.vault].contract.deposit.estimateGas(_amount, { ...llamalend.constantOptions });
|
|
420
|
+
const gas = await this.llamalend.contracts[this.addresses.vault].contract.deposit.estimateGas(_amount, { ...this.llamalend.constantOptions });
|
|
419
421
|
if (estimateGas) return smartNumber(gas);
|
|
420
422
|
|
|
421
|
-
await llamalend.updateFeeData();
|
|
423
|
+
await this.llamalend.updateFeeData();
|
|
422
424
|
|
|
423
425
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
424
426
|
|
|
425
|
-
return (await llamalend.contracts[this.addresses.vault].contract.deposit(_amount, { ...llamalend.options, gasLimit })).hash;
|
|
427
|
+
return (await this.llamalend.contracts[this.addresses.vault].contract.deposit(_amount, { ...this.llamalend.options, gasLimit })).hash;
|
|
426
428
|
}
|
|
427
429
|
|
|
428
430
|
private async vaultDepositEstimateGas(amount: TAmount): Promise<TGas> {
|
|
@@ -437,43 +439,43 @@ export class LendMarketTemplate {
|
|
|
437
439
|
|
|
438
440
|
|
|
439
441
|
private async vaultMaxMint(address = ""): Promise<string> {
|
|
440
|
-
address = _getAddress(address);
|
|
441
|
-
// const _shares = await llamalend.contracts[this.addresses.vault].contract.maxMint(address); TODO use maxMint
|
|
442
|
-
const _assetBalance = await llamalend.contracts[this.addresses.borrowed_token].contract.balanceOf(address);
|
|
443
|
-
const _shares = await llamalend.contracts[this.addresses.vault].contract.convertToShares(_assetBalance);
|
|
442
|
+
address = _getAddress.call(this.llamalend, address);
|
|
443
|
+
// const _shares = await this.llamalend.contracts[this.addresses.vault].contract.maxMint(address); TODO use maxMint
|
|
444
|
+
const _assetBalance = await this.llamalend.contracts[this.addresses.borrowed_token].contract.balanceOf(address);
|
|
445
|
+
const _shares = await this.llamalend.contracts[this.addresses.vault].contract.convertToShares(_assetBalance);
|
|
444
446
|
|
|
445
447
|
return formatUnits(_shares, 18);
|
|
446
448
|
}
|
|
447
449
|
|
|
448
450
|
private async vaultPreviewMint(amount: TAmount): Promise<string> {
|
|
449
451
|
const _amount = parseUnits(amount, 18);
|
|
450
|
-
const _assets = await llamalend.contracts[this.addresses.vault].contract.previewMint(_amount);
|
|
452
|
+
const _assets = await this.llamalend.contracts[this.addresses.vault].contract.previewMint(_amount);
|
|
451
453
|
|
|
452
454
|
return formatUnits(_assets, this.borrowed_token.decimals);
|
|
453
455
|
}
|
|
454
456
|
|
|
455
457
|
private async vaultMintIsApproved(borrowed: TAmount): Promise<boolean> {
|
|
456
|
-
return await hasAllowance([this.borrowed_token.address], [borrowed], llamalend.signerAddress, this.addresses.vault);
|
|
458
|
+
return await hasAllowance.call(this.llamalend, [this.borrowed_token.address], [borrowed], this.llamalend.signerAddress, this.addresses.vault);
|
|
457
459
|
}
|
|
458
460
|
|
|
459
461
|
private async vaultMintApproveEstimateGas (borrowed: TAmount): Promise<TGas> {
|
|
460
|
-
return await ensureAllowanceEstimateGas([this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
462
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
461
463
|
}
|
|
462
464
|
|
|
463
465
|
private async vaultMintApprove(borrowed: TAmount): Promise<string[]> {
|
|
464
|
-
return await ensureAllowance([this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
466
|
+
return await ensureAllowance.call(this.llamalend, [this.borrowed_token.address], [borrowed], this.addresses.vault);
|
|
465
467
|
}
|
|
466
468
|
|
|
467
469
|
private async _vaultMint(amount: TAmount, estimateGas = false): Promise<string | TGas> {
|
|
468
470
|
const _amount = parseUnits(amount, 18);
|
|
469
|
-
const gas = await llamalend.contracts[this.addresses.vault].contract.mint.estimateGas(_amount, { ...llamalend.constantOptions });
|
|
471
|
+
const gas = await this.llamalend.contracts[this.addresses.vault].contract.mint.estimateGas(_amount, { ...this.llamalend.constantOptions });
|
|
470
472
|
if (estimateGas) return smartNumber(gas);
|
|
471
473
|
|
|
472
|
-
await llamalend.updateFeeData();
|
|
474
|
+
await this.llamalend.updateFeeData();
|
|
473
475
|
|
|
474
476
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
475
477
|
|
|
476
|
-
return (await llamalend.contracts[this.addresses.vault].contract.mint(_amount, { ...llamalend.options, gasLimit })).hash;
|
|
478
|
+
return (await this.llamalend.contracts[this.addresses.vault].contract.mint(_amount, { ...this.llamalend.options, gasLimit })).hash;
|
|
477
479
|
}
|
|
478
480
|
|
|
479
481
|
private async vaultMintEstimateGas(amount: TAmount): Promise<TGas> {
|
|
@@ -488,29 +490,29 @@ export class LendMarketTemplate {
|
|
|
488
490
|
|
|
489
491
|
|
|
490
492
|
private async vaultMaxWithdraw(address = ""): Promise<string> {
|
|
491
|
-
address = _getAddress(address);
|
|
492
|
-
const _assets = await llamalend.contracts[this.addresses.vault].contract.maxWithdraw(address);
|
|
493
|
+
address = _getAddress.call(this.llamalend, address);
|
|
494
|
+
const _assets = await this.llamalend.contracts[this.addresses.vault].contract.maxWithdraw(address);
|
|
493
495
|
|
|
494
496
|
return formatUnits(_assets, this.borrowed_token.decimals);
|
|
495
497
|
}
|
|
496
498
|
|
|
497
499
|
private async vaultPreviewWithdraw(amount: TAmount): Promise<string> {
|
|
498
500
|
const _amount = parseUnits(amount, this.borrowed_token.decimals);
|
|
499
|
-
const _shares = await llamalend.contracts[this.addresses.vault].contract.previewWithdraw(_amount);
|
|
501
|
+
const _shares = await this.llamalend.contracts[this.addresses.vault].contract.previewWithdraw(_amount);
|
|
500
502
|
|
|
501
503
|
return formatUnits(_shares, 18);
|
|
502
504
|
}
|
|
503
505
|
|
|
504
506
|
private async _vaultWithdraw(amount: TAmount, estimateGas = false): Promise<string | TGas> {
|
|
505
507
|
const _amount = parseUnits(amount, this.borrowed_token.decimals);
|
|
506
|
-
const gas = await llamalend.contracts[this.addresses.vault].contract.withdraw.estimateGas(_amount, { ...llamalend.constantOptions });
|
|
508
|
+
const gas = await this.llamalend.contracts[this.addresses.vault].contract.withdraw.estimateGas(_amount, { ...this.llamalend.constantOptions });
|
|
507
509
|
if (estimateGas) return smartNumber(gas);
|
|
508
510
|
|
|
509
|
-
await llamalend.updateFeeData();
|
|
511
|
+
await this.llamalend.updateFeeData();
|
|
510
512
|
|
|
511
513
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
512
514
|
|
|
513
|
-
return (await llamalend.contracts[this.addresses.vault].contract.withdraw(_amount, { ...llamalend.options, gasLimit })).hash;
|
|
515
|
+
return (await this.llamalend.contracts[this.addresses.vault].contract.withdraw(_amount, { ...this.llamalend.options, gasLimit })).hash;
|
|
514
516
|
}
|
|
515
517
|
|
|
516
518
|
private async vaultWithdrawEstimateGas(amount: TAmount): Promise<TGas> {
|
|
@@ -523,29 +525,29 @@ export class LendMarketTemplate {
|
|
|
523
525
|
|
|
524
526
|
|
|
525
527
|
private async vaultMaxRedeem(address = ""): Promise<string> {
|
|
526
|
-
address = _getAddress(address);
|
|
527
|
-
const _shares = await llamalend.contracts[this.addresses.vault].contract.maxRedeem(address)
|
|
528
|
+
address = _getAddress.call(this.llamalend, address);
|
|
529
|
+
const _shares = await this.llamalend.contracts[this.addresses.vault].contract.maxRedeem(address)
|
|
528
530
|
|
|
529
531
|
return formatUnits(_shares, 18);
|
|
530
532
|
}
|
|
531
533
|
|
|
532
534
|
private async vaultPreviewRedeem(amount: TAmount): Promise<string> {
|
|
533
535
|
const _amount = parseUnits(amount, 18);
|
|
534
|
-
const _assets = await llamalend.contracts[this.addresses.vault].contract.previewRedeem(_amount);
|
|
536
|
+
const _assets = await this.llamalend.contracts[this.addresses.vault].contract.previewRedeem(_amount);
|
|
535
537
|
|
|
536
538
|
return formatUnits(_assets, this.borrowed_token.decimals);
|
|
537
539
|
}
|
|
538
540
|
|
|
539
541
|
private async _vaultRedeem(amount: TAmount, estimateGas = false): Promise<string | TGas> {
|
|
540
542
|
const _amount = parseUnits(amount, 18);
|
|
541
|
-
const gas = await llamalend.contracts[this.addresses.vault].contract.redeem.estimateGas(_amount, { ...llamalend.constantOptions });
|
|
543
|
+
const gas = await this.llamalend.contracts[this.addresses.vault].contract.redeem.estimateGas(_amount, { ...this.llamalend.constantOptions });
|
|
542
544
|
if (estimateGas) return smartNumber(gas);
|
|
543
545
|
|
|
544
|
-
await llamalend.updateFeeData();
|
|
546
|
+
await this.llamalend.updateFeeData();
|
|
545
547
|
|
|
546
548
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
547
549
|
|
|
548
|
-
return (await llamalend.contracts[this.addresses.vault].contract.redeem(_amount, { ...llamalend.options, gasLimit })).hash;
|
|
550
|
+
return (await this.llamalend.contracts[this.addresses.vault].contract.redeem(_amount, { ...this.llamalend.options, gasLimit })).hash;
|
|
549
551
|
}
|
|
550
552
|
|
|
551
553
|
private async vaultRedeemEstimateGas(amount: TAmount): Promise<TGas> {
|
|
@@ -560,93 +562,93 @@ export class LendMarketTemplate {
|
|
|
560
562
|
|
|
561
563
|
private async vaultConvertToShares(assets: TAmount): Promise<string> {
|
|
562
564
|
const _assets = parseUnits(assets, this.borrowed_token.decimals);
|
|
563
|
-
const _shares = await llamalend.contracts[this.addresses.vault].contract.convertToShares(_assets);
|
|
565
|
+
const _shares = await this.llamalend.contracts[this.addresses.vault].contract.convertToShares(_assets);
|
|
564
566
|
|
|
565
|
-
return llamalend.formatUnits(_shares);
|
|
567
|
+
return this.llamalend.formatUnits(_shares);
|
|
566
568
|
}
|
|
567
569
|
|
|
568
570
|
private async vaultConvertToAssets(shares: TAmount): Promise<string> {
|
|
569
571
|
const _shares = parseUnits(shares);
|
|
570
|
-
const _assets = await llamalend.contracts[this.addresses.vault].contract.convertToAssets(_shares);
|
|
572
|
+
const _assets = await this.llamalend.contracts[this.addresses.vault].contract.convertToAssets(_shares);
|
|
571
573
|
|
|
572
|
-
return llamalend.formatUnits(_assets, this.borrowed_token.decimals);
|
|
574
|
+
return this.llamalend.formatUnits(_assets, this.borrowed_token.decimals);
|
|
573
575
|
}
|
|
574
576
|
|
|
575
577
|
// ---------------- VAULT STAKING ----------------
|
|
576
578
|
|
|
577
579
|
private async vaultStakeIsApproved(vaultShares: number | string): Promise<boolean> {
|
|
578
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
580
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
579
581
|
throw Error(`stakeIsApproved method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
580
582
|
}
|
|
581
|
-
return await hasAllowance([this.addresses.vault], [vaultShares], llamalend.signerAddress, this.addresses.gauge);
|
|
583
|
+
return await hasAllowance.call(this.llamalend, [this.addresses.vault], [vaultShares], this.llamalend.signerAddress, this.addresses.gauge);
|
|
582
584
|
}
|
|
583
585
|
|
|
584
586
|
private async vaultStakeApproveEstimateGas(vaultShares: number | string): Promise<TGas> {
|
|
585
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
587
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
586
588
|
throw Error(`stakeApproveEstimateGas method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
587
589
|
}
|
|
588
|
-
return await ensureAllowanceEstimateGas([this.addresses.vault], [vaultShares], this.addresses.gauge);
|
|
590
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.addresses.vault], [vaultShares], this.addresses.gauge);
|
|
589
591
|
}
|
|
590
592
|
|
|
591
593
|
private async vaultStakeApprove(vaultShares: number | string): Promise<string[]> {
|
|
592
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
594
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
593
595
|
throw Error(`stakeApprove method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
594
596
|
}
|
|
595
|
-
return await ensureAllowance([this.addresses.vault], [vaultShares], this.addresses.gauge);
|
|
597
|
+
return await ensureAllowance.call(this.llamalend, [this.addresses.vault], [vaultShares], this.addresses.gauge);
|
|
596
598
|
}
|
|
597
599
|
|
|
598
600
|
private async vaultStakeEstimateGas(vaultShares: number | string): Promise<TGas> {
|
|
599
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
601
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
600
602
|
throw Error(`stakeEstimateGas method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
601
603
|
}
|
|
602
604
|
const _vaultShares = parseUnits(vaultShares);
|
|
603
|
-
return smartNumber(await llamalend.contracts[this.addresses.gauge].contract.deposit.estimateGas(_vaultShares, llamalend.constantOptions));
|
|
605
|
+
return smartNumber(await this.llamalend.contracts[this.addresses.gauge].contract.deposit.estimateGas(_vaultShares, this.llamalend.constantOptions));
|
|
604
606
|
}
|
|
605
607
|
|
|
606
608
|
private async vaultStake(vaultShares: number | string): Promise<string> {
|
|
607
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
609
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
608
610
|
throw Error(`stake method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
609
611
|
}
|
|
610
612
|
const _vaultShares = parseUnits(vaultShares);
|
|
611
|
-
await _ensureAllowance([this.addresses.vault], [_vaultShares], this.addresses.gauge)
|
|
613
|
+
await _ensureAllowance.call(this.llamalend, [this.addresses.vault], [_vaultShares], this.addresses.gauge)
|
|
612
614
|
|
|
613
|
-
await llamalend.updateFeeData();
|
|
614
|
-
const gasLimit = _mulBy1_3(DIGas(await llamalend.contracts[this.addresses.gauge].contract.deposit.estimateGas(_vaultShares, llamalend.constantOptions)));
|
|
615
|
-
return (await llamalend.contracts[this.addresses.gauge].contract.deposit(_vaultShares, { ...llamalend.options, gasLimit })).hash;
|
|
615
|
+
await this.llamalend.updateFeeData();
|
|
616
|
+
const gasLimit = _mulBy1_3(DIGas(await this.llamalend.contracts[this.addresses.gauge].contract.deposit.estimateGas(_vaultShares, this.llamalend.constantOptions)));
|
|
617
|
+
return (await this.llamalend.contracts[this.addresses.gauge].contract.deposit(_vaultShares, { ...this.llamalend.options, gasLimit })).hash;
|
|
616
618
|
}
|
|
617
619
|
|
|
618
620
|
private async vaultUnstakeEstimateGas(vaultShares: number | string): Promise<TGas> {
|
|
619
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
621
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
620
622
|
throw Error(`unstakeEstimateGas method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
621
623
|
}
|
|
622
624
|
const _vaultShares = parseUnits(vaultShares);
|
|
623
|
-
return smartNumber(await llamalend.contracts[this.addresses.gauge].contract.withdraw.estimateGas(_vaultShares, llamalend.constantOptions));
|
|
625
|
+
return smartNumber(await this.llamalend.contracts[this.addresses.gauge].contract.withdraw.estimateGas(_vaultShares, this.llamalend.constantOptions));
|
|
624
626
|
}
|
|
625
627
|
|
|
626
628
|
private async vaultUnstake(vaultShares: number | string): Promise<string> {
|
|
627
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
629
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
628
630
|
throw Error(`unstake method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
629
631
|
}
|
|
630
632
|
const _vaultShares = parseUnits(vaultShares);
|
|
631
633
|
|
|
632
|
-
await llamalend.updateFeeData();
|
|
633
|
-
const gasLimit = _mulBy1_3(DIGas((await llamalend.contracts[this.addresses.gauge].contract.withdraw.estimateGas(_vaultShares, llamalend.constantOptions))));
|
|
634
|
-
return (await llamalend.contracts[this.addresses.gauge].contract.withdraw(_vaultShares, { ...llamalend.options, gasLimit })).hash;
|
|
634
|
+
await this.llamalend.updateFeeData();
|
|
635
|
+
const gasLimit = _mulBy1_3(DIGas((await this.llamalend.contracts[this.addresses.gauge].contract.withdraw.estimateGas(_vaultShares, this.llamalend.constantOptions))));
|
|
636
|
+
return (await this.llamalend.contracts[this.addresses.gauge].contract.withdraw(_vaultShares, { ...this.llamalend.options, gasLimit })).hash;
|
|
635
637
|
}
|
|
636
638
|
|
|
637
639
|
// ---------------- VAULT STAKING REWARDS ----------------
|
|
638
640
|
|
|
639
641
|
private vaultRewardsOnly(): boolean {
|
|
640
|
-
if (llamalend.chainId === 2222 || llamalend.chainId === 324) return true; // TODO remove this for Kava and ZkSync
|
|
641
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) throw Error(`${this.name} doesn't have gauge`);
|
|
642
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].contract;
|
|
642
|
+
if (this.llamalend.chainId === 2222 || this.llamalend.chainId === 324) return true; // TODO remove this for Kava and ZkSync
|
|
643
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) throw Error(`${this.name} doesn't have gauge`);
|
|
644
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].contract;
|
|
643
645
|
|
|
644
646
|
return !('inflation_rate()' in gaugeContract || 'inflation_rate(uint256)' in gaugeContract);
|
|
645
647
|
}
|
|
646
648
|
|
|
647
649
|
private async vaultTotalLiquidity(useAPI = true): Promise<string> {
|
|
648
650
|
const { cap } = await this.statsCapAndAvailable(true, useAPI);
|
|
649
|
-
const price = await _getUsdRate(this.addresses.borrowed_token);
|
|
651
|
+
const price = await _getUsdRate.call(this.llamalend, this.addresses.borrowed_token);
|
|
650
652
|
|
|
651
653
|
return BN(cap).times(price).toFixed(6)
|
|
652
654
|
}
|
|
@@ -656,28 +658,28 @@ export class LendMarketTemplate {
|
|
|
656
658
|
if (Number(totalLiquidityUSD) === 0) return [0, 0];
|
|
657
659
|
|
|
658
660
|
let inflationRateBN, workingSupplyBN, totalSupplyBN;
|
|
659
|
-
if (llamalend.chainId !== 1) {
|
|
660
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
661
|
-
const lpTokenContract = llamalend.contracts[this.addresses.vault].multicallContract;
|
|
662
|
-
const crvContract = llamalend.contracts[llamalend.constants.ALIASES.crv].contract;
|
|
661
|
+
if (this.llamalend.chainId !== 1) {
|
|
662
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
663
|
+
const lpTokenContract = this.llamalend.contracts[this.addresses.vault].multicallContract;
|
|
664
|
+
const crvContract = this.llamalend.contracts[this.llamalend.constants.ALIASES.crv].contract;
|
|
663
665
|
|
|
664
666
|
const currentWeek = Math.floor(Date.now() / 1000 / WEEK);
|
|
665
|
-
[inflationRateBN, workingSupplyBN, totalSupplyBN] = (await llamalend.multicallProvider.all([
|
|
667
|
+
[inflationRateBN, workingSupplyBN, totalSupplyBN] = (await this.llamalend.multicallProvider.all([
|
|
666
668
|
gaugeContract.inflation_rate(currentWeek),
|
|
667
669
|
gaugeContract.working_supply(),
|
|
668
670
|
lpTokenContract.totalSupply(),
|
|
669
671
|
]) as bigint[]).map((value) => toBN(value));
|
|
670
672
|
|
|
671
673
|
if (inflationRateBN.eq(0)) {
|
|
672
|
-
inflationRateBN = toBN(await crvContract.balanceOf(this.addresses.gauge, llamalend.constantOptions)).div(WEEK);
|
|
674
|
+
inflationRateBN = toBN(await crvContract.balanceOf(this.addresses.gauge, this.llamalend.constantOptions)).div(WEEK);
|
|
673
675
|
}
|
|
674
676
|
} else {
|
|
675
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
676
|
-
const lpTokenContract = llamalend.contracts[this.addresses.vault].multicallContract;
|
|
677
|
-
const gaugeControllerContract = llamalend.contracts[llamalend.constants.ALIASES.gauge_controller].multicallContract;
|
|
677
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
678
|
+
const lpTokenContract = this.llamalend.contracts[this.addresses.vault].multicallContract;
|
|
679
|
+
const gaugeControllerContract = this.llamalend.contracts[this.llamalend.constants.ALIASES.gauge_controller].multicallContract;
|
|
678
680
|
|
|
679
681
|
let weightBN;
|
|
680
|
-
[inflationRateBN, weightBN, workingSupplyBN, totalSupplyBN] = (await llamalend.multicallProvider.all([
|
|
682
|
+
[inflationRateBN, weightBN, workingSupplyBN, totalSupplyBN] = (await this.llamalend.multicallProvider.all([
|
|
681
683
|
gaugeContract.inflation_rate(),
|
|
682
684
|
gaugeControllerContract.gauge_relative_weight(this.addresses.gauge),
|
|
683
685
|
gaugeContract.working_supply(),
|
|
@@ -693,7 +695,7 @@ export class LendMarketTemplate {
|
|
|
693
695
|
// If you added 1$ value of LP it would be 0.4$ of working LP. So your annual reward per 1$ in USD is:
|
|
694
696
|
// (annual reward per working liquidity in $) * (0.4$ of working LP)
|
|
695
697
|
const rateBN = inflationRateBN.times(31536000).div(workingSupplyBN).times(totalSupplyBN).div(Number(totalLiquidityUSD)).times(0.4);
|
|
696
|
-
const crvPrice = await _getUsdRate(llamalend.constants.ALIASES.crv);
|
|
698
|
+
const crvPrice = await _getUsdRate.call(this.llamalend, this.llamalend.constants.ALIASES.crv);
|
|
697
699
|
const baseApyBN = rateBN.times(crvPrice);
|
|
698
700
|
const boostedApyBN = baseApyBN.times(2.5);
|
|
699
701
|
|
|
@@ -703,7 +705,7 @@ export class LendMarketTemplate {
|
|
|
703
705
|
private async vaultCrvApr(): Promise<[baseApy: number, boostedApy: number]> {
|
|
704
706
|
if (this.vaultRewardsOnly()) throw Error(`${this.name} has Rewards-Only Gauge. Use stats.rewardsApy instead`);
|
|
705
707
|
|
|
706
|
-
// const isDisabledChain = [1313161554].includes(llamalend.chainId); // Disable Aurora
|
|
708
|
+
// const isDisabledChain = [1313161554].includes(this.llamalend.chainId); // Disable Aurora
|
|
707
709
|
// if (useApi && !isDisabledChain) {
|
|
708
710
|
// const crvAPYs = await _getCrvApyFromApi();
|
|
709
711
|
// const poolCrvApy = crvAPYs[this.addresses.gauge] ?? [0, 0]; // new pools might be missing
|
|
@@ -715,10 +717,10 @@ export class LendMarketTemplate {
|
|
|
715
717
|
|
|
716
718
|
private async vaultClaimableCrv (address = ""): Promise<string> {
|
|
717
719
|
if (this.vaultRewardsOnly()) throw Error(`${this.name} has Rewards-Only Gauge. Use claimableRewards instead`);
|
|
718
|
-
address = address || llamalend.signerAddress;
|
|
720
|
+
address = address || this.llamalend.signerAddress;
|
|
719
721
|
if (!address) throw Error("Need to connect wallet or pass address into args");
|
|
720
722
|
|
|
721
|
-
return llamalend.formatUnits(await llamalend.contracts[this.addresses.gauge].contract.claimable_tokens(address, llamalend.constantOptions));
|
|
723
|
+
return this.llamalend.formatUnits(await this.llamalend.contracts[this.addresses.gauge].contract.claimable_tokens(address, this.llamalend.constantOptions));
|
|
722
724
|
}
|
|
723
725
|
|
|
724
726
|
private async _vaultClaimCrv(estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -727,10 +729,10 @@ export class LendMarketTemplate {
|
|
|
727
729
|
let isOldFactory = false;
|
|
728
730
|
let contract;
|
|
729
731
|
|
|
730
|
-
if (llamalend.chainId !== 1) {
|
|
731
|
-
if (llamalend.constants.ALIASES.gauge_factory_old && llamalend.constants.ALIASES.gauge_factory_old !== llamalend.constants.ZERO_ADDRESS) {
|
|
732
|
-
const oldFactoryContract = llamalend.contracts[llamalend.constants.ALIASES.gauge_factory_old].contract;
|
|
733
|
-
const lpToken = await llamalend.contracts[this.addresses.gauge].contract.lp_token();
|
|
732
|
+
if (this.llamalend.chainId !== 1) {
|
|
733
|
+
if (this.llamalend.constants.ALIASES.gauge_factory_old && this.llamalend.constants.ALIASES.gauge_factory_old !== this.llamalend.constants.ZERO_ADDRESS) {
|
|
734
|
+
const oldFactoryContract = this.llamalend.contracts[this.llamalend.constants.ALIASES.gauge_factory_old].contract;
|
|
735
|
+
const lpToken = await this.llamalend.contracts[this.addresses.gauge].contract.lp_token();
|
|
734
736
|
const gaugeAddress = await oldFactoryContract.get_gauge_from_lp_token(lpToken);
|
|
735
737
|
|
|
736
738
|
isOldFactory = gaugeAddress.toLowerCase() === this.addresses.gauge.toLowerCase();
|
|
@@ -742,19 +744,19 @@ export class LendMarketTemplate {
|
|
|
742
744
|
}
|
|
743
745
|
|
|
744
746
|
if (!isOldFactory) {
|
|
745
|
-
contract = llamalend.contracts[llamalend.constants.ALIASES.minter].contract
|
|
747
|
+
contract = this.llamalend.contracts[this.llamalend.constants.ALIASES.minter].contract
|
|
746
748
|
}
|
|
747
749
|
|
|
748
750
|
if(!contract) {
|
|
749
751
|
throw Error(`${this.name} couldn't match gauge factory`);
|
|
750
752
|
}
|
|
751
753
|
|
|
752
|
-
const gas = await contract.mint.estimateGas(this.addresses.gauge, llamalend.constantOptions);
|
|
754
|
+
const gas = await contract.mint.estimateGas(this.addresses.gauge, this.llamalend.constantOptions);
|
|
753
755
|
if (estimateGas) return smartNumber(gas);
|
|
754
756
|
|
|
755
|
-
await llamalend.updateFeeData();
|
|
757
|
+
await this.llamalend.updateFeeData();
|
|
756
758
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
757
|
-
return (await contract.mint(this.addresses.gauge, { ...llamalend.options, gasLimit })).hash
|
|
759
|
+
return (await contract.mint(this.addresses.gauge, { ...this.llamalend.options, gasLimit })).hash
|
|
758
760
|
}
|
|
759
761
|
|
|
760
762
|
private async vaultClaimCrvEstimateGas(): Promise<TGas> {
|
|
@@ -766,7 +768,7 @@ export class LendMarketTemplate {
|
|
|
766
768
|
}
|
|
767
769
|
|
|
768
770
|
private vaultRewardTokens = memoize(async (): Promise<{token: string, symbol: string, decimals: number}[]> => {
|
|
769
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) return []
|
|
771
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) return []
|
|
770
772
|
|
|
771
773
|
// if (useApi) {
|
|
772
774
|
// const rewards = await _getRewardsFromApi();
|
|
@@ -775,28 +777,28 @@ export class LendMarketTemplate {
|
|
|
775
777
|
// return rewards[this.addresses.gauge].map((r) => ({ token: r.tokenAddress, symbol: r.symbol, decimals: Number(r.decimals) }));
|
|
776
778
|
// }
|
|
777
779
|
|
|
778
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].contract;
|
|
779
|
-
const gaugeMulticallContract = llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
780
|
-
const rewardCount = Number(llamalend.formatUnits(await gaugeContract.reward_count(llamalend.constantOptions), 0));
|
|
780
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].contract;
|
|
781
|
+
const gaugeMulticallContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
782
|
+
const rewardCount = Number(this.llamalend.formatUnits(await gaugeContract.reward_count(this.llamalend.constantOptions), 0));
|
|
781
783
|
|
|
782
784
|
const tokenCalls = [];
|
|
783
785
|
for (let i = 0; i < rewardCount; i++) {
|
|
784
786
|
tokenCalls.push(gaugeMulticallContract.reward_tokens(i));
|
|
785
787
|
}
|
|
786
|
-
const tokens = (await llamalend.multicallProvider.all(tokenCalls) as string[])
|
|
787
|
-
.filter((addr) => addr !== llamalend.constants.ZERO_ADDRESS)
|
|
788
|
+
const tokens = (await this.llamalend.multicallProvider.all(tokenCalls) as string[])
|
|
789
|
+
.filter((addr) => addr !== this.llamalend.constants.ZERO_ADDRESS)
|
|
788
790
|
.map((addr) => addr.toLowerCase())
|
|
789
|
-
.filter((addr) => llamalend.chainId === 1 || addr !== llamalend.constants.COINS.crv);
|
|
791
|
+
.filter((addr) => this.llamalend.chainId === 1 || addr !== this.llamalend.constants.COINS.crv);
|
|
790
792
|
|
|
791
793
|
const tokenInfoCalls = [];
|
|
792
794
|
for (const token of tokens) {
|
|
793
|
-
llamalend.setContract(token, ERC20Abi);
|
|
794
|
-
const tokenMulticallContract = llamalend.contracts[token].multicallContract;
|
|
795
|
+
this.llamalend.setContract(token, ERC20Abi);
|
|
796
|
+
const tokenMulticallContract = this.llamalend.contracts[token].multicallContract;
|
|
795
797
|
tokenInfoCalls.push(tokenMulticallContract.symbol(), tokenMulticallContract.decimals());
|
|
796
798
|
}
|
|
797
|
-
const tokenInfo = await llamalend.multicallProvider.all(tokenInfoCalls);
|
|
799
|
+
const tokenInfo = await this.llamalend.multicallProvider.all(tokenInfoCalls);
|
|
798
800
|
for (let i = 0; i < tokens.length; i++) {
|
|
799
|
-
llamalend.constants.DECIMALS[tokens[i]] = Number(tokenInfo[(i * 2) + 1]);
|
|
801
|
+
this.llamalend.constants.DECIMALS[tokens[i]] = Number(tokenInfo[(i * 2) + 1]);
|
|
800
802
|
}
|
|
801
803
|
|
|
802
804
|
return tokens.map((token, i) => ({ token, symbol: tokenInfo[i * 2] as string, decimals: Number(tokenInfo[(i * 2) + 1]) }));
|
|
@@ -808,7 +810,7 @@ export class LendMarketTemplate {
|
|
|
808
810
|
|
|
809
811
|
private vaultRewardsApr = async (useApi = true): Promise<IReward[]> => {
|
|
810
812
|
if(useApi) {
|
|
811
|
-
const response = await _getMarketsData(llamalend.constants.NETWORK_NAME);
|
|
813
|
+
const response = await _getMarketsData(this.llamalend.constants.NETWORK_NAME);
|
|
812
814
|
|
|
813
815
|
const market = response.lendingVaultData.find((item) => item.address.toLowerCase() === this.addresses.vault.toLowerCase())
|
|
814
816
|
|
|
@@ -818,9 +820,9 @@ export class LendMarketTemplate {
|
|
|
818
820
|
throw new Error('Market not found in API')
|
|
819
821
|
}
|
|
820
822
|
} else {
|
|
821
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) return [];
|
|
823
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) return [];
|
|
822
824
|
|
|
823
|
-
// const isDisabledChain = [1313161554].includes(llamalend.chainId); // Disable Aurora
|
|
825
|
+
// const isDisabledChain = [1313161554].includes(this.llamalend.chainId); // Disable Aurora
|
|
824
826
|
// if (useApi && !isDisabledChain) {
|
|
825
827
|
// const rewards = await _getRewardsFromApi();
|
|
826
828
|
// if (!rewards[this.addresses.gauge]) return [];
|
|
@@ -830,14 +832,14 @@ export class LendMarketTemplate {
|
|
|
830
832
|
const apy: IReward[] = [];
|
|
831
833
|
const rewardTokens = await this.vaultRewardTokens();
|
|
832
834
|
for (const rewardToken of rewardTokens) {
|
|
833
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
834
|
-
const lpTokenContract = llamalend.contracts[this.addresses.vault].multicallContract;
|
|
835
|
-
const rewardContract = llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
835
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
836
|
+
const lpTokenContract = this.llamalend.contracts[this.addresses.vault].multicallContract;
|
|
837
|
+
const rewardContract = this.llamalend.contracts[this.addresses.gauge].multicallContract;
|
|
836
838
|
|
|
837
839
|
const totalLiquidityUSD = await this.vaultTotalLiquidity();
|
|
838
|
-
const rewardRate = await _getUsdRate(rewardToken.token);
|
|
840
|
+
const rewardRate = await _getUsdRate.call(this.llamalend, rewardToken.token);
|
|
839
841
|
|
|
840
|
-
const [rewardData, _stakedSupply, _totalSupply] = (await llamalend.multicallProvider.all([
|
|
842
|
+
const [rewardData, _stakedSupply, _totalSupply] = (await this.llamalend.multicallProvider.all([
|
|
841
843
|
rewardContract.reward_data(rewardToken.token),
|
|
842
844
|
gaugeContract.totalSupply(),
|
|
843
845
|
lpTokenContract.totalSupply(),
|
|
@@ -845,7 +847,7 @@ export class LendMarketTemplate {
|
|
|
845
847
|
const stakedSupplyBN = toBN(_stakedSupply as bigint);
|
|
846
848
|
const totalSupplyBN = toBN(_totalSupply as bigint);
|
|
847
849
|
const inflationBN = toBN(rewardData.rate, rewardToken.decimals);
|
|
848
|
-
const periodFinish = Number(llamalend.formatUnits(rewardData.period_finish, 0)) * 1000;
|
|
850
|
+
const periodFinish = Number(this.llamalend.formatUnits(rewardData.period_finish, 0)) * 1000;
|
|
849
851
|
const baseApy = periodFinish > Date.now() ?
|
|
850
852
|
inflationBN.times(31536000).times(rewardRate).div(stakedSupplyBN).times(totalSupplyBN).div(Number(totalLiquidityUSD)) :
|
|
851
853
|
BN(0);
|
|
@@ -863,21 +865,21 @@ export class LendMarketTemplate {
|
|
|
863
865
|
}
|
|
864
866
|
|
|
865
867
|
private async vaultClaimableRewards(address = ""): Promise<{token: string, symbol: string, amount: string}[]> {
|
|
866
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
868
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
867
869
|
throw Error(`claimableRewards method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
868
870
|
}
|
|
869
|
-
address = address || llamalend.signerAddress;
|
|
871
|
+
address = address || this.llamalend.signerAddress;
|
|
870
872
|
if (!address) throw Error("Need to connect wallet or pass address into args");
|
|
871
873
|
|
|
872
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].contract;
|
|
874
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].contract;
|
|
873
875
|
const rewardTokens = await this.vaultRewardTokens();
|
|
874
876
|
const rewards = [];
|
|
875
877
|
for (const rewardToken of rewardTokens) {
|
|
876
|
-
const _amount = await gaugeContract.claimable_reward(address, rewardToken.token, llamalend.constantOptions);
|
|
878
|
+
const _amount = await gaugeContract.claimable_reward(address, rewardToken.token, this.llamalend.constantOptions);
|
|
877
879
|
rewards.push({
|
|
878
880
|
token: rewardToken.token,
|
|
879
881
|
symbol: rewardToken.symbol,
|
|
880
|
-
amount: llamalend.formatUnits(_amount, rewardToken.decimals),
|
|
882
|
+
amount: this.llamalend.formatUnits(_amount, rewardToken.decimals),
|
|
881
883
|
});
|
|
882
884
|
}
|
|
883
885
|
|
|
@@ -885,17 +887,17 @@ export class LendMarketTemplate {
|
|
|
885
887
|
}
|
|
886
888
|
|
|
887
889
|
private async _vaultClaimRewards(estimateGas: boolean): Promise<string | TGas> {
|
|
888
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
890
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
889
891
|
throw Error(`claimRewards method doesn't exist for pool ${this.name} (id: ${this.name}). There is no gauge`);
|
|
890
892
|
}
|
|
891
|
-
const gaugeContract = llamalend.contracts[this.addresses.gauge].contract;
|
|
893
|
+
const gaugeContract = this.llamalend.contracts[this.addresses.gauge].contract;
|
|
892
894
|
if (!("claim_rewards()" in gaugeContract)) throw Error (`${this.name} pool doesn't have such method`);
|
|
893
|
-
const gas = await gaugeContract.claim_rewards.estimateGas(llamalend.constantOptions);
|
|
895
|
+
const gas = await gaugeContract.claim_rewards.estimateGas(this.llamalend.constantOptions);
|
|
894
896
|
if (estimateGas) return smartNumber(gas);
|
|
895
897
|
|
|
896
|
-
await llamalend.updateFeeData();
|
|
898
|
+
await this.llamalend.updateFeeData();
|
|
897
899
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
898
|
-
return (await gaugeContract.claim_rewards({ ...llamalend.options, gasLimit })).hash;
|
|
900
|
+
return (await gaugeContract.claim_rewards({ ...this.llamalend.options, gasLimit })).hash;
|
|
899
901
|
}
|
|
900
902
|
|
|
901
903
|
private async vaultClaimRewardsEstimateGas(): Promise<TGas> {
|
|
@@ -916,8 +918,8 @@ export class LendMarketTemplate {
|
|
|
916
918
|
base_price: string,
|
|
917
919
|
A: string,
|
|
918
920
|
}> => {
|
|
919
|
-
const llammaContract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
920
|
-
const controllerContract = llamalend.contracts[this.addresses.controller].multicallContract;
|
|
921
|
+
const llammaContract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
922
|
+
const controllerContract = this.llamalend.contracts[this.addresses.controller].multicallContract;
|
|
921
923
|
|
|
922
924
|
const calls = [
|
|
923
925
|
llammaContract.fee(),
|
|
@@ -928,7 +930,7 @@ export class LendMarketTemplate {
|
|
|
928
930
|
llammaContract.A(),
|
|
929
931
|
]
|
|
930
932
|
|
|
931
|
-
const [_fee, _admin_fee, _liquidation_discount, _loan_discount, _base_price, _A]: bigint[] = await llamalend.multicallProvider.all(calls) as bigint[];
|
|
933
|
+
const [_fee, _admin_fee, _liquidation_discount, _loan_discount, _base_price, _A]: bigint[] = await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
932
934
|
const A = formatUnits(_A, 0)
|
|
933
935
|
const base_price = formatUnits(_base_price)
|
|
934
936
|
const [fee, admin_fee, liquidation_discount, loan_discount] = [_fee, _admin_fee, _liquidation_discount, _loan_discount]
|
|
@@ -946,20 +948,20 @@ export class LendMarketTemplate {
|
|
|
946
948
|
if(isGetter) {
|
|
947
949
|
_rate = cacheStats.get(cacheKey(this.addresses.amm, 'rate'));
|
|
948
950
|
} else {
|
|
949
|
-
_rate = await llamalend.contracts[this.addresses.amm].contract.rate(llamalend.constantOptions);
|
|
951
|
+
_rate = await this.llamalend.contracts[this.addresses.amm].contract.rate(this.llamalend.constantOptions);
|
|
950
952
|
cacheStats.set(cacheKey(this.addresses.controller, 'rate'), _rate);
|
|
951
953
|
}
|
|
952
954
|
return _rate;
|
|
953
955
|
}
|
|
954
956
|
|
|
955
957
|
private _getFutureRate = async (_dReserves: bigint, _dDebt: bigint): Promise<bigint> => {
|
|
956
|
-
const mpContract = llamalend.contracts[this.addresses.monetary_policy].contract;
|
|
958
|
+
const mpContract = this.llamalend.contracts[this.addresses.monetary_policy].contract;
|
|
957
959
|
return await mpContract.future_rate(this.addresses.controller, _dReserves, _dDebt);
|
|
958
960
|
}
|
|
959
961
|
|
|
960
962
|
private async statsRates(isGetter = true, useAPI = false): Promise<{borrowApr: string, lendApr: string, borrowApy: string, lendApy: string}> {
|
|
961
963
|
if(useAPI) {
|
|
962
|
-
const response = await _getMarketsData(llamalend.constants.NETWORK_NAME);
|
|
964
|
+
const response = await _getMarketsData(this.llamalend.constants.NETWORK_NAME);
|
|
963
965
|
|
|
964
966
|
const market = response.lendingVaultData.find((item) => item.address.toLowerCase() === this.addresses.vault.toLowerCase())
|
|
965
967
|
|
|
@@ -1015,16 +1017,16 @@ export class LendMarketTemplate {
|
|
|
1015
1017
|
}
|
|
1016
1018
|
|
|
1017
1019
|
private async statsBalances(): Promise<[string, string]> {
|
|
1018
|
-
const borrowedContract = llamalend.contracts[this.borrowed_token.address].multicallContract;
|
|
1019
|
-
const collateralContract = llamalend.contracts[this.collateral_token.address].multicallContract;
|
|
1020
|
-
const ammContract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1020
|
+
const borrowedContract = this.llamalend.contracts[this.borrowed_token.address].multicallContract;
|
|
1021
|
+
const collateralContract = this.llamalend.contracts[this.collateral_token.address].multicallContract;
|
|
1022
|
+
const ammContract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1021
1023
|
const calls = [
|
|
1022
1024
|
borrowedContract.balanceOf(this.addresses.amm),
|
|
1023
1025
|
collateralContract.balanceOf(this.addresses.amm),
|
|
1024
1026
|
ammContract.admin_fees_x(),
|
|
1025
1027
|
ammContract.admin_fees_y(),
|
|
1026
1028
|
]
|
|
1027
|
-
const [_borrowedBalance, _collateralBalance, _borrowedAdminFees, _collateralAdminFees]: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
1029
|
+
const [_borrowedBalance, _collateralBalance, _borrowedAdminFees, _collateralAdminFees]: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
1028
1030
|
|
|
1029
1031
|
return [
|
|
1030
1032
|
formatUnits(_borrowedBalance - _borrowedAdminFees, this.borrowed_token.decimals),
|
|
@@ -1033,14 +1035,14 @@ export class LendMarketTemplate {
|
|
|
1033
1035
|
}
|
|
1034
1036
|
|
|
1035
1037
|
private statsBandsInfo = memoize(async (): Promise<{ activeBand: number, maxBand: number, minBand: number, liquidationBand: number | null }> => {
|
|
1036
|
-
const ammContract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1038
|
+
const ammContract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1037
1039
|
const calls = [
|
|
1038
1040
|
ammContract.active_band_with_skip(),
|
|
1039
1041
|
ammContract.max_band(),
|
|
1040
1042
|
ammContract.min_band(),
|
|
1041
1043
|
]
|
|
1042
1044
|
|
|
1043
|
-
const [activeBand, maxBand, minBand] = (await llamalend.multicallProvider.all(calls) as bigint[]).map((_b) => Number(_b));
|
|
1045
|
+
const [activeBand, maxBand, minBand] = (await this.llamalend.multicallProvider.all(calls) as bigint[]).map((_b) => Number(_b));
|
|
1044
1046
|
const { borrowed, collateral } = await this.statsBandBalances(activeBand);
|
|
1045
1047
|
let liquidationBand = null;
|
|
1046
1048
|
if (Number(borrowed) > 0 && Number(collateral) > 0) liquidationBand = activeBand;
|
|
@@ -1052,10 +1054,10 @@ export class LendMarketTemplate {
|
|
|
1052
1054
|
});
|
|
1053
1055
|
|
|
1054
1056
|
private async statsBandBalances(n: number): Promise<{ borrowed: string, collateral: string }> {
|
|
1055
|
-
const ammContract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1057
|
+
const ammContract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1056
1058
|
const calls = [];
|
|
1057
1059
|
calls.push(ammContract.bands_x(n), ammContract.bands_y(n));
|
|
1058
|
-
const _balances: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
1060
|
+
const _balances: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
1059
1061
|
|
|
1060
1062
|
// bands_x and bands_y always return amounts with 18 decimals
|
|
1061
1063
|
return {
|
|
@@ -1067,13 +1069,13 @@ export class LendMarketTemplate {
|
|
|
1067
1069
|
private async statsBandsBalances(): Promise<{ [index: number]: { borrowed: string, collateral: string } }> {
|
|
1068
1070
|
const { maxBand, minBand } = await this.statsBandsInfo();
|
|
1069
1071
|
|
|
1070
|
-
const ammContract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1072
|
+
const ammContract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1071
1073
|
const calls = [];
|
|
1072
1074
|
for (let i = minBand; i <= maxBand; i++) {
|
|
1073
1075
|
calls.push(ammContract.bands_x(i), ammContract.bands_y(i));
|
|
1074
1076
|
}
|
|
1075
1077
|
|
|
1076
|
-
const _bands: bigint[] = await llamalend.multicallProvider.all(calls);
|
|
1078
|
+
const _bands: bigint[] = await this.llamalend.multicallProvider.all(calls);
|
|
1077
1079
|
|
|
1078
1080
|
const bands: { [index: number]: { borrowed: string, collateral: string } } = {};
|
|
1079
1081
|
for (let i = minBand; i <= maxBand; i++) {
|
|
@@ -1090,7 +1092,7 @@ export class LendMarketTemplate {
|
|
|
1090
1092
|
|
|
1091
1093
|
private async statsTotalDebt(isGetter = true, useAPI = true): Promise<string> {
|
|
1092
1094
|
if(useAPI) {
|
|
1093
|
-
const response = await _getMarketsData(llamalend.constants.NETWORK_NAME);
|
|
1095
|
+
const response = await _getMarketsData(this.llamalend.constants.NETWORK_NAME);
|
|
1094
1096
|
|
|
1095
1097
|
const market = response.lendingVaultData.find((item) => item.address.toLowerCase() === this.addresses.vault.toLowerCase())
|
|
1096
1098
|
|
|
@@ -1104,7 +1106,7 @@ export class LendMarketTemplate {
|
|
|
1104
1106
|
if(isGetter) {
|
|
1105
1107
|
_debt = cacheStats.get(cacheKey(this.addresses.controller, 'total_debt'));
|
|
1106
1108
|
} else {
|
|
1107
|
-
_debt = await llamalend.contracts[this.addresses.controller].contract.total_debt(llamalend.constantOptions);
|
|
1109
|
+
_debt = await this.llamalend.contracts[this.addresses.controller].contract.total_debt(this.llamalend.constantOptions);
|
|
1108
1110
|
cacheStats.set(cacheKey(this.addresses.controller, 'total_debt'), _debt);
|
|
1109
1111
|
}
|
|
1110
1112
|
|
|
@@ -1114,7 +1116,7 @@ export class LendMarketTemplate {
|
|
|
1114
1116
|
|
|
1115
1117
|
private statsAmmBalances = async (isGetter = true, useAPI = false): Promise<{ borrowed: string, collateral: string }> => {
|
|
1116
1118
|
if(useAPI) {
|
|
1117
|
-
const response = await _getMarketsData(llamalend.constants.NETWORK_NAME);
|
|
1119
|
+
const response = await _getMarketsData(this.llamalend.constants.NETWORK_NAME);
|
|
1118
1120
|
|
|
1119
1121
|
const market = response.lendingVaultData.find((item) => item.address.toLowerCase() === this.addresses.vault.toLowerCase())
|
|
1120
1122
|
|
|
@@ -1127,9 +1129,9 @@ export class LendMarketTemplate {
|
|
|
1127
1129
|
throw new Error('Market not found in API')
|
|
1128
1130
|
}
|
|
1129
1131
|
} else {
|
|
1130
|
-
const borrowedContract = llamalend.contracts[this.addresses.borrowed_token].multicallContract;
|
|
1131
|
-
const collateralContract = llamalend.contracts[this.addresses.collateral_token].multicallContract;
|
|
1132
|
-
const ammContract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1132
|
+
const borrowedContract = this.llamalend.contracts[this.addresses.borrowed_token].multicallContract;
|
|
1133
|
+
const collateralContract = this.llamalend.contracts[this.addresses.collateral_token].multicallContract;
|
|
1134
|
+
const ammContract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1133
1135
|
|
|
1134
1136
|
let _balance_x, _fee_x, _balance_y, _fee_y;
|
|
1135
1137
|
if(isGetter) {
|
|
@@ -1140,7 +1142,7 @@ export class LendMarketTemplate {
|
|
|
1140
1142
|
cacheStats.get(cacheKey(this.addresses.amm, 'admin_fees_y')),
|
|
1141
1143
|
]
|
|
1142
1144
|
} else {
|
|
1143
|
-
[_balance_x, _fee_x, _balance_y, _fee_y] = await llamalend.multicallProvider.all([
|
|
1145
|
+
[_balance_x, _fee_x, _balance_y, _fee_y] = await this.llamalend.multicallProvider.all([
|
|
1144
1146
|
borrowedContract.balanceOf(this.addresses.amm),
|
|
1145
1147
|
ammContract.admin_fees_x(),
|
|
1146
1148
|
collateralContract.balanceOf(this.addresses.amm),
|
|
@@ -1161,7 +1163,7 @@ export class LendMarketTemplate {
|
|
|
1161
1163
|
|
|
1162
1164
|
private async statsCapAndAvailable(isGetter = true, useAPI = false): Promise<{ cap: string, available: string }> {
|
|
1163
1165
|
if(useAPI) {
|
|
1164
|
-
const response = await _getMarketsData(llamalend.constants.NETWORK_NAME);
|
|
1166
|
+
const response = await _getMarketsData(this.llamalend.constants.NETWORK_NAME);
|
|
1165
1167
|
|
|
1166
1168
|
const market = response.lendingVaultData.find((item) => item.address.toLowerCase() === this.addresses.vault.toLowerCase())
|
|
1167
1169
|
|
|
@@ -1174,15 +1176,15 @@ export class LendMarketTemplate {
|
|
|
1174
1176
|
throw new Error('Market not found in API')
|
|
1175
1177
|
}
|
|
1176
1178
|
} else {
|
|
1177
|
-
const vaultContract = llamalend.contracts[this.addresses.vault].multicallContract;
|
|
1178
|
-
const borrowedContract = llamalend.contracts[this.addresses.borrowed_token].multicallContract;
|
|
1179
|
+
const vaultContract = this.llamalend.contracts[this.addresses.vault].multicallContract;
|
|
1180
|
+
const borrowedContract = this.llamalend.contracts[this.addresses.borrowed_token].multicallContract;
|
|
1179
1181
|
|
|
1180
1182
|
let _cap, _available;
|
|
1181
1183
|
if(isGetter) {
|
|
1182
1184
|
_cap = cacheStats.get(cacheKey(this.addresses.vault, 'totalAssets', this.addresses.controller));
|
|
1183
1185
|
_available = cacheStats.get(cacheKey(this.addresses.borrowed_token, 'balanceOf', this.addresses.controller));
|
|
1184
1186
|
} else {
|
|
1185
|
-
[_cap, _available] =await llamalend.multicallProvider.all([
|
|
1187
|
+
[_cap, _available] =await this.llamalend.multicallProvider.all([
|
|
1186
1188
|
vaultContract.totalAssets(this.addresses.controller),
|
|
1187
1189
|
borrowedContract.balanceOf(this.addresses.controller),
|
|
1188
1190
|
]);
|
|
@@ -1191,8 +1193,8 @@ export class LendMarketTemplate {
|
|
|
1191
1193
|
}
|
|
1192
1194
|
|
|
1193
1195
|
return {
|
|
1194
|
-
cap: llamalend.formatUnits(_cap, this.borrowed_token.decimals),
|
|
1195
|
-
available: llamalend.formatUnits(_available, this.borrowed_token.decimals),
|
|
1196
|
+
cap: this.llamalend.formatUnits(_cap, this.borrowed_token.decimals),
|
|
1197
|
+
available: this.llamalend.formatUnits(_available, this.borrowed_token.decimals),
|
|
1196
1198
|
}
|
|
1197
1199
|
}
|
|
1198
1200
|
}
|
|
@@ -1200,7 +1202,7 @@ export class LendMarketTemplate {
|
|
|
1200
1202
|
// ---------------- PRICES ----------------
|
|
1201
1203
|
|
|
1202
1204
|
public A = memoize(async(): Promise<string> => {
|
|
1203
|
-
const _A = await llamalend.contracts[this.addresses.amm].contract.A(llamalend.constantOptions) as bigint;
|
|
1205
|
+
const _A = await this.llamalend.contracts[this.addresses.amm].contract.A(this.llamalend.constantOptions) as bigint;
|
|
1204
1206
|
return formatUnits(_A, 0);
|
|
1205
1207
|
},
|
|
1206
1208
|
{
|
|
@@ -1209,7 +1211,7 @@ export class LendMarketTemplate {
|
|
|
1209
1211
|
});
|
|
1210
1212
|
|
|
1211
1213
|
public basePrice = memoize(async(): Promise<string> => {
|
|
1212
|
-
const _price = await llamalend.contracts[this.addresses.amm].contract.get_base_price(llamalend.constantOptions) as bigint;
|
|
1214
|
+
const _price = await this.llamalend.contracts[this.addresses.amm].contract.get_base_price(this.llamalend.constantOptions) as bigint;
|
|
1213
1215
|
return formatUnits(_price);
|
|
1214
1216
|
},
|
|
1215
1217
|
{
|
|
@@ -1218,7 +1220,7 @@ export class LendMarketTemplate {
|
|
|
1218
1220
|
});
|
|
1219
1221
|
|
|
1220
1222
|
public oraclePrice = memoize(async (): Promise<string> => {
|
|
1221
|
-
const _price = await llamalend.contracts[this.addresses.amm].contract.price_oracle(llamalend.constantOptions) as bigint;
|
|
1223
|
+
const _price = await this.llamalend.contracts[this.addresses.amm].contract.price_oracle(this.llamalend.constantOptions) as bigint;
|
|
1222
1224
|
return formatUnits(_price);
|
|
1223
1225
|
},
|
|
1224
1226
|
{
|
|
@@ -1247,7 +1249,7 @@ export class LendMarketTemplate {
|
|
|
1247
1249
|
}
|
|
1248
1250
|
|
|
1249
1251
|
public async price(): Promise<string> {
|
|
1250
|
-
const _price = await llamalend.contracts[this.addresses.amm].contract.get_p(llamalend.constantOptions) as bigint;
|
|
1252
|
+
const _price = await this.llamalend.contracts[this.addresses.amm].contract.get_p(this.llamalend.constantOptions) as bigint;
|
|
1251
1253
|
return formatUnits(_price);
|
|
1252
1254
|
}
|
|
1253
1255
|
|
|
@@ -1274,13 +1276,13 @@ export class LendMarketTemplate {
|
|
|
1274
1276
|
// ---------------- WALLET BALANCES ----------------
|
|
1275
1277
|
|
|
1276
1278
|
private async walletBalances(address = ""): Promise<{ collateral: string, borrowed: string, vaultShares: string, gauge: string }> {
|
|
1277
|
-
if (this.addresses.gauge === llamalend.constants.ZERO_ADDRESS) {
|
|
1279
|
+
if (this.addresses.gauge === this.llamalend.constants.ZERO_ADDRESS) {
|
|
1278
1280
|
const [collateral, borrowed, vaultShares] =
|
|
1279
|
-
await getBalances([this.collateral_token.address, this.borrowed_token.address, this.addresses.vault], address);
|
|
1281
|
+
await getBalances.call(this.llamalend, [this.collateral_token.address, this.borrowed_token.address, this.addresses.vault], address);
|
|
1280
1282
|
return { collateral, borrowed, vaultShares, gauge: "0" }
|
|
1281
1283
|
} else {
|
|
1282
1284
|
const [collateral, borrowed, vaultShares, gauge] =
|
|
1283
|
-
await getBalances([this.collateral_token.address, this.borrowed_token.address, this.addresses.vault, this.addresses.gauge], address);
|
|
1285
|
+
await getBalances.call(this.llamalend, [this.collateral_token.address, this.borrowed_token.address, this.addresses.vault, this.addresses.gauge], address);
|
|
1284
1286
|
return { collateral, borrowed, vaultShares, gauge }
|
|
1285
1287
|
}
|
|
1286
1288
|
}
|
|
@@ -1288,14 +1290,14 @@ export class LendMarketTemplate {
|
|
|
1288
1290
|
// ---------------- USER POSITION ----------------
|
|
1289
1291
|
|
|
1290
1292
|
public async userLoanExists(address = ""): Promise<boolean> {
|
|
1291
|
-
address = _getAddress(address);
|
|
1292
|
-
return await llamalend.contracts[this.addresses.controller].contract.loan_exists(address, llamalend.constantOptions);
|
|
1293
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1294
|
+
return await this.llamalend.contracts[this.addresses.controller].contract.loan_exists(address, this.llamalend.constantOptions);
|
|
1293
1295
|
}
|
|
1294
1296
|
|
|
1295
1297
|
public _userState = memoize(async (address = ""): Promise<{ _collateral: bigint, _borrowed: bigint, _debt: bigint, _N: bigint }> => {
|
|
1296
|
-
address = _getAddress(address);
|
|
1297
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1298
|
-
const [_collateral, _borrowed, _debt, _N] = await contract.user_state(address, llamalend.constantOptions) as bigint[];
|
|
1298
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1299
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1300
|
+
const [_collateral, _borrowed, _debt, _N] = await contract.user_state(address, this.llamalend.constantOptions) as bigint[];
|
|
1299
1301
|
|
|
1300
1302
|
return { _collateral, _borrowed, _debt, _N }
|
|
1301
1303
|
},
|
|
@@ -1316,16 +1318,16 @@ export class LendMarketTemplate {
|
|
|
1316
1318
|
}
|
|
1317
1319
|
|
|
1318
1320
|
public async userHealth(full = true, address = ""): Promise<string> {
|
|
1319
|
-
address = _getAddress(address);
|
|
1320
|
-
let _health = await llamalend.contracts[this.addresses.controller].contract.health(address, full, llamalend.constantOptions) as bigint;
|
|
1321
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1322
|
+
let _health = await this.llamalend.contracts[this.addresses.controller].contract.health(address, full, this.llamalend.constantOptions) as bigint;
|
|
1321
1323
|
_health = _health * BigInt(100);
|
|
1322
1324
|
|
|
1323
1325
|
return formatUnits(_health);
|
|
1324
1326
|
}
|
|
1325
1327
|
|
|
1326
1328
|
private async _userBands(address: string): Promise<bigint[]> {
|
|
1327
|
-
address = _getAddress(address);
|
|
1328
|
-
const _bands = await llamalend.contracts[this.addresses.amm].contract.read_user_tick_numbers(address, llamalend.constantOptions) as bigint[];
|
|
1329
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1330
|
+
const _bands = await this.llamalend.contracts[this.addresses.amm].contract.read_user_tick_numbers(address, this.llamalend.constantOptions) as bigint[];
|
|
1329
1331
|
|
|
1330
1332
|
return Array.from(_bands).reverse();
|
|
1331
1333
|
}
|
|
@@ -1341,22 +1343,22 @@ export class LendMarketTemplate {
|
|
|
1341
1343
|
}
|
|
1342
1344
|
|
|
1343
1345
|
public async userPrices(address = ""): Promise<string[]> {
|
|
1344
|
-
address = _getAddress(address);
|
|
1345
|
-
const _prices = await llamalend.contracts[this.addresses.controller].contract.user_prices(address, llamalend.constantOptions) as bigint[];
|
|
1346
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1347
|
+
const _prices = await this.llamalend.contracts[this.addresses.controller].contract.user_prices(address, this.llamalend.constantOptions) as bigint[];
|
|
1346
1348
|
|
|
1347
1349
|
return _prices.map((_p) => formatUnits(_p)).reverse();
|
|
1348
1350
|
}
|
|
1349
1351
|
|
|
1350
1352
|
public async userLoss(userAddress = ""): Promise<{ deposited_collateral: string, current_collateral_estimation: string, loss: string, loss_pct: string }> {
|
|
1351
|
-
userAddress = _getAddress(userAddress);
|
|
1353
|
+
userAddress = _getAddress.call(this.llamalend, userAddress);
|
|
1352
1354
|
const [userCollateral, _current_collateral_estimation] = await Promise.all([
|
|
1353
|
-
_getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
|
|
1354
|
-
llamalend.contracts[this.addresses.amm].contract.get_y_up(userAddress),
|
|
1355
|
+
_getUserCollateral(this.llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
|
|
1356
|
+
this.llamalend.contracts[this.addresses.amm].contract.get_y_up(userAddress),
|
|
1355
1357
|
]);
|
|
1356
1358
|
|
|
1357
1359
|
const deposited_collateral = userCollateral.total_deposit_precise;
|
|
1358
1360
|
|
|
1359
|
-
const current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateral_token.decimals);
|
|
1361
|
+
const current_collateral_estimation = this.llamalend.formatUnits(_current_collateral_estimation, this.collateral_token.decimals);
|
|
1360
1362
|
if (BN(deposited_collateral).lte(0)) {
|
|
1361
1363
|
return {
|
|
1362
1364
|
deposited_collateral,
|
|
@@ -1380,9 +1382,9 @@ export class LendMarketTemplate {
|
|
|
1380
1382
|
const [n2, n1] = await this.userBands(address);
|
|
1381
1383
|
if (n1 == 0 && n2 == 0) return {};
|
|
1382
1384
|
|
|
1383
|
-
address = _getAddress(address);
|
|
1384
|
-
const contract = llamalend.contracts[this.addresses.amm].contract;
|
|
1385
|
-
const [_borrowed, _collateral] = await contract.get_xy(address, llamalend.constantOptions) as [bigint[], bigint[]];
|
|
1385
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1386
|
+
const contract = this.llamalend.contracts[this.addresses.amm].contract;
|
|
1387
|
+
const [_borrowed, _collateral] = await contract.get_xy(address, this.llamalend.constantOptions) as [bigint[], bigint[]];
|
|
1386
1388
|
|
|
1387
1389
|
const res: IDict<{ borrowed: string, collateral: string }> = {};
|
|
1388
1390
|
for (let i = n1; i <= n2; i++) {
|
|
@@ -1405,9 +1407,9 @@ export class LendMarketTemplate {
|
|
|
1405
1407
|
public async createLoanMaxRecv(collateral: number | string, range: number): Promise<string> {
|
|
1406
1408
|
this._checkRange(range);
|
|
1407
1409
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1408
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1410
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1409
1411
|
|
|
1410
|
-
return formatUnits(await contract.max_borrowable(_collateral, range, 0, llamalend.constantOptions), this.borrowed_token.decimals);
|
|
1412
|
+
return formatUnits(await contract.max_borrowable(_collateral, range, 0, this.llamalend.constantOptions), this.borrowed_token.decimals);
|
|
1411
1413
|
}
|
|
1412
1414
|
|
|
1413
1415
|
public createLoanMaxRecvAllRanges = memoize(async (collateral: number | string): Promise<{ [index: number]: string }> => {
|
|
@@ -1415,9 +1417,9 @@ export class LendMarketTemplate {
|
|
|
1415
1417
|
|
|
1416
1418
|
const calls = [];
|
|
1417
1419
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1418
|
-
calls.push(llamalend.contracts[this.addresses.controller].multicallContract.max_borrowable(_collateral, N, 0));
|
|
1420
|
+
calls.push(this.llamalend.contracts[this.addresses.controller].multicallContract.max_borrowable(_collateral, N, 0));
|
|
1419
1421
|
}
|
|
1420
|
-
const _amounts = await llamalend.multicallProvider.all(calls) as bigint[];
|
|
1422
|
+
const _amounts = await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
1421
1423
|
|
|
1422
1424
|
const res: { [index: number]: string } = {};
|
|
1423
1425
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
@@ -1442,20 +1444,20 @@ export class LendMarketTemplate {
|
|
|
1442
1444
|
|
|
1443
1445
|
private async _calcN1(_collateral: bigint, _debt: bigint, range: number): Promise<bigint> {
|
|
1444
1446
|
this._checkRange(range);
|
|
1445
|
-
return await llamalend.contracts[this.addresses.controller].contract.calculate_debt_n1(_collateral, _debt, range, llamalend.constantOptions);
|
|
1447
|
+
return await this.llamalend.contracts[this.addresses.controller].contract.calculate_debt_n1(_collateral, _debt, range, this.llamalend.constantOptions);
|
|
1446
1448
|
}
|
|
1447
1449
|
|
|
1448
1450
|
private async _calcN1AllRanges(_collateral: bigint, _debt: bigint, maxN: number): Promise<bigint[]> {
|
|
1449
1451
|
const calls = [];
|
|
1450
1452
|
for (let N = this.minBands; N <= maxN; N++) {
|
|
1451
|
-
calls.push(llamalend.contracts[this.addresses.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
1453
|
+
calls.push(this.llamalend.contracts[this.addresses.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
1452
1454
|
}
|
|
1453
|
-
return await llamalend.multicallProvider.all(calls) as bigint[];
|
|
1455
|
+
return await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
1454
1456
|
}
|
|
1455
1457
|
|
|
1456
1458
|
private async _getPrices(_n2: bigint, _n1: bigint): Promise<string[]> {
|
|
1457
|
-
const contract = llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1458
|
-
return (await llamalend.multicallProvider.all([
|
|
1459
|
+
const contract = this.llamalend.contracts[this.addresses.amm].multicallContract;
|
|
1460
|
+
return (await this.llamalend.multicallProvider.all([
|
|
1459
1461
|
contract.p_oracle_down(_n2),
|
|
1460
1462
|
contract.p_oracle_up(_n1),
|
|
1461
1463
|
]) as bigint[]).map((_p) => formatUnits(_p));
|
|
@@ -1534,23 +1536,23 @@ export class LendMarketTemplate {
|
|
|
1534
1536
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1535
1537
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
1536
1538
|
|
|
1537
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1538
|
-
let _health = await contract.health_calculator(llamalend.constants.ZERO_ADDRESS, _collateral, _debt, full, range, llamalend.constantOptions) as bigint;
|
|
1539
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1540
|
+
let _health = await contract.health_calculator(this.llamalend.constants.ZERO_ADDRESS, _collateral, _debt, full, range, this.llamalend.constantOptions) as bigint;
|
|
1539
1541
|
_health = _health * BigInt(100);
|
|
1540
1542
|
|
|
1541
1543
|
return formatUnits(_health);
|
|
1542
1544
|
}
|
|
1543
1545
|
|
|
1544
1546
|
public async createLoanIsApproved(collateral: number | string): Promise<boolean> {
|
|
1545
|
-
return await hasAllowance([this.collateral_token.address], [collateral], llamalend.signerAddress, this.addresses.controller);
|
|
1547
|
+
return await hasAllowance.call(this.llamalend, [this.collateral_token.address], [collateral], this.llamalend.signerAddress, this.addresses.controller);
|
|
1546
1548
|
}
|
|
1547
1549
|
|
|
1548
1550
|
private async createLoanApproveEstimateGas (collateral: number | string): Promise<TGas> {
|
|
1549
|
-
return await ensureAllowanceEstimateGas([this.collateral_token.address], [collateral], this.addresses.controller);
|
|
1551
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral_token.address], [collateral], this.addresses.controller);
|
|
1550
1552
|
}
|
|
1551
1553
|
|
|
1552
1554
|
public async createLoanApprove(collateral: number | string): Promise<string[]> {
|
|
1553
|
-
return await ensureAllowance([this.collateral_token.address], [collateral], this.addresses.controller);
|
|
1555
|
+
return await ensureAllowance.call(this.llamalend, [this.collateral_token.address], [collateral], this.addresses.controller);
|
|
1554
1556
|
}
|
|
1555
1557
|
|
|
1556
1558
|
private async _createLoan(collateral: number | string, debt: number | string, range: number, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -1559,13 +1561,13 @@ export class LendMarketTemplate {
|
|
|
1559
1561
|
|
|
1560
1562
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1561
1563
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
1562
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1563
|
-
const gas = await contract.create_loan.estimateGas(_collateral, _debt, range, { ...llamalend.constantOptions });
|
|
1564
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1565
|
+
const gas = await contract.create_loan.estimateGas(_collateral, _debt, range, { ...this.llamalend.constantOptions });
|
|
1564
1566
|
if (estimateGas) return smartNumber(gas);
|
|
1565
1567
|
|
|
1566
|
-
await llamalend.updateFeeData();
|
|
1568
|
+
await this.llamalend.updateFeeData();
|
|
1567
1569
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1568
|
-
return (await contract.create_loan(_collateral, _debt, range, { ...llamalend.options, gasLimit })).hash
|
|
1570
|
+
return (await contract.create_loan(_collateral, _debt, range, { ...this.llamalend.options, gasLimit })).hash
|
|
1569
1571
|
}
|
|
1570
1572
|
|
|
1571
1573
|
public async createLoanEstimateGas(collateral: number | string, debt: number | string, range: number): Promise<TGas> {
|
|
@@ -1584,15 +1586,15 @@ export class LendMarketTemplate {
|
|
|
1584
1586
|
const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = await this._userState();
|
|
1585
1587
|
const _collateral = _currentCollateral + parseUnits(collateralAmount, this.collateral_token.decimals);
|
|
1586
1588
|
|
|
1587
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1588
|
-
const _debt: bigint = await contract.max_borrowable(_collateral, _N, _currentDebt, llamalend.constantOptions);
|
|
1589
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1590
|
+
const _debt: bigint = await contract.max_borrowable(_collateral, _N, _currentDebt, this.llamalend.constantOptions);
|
|
1589
1591
|
|
|
1590
1592
|
return formatUnits(_debt - _currentDebt, this.borrowed_token.decimals);
|
|
1591
1593
|
}
|
|
1592
1594
|
|
|
1593
1595
|
private async _borrowMoreBands(collateral: number | string, debt: number | string): Promise<[bigint, bigint]> {
|
|
1594
1596
|
const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = await this._userState();
|
|
1595
|
-
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1597
|
+
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
1596
1598
|
|
|
1597
1599
|
const _collateral = _currentCollateral + parseUnits(collateral, this.collateral_token.decimals);
|
|
1598
1600
|
const _debt = _currentDebt + parseUnits(debt, this.borrowed_token.decimals);
|
|
@@ -1616,43 +1618,43 @@ export class LendMarketTemplate {
|
|
|
1616
1618
|
}
|
|
1617
1619
|
|
|
1618
1620
|
public async borrowMoreHealth(collateral: number | string, debt: number | string, full = true, address = ""): Promise<string> {
|
|
1619
|
-
address = _getAddress(address);
|
|
1621
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1620
1622
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1621
1623
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
1622
1624
|
|
|
1623
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1624
|
-
let _health = await contract.health_calculator(address, _collateral, _debt, full, 0, llamalend.constantOptions) as bigint;
|
|
1625
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1626
|
+
let _health = await contract.health_calculator(address, _collateral, _debt, full, 0, this.llamalend.constantOptions) as bigint;
|
|
1625
1627
|
_health = _health * BigInt(100);
|
|
1626
1628
|
|
|
1627
1629
|
return formatUnits(_health);
|
|
1628
1630
|
}
|
|
1629
1631
|
|
|
1630
1632
|
public async borrowMoreIsApproved(collateral: number | string): Promise<boolean> {
|
|
1631
|
-
return await hasAllowance([this.addresses.collateral_token], [collateral], llamalend.signerAddress, this.addresses.controller);
|
|
1633
|
+
return await hasAllowance.call(this.llamalend, [this.addresses.collateral_token], [collateral], this.llamalend.signerAddress, this.addresses.controller);
|
|
1632
1634
|
}
|
|
1633
1635
|
|
|
1634
1636
|
private async borrowMoreApproveEstimateGas (collateral: number | string): Promise<TGas> {
|
|
1635
|
-
return await ensureAllowanceEstimateGas([this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1637
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1636
1638
|
}
|
|
1637
1639
|
|
|
1638
1640
|
public async borrowMoreApprove(collateral: number | string): Promise<string[]> {
|
|
1639
|
-
return await ensureAllowance([this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1641
|
+
return await ensureAllowance.call(this.llamalend, [this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1640
1642
|
}
|
|
1641
1643
|
|
|
1642
1644
|
private async _borrowMore(collateral: number | string, debt: number | string, estimateGas: boolean): Promise<string | TGas> {
|
|
1643
1645
|
const { borrowed, debt: currentDebt } = await this.userState();
|
|
1644
|
-
if (Number(currentDebt) === 0) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1645
|
-
if (Number(borrowed) > 0) throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
1646
|
+
if (Number(currentDebt) === 0) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
1647
|
+
if (Number(borrowed) > 0) throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
|
|
1646
1648
|
|
|
1647
1649
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1648
1650
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
1649
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1650
|
-
const gas = await contract.borrow_more.estimateGas(_collateral, _debt, { ...llamalend.constantOptions });
|
|
1651
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1652
|
+
const gas = await contract.borrow_more.estimateGas(_collateral, _debt, { ...this.llamalend.constantOptions });
|
|
1651
1653
|
if (estimateGas) return smartNumber(gas);
|
|
1652
1654
|
|
|
1653
|
-
await llamalend.updateFeeData();
|
|
1655
|
+
await this.llamalend.updateFeeData();
|
|
1654
1656
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1655
|
-
return (await contract.borrow_more(_collateral, _debt, { ...llamalend.options, gasLimit })).hash
|
|
1657
|
+
return (await contract.borrow_more(_collateral, _debt, { ...this.llamalend.options, gasLimit })).hash
|
|
1656
1658
|
}
|
|
1657
1659
|
|
|
1658
1660
|
public async borrowMoreEstimateGas(collateral: number | string, debt: number | string): Promise<TGas> {
|
|
@@ -1668,7 +1670,7 @@ export class LendMarketTemplate {
|
|
|
1668
1670
|
// ---------------- ADD COLLATERAL ----------------
|
|
1669
1671
|
|
|
1670
1672
|
private async _addCollateralBands(collateral: number | string, address = ""): Promise<[bigint, bigint]> {
|
|
1671
|
-
address = _getAddress(address);
|
|
1673
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1672
1674
|
const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = await this._userState(address);
|
|
1673
1675
|
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${address} does not exist`);
|
|
1674
1676
|
|
|
@@ -1692,26 +1694,26 @@ export class LendMarketTemplate {
|
|
|
1692
1694
|
}
|
|
1693
1695
|
|
|
1694
1696
|
public async addCollateralHealth(collateral: number | string, full = true, address = ""): Promise<string> {
|
|
1695
|
-
address = _getAddress(address);
|
|
1697
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1696
1698
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1697
1699
|
|
|
1698
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1699
|
-
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions) as bigint;
|
|
1700
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1701
|
+
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions) as bigint;
|
|
1700
1702
|
_health = _health * BigInt(100);
|
|
1701
1703
|
|
|
1702
1704
|
return formatUnits(_health);
|
|
1703
1705
|
}
|
|
1704
1706
|
|
|
1705
1707
|
public async addCollateralIsApproved(collateral: number | string): Promise<boolean> {
|
|
1706
|
-
return await hasAllowance([this.addresses.collateral_token], [collateral], llamalend.signerAddress, this.addresses.controller);
|
|
1708
|
+
return await hasAllowance.call(this.llamalend, [this.addresses.collateral_token], [collateral], this.llamalend.signerAddress, this.addresses.controller);
|
|
1707
1709
|
}
|
|
1708
1710
|
|
|
1709
1711
|
private async addCollateralApproveEstimateGas (collateral: number | string): Promise<TGas> {
|
|
1710
|
-
return await ensureAllowanceEstimateGas([this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1712
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1711
1713
|
}
|
|
1712
1714
|
|
|
1713
1715
|
public async addCollateralApprove(collateral: number | string): Promise<string[]> {
|
|
1714
|
-
return await ensureAllowance([this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1716
|
+
return await ensureAllowance.call(this.llamalend, [this.addresses.collateral_token], [collateral], this.addresses.controller);
|
|
1715
1717
|
}
|
|
1716
1718
|
|
|
1717
1719
|
private async _addCollateral(collateral: number | string, address: string, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -1720,23 +1722,23 @@ export class LendMarketTemplate {
|
|
|
1720
1722
|
if (Number(borrowed) > 0) throw Error(`User ${address} is already in liquidation mode`);
|
|
1721
1723
|
|
|
1722
1724
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1723
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1724
|
-
const gas = await contract.add_collateral.estimateGas(_collateral, address, { ...llamalend.constantOptions });
|
|
1725
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1726
|
+
const gas = await contract.add_collateral.estimateGas(_collateral, address, { ...this.llamalend.constantOptions });
|
|
1725
1727
|
if (estimateGas) return smartNumber(gas);
|
|
1726
1728
|
|
|
1727
|
-
await llamalend.updateFeeData();
|
|
1729
|
+
await this.llamalend.updateFeeData();
|
|
1728
1730
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1729
|
-
return (await contract.add_collateral(_collateral, address, { ...llamalend.options, gasLimit })).hash
|
|
1731
|
+
return (await contract.add_collateral(_collateral, address, { ...this.llamalend.options, gasLimit })).hash
|
|
1730
1732
|
}
|
|
1731
1733
|
|
|
1732
1734
|
public async addCollateralEstimateGas(collateral: number | string, address = ""): Promise<TGas> {
|
|
1733
|
-
address = _getAddress(address);
|
|
1735
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1734
1736
|
if (!(await this.addCollateralIsApproved(collateral))) throw Error("Approval is needed for gas estimation");
|
|
1735
1737
|
return await this._addCollateral(collateral, address, true) as TGas;
|
|
1736
1738
|
}
|
|
1737
1739
|
|
|
1738
1740
|
public async addCollateral(collateral: number | string, address = ""): Promise<string> {
|
|
1739
|
-
address = _getAddress(address);
|
|
1741
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1740
1742
|
await this.addCollateralApprove(collateral);
|
|
1741
1743
|
return await this._addCollateral(collateral, address, false) as string;
|
|
1742
1744
|
}
|
|
@@ -1745,14 +1747,14 @@ export class LendMarketTemplate {
|
|
|
1745
1747
|
|
|
1746
1748
|
public async maxRemovable(): Promise<string> {
|
|
1747
1749
|
const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = await this._userState();
|
|
1748
|
-
const _requiredCollateral = await llamalend.contracts[this.addresses.controller].contract.min_collateral(_currentDebt, _N, llamalend.constantOptions)
|
|
1750
|
+
const _requiredCollateral = await this.llamalend.contracts[this.addresses.controller].contract.min_collateral(_currentDebt, _N, this.llamalend.constantOptions)
|
|
1749
1751
|
|
|
1750
1752
|
return formatUnits(_currentCollateral - _requiredCollateral, this.collateral_token.decimals);
|
|
1751
1753
|
}
|
|
1752
1754
|
|
|
1753
1755
|
private async _removeCollateralBands(collateral: number | string): Promise<[bigint, bigint]> {
|
|
1754
1756
|
const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = await this._userState();
|
|
1755
|
-
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1757
|
+
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
1756
1758
|
|
|
1757
1759
|
const _collateral = _currentCollateral - parseUnits(collateral, this.collateral_token.decimals);
|
|
1758
1760
|
const _n1 = await this._calcN1(_collateral, _currentDebt, Number(_N));
|
|
@@ -1774,11 +1776,11 @@ export class LendMarketTemplate {
|
|
|
1774
1776
|
}
|
|
1775
1777
|
|
|
1776
1778
|
public async removeCollateralHealth(collateral: number | string, full = true, address = ""): Promise<string> {
|
|
1777
|
-
address = _getAddress(address);
|
|
1779
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1778
1780
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals) * BigInt(-1);
|
|
1779
1781
|
|
|
1780
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1781
|
-
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions) as bigint;
|
|
1782
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1783
|
+
let _health = await contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions) as bigint;
|
|
1782
1784
|
_health = _health * BigInt(100);
|
|
1783
1785
|
|
|
1784
1786
|
return formatUnits(_health);
|
|
@@ -1786,17 +1788,17 @@ export class LendMarketTemplate {
|
|
|
1786
1788
|
|
|
1787
1789
|
private async _removeCollateral(collateral: number | string, estimateGas: boolean): Promise<string | TGas> {
|
|
1788
1790
|
const { borrowed, debt: currentDebt } = await this.userState();
|
|
1789
|
-
if (Number(currentDebt) === 0) throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1790
|
-
if (Number(borrowed) > 0) throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
1791
|
+
if (Number(currentDebt) === 0) throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
1792
|
+
if (Number(borrowed) > 0) throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
|
|
1791
1793
|
|
|
1792
1794
|
const _collateral = parseUnits(collateral, this.collateral_token.decimals);
|
|
1793
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1794
|
-
const gas = await contract.remove_collateral.estimateGas(_collateral, llamalend.constantOptions);
|
|
1795
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1796
|
+
const gas = await contract.remove_collateral.estimateGas(_collateral, this.llamalend.constantOptions);
|
|
1795
1797
|
if (estimateGas) return smartNumber(gas);
|
|
1796
1798
|
|
|
1797
|
-
await llamalend.updateFeeData();
|
|
1799
|
+
await this.llamalend.updateFeeData();
|
|
1798
1800
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1799
|
-
return (await contract.remove_collateral(_collateral, { ...llamalend.options, gasLimit })).hash
|
|
1801
|
+
return (await contract.remove_collateral(_collateral, { ...this.llamalend.options, gasLimit })).hash
|
|
1800
1802
|
}
|
|
1801
1803
|
|
|
1802
1804
|
public async removeCollateralEstimateGas(collateral: number | string): Promise<TGas> {
|
|
@@ -1834,44 +1836,44 @@ export class LendMarketTemplate {
|
|
|
1834
1836
|
}
|
|
1835
1837
|
|
|
1836
1838
|
public async repayIsApproved(debt: number | string): Promise<boolean> {
|
|
1837
|
-
return await hasAllowance([this.borrowed_token.address], [debt], llamalend.signerAddress, this.addresses.controller);
|
|
1839
|
+
return await hasAllowance.call(this.llamalend, [this.borrowed_token.address], [debt], this.llamalend.signerAddress, this.addresses.controller);
|
|
1838
1840
|
}
|
|
1839
1841
|
|
|
1840
1842
|
private async repayApproveEstimateGas (debt: number | string): Promise<TGas> {
|
|
1841
|
-
return await ensureAllowanceEstimateGas([this.borrowed_token.address], [debt], this.addresses.controller);
|
|
1843
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.borrowed_token.address], [debt], this.addresses.controller);
|
|
1842
1844
|
}
|
|
1843
1845
|
|
|
1844
1846
|
public async repayApprove(debt: number | string): Promise<string[]> {
|
|
1845
|
-
return await ensureAllowance([this.borrowed_token.address], [debt], this.addresses.controller);
|
|
1847
|
+
return await ensureAllowance.call(this.llamalend, [this.borrowed_token.address], [debt], this.addresses.controller);
|
|
1846
1848
|
}
|
|
1847
1849
|
|
|
1848
1850
|
public async repayHealth(debt: number | string, full = true, address = ""): Promise<string> {
|
|
1849
|
-
address = _getAddress(address);
|
|
1851
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1850
1852
|
const _debt = parseUnits(debt) * BigInt(-1);
|
|
1851
1853
|
|
|
1852
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1853
|
-
let _health = await contract.health_calculator(address, 0, _debt, full, 0, llamalend.constantOptions) as bigint;
|
|
1854
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1855
|
+
let _health = await contract.health_calculator(address, 0, _debt, full, 0, this.llamalend.constantOptions) as bigint;
|
|
1854
1856
|
_health = _health * BigInt(100);
|
|
1855
1857
|
|
|
1856
1858
|
return formatUnits(_health);
|
|
1857
1859
|
}
|
|
1858
1860
|
|
|
1859
1861
|
private async _repay(debt: number | string, address: string, estimateGas: boolean): Promise<string | TGas> {
|
|
1860
|
-
address = _getAddress(address);
|
|
1862
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1861
1863
|
const { debt: currentDebt } = await this.userState(address);
|
|
1862
1864
|
if (Number(currentDebt) === 0) throw Error(`Loan for ${address} does not exist`);
|
|
1863
1865
|
|
|
1864
1866
|
const _debt = parseUnits(debt);
|
|
1865
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
1867
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
1866
1868
|
const [, n1] = await this.userBands(address);
|
|
1867
1869
|
const { borrowed } = await this.userState(address);
|
|
1868
1870
|
const n = (BN(borrowed).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1; // In liquidation mode it doesn't matter if active band moves
|
|
1869
|
-
const gas = await contract.repay.estimateGas(_debt, address, n, llamalend.constantOptions);
|
|
1871
|
+
const gas = await contract.repay.estimateGas(_debt, address, n, this.llamalend.constantOptions);
|
|
1870
1872
|
if (estimateGas) return smartNumber(gas);
|
|
1871
1873
|
|
|
1872
|
-
await llamalend.updateFeeData();
|
|
1874
|
+
await this.llamalend.updateFeeData();
|
|
1873
1875
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1874
|
-
return (await contract.repay(_debt, address, n, { ...llamalend.options, gasLimit })).hash
|
|
1876
|
+
return (await contract.repay(_debt, address, n, { ...this.llamalend.options, gasLimit })).hash
|
|
1875
1877
|
}
|
|
1876
1878
|
|
|
1877
1879
|
public async repayEstimateGas(debt: number | string, address = ""): Promise<TGas> {
|
|
@@ -1887,38 +1889,38 @@ export class LendMarketTemplate {
|
|
|
1887
1889
|
// ---------------- FULL REPAY ----------------
|
|
1888
1890
|
|
|
1889
1891
|
private async _fullRepayAmount(address = ""): Promise<string> {
|
|
1890
|
-
address = _getAddress(address);
|
|
1892
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1891
1893
|
const { debt } = await this.userState(address);
|
|
1892
1894
|
return BN(debt).times(1.0001).toString();
|
|
1893
1895
|
}
|
|
1894
1896
|
|
|
1895
1897
|
public async fullRepayIsApproved(address = ""): Promise<boolean> {
|
|
1896
|
-
address = _getAddress(address);
|
|
1898
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1897
1899
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1898
1900
|
return await this.repayIsApproved(fullRepayAmount);
|
|
1899
1901
|
}
|
|
1900
1902
|
|
|
1901
1903
|
private async fullRepayApproveEstimateGas (address = ""): Promise<TGas> {
|
|
1902
|
-
address = _getAddress(address);
|
|
1904
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1903
1905
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1904
1906
|
return await this.repayApproveEstimateGas(fullRepayAmount);
|
|
1905
1907
|
}
|
|
1906
1908
|
|
|
1907
1909
|
public async fullRepayApprove(address = ""): Promise<string[]> {
|
|
1908
|
-
address = _getAddress(address);
|
|
1910
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1909
1911
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1910
1912
|
return await this.repayApprove(fullRepayAmount);
|
|
1911
1913
|
}
|
|
1912
1914
|
|
|
1913
1915
|
public async fullRepayEstimateGas(address = ""): Promise<TGas> {
|
|
1914
|
-
address = _getAddress(address);
|
|
1916
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1915
1917
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1916
1918
|
if (!(await this.repayIsApproved(fullRepayAmount))) throw Error("Approval is needed for gas estimation");
|
|
1917
1919
|
return await this._repay(fullRepayAmount, address, true) as TGas;
|
|
1918
1920
|
}
|
|
1919
1921
|
|
|
1920
1922
|
public async fullRepay(address = ""): Promise<string> {
|
|
1921
|
-
address = _getAddress(address);
|
|
1923
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1922
1924
|
const fullRepayAmount = await this._fullRepayAmount(address);
|
|
1923
1925
|
await this.repayApprove(fullRepayAmount);
|
|
1924
1926
|
return await this._repay(fullRepayAmount, address, false) as string;
|
|
@@ -1929,15 +1931,15 @@ export class LendMarketTemplate {
|
|
|
1929
1931
|
public async maxSwappable(i: number, j: number): Promise<string> {
|
|
1930
1932
|
if (!(i === 0 && j === 1) && !(i === 1 && j === 0)) throw Error("Wrong index");
|
|
1931
1933
|
const inDecimals = this.coinDecimals[i];
|
|
1932
|
-
const contract = llamalend.contracts[this.addresses.amm].contract;
|
|
1933
|
-
const [_inAmount, _outAmount] = await contract.get_dxdy(i, j, MAX_ALLOWANCE, llamalend.constantOptions) as bigint[];
|
|
1934
|
+
const contract = this.llamalend.contracts[this.addresses.amm].contract;
|
|
1935
|
+
const [_inAmount, _outAmount] = await contract.get_dxdy(i, j, MAX_ALLOWANCE, this.llamalend.constantOptions) as bigint[];
|
|
1934
1936
|
if (_outAmount === BigInt(0)) return "0";
|
|
1935
1937
|
|
|
1936
1938
|
return formatUnits(_inAmount, inDecimals)
|
|
1937
1939
|
}
|
|
1938
1940
|
|
|
1939
1941
|
private async _swapExpected(i: number, j: number, _amount: bigint): Promise<bigint> {
|
|
1940
|
-
return await llamalend.contracts[this.addresses.amm].contract.get_dy(i, j, _amount, llamalend.constantOptions) as bigint;
|
|
1942
|
+
return await this.llamalend.contracts[this.addresses.amm].contract.get_dy(i, j, _amount, this.llamalend.constantOptions) as bigint;
|
|
1941
1943
|
}
|
|
1942
1944
|
|
|
1943
1945
|
public async swapExpected(i: number, j: number, amount: number | string): Promise<string> {
|
|
@@ -1953,7 +1955,7 @@ export class LendMarketTemplate {
|
|
|
1953
1955
|
if (!(i === 0 && j === 1) && !(i === 1 && j === 0)) throw Error("Wrong index");
|
|
1954
1956
|
const [inDecimals, outDecimals] = this.coinDecimals;
|
|
1955
1957
|
const _amount = parseUnits(outAmount, outDecimals);
|
|
1956
|
-
const _expected = await llamalend.contracts[this.addresses.amm].contract.get_dx(i, j, _amount, llamalend.constantOptions) as bigint;
|
|
1958
|
+
const _expected = await this.llamalend.contracts[this.addresses.amm].contract.get_dx(i, j, _amount, this.llamalend.constantOptions) as bigint;
|
|
1957
1959
|
|
|
1958
1960
|
return formatUnits(_expected, inDecimals)
|
|
1959
1961
|
}
|
|
@@ -1994,19 +1996,19 @@ export class LendMarketTemplate {
|
|
|
1994
1996
|
public async swapIsApproved(i: number, amount: number | string): Promise<boolean> {
|
|
1995
1997
|
if (i !== 0 && i !== 1) throw Error("Wrong index");
|
|
1996
1998
|
|
|
1997
|
-
return await hasAllowance([this.coinAddresses[i]], [amount], llamalend.signerAddress, this.addresses.amm);
|
|
1999
|
+
return await hasAllowance.call(this.llamalend, [this.coinAddresses[i]], [amount], this.llamalend.signerAddress, this.addresses.amm);
|
|
1998
2000
|
}
|
|
1999
2001
|
|
|
2000
2002
|
private async swapApproveEstimateGas (i: number, amount: number | string): Promise<TGas> {
|
|
2001
2003
|
if (i !== 0 && i !== 1) throw Error("Wrong index");
|
|
2002
2004
|
|
|
2003
|
-
return await ensureAllowanceEstimateGas([this.coinAddresses[i]], [amount], this.addresses.amm);
|
|
2005
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.coinAddresses[i]], [amount], this.addresses.amm);
|
|
2004
2006
|
}
|
|
2005
2007
|
|
|
2006
2008
|
public async swapApprove(i: number, amount: number | string): Promise<string[]> {
|
|
2007
2009
|
if (i !== 0 && i !== 1) throw Error("Wrong index");
|
|
2008
2010
|
|
|
2009
|
-
return await ensureAllowance([this.coinAddresses[i]], [amount], this.addresses.amm);
|
|
2011
|
+
return await ensureAllowance.call(this.llamalend, [this.coinAddresses[i]], [amount], this.addresses.amm);
|
|
2010
2012
|
}
|
|
2011
2013
|
|
|
2012
2014
|
private async _swap(i: number, j: number, amount: number | string, slippage: number, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -2017,13 +2019,13 @@ export class LendMarketTemplate {
|
|
|
2017
2019
|
const _expected = await this._swapExpected(i, j, _amount);
|
|
2018
2020
|
const minRecvAmountBN: BigNumber = toBN(_expected, outDecimals).times(100 - slippage).div(100);
|
|
2019
2021
|
const _minRecvAmount = fromBN(minRecvAmountBN, outDecimals);
|
|
2020
|
-
const contract = llamalend.contracts[this.addresses.amm].contract;
|
|
2021
|
-
const gas = await contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, llamalend.constantOptions);
|
|
2022
|
+
const contract = this.llamalend.contracts[this.addresses.amm].contract;
|
|
2023
|
+
const gas = await contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, this.llamalend.constantOptions);
|
|
2022
2024
|
if (estimateGas) return smartNumber(gas);
|
|
2023
2025
|
|
|
2024
|
-
await llamalend.updateFeeData();
|
|
2026
|
+
await this.llamalend.updateFeeData();
|
|
2025
2027
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
2026
|
-
return (await contract.exchange(i, j, _amount, _minRecvAmount, { ...llamalend.options, gasLimit })).hash
|
|
2028
|
+
return (await contract.exchange(i, j, _amount, _minRecvAmount, { ...this.llamalend.options, gasLimit })).hash
|
|
2027
2029
|
}
|
|
2028
2030
|
|
|
2029
2031
|
public async swapEstimateGas(i: number, j: number, amount: number | string, slippage = 0.1): Promise<TGas> {
|
|
@@ -2039,25 +2041,25 @@ export class LendMarketTemplate {
|
|
|
2039
2041
|
// ---------------- LIQUIDATE ----------------
|
|
2040
2042
|
|
|
2041
2043
|
public async tokensToLiquidate(address = ""): Promise<string> {
|
|
2042
|
-
address = _getAddress(address);
|
|
2043
|
-
const _tokens = await llamalend.contracts[this.addresses.controller].contract.tokens_to_liquidate(address, llamalend.constantOptions) as bigint;
|
|
2044
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2045
|
+
const _tokens = await this.llamalend.contracts[this.addresses.controller].contract.tokens_to_liquidate(address, this.llamalend.constantOptions) as bigint;
|
|
2044
2046
|
|
|
2045
2047
|
return formatUnits(_tokens, this.borrowed_token.decimals)
|
|
2046
2048
|
}
|
|
2047
2049
|
|
|
2048
2050
|
public async liquidateIsApproved(address = ""): Promise<boolean> {
|
|
2049
2051
|
const tokensToLiquidate = await this.tokensToLiquidate(address);
|
|
2050
|
-
return await hasAllowance([this.addresses.borrowed_token], [tokensToLiquidate], llamalend.signerAddress, this.addresses.controller);
|
|
2052
|
+
return await hasAllowance.call(this.llamalend, [this.addresses.borrowed_token], [tokensToLiquidate], this.llamalend.signerAddress, this.addresses.controller);
|
|
2051
2053
|
}
|
|
2052
2054
|
|
|
2053
2055
|
private async liquidateApproveEstimateGas (address = ""): Promise<TGas> {
|
|
2054
2056
|
const tokensToLiquidate = await this.tokensToLiquidate(address);
|
|
2055
|
-
return await ensureAllowanceEstimateGas([this.addresses.borrowed_token], [tokensToLiquidate], this.addresses.controller);
|
|
2057
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend, [this.addresses.borrowed_token], [tokensToLiquidate], this.addresses.controller);
|
|
2056
2058
|
}
|
|
2057
2059
|
|
|
2058
2060
|
public async liquidateApprove(address = ""): Promise<string[]> {
|
|
2059
2061
|
const tokensToLiquidate = await this.tokensToLiquidate(address);
|
|
2060
|
-
return await ensureAllowance([this.addresses.borrowed_token], [tokensToLiquidate], this.addresses.controller);
|
|
2062
|
+
return await ensureAllowance.call(this.llamalend, [this.addresses.borrowed_token], [tokensToLiquidate], this.addresses.controller);
|
|
2061
2063
|
}
|
|
2062
2064
|
|
|
2063
2065
|
private async _liquidate(address: string, slippage: number, estimateGas: boolean): Promise<string | TGas> {
|
|
@@ -2069,13 +2071,13 @@ export class LendMarketTemplate {
|
|
|
2069
2071
|
|
|
2070
2072
|
const minAmountBN: BigNumber = BN(borrowed).times(100 - slippage).div(100);
|
|
2071
2073
|
const _minAmount = fromBN(minAmountBN);
|
|
2072
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
2073
|
-
const gas = (await contract.liquidate.estimateGas(address, _minAmount, llamalend.constantOptions))
|
|
2074
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2075
|
+
const gas = (await contract.liquidate.estimateGas(address, _minAmount, this.llamalend.constantOptions))
|
|
2074
2076
|
if (estimateGas) return smartNumber(gas);
|
|
2075
2077
|
|
|
2076
|
-
await llamalend.updateFeeData();
|
|
2078
|
+
await this.llamalend.updateFeeData();
|
|
2077
2079
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
2078
|
-
return (await contract.liquidate(address, _minAmount, { ...llamalend.options, gasLimit })).hash
|
|
2080
|
+
return (await contract.liquidate(address, _minAmount, { ...this.llamalend.options, gasLimit })).hash
|
|
2079
2081
|
}
|
|
2080
2082
|
|
|
2081
2083
|
public async liquidateEstimateGas(address: string, slippage = 0.1): Promise<TGas> {
|
|
@@ -2104,19 +2106,19 @@ export class LendMarketTemplate {
|
|
|
2104
2106
|
|
|
2105
2107
|
public async selfLiquidateEstimateGas(slippage = 0.1): Promise<TGas> {
|
|
2106
2108
|
if (!(await this.selfLiquidateIsApproved())) throw Error("Approval is needed for gas estimation");
|
|
2107
|
-
return await this._liquidate(llamalend.signerAddress, slippage, true) as TGas;
|
|
2109
|
+
return await this._liquidate(this.llamalend.signerAddress, slippage, true) as TGas;
|
|
2108
2110
|
}
|
|
2109
2111
|
|
|
2110
2112
|
public async selfLiquidate(slippage = 0.1): Promise<string> {
|
|
2111
2113
|
await this.selfLiquidateApprove();
|
|
2112
|
-
return await this._liquidate(llamalend.signerAddress, slippage, false) as string;
|
|
2114
|
+
return await this._liquidate(this.llamalend.signerAddress, slippage, false) as string;
|
|
2113
2115
|
}
|
|
2114
2116
|
|
|
2115
2117
|
// ---------------- LEVERAGE CREATE LOAN ----------------
|
|
2116
2118
|
|
|
2117
2119
|
private hasLeverage = (): boolean => {
|
|
2118
|
-
return llamalend.constants.ALIASES.leverage_zap !== llamalend.constants.ZERO_ADDRESS &&
|
|
2119
|
-
this._getMarketId() >= Number(llamalend.constants.ALIASES["leverage_markets_start_id"]);
|
|
2120
|
+
return this.llamalend.constants.ALIASES.leverage_zap !== this.llamalend.constants.ZERO_ADDRESS &&
|
|
2121
|
+
this._getMarketId() >= Number(this.llamalend.constants.ALIASES["leverage_markets_start_id"]);
|
|
2120
2122
|
}
|
|
2121
2123
|
|
|
2122
2124
|
private _checkLeverageZap(): void {
|
|
@@ -2171,7 +2173,7 @@ export class LendMarketTemplate {
|
|
|
2171
2173
|
let _userEffectiveCollateral = BigInt(0);
|
|
2172
2174
|
let _maxLeverageCollateral = BigInt(0);
|
|
2173
2175
|
|
|
2174
|
-
const contract = llamalend.contracts[llamalend.constants.ALIASES.leverage_zap].contract;
|
|
2176
|
+
const contract = this.llamalend.contracts[this.llamalend.constants.ALIASES.leverage_zap].contract;
|
|
2175
2177
|
for (let i = 0; i < 5; i++) {
|
|
2176
2178
|
maxBorrowablePrevBN = maxBorrowableBN;
|
|
2177
2179
|
_userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.collateral_token.decimals);
|
|
@@ -2186,7 +2188,7 @@ export class LendMarketTemplate {
|
|
|
2186
2188
|
}
|
|
2187
2189
|
|
|
2188
2190
|
// additionalCollateral = (userBorrowed / p) + leverageCollateral
|
|
2189
|
-
const _maxAdditionalCollateral = BigInt(await _getExpectedOdos(
|
|
2191
|
+
const _maxAdditionalCollateral = BigInt(await _getExpectedOdos.call(this.llamalend,
|
|
2190
2192
|
this.addresses.borrowed_token, this.addresses.collateral_token, _maxBorrowable + _userBorrowed, this.addresses.amm));
|
|
2191
2193
|
pAvgBN = maxBorrowableBN.plus(userBorrowed).div(toBN(_maxAdditionalCollateral, this.collateral_token.decimals));
|
|
2192
2194
|
_maxLeverageCollateral = _maxAdditionalCollateral - fromBN(BN(userBorrowed).div(pAvgBN), this.collateral_token.decimals);
|
|
@@ -2218,7 +2220,7 @@ export class LendMarketTemplate {
|
|
|
2218
2220
|
}>> => {
|
|
2219
2221
|
this._checkLeverageZap();
|
|
2220
2222
|
const _userCollateral = parseUnits(userCollateral, this.collateral_token.decimals);
|
|
2221
|
-
const contract = llamalend.contracts[llamalend.constants.ALIASES.leverage_zap].multicallContract;
|
|
2223
|
+
const contract = this.llamalend.contracts[this.llamalend.constants.ALIASES.leverage_zap].multicallContract;
|
|
2222
2224
|
|
|
2223
2225
|
const oraclePriceBand = await this.oraclePriceBand();
|
|
2224
2226
|
const pAvgApproxBN = BN(await this.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
@@ -2239,7 +2241,7 @@ export class LendMarketTemplate {
|
|
|
2239
2241
|
const j = N - this.minBands;
|
|
2240
2242
|
calls.push(contract.max_borrowable(this.addresses.controller, _userEffectiveCollateral, _maxLeverageCollateral[j], N, fromBN(pBN)));
|
|
2241
2243
|
}
|
|
2242
|
-
_maxBorrowable = (await llamalend.multicallProvider.all(calls) as bigint[]).map((_mb) => _mb * BigInt(998) / BigInt(1000));
|
|
2244
|
+
_maxBorrowable = (await this.llamalend.multicallProvider.all(calls) as bigint[]).map((_mb) => _mb * BigInt(998) / BigInt(1000));
|
|
2243
2245
|
maxBorrowableBN = _maxBorrowable.map((_mb) => toBN(_mb, this.borrowed_token.decimals));
|
|
2244
2246
|
|
|
2245
2247
|
const deltaBN = maxBorrowableBN.map((mb, l) => mb.minus(maxBorrowablePrevBN[l]).abs().div(mb));
|
|
@@ -2249,7 +2251,7 @@ export class LendMarketTemplate {
|
|
|
2249
2251
|
}
|
|
2250
2252
|
|
|
2251
2253
|
if (pAvgBN === null){
|
|
2252
|
-
const _y = BigInt(await _getExpectedOdos(this.addresses.borrowed_token, this.addresses.collateral_token, _maxBorrowable[0], this.addresses.amm));
|
|
2254
|
+
const _y = BigInt(await _getExpectedOdos.call(this.llamalend, this.addresses.borrowed_token, this.addresses.collateral_token, _maxBorrowable[0], this.addresses.amm));
|
|
2253
2255
|
const yBN = toBN(_y, this.collateral_token.decimals);
|
|
2254
2256
|
pAvgBN = maxBorrowableBN[0].div(yBN);
|
|
2255
2257
|
}
|
|
@@ -2290,9 +2292,9 @@ export class LendMarketTemplate {
|
|
|
2290
2292
|
});
|
|
2291
2293
|
|
|
2292
2294
|
private _setSwapDataToCache = async (inputCoinAddress: string, outputCoinAddress: string, _amount: bigint, slippage: number) => {
|
|
2293
|
-
let swapData = await _getQuoteOdos(inputCoinAddress, outputCoinAddress, _amount, this.addresses.amm, true, slippage);
|
|
2295
|
+
let swapData = await _getQuoteOdos.call(this.llamalend, inputCoinAddress, outputCoinAddress, _amount, this.addresses.amm, true, slippage);
|
|
2294
2296
|
while (swapData.pathId == null) {
|
|
2295
|
-
swapData = await _getQuoteOdos(inputCoinAddress, outputCoinAddress, _amount, this.addresses.amm, true, slippage);
|
|
2297
|
+
swapData = await _getQuoteOdos.call(this.llamalend, inputCoinAddress, outputCoinAddress, _amount, this.addresses.amm, true, slippage);
|
|
2296
2298
|
}
|
|
2297
2299
|
const key = `${inputCoinAddress}-${_amount}`;
|
|
2298
2300
|
this.swapDataCache[key] = { ...swapData, slippage };
|
|
@@ -2374,11 +2376,11 @@ export class LendMarketTemplate {
|
|
|
2374
2376
|
const { _debt, _borrowed, _N } = await this._userState(user);
|
|
2375
2377
|
if (_borrowed > BigInt(0)) throw Error(`User ${user} is already in liquidation mode`);
|
|
2376
2378
|
_stateDebt = _debt;
|
|
2377
|
-
if (range < 0) range = Number(llamalend.formatUnits(_N, 0));
|
|
2379
|
+
if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
|
|
2378
2380
|
}
|
|
2379
2381
|
const { _futureStateCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, debt, user);
|
|
2380
2382
|
const _debt = _stateDebt + parseUnits(debt, this.borrowed_token.decimals);
|
|
2381
|
-
return await llamalend.contracts[this.addresses.controller].contract.calculate_debt_n1(_futureStateCollateral, _debt, range, llamalend.constantOptions);
|
|
2383
|
+
return await this.llamalend.contracts[this.addresses.controller].contract.calculate_debt_n1(_futureStateCollateral, _debt, range, this.llamalend.constantOptions);
|
|
2382
2384
|
},
|
|
2383
2385
|
{
|
|
2384
2386
|
promise: true,
|
|
@@ -2390,9 +2392,9 @@ export class LendMarketTemplate {
|
|
|
2390
2392
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
2391
2393
|
const calls = [];
|
|
2392
2394
|
for (let N = this.minBands; N <= maxN; N++) {
|
|
2393
|
-
calls.push(llamalend.contracts[this.addresses.controller].multicallContract.calculate_debt_n1(_futureStateCollateral, _debt, N));
|
|
2395
|
+
calls.push(this.llamalend.contracts[this.addresses.controller].multicallContract.calculate_debt_n1(_futureStateCollateral, _debt, N));
|
|
2394
2396
|
}
|
|
2395
|
-
return await llamalend.multicallProvider.all(calls) as bigint[];
|
|
2397
|
+
return await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
2396
2398
|
},
|
|
2397
2399
|
{
|
|
2398
2400
|
promise: true,
|
|
@@ -2478,17 +2480,17 @@ export class LendMarketTemplate {
|
|
|
2478
2480
|
dDebt: TAmount,
|
|
2479
2481
|
range: number,
|
|
2480
2482
|
full: boolean,
|
|
2481
|
-
user = llamalend.constants.ZERO_ADDRESS
|
|
2483
|
+
user = this.llamalend.constants.ZERO_ADDRESS
|
|
2482
2484
|
): Promise<string> {
|
|
2483
2485
|
if (range > 0) this._checkRange(range);
|
|
2484
2486
|
const { _totalCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, dDebt, user);
|
|
2485
2487
|
const { _borrowed, _N } = await this._userState(user);
|
|
2486
2488
|
if (_borrowed > BigInt(0)) throw Error(`User ${user} is already in liquidation mode`);
|
|
2487
|
-
if (range < 0) range = Number(llamalend.formatUnits(_N, 0));
|
|
2489
|
+
if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
|
|
2488
2490
|
const _dDebt = parseUnits(dDebt, this.borrowed_token.decimals);
|
|
2489
2491
|
|
|
2490
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
2491
|
-
let _health = await contract.health_calculator(user, _totalCollateral, _dDebt, full, range, llamalend.constantOptions) as bigint;
|
|
2492
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2493
|
+
let _health = await contract.health_calculator(user, _totalCollateral, _dDebt, full, range, this.llamalend.constantOptions) as bigint;
|
|
2492
2494
|
_health = _health * BigInt(100);
|
|
2493
2495
|
|
|
2494
2496
|
return formatUnits(_health);
|
|
@@ -2501,20 +2503,20 @@ export class LendMarketTemplate {
|
|
|
2501
2503
|
|
|
2502
2504
|
private async leverageCreateLoanIsApproved(userCollateral: TAmount, userBorrowed: TAmount): Promise<boolean> {
|
|
2503
2505
|
this._checkLeverageZap();
|
|
2504
|
-
const collateralAllowance = await hasAllowance(
|
|
2505
|
-
[this.collateral_token.address], [userCollateral], llamalend.signerAddress, this.addresses.controller);
|
|
2506
|
-
const borrowedAllowance = await hasAllowance(
|
|
2507
|
-
[this.borrowed_token.address], [userBorrowed], llamalend.signerAddress, llamalend.constants.ALIASES.leverage_zap);
|
|
2506
|
+
const collateralAllowance = await hasAllowance.call(this.llamalend,
|
|
2507
|
+
[this.collateral_token.address], [userCollateral], this.llamalend.signerAddress, this.addresses.controller);
|
|
2508
|
+
const borrowedAllowance = await hasAllowance.call(this.llamalend,
|
|
2509
|
+
[this.borrowed_token.address], [userBorrowed], this.llamalend.signerAddress, this.llamalend.constants.ALIASES.leverage_zap);
|
|
2508
2510
|
|
|
2509
2511
|
return collateralAllowance && borrowedAllowance
|
|
2510
2512
|
}
|
|
2511
2513
|
|
|
2512
2514
|
private async leverageCreateLoanApproveEstimateGas (userCollateral: TAmount, userBorrowed: TAmount): Promise<TGas> {
|
|
2513
2515
|
this._checkLeverageZap();
|
|
2514
|
-
const collateralGas = await ensureAllowanceEstimateGas(
|
|
2516
|
+
const collateralGas = await ensureAllowanceEstimateGas.call(this.llamalend,
|
|
2515
2517
|
[this.collateral_token.address], [userCollateral], this.addresses.controller);
|
|
2516
|
-
const borrowedGas = await ensureAllowanceEstimateGas(
|
|
2517
|
-
[this.borrowed_token.address], [userBorrowed], llamalend.constants.ALIASES.leverage_zap);
|
|
2518
|
+
const borrowedGas = await ensureAllowanceEstimateGas.call(this.llamalend,
|
|
2519
|
+
[this.borrowed_token.address], [userBorrowed], this.llamalend.constants.ALIASES.leverage_zap);
|
|
2518
2520
|
|
|
2519
2521
|
if(Array.isArray(collateralGas) && Array.isArray(borrowedGas)) {
|
|
2520
2522
|
return [collateralGas[0] + borrowedGas[0], collateralGas[1] + borrowedGas[1]]
|
|
@@ -2525,10 +2527,10 @@ export class LendMarketTemplate {
|
|
|
2525
2527
|
|
|
2526
2528
|
private async leverageCreateLoanApprove(userCollateral: TAmount, userBorrowed: TAmount): Promise<string[]> {
|
|
2527
2529
|
this._checkLeverageZap();
|
|
2528
|
-
const collateralApproveTx = await ensureAllowance(
|
|
2530
|
+
const collateralApproveTx = await ensureAllowance.call(this.llamalend,
|
|
2529
2531
|
[this.collateral_token.address], [userCollateral], this.addresses.controller);
|
|
2530
|
-
const borrowedApproveTx = await ensureAllowance(
|
|
2531
|
-
[this.borrowed_token.address], [userBorrowed], llamalend.constants.ALIASES.leverage_zap);
|
|
2532
|
+
const borrowedApproveTx = await ensureAllowance.call(this.llamalend,
|
|
2533
|
+
[this.borrowed_token.address], [userBorrowed], this.llamalend.constants.ALIASES.leverage_zap);
|
|
2532
2534
|
|
|
2533
2535
|
return [...collateralApproveTx, ...borrowedApproveTx]
|
|
2534
2536
|
}
|
|
@@ -2557,29 +2559,29 @@ export class LendMarketTemplate {
|
|
|
2557
2559
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
2558
2560
|
const swapData = this._getSwapDataFromCache(this.addresses.borrowed_token, _debt + _userBorrowed);
|
|
2559
2561
|
if (slippage !== swapData.slippage) throw Error(`You must call leverage.createLoanExpectedCollateral() with slippage=${slippage} first`);
|
|
2560
|
-
const calldata = await _assembleTxOdos(swapData.pathId as string);
|
|
2561
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
2562
|
+
const calldata = await _assembleTxOdos.call(this.llamalend, swapData.pathId as string);
|
|
2563
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2562
2564
|
const gas = await contract.create_loan_extended.estimateGas(
|
|
2563
2565
|
_userCollateral,
|
|
2564
2566
|
_debt,
|
|
2565
2567
|
range,
|
|
2566
|
-
llamalend.constants.ALIASES.leverage_zap,
|
|
2568
|
+
this.llamalend.constants.ALIASES.leverage_zap,
|
|
2567
2569
|
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
2568
2570
|
calldata,
|
|
2569
|
-
{ ...llamalend.constantOptions }
|
|
2571
|
+
{ ...this.llamalend.constantOptions }
|
|
2570
2572
|
);
|
|
2571
2573
|
if (estimateGas) return smartNumber(gas);
|
|
2572
2574
|
|
|
2573
|
-
await llamalend.updateFeeData();
|
|
2575
|
+
await this.llamalend.updateFeeData();
|
|
2574
2576
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
2575
2577
|
return (await contract.create_loan_extended(
|
|
2576
2578
|
_userCollateral,
|
|
2577
2579
|
_debt,
|
|
2578
2580
|
range,
|
|
2579
|
-
llamalend.constants.ALIASES.leverage_zap,
|
|
2581
|
+
this.llamalend.constants.ALIASES.leverage_zap,
|
|
2580
2582
|
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
2581
2583
|
calldata,
|
|
2582
|
-
{ ...llamalend.options, gasLimit }
|
|
2584
|
+
{ ...this.llamalend.options, gasLimit }
|
|
2583
2585
|
)).hash
|
|
2584
2586
|
}
|
|
2585
2587
|
|
|
@@ -2608,12 +2610,12 @@ export class LendMarketTemplate {
|
|
|
2608
2610
|
}> {
|
|
2609
2611
|
// max_borrowable = userCollateral / (1 / (k_effective * max_p_base) - 1 / p_avg)
|
|
2610
2612
|
this._checkLeverageZap();
|
|
2611
|
-
address = _getAddress(address);
|
|
2613
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2612
2614
|
const { _collateral: _stateCollateral, _borrowed: _stateBorrowed, _debt: _stateDebt, _N } = await this._userState(address);
|
|
2613
2615
|
if (_stateBorrowed > BigInt(0)) throw Error(`User ${address} is already in liquidation mode`);
|
|
2614
2616
|
const _userCollateral = parseUnits(userCollateral, this.collateral_token.decimals);
|
|
2615
|
-
const controllerContract = llamalend.contracts[this.addresses.controller].contract;
|
|
2616
|
-
const _borrowedFromStateCollateral = await controllerContract.max_borrowable(_stateCollateral, _N, _stateDebt, llamalend.constantOptions) - _stateDebt;
|
|
2617
|
+
const controllerContract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2618
|
+
const _borrowedFromStateCollateral = await controllerContract.max_borrowable(_stateCollateral, _N, _stateDebt, this.llamalend.constantOptions) - _stateDebt;
|
|
2617
2619
|
const _userBorrowed = _borrowedFromStateCollateral + parseUnits(userBorrowed, this.borrowed_token.decimals);
|
|
2618
2620
|
userBorrowed = formatUnits(_userBorrowed, this.borrowed_token.decimals);
|
|
2619
2621
|
|
|
@@ -2624,7 +2626,7 @@ export class LendMarketTemplate {
|
|
|
2624
2626
|
let _userEffectiveCollateral = BigInt(0);
|
|
2625
2627
|
let _maxLeverageCollateral = BigInt(0);
|
|
2626
2628
|
|
|
2627
|
-
const contract = llamalend.contracts[llamalend.constants.ALIASES.leverage_zap].contract;
|
|
2629
|
+
const contract = this.llamalend.contracts[this.llamalend.constants.ALIASES.leverage_zap].contract;
|
|
2628
2630
|
for (let i = 0; i < 5; i++) {
|
|
2629
2631
|
maxBorrowablePrevBN = maxBorrowableBN;
|
|
2630
2632
|
_userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.collateral_token.decimals);
|
|
@@ -2639,7 +2641,7 @@ export class LendMarketTemplate {
|
|
|
2639
2641
|
}
|
|
2640
2642
|
|
|
2641
2643
|
// additionalCollateral = (userBorrowed / p) + leverageCollateral
|
|
2642
|
-
const _maxAdditionalCollateral = BigInt(await _getExpectedOdos(
|
|
2644
|
+
const _maxAdditionalCollateral = BigInt(await _getExpectedOdos.call(this.llamalend,
|
|
2643
2645
|
this.addresses.borrowed_token, this.addresses.collateral_token, _maxBorrowable + _userBorrowed, this.addresses.amm));
|
|
2644
2646
|
pAvgBN = maxBorrowableBN.plus(userBorrowed).div(toBN(_maxAdditionalCollateral, this.collateral_token.decimals));
|
|
2645
2647
|
_maxLeverageCollateral = _maxAdditionalCollateral - fromBN(BN(userBorrowed).div(pAvgBN), this.collateral_token.decimals);
|
|
@@ -2647,7 +2649,7 @@ export class LendMarketTemplate {
|
|
|
2647
2649
|
|
|
2648
2650
|
if (maxBorrowableBN.eq(0)) _userEffectiveCollateral = BigInt(0);
|
|
2649
2651
|
const _maxTotalCollateral = _userEffectiveCollateral + _maxLeverageCollateral
|
|
2650
|
-
let _maxBorrowable = await controllerContract.max_borrowable(_stateCollateral + _maxTotalCollateral, _N, _stateDebt, llamalend.constantOptions) - _stateDebt;
|
|
2652
|
+
let _maxBorrowable = await controllerContract.max_borrowable(_stateCollateral + _maxTotalCollateral, _N, _stateDebt, this.llamalend.constantOptions) - _stateDebt;
|
|
2651
2653
|
_maxBorrowable = _maxBorrowable * BigInt(998) / BigInt(1000);
|
|
2652
2654
|
|
|
2653
2655
|
return {
|
|
@@ -2663,7 +2665,7 @@ export class LendMarketTemplate {
|
|
|
2663
2665
|
private async leverageBorrowMoreExpectedCollateral(userCollateral: TAmount, userBorrowed: TAmount, dDebt: TAmount, slippage = 0.1, address = ""):
|
|
2664
2666
|
Promise<{ totalCollateral: string, userCollateral: string, collateralFromUserBorrowed: string, collateralFromDebt: string, avgPrice: string }> {
|
|
2665
2667
|
this._checkLeverageZap();
|
|
2666
|
-
address = _getAddress(address);
|
|
2668
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2667
2669
|
const _dDebt = parseUnits(dDebt, this.borrowed_token.decimals);
|
|
2668
2670
|
const _userBorrowed = parseUnits(userBorrowed, this.borrowed_token.decimals);
|
|
2669
2671
|
await this._setSwapDataToCache(this.addresses.borrowed_token, this.addresses.collateral_token, _dDebt + _userBorrowed, slippage);
|
|
@@ -2686,7 +2688,7 @@ export class LendMarketTemplate {
|
|
|
2686
2688
|
}
|
|
2687
2689
|
|
|
2688
2690
|
private async leverageBorrowMoreBands(userCollateral: TAmount, userBorrowed: TAmount, dDebt: TAmount, address = ""): Promise<[number, number]> {
|
|
2689
|
-
address = _getAddress(address);
|
|
2691
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2690
2692
|
this._checkLeverageZap();
|
|
2691
2693
|
const [_n2, _n1] = await this._leverageBands(userCollateral, userBorrowed, dDebt, -1, address);
|
|
2692
2694
|
|
|
@@ -2694,7 +2696,7 @@ export class LendMarketTemplate {
|
|
|
2694
2696
|
}
|
|
2695
2697
|
|
|
2696
2698
|
private async leverageBorrowMorePrices(userCollateral: TAmount, userBorrowed: TAmount, dDebt: TAmount, address = ""): Promise<string[]> {
|
|
2697
|
-
address = _getAddress(address);
|
|
2699
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2698
2700
|
this._checkLeverageZap();
|
|
2699
2701
|
const [_n2, _n1] = await this._leverageBands(userCollateral, userBorrowed, dDebt, -1, address);
|
|
2700
2702
|
|
|
@@ -2703,7 +2705,7 @@ export class LendMarketTemplate {
|
|
|
2703
2705
|
|
|
2704
2706
|
private async leverageBorrowMoreHealth(userCollateral: TAmount, userBorrowed: TAmount, dDebt: TAmount, full = true, address = ""): Promise<string> {
|
|
2705
2707
|
this._checkLeverageZap();
|
|
2706
|
-
address = _getAddress(address);
|
|
2708
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2707
2709
|
return await this._leverageHealth(userCollateral, userBorrowed, dDebt, -1, full, address);
|
|
2708
2710
|
}
|
|
2709
2711
|
|
|
@@ -2728,28 +2730,28 @@ export class LendMarketTemplate {
|
|
|
2728
2730
|
const _debt = parseUnits(debt, this.borrowed_token.decimals);
|
|
2729
2731
|
const swapData = this._getSwapDataFromCache(this.addresses.borrowed_token, _debt + _userBorrowed);
|
|
2730
2732
|
if (slippage !== swapData.slippage) throw Error(`You must call leverage.borrowMoreExpectedCollateral() with slippage=${slippage} first`)
|
|
2731
|
-
const calldata = await _assembleTxOdos(swapData.pathId as string);
|
|
2732
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
2733
|
+
const calldata = await _assembleTxOdos.call(this.llamalend, swapData.pathId as string);
|
|
2734
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2733
2735
|
const gas = await contract.borrow_more_extended.estimateGas(
|
|
2734
2736
|
_userCollateral,
|
|
2735
2737
|
_debt,
|
|
2736
|
-
llamalend.constants.ALIASES.leverage_zap,
|
|
2738
|
+
this.llamalend.constants.ALIASES.leverage_zap,
|
|
2737
2739
|
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
2738
2740
|
calldata,
|
|
2739
|
-
{ ...llamalend.constantOptions }
|
|
2741
|
+
{ ...this.llamalend.constantOptions }
|
|
2740
2742
|
);
|
|
2741
2743
|
if (estimateGas) return smartNumber(gas);
|
|
2742
2744
|
|
|
2743
|
-
await llamalend.updateFeeData();
|
|
2745
|
+
await this.llamalend.updateFeeData();
|
|
2744
2746
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
2745
2747
|
|
|
2746
2748
|
return (await contract.borrow_more_extended(
|
|
2747
2749
|
_userCollateral,
|
|
2748
2750
|
_debt,
|
|
2749
|
-
llamalend.constants.ALIASES.leverage_zap,
|
|
2751
|
+
this.llamalend.constants.ALIASES.leverage_zap,
|
|
2750
2752
|
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
2751
2753
|
calldata,
|
|
2752
|
-
{ ...llamalend.options, gasLimit }
|
|
2754
|
+
{ ...this.llamalend.options, gasLimit }
|
|
2753
2755
|
)).hash
|
|
2754
2756
|
}
|
|
2755
2757
|
|
|
@@ -2819,7 +2821,7 @@ export class LendMarketTemplate {
|
|
|
2819
2821
|
|
|
2820
2822
|
private async leverageRepayIsFull(stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, address = ""): Promise<boolean> {
|
|
2821
2823
|
this._checkLeverageZap();
|
|
2822
|
-
address = _getAddress(address);
|
|
2824
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2823
2825
|
const { _borrowed: _stateBorrowed, _debt } = await this._userState(address);
|
|
2824
2826
|
const { _totalBorrowed } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed);
|
|
2825
2827
|
|
|
@@ -2832,7 +2834,7 @@ export class LendMarketTemplate {
|
|
|
2832
2834
|
// 2. If user is underwater (stablecoin > 0), only full repayment is available:
|
|
2833
2835
|
// await this.deleverageRepayStablecoins(deleverageCollateral) + stablecoin > debt
|
|
2834
2836
|
this._checkLeverageZap();
|
|
2835
|
-
address = _getAddress(address);
|
|
2837
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2836
2838
|
const { collateral, borrowed, debt } = await this.userState(address);
|
|
2837
2839
|
// Loan does not exist
|
|
2838
2840
|
if (BN(debt).eq(0)) return false;
|
|
@@ -2845,7 +2847,7 @@ export class LendMarketTemplate {
|
|
|
2845
2847
|
}
|
|
2846
2848
|
|
|
2847
2849
|
private _leverageRepayBands = memoize( async (stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, address: string): Promise<[bigint, bigint]> => {
|
|
2848
|
-
address = _getAddress(address);
|
|
2850
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2849
2851
|
if (!(await this.leverageRepayIsAvailable(stateCollateral, userCollateral, userBorrowed, address))) return [parseUnits(0, 0), parseUnits(0, 0)];
|
|
2850
2852
|
|
|
2851
2853
|
const _stateRepayCollateral = parseUnits(stateCollateral, this.collateral_token.decimals);
|
|
@@ -2857,7 +2859,7 @@ export class LendMarketTemplate {
|
|
|
2857
2859
|
let _n2 = parseUnits(0, 0);
|
|
2858
2860
|
const { _totalBorrowed: _repayExpected } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed);
|
|
2859
2861
|
try {
|
|
2860
|
-
_n1 = await llamalend.contracts[this.addresses.controller].contract.calculate_debt_n1(_stateCollateral - _stateRepayCollateral, _stateDebt - _repayExpected, _N);
|
|
2862
|
+
_n1 = await this.llamalend.contracts[this.addresses.controller].contract.calculate_debt_n1(_stateCollateral - _stateRepayCollateral, _stateDebt - _repayExpected, _N);
|
|
2861
2863
|
_n2 = _n1 + (_N - BigInt(1));
|
|
2862
2864
|
} catch {
|
|
2863
2865
|
console.log("Full repayment");
|
|
@@ -2886,7 +2888,7 @@ export class LendMarketTemplate {
|
|
|
2886
2888
|
|
|
2887
2889
|
private async leverageRepayHealth(stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, full = true, address = ""): Promise<string> {
|
|
2888
2890
|
this._checkLeverageZap();
|
|
2889
|
-
address = _getAddress(address);
|
|
2891
|
+
address = _getAddress.call(this.llamalend, address);
|
|
2890
2892
|
const { _borrowed: _stateBorrowed, _debt, _N } = await this._userState(address);
|
|
2891
2893
|
if (_stateBorrowed > BigInt(0)) return "0.0";
|
|
2892
2894
|
if (!(await this.leverageRepayIsAvailable(stateCollateral, userCollateral, userBorrowed, address))) return "0.0";
|
|
@@ -2896,38 +2898,38 @@ export class LendMarketTemplate {
|
|
|
2896
2898
|
const _dDebt = _totalBorrowed * BigInt(-1);
|
|
2897
2899
|
|
|
2898
2900
|
if (_debt + _dDebt <= BigInt(0)) return "0.0";
|
|
2899
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
2900
|
-
let _health = await contract.health_calculator(address, _dCollateral, _dDebt, full, _N, llamalend.constantOptions) as bigint;
|
|
2901
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2902
|
+
let _health = await contract.health_calculator(address, _dCollateral, _dDebt, full, _N, this.llamalend.constantOptions) as bigint;
|
|
2901
2903
|
_health = _health * BigInt(100);
|
|
2902
2904
|
|
|
2903
|
-
return llamalend.formatUnits(_health);
|
|
2905
|
+
return this.llamalend.formatUnits(_health);
|
|
2904
2906
|
}
|
|
2905
2907
|
|
|
2906
2908
|
private async leverageRepayIsApproved(userCollateral: TAmount, userBorrowed: TAmount): Promise<boolean> {
|
|
2907
2909
|
this._checkLeverageZap();
|
|
2908
|
-
return await hasAllowance(
|
|
2910
|
+
return await hasAllowance.call(this.llamalend,
|
|
2909
2911
|
[this.collateral_token.address, this.borrowed_token.address],
|
|
2910
2912
|
[userCollateral, userBorrowed],
|
|
2911
|
-
llamalend.signerAddress,
|
|
2912
|
-
llamalend.constants.ALIASES.leverage_zap
|
|
2913
|
+
this.llamalend.signerAddress,
|
|
2914
|
+
this.llamalend.constants.ALIASES.leverage_zap
|
|
2913
2915
|
);
|
|
2914
2916
|
}
|
|
2915
2917
|
|
|
2916
2918
|
private async leverageRepayApproveEstimateGas (userCollateral: TAmount, userBorrowed: TAmount): Promise<TGas> {
|
|
2917
2919
|
this._checkLeverageZap();
|
|
2918
|
-
return await ensureAllowanceEstimateGas(
|
|
2920
|
+
return await ensureAllowanceEstimateGas.call(this.llamalend,
|
|
2919
2921
|
[this.collateral_token.address, this.borrowed_token.address],
|
|
2920
2922
|
[userCollateral, userBorrowed],
|
|
2921
|
-
llamalend.constants.ALIASES.leverage_zap
|
|
2923
|
+
this.llamalend.constants.ALIASES.leverage_zap
|
|
2922
2924
|
);
|
|
2923
2925
|
}
|
|
2924
2926
|
|
|
2925
2927
|
private async leverageRepayApprove(userCollateral: TAmount, userBorrowed: TAmount): Promise<string[]> {
|
|
2926
2928
|
this._checkLeverageZap();
|
|
2927
|
-
return await ensureAllowance(
|
|
2929
|
+
return await ensureAllowance.call(this.llamalend,
|
|
2928
2930
|
[this.collateral_token.address, this.borrowed_token.address],
|
|
2929
2931
|
[userCollateral, userBorrowed],
|
|
2930
|
-
llamalend.constants.ALIASES.leverage_zap
|
|
2932
|
+
this.llamalend.constants.ALIASES.leverage_zap
|
|
2931
2933
|
);
|
|
2932
2934
|
}
|
|
2933
2935
|
|
|
@@ -2954,26 +2956,26 @@ export class LendMarketTemplate {
|
|
|
2954
2956
|
if (_stateCollateral + _userCollateral > BigInt(0)) {
|
|
2955
2957
|
const swapData = this._getSwapDataFromCache(this.addresses.collateral_token, _stateCollateral + _userCollateral);
|
|
2956
2958
|
if (slippage !== swapData.slippage) throw Error(`You must call leverage.repayExpectedBorrowed() with slippage=${slippage} first`)
|
|
2957
|
-
calldata = await _assembleTxOdos(swapData.pathId as string);
|
|
2959
|
+
calldata = await _assembleTxOdos.call(this.llamalend, swapData.pathId as string);
|
|
2958
2960
|
}
|
|
2959
2961
|
|
|
2960
2962
|
console.log('params', [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed], calldata)
|
|
2961
|
-
const contract = llamalend.contracts[this.addresses.controller].contract;
|
|
2963
|
+
const contract = this.llamalend.contracts[this.addresses.controller].contract;
|
|
2962
2964
|
const gas = await contract.repay_extended.estimateGas(
|
|
2963
|
-
llamalend.constants.ALIASES.leverage_zap,
|
|
2965
|
+
this.llamalend.constants.ALIASES.leverage_zap,
|
|
2964
2966
|
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed],
|
|
2965
2967
|
calldata
|
|
2966
2968
|
);
|
|
2967
2969
|
if (estimateGas) return smartNumber(gas);
|
|
2968
2970
|
|
|
2969
|
-
await llamalend.updateFeeData();
|
|
2971
|
+
await this.llamalend.updateFeeData();
|
|
2970
2972
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
2971
2973
|
|
|
2972
2974
|
return (await contract.repay_extended(
|
|
2973
|
-
llamalend.constants.ALIASES.leverage_zap,
|
|
2975
|
+
this.llamalend.constants.ALIASES.leverage_zap,
|
|
2974
2976
|
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed],
|
|
2975
2977
|
calldata,
|
|
2976
|
-
{ ...llamalend.options, gasLimit }
|
|
2978
|
+
{ ...this.llamalend.options, gasLimit }
|
|
2977
2979
|
)).hash
|
|
2978
2980
|
}
|
|
2979
2981
|
|
|
@@ -2990,9 +2992,9 @@ export class LendMarketTemplate {
|
|
|
2990
2992
|
}
|
|
2991
2993
|
|
|
2992
2994
|
public async currentLeverage(userAddress = ''): Promise<string> {
|
|
2993
|
-
userAddress = _getAddress(userAddress);
|
|
2995
|
+
userAddress = _getAddress.call(this.llamalend, userAddress);
|
|
2994
2996
|
const [userCollateral, {collateral}] = await Promise.all([
|
|
2995
|
-
_getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
|
|
2997
|
+
_getUserCollateral(this.llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress),
|
|
2996
2998
|
this.userState(userAddress),
|
|
2997
2999
|
]);
|
|
2998
3000
|
|
|
@@ -3002,14 +3004,14 @@ export class LendMarketTemplate {
|
|
|
3002
3004
|
}
|
|
3003
3005
|
|
|
3004
3006
|
public async currentPnL(userAddress = ''): Promise<Record<string, string>> {
|
|
3005
|
-
userAddress = _getAddress(userAddress);
|
|
3007
|
+
userAddress = _getAddress.call(this.llamalend, userAddress);
|
|
3006
3008
|
|
|
3007
3009
|
const calls = [
|
|
3008
|
-
llamalend.contracts[this.addresses.controller].multicallContract.user_state(userAddress, llamalend.constantOptions),
|
|
3009
|
-
llamalend.contracts[this.addresses.amm].multicallContract.price_oracle(userAddress),
|
|
3010
|
+
this.llamalend.contracts[this.addresses.controller].multicallContract.user_state(userAddress, this.llamalend.constantOptions),
|
|
3011
|
+
this.llamalend.contracts[this.addresses.amm].multicallContract.price_oracle(userAddress),
|
|
3010
3012
|
];
|
|
3011
3013
|
|
|
3012
|
-
const [userState, oraclePrice] = await llamalend.multicallProvider.all(calls) as [bigint[],bigint];
|
|
3014
|
+
const [userState, oraclePrice] = await this.llamalend.multicallProvider.all(calls) as [bigint[],bigint];
|
|
3013
3015
|
|
|
3014
3016
|
if(!(userState || oraclePrice)) {
|
|
3015
3017
|
throw new Error('Multicall error')
|
|
@@ -3017,16 +3019,16 @@ export class LendMarketTemplate {
|
|
|
3017
3019
|
|
|
3018
3020
|
const debt = userState[2];
|
|
3019
3021
|
|
|
3020
|
-
const userCollateral = await _getUserCollateral(llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress);
|
|
3022
|
+
const userCollateral = await _getUserCollateral(this.llamalend.constants.NETWORK_NAME, this.addresses.controller, userAddress);
|
|
3021
3023
|
const totalDepositUsdValueFull = userCollateral.total_deposit_usd_value;
|
|
3022
3024
|
const totalDepositUsdValueUser = userCollateral.total_deposit_from_user_usd_value;
|
|
3023
3025
|
const totalBorrowed = userCollateral.total_borrowed;
|
|
3024
3026
|
|
|
3025
|
-
const oraclePriceFormatted = llamalend.formatUnits(oraclePrice, 18);
|
|
3026
|
-
const debtFormatted = llamalend.formatUnits(debt, 18);
|
|
3027
|
+
const oraclePriceFormatted = this.llamalend.formatUnits(oraclePrice, 18);
|
|
3028
|
+
const debtFormatted = this.llamalend.formatUnits(debt, 18);
|
|
3027
3029
|
|
|
3028
3030
|
const {_collateral: AmmCollateral, _borrowed: AmmBorrowed} = await this._userState(userAddress)
|
|
3029
|
-
const [AmmCollateralFormatted, AmmBorrowedFormatted] = [llamalend.formatUnits(AmmCollateral, this.collateral_token.decimals), llamalend.formatUnits(AmmBorrowed, this.borrowed_token.decimals)];
|
|
3031
|
+
const [AmmCollateralFormatted, AmmBorrowedFormatted] = [this.llamalend.formatUnits(AmmCollateral, this.collateral_token.decimals), this.llamalend.formatUnits(AmmBorrowed, this.borrowed_token.decimals)];
|
|
3030
3032
|
|
|
3031
3033
|
const a = BN(AmmCollateralFormatted).times(oraclePriceFormatted);
|
|
3032
3034
|
const b = BN(totalBorrowed).minus(debtFormatted)
|