@curvefi/llamalend-api 2.0.28 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/constants/aliases.js +10 -7
- package/lib/lendMarkets/modules/common/leverageZapV1Base.js +16 -16
- package/lib/lendMarkets/modules/common/leverageZapV2Base.d.ts +12 -2
- package/lib/lendMarkets/modules/common/leverageZapV2Base.js +25 -48
- package/lib/lendMarkets/modules/v1/leverageV1ZapV2.d.ts +11 -0
- package/lib/lendMarkets/modules/v1/leverageV1ZapV2.js +78 -0
- package/lib/lendMarkets/modules/v2/leverageV2ZapV2.d.ts +11 -0
- package/lib/lendMarkets/modules/v2/leverageV2ZapV2.js +112 -0
- package/lib/llamalend.js +2 -1
- package/lib/mintMarkets/MintMarketTemplate.js +1 -0
- package/lib/mintMarkets/fetch/fetchMintMarkets.js +1 -1
- package/lib/utils.d.ts +14 -0
- package/lib/utils.js +9 -0
- package/package.json +1 -1
- package/src/constants/aliases.ts +10 -7
- package/src/lendMarkets/modules/common/leverageZapV1Base.ts +16 -16
- package/src/lendMarkets/modules/common/leverageZapV2Base.ts +81 -95
- package/src/lendMarkets/modules/v1/leverageV1ZapV2.ts +154 -1
- package/src/lendMarkets/modules/v2/leverageV2ZapV2.ts +200 -1
- package/src/llamalend.ts +3 -1
- package/src/mintMarkets/MintMarketTemplate.ts +1 -0
- package/src/mintMarkets/fetch/fetchMintMarkets.ts +1 -1
- package/src/utils.ts +31 -1
|
@@ -11,11 +11,7 @@ import {
|
|
|
11
11
|
hasAllowance,
|
|
12
12
|
ensureAllowanceEstimateGas,
|
|
13
13
|
formatUnits,
|
|
14
|
-
smartNumber,
|
|
15
14
|
formatNumber,
|
|
16
|
-
_mulBy1_3,
|
|
17
|
-
DIGas,
|
|
18
|
-
buildCalldataForLeverageZapV2,
|
|
19
15
|
} from "../../../utils";
|
|
20
16
|
import {Llamalend} from "../../../llamalend";
|
|
21
17
|
import BigNumber from "bignumber.js";
|
|
@@ -27,7 +23,7 @@ import BigNumber from "bignumber.js";
|
|
|
27
23
|
* - prices
|
|
28
24
|
* - userPosition
|
|
29
25
|
*/
|
|
30
|
-
export class LeverageZapV2BaseModule {
|
|
26
|
+
export abstract class LeverageZapV2BaseModule {
|
|
31
27
|
protected market: LendMarketTemplate;
|
|
32
28
|
protected llamalend: Llamalend;
|
|
33
29
|
|
|
@@ -36,12 +32,62 @@ export class LeverageZapV2BaseModule {
|
|
|
36
32
|
this.llamalend = market.getLlamalend();
|
|
37
33
|
}
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
protected abstract _getLeverageZapAddress(): string;
|
|
36
|
+
|
|
37
|
+
protected abstract _calcCreateLoanHealthCall(_collateral: bigint, _dDebt: bigint, N: number | bigint, full: boolean): Promise<bigint>;
|
|
38
|
+
|
|
39
|
+
protected abstract _calcBorrowMoreHealthCall(_collateral: bigint, _dDebt: bigint, N: number | bigint, user: string, full: boolean): Promise<bigint>;
|
|
40
|
+
|
|
41
|
+
protected abstract _calcRepayHealthCall(_dCollateral: bigint, _dDebt: bigint, N: number | bigint, user: string, full: boolean): Promise<bigint>;
|
|
42
|
+
|
|
43
|
+
protected abstract _calcDebtN1Call(_collateral: bigint, _debt: bigint, N: number | bigint): Promise<bigint>;
|
|
44
|
+
|
|
45
|
+
protected abstract _calcDebtN1MulticallCall(_collateral: bigint, _debt: bigint, N: number | bigint): any;
|
|
46
|
+
|
|
47
|
+
protected abstract _createLoanContractCall(
|
|
48
|
+
_userCollateral: bigint,
|
|
49
|
+
_userBorrowed: bigint,
|
|
50
|
+
_debt: bigint,
|
|
51
|
+
_minRecv: bigint,
|
|
52
|
+
range: number,
|
|
53
|
+
router: string,
|
|
54
|
+
exchangeCalldata: string,
|
|
55
|
+
estimateGas: boolean
|
|
56
|
+
): Promise<string | TGas>;
|
|
57
|
+
|
|
58
|
+
protected abstract _borrowMoreContractCall(
|
|
59
|
+
_userCollateral: bigint,
|
|
60
|
+
_userBorrowed: bigint,
|
|
61
|
+
_debt: bigint,
|
|
62
|
+
_minRecv: bigint,
|
|
63
|
+
router: string,
|
|
64
|
+
exchangeCalldata: string,
|
|
65
|
+
estimateGas: boolean
|
|
66
|
+
): Promise<string | TGas>;
|
|
67
|
+
|
|
68
|
+
protected abstract _repayContractCall(
|
|
69
|
+
_userCollateral: bigint,
|
|
70
|
+
_userBorrowed: bigint,
|
|
71
|
+
_minRecv: bigint,
|
|
72
|
+
router: string,
|
|
73
|
+
exchangeCalldata: string,
|
|
74
|
+
estimateGas: boolean
|
|
75
|
+
): Promise<string | TGas>;
|
|
76
|
+
|
|
77
|
+
protected abstract _getMaxAdditionalBorrowable(
|
|
78
|
+
_stateCollateral: bigint,
|
|
79
|
+
_dCollateral: bigint,
|
|
80
|
+
_N: bigint,
|
|
81
|
+
_stateDebt: bigint,
|
|
82
|
+
address: string,
|
|
83
|
+
): Promise<bigint>;
|
|
84
|
+
|
|
85
|
+
protected _getMarketId = (): number => Number(this.market.id.split("-").slice(-1)[0]);
|
|
40
86
|
|
|
41
87
|
// ============ CREATE LOAN METHODS ============
|
|
42
88
|
|
|
43
89
|
public hasLeverage = (): boolean => {
|
|
44
|
-
return this.
|
|
90
|
+
return this._getLeverageZapAddress() !== this.llamalend.constants.ZERO_ADDRESS &&
|
|
45
91
|
this._getMarketId() >= Number(this.llamalend.constants.ALIASES["leverage_markets_start_id"]);
|
|
46
92
|
}
|
|
47
93
|
|
|
@@ -106,7 +152,7 @@ export class LeverageZapV2BaseModule {
|
|
|
106
152
|
let _userEffectiveCollateral = BigInt(0);
|
|
107
153
|
let _maxLeverageCollateral = BigInt(0);
|
|
108
154
|
|
|
109
|
-
const contract = this.llamalend.contracts[this.
|
|
155
|
+
const contract = this.llamalend.contracts[this._getLeverageZapAddress()].contract;
|
|
110
156
|
for (let i = 0; i < 5; i++) {
|
|
111
157
|
maxBorrowablePrevBN = maxBorrowableBN;
|
|
112
158
|
_userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.market.collateral_token.decimals);
|
|
@@ -162,7 +208,7 @@ export class LeverageZapV2BaseModule {
|
|
|
162
208
|
}>> => {
|
|
163
209
|
this._checkLeverageZap();
|
|
164
210
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
165
|
-
const contract = this.llamalend.contracts[this.
|
|
211
|
+
const contract = this.llamalend.contracts[this._getLeverageZapAddress()].multicallContract;
|
|
166
212
|
|
|
167
213
|
const oraclePriceBand = await this.market.prices.oraclePriceBand();
|
|
168
214
|
const pAvgApproxBN = BN(await this.market.prices.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
@@ -331,7 +377,7 @@ export class LeverageZapV2BaseModule {
|
|
|
331
377
|
}
|
|
332
378
|
const { _futureStateCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, debt, quote, user);
|
|
333
379
|
const _debt = _stateDebt + parseUnits(debt, this.market.borrowed_token.decimals);
|
|
334
|
-
return await this.
|
|
380
|
+
return await this._calcDebtN1Call(_futureStateCollateral, _debt, range);
|
|
335
381
|
},
|
|
336
382
|
{
|
|
337
383
|
promise: true,
|
|
@@ -343,7 +389,7 @@ export class LeverageZapV2BaseModule {
|
|
|
343
389
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
344
390
|
const calls = [];
|
|
345
391
|
for (let N = this.market.minBands; N <= maxN; N++) {
|
|
346
|
-
calls.push(this.
|
|
392
|
+
calls.push(this._calcDebtN1MulticallCall(_futureStateCollateral, _debt, N));
|
|
347
393
|
}
|
|
348
394
|
return await this.llamalend.multicallProvider.all(calls) as bigint[];
|
|
349
395
|
},
|
|
@@ -439,8 +485,10 @@ export class LeverageZapV2BaseModule {
|
|
|
439
485
|
if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
|
|
440
486
|
const _dDebt = parseUnits(dDebt, this.market.borrowed_token.decimals);
|
|
441
487
|
|
|
442
|
-
const
|
|
443
|
-
let _health =
|
|
488
|
+
const isCreateLoan = user === this.llamalend.constants.ZERO_ADDRESS;
|
|
489
|
+
let _health = isCreateLoan
|
|
490
|
+
? await this._calcCreateLoanHealthCall(_totalCollateral, _dDebt, range, full)
|
|
491
|
+
: await this._calcBorrowMoreHealthCall(_totalCollateral, _dDebt, range, user, full);
|
|
444
492
|
_health = _health * BigInt(100);
|
|
445
493
|
|
|
446
494
|
return formatUnits(_health);
|
|
@@ -454,7 +502,7 @@ export class LeverageZapV2BaseModule {
|
|
|
454
502
|
const collateralAllowance = await hasAllowance.call(this.llamalend,
|
|
455
503
|
[this.market.collateral_token.address], [userCollateral], this.llamalend.signerAddress, this.market.addresses.controller);
|
|
456
504
|
const borrowedAllowance = await hasAllowance.call(this.llamalend,
|
|
457
|
-
[this.market.borrowed_token.address], [userBorrowed], this.llamalend.signerAddress, this.
|
|
505
|
+
[this.market.borrowed_token.address], [userBorrowed], this.llamalend.signerAddress, this._getLeverageZapAddress());
|
|
458
506
|
|
|
459
507
|
return collateralAllowance && borrowedAllowance
|
|
460
508
|
}
|
|
@@ -467,7 +515,7 @@ export class LeverageZapV2BaseModule {
|
|
|
467
515
|
const collateralGas = await ensureAllowanceEstimateGas.call(this.llamalend,
|
|
468
516
|
[this.market.collateral_token.address], [userCollateral], this.market.addresses.controller);
|
|
469
517
|
const borrowedGas = await ensureAllowanceEstimateGas.call(this.llamalend,
|
|
470
|
-
[this.market.borrowed_token.address], [userBorrowed], this.
|
|
518
|
+
[this.market.borrowed_token.address], [userBorrowed], this._getLeverageZapAddress());
|
|
471
519
|
|
|
472
520
|
if(Array.isArray(collateralGas) && Array.isArray(borrowedGas)) {
|
|
473
521
|
return [collateralGas[0] + borrowedGas[0], collateralGas[1] + borrowedGas[1]]
|
|
@@ -484,7 +532,7 @@ export class LeverageZapV2BaseModule {
|
|
|
484
532
|
const collateralApproveTx = await ensureAllowance.call(this.llamalend,
|
|
485
533
|
[this.market.collateral_token.address], [userCollateral], this.market.addresses.controller);
|
|
486
534
|
const borrowedApproveTx = await ensureAllowance.call(this.llamalend,
|
|
487
|
-
[this.market.borrowed_token.address], [userBorrowed], this.
|
|
535
|
+
[this.market.borrowed_token.address], [userBorrowed], this._getLeverageZapAddress());
|
|
488
536
|
|
|
489
537
|
return [...collateralApproveTx, ...borrowedApproveTx]
|
|
490
538
|
}
|
|
@@ -507,31 +555,9 @@ export class LeverageZapV2BaseModule {
|
|
|
507
555
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
508
556
|
const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
|
|
509
557
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
513
|
-
const gas = await contract.create_loan_extended.estimateGas(
|
|
514
|
-
_userCollateral,
|
|
515
|
-
_debt,
|
|
516
|
-
range,
|
|
517
|
-
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
518
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
519
|
-
zapCalldata,
|
|
520
|
-
{ ...this.llamalend.constantOptions }
|
|
558
|
+
return await this._createLoanContractCall(
|
|
559
|
+
_userCollateral, _userBorrowed, _debt, _minRecv, range, router, calldata, estimateGas
|
|
521
560
|
);
|
|
522
|
-
if (estimateGas) return smartNumber(gas);
|
|
523
|
-
|
|
524
|
-
await this.llamalend.updateFeeData();
|
|
525
|
-
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
526
|
-
return (await contract.create_loan_extended(
|
|
527
|
-
_userCollateral,
|
|
528
|
-
_debt,
|
|
529
|
-
range,
|
|
530
|
-
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
531
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
532
|
-
zapCalldata,
|
|
533
|
-
{ ...this.llamalend.options, gasLimit }
|
|
534
|
-
)).hash
|
|
535
561
|
}
|
|
536
562
|
|
|
537
563
|
public async leverageCreateLoanEstimateGas({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
@@ -583,8 +609,7 @@ export class LeverageZapV2BaseModule {
|
|
|
583
609
|
const { _collateral: _stateCollateral, _borrowed: _stateBorrowed, _debt: _stateDebt, _N } = await this.market.userPosition.userStateBigInt(address);
|
|
584
610
|
if (_stateBorrowed > BigInt(0)) throw Error(`User ${address} is already in liquidation mode`);
|
|
585
611
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
586
|
-
const
|
|
587
|
-
const _borrowedFromStateCollateral = await controllerContract.max_borrowable(_stateCollateral, _N, _stateDebt, this.llamalend.constantOptions) - _stateDebt;
|
|
612
|
+
const _borrowedFromStateCollateral = await this._getMaxAdditionalBorrowable(_stateCollateral, BigInt(0), _N, _stateDebt, address);
|
|
588
613
|
const _userBorrowed = _borrowedFromStateCollateral + parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
589
614
|
userBorrowed = formatUnits(_userBorrowed, this.market.borrowed_token.decimals);
|
|
590
615
|
|
|
@@ -595,7 +620,7 @@ export class LeverageZapV2BaseModule {
|
|
|
595
620
|
let _userEffectiveCollateral = BigInt(0);
|
|
596
621
|
let _maxLeverageCollateral = BigInt(0);
|
|
597
622
|
|
|
598
|
-
const contract = this.llamalend.contracts[this.
|
|
623
|
+
const contract = this.llamalend.contracts[this._getLeverageZapAddress()].contract;
|
|
599
624
|
for (let i = 0; i < 5; i++) {
|
|
600
625
|
maxBorrowablePrevBN = maxBorrowableBN;
|
|
601
626
|
_userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.market.collateral_token.decimals);
|
|
@@ -618,7 +643,7 @@ export class LeverageZapV2BaseModule {
|
|
|
618
643
|
|
|
619
644
|
if (maxBorrowableBN.eq(0)) _userEffectiveCollateral = BigInt(0);
|
|
620
645
|
const _maxTotalCollateral = _userEffectiveCollateral + _maxLeverageCollateral
|
|
621
|
-
let _maxBorrowable = await
|
|
646
|
+
let _maxBorrowable = await this._getMaxAdditionalBorrowable(_stateCollateral, _maxTotalCollateral, _N, _stateDebt, address);
|
|
622
647
|
_maxBorrowable = _maxBorrowable * BigInt(970) / BigInt(1000);
|
|
623
648
|
|
|
624
649
|
return {
|
|
@@ -692,30 +717,9 @@ export class LeverageZapV2BaseModule {
|
|
|
692
717
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
693
718
|
const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
|
|
694
719
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
698
|
-
const gas = await contract.borrow_more_extended.estimateGas(
|
|
699
|
-
_userCollateral,
|
|
700
|
-
_debt,
|
|
701
|
-
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
702
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
703
|
-
zapCalldata,
|
|
704
|
-
{ ...this.llamalend.constantOptions }
|
|
720
|
+
return await this._borrowMoreContractCall(
|
|
721
|
+
_userCollateral, _userBorrowed, _debt, _minRecv, router, calldata, estimateGas
|
|
705
722
|
);
|
|
706
|
-
if (estimateGas) return smartNumber(gas);
|
|
707
|
-
|
|
708
|
-
await this.llamalend.updateFeeData();
|
|
709
|
-
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
710
|
-
|
|
711
|
-
return (await contract.borrow_more_extended(
|
|
712
|
-
_userCollateral,
|
|
713
|
-
_debt,
|
|
714
|
-
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
715
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
716
|
-
zapCalldata,
|
|
717
|
-
{ ...this.llamalend.options, gasLimit }
|
|
718
|
-
)).hash
|
|
719
723
|
}
|
|
720
724
|
|
|
721
725
|
public async leverageBorrowMoreIsApproved({ userCollateral, userBorrowed }: {
|
|
@@ -904,7 +908,7 @@ export class LeverageZapV2BaseModule {
|
|
|
904
908
|
let _n2 = parseUnits(0, 0);
|
|
905
909
|
const { _totalBorrowed: _repayExpected } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed, quote);
|
|
906
910
|
try {
|
|
907
|
-
_n1 = await this.
|
|
911
|
+
_n1 = await this._calcDebtN1Call(_stateCollateral - _stateRepayCollateral, _stateDebt - _repayExpected, _N);
|
|
908
912
|
_n2 = _n1 + (_N - BigInt(1));
|
|
909
913
|
return [_n2, _n1];
|
|
910
914
|
} catch {
|
|
@@ -929,8 +933,7 @@ export class LeverageZapV2BaseModule {
|
|
|
929
933
|
const _dDebt = _totalBorrowed * BigInt(-1);
|
|
930
934
|
|
|
931
935
|
if (_debt + _dDebt <= BigInt(0)) return "0.0";
|
|
932
|
-
|
|
933
|
-
let _health = await contract.health_calculator(address, _dCollateral, _dDebt, full, _N, this.llamalend.constantOptions) as bigint;
|
|
936
|
+
let _health = await this._calcRepayHealthCall(_dCollateral, _dDebt, _N, address, full);
|
|
934
937
|
_health = _health * BigInt(100);
|
|
935
938
|
|
|
936
939
|
return this.llamalend.formatUnits(_health);
|
|
@@ -945,7 +948,7 @@ export class LeverageZapV2BaseModule {
|
|
|
945
948
|
[this.market.collateral_token.address, this.market.borrowed_token.address],
|
|
946
949
|
[userCollateral, userBorrowed],
|
|
947
950
|
this.llamalend.signerAddress,
|
|
948
|
-
this.
|
|
951
|
+
this._getLeverageZapAddress()
|
|
949
952
|
);
|
|
950
953
|
}
|
|
951
954
|
|
|
@@ -957,7 +960,7 @@ export class LeverageZapV2BaseModule {
|
|
|
957
960
|
return await ensureAllowanceEstimateGas.call(this.llamalend,
|
|
958
961
|
[this.market.collateral_token.address, this.market.borrowed_token.address],
|
|
959
962
|
[userCollateral, userBorrowed],
|
|
960
|
-
this.
|
|
963
|
+
this._getLeverageZapAddress()
|
|
961
964
|
);
|
|
962
965
|
}
|
|
963
966
|
|
|
@@ -969,7 +972,7 @@ export class LeverageZapV2BaseModule {
|
|
|
969
972
|
return await ensureAllowance.call(this.llamalend,
|
|
970
973
|
[this.market.collateral_token.address, this.market.borrowed_token.address],
|
|
971
974
|
[userCollateral, userBorrowed],
|
|
972
|
-
this.
|
|
975
|
+
this._getLeverageZapAddress()
|
|
973
976
|
);
|
|
974
977
|
}
|
|
975
978
|
|
|
@@ -988,28 +991,11 @@ export class LeverageZapV2BaseModule {
|
|
|
988
991
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
989
992
|
const _minRecv = parseUnits(minRecv, this.market.borrowed_token.decimals);
|
|
990
993
|
|
|
991
|
-
|
|
992
|
-
if (_stateCollateral + _userCollateral > BigInt(0)) {
|
|
993
|
-
zapCalldata = buildCalldataForLeverageZapV2(router, calldata)
|
|
994
|
-
}
|
|
994
|
+
const exchangeCalldata = _stateCollateral + _userCollateral > BigInt(0) ? calldata : "0x";
|
|
995
995
|
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
999
|
-
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
|
|
1000
|
-
zapCalldata
|
|
996
|
+
return await this._repayContractCall(
|
|
997
|
+
_userCollateral, _userBorrowed, _minRecv, router, exchangeCalldata, estimateGas
|
|
1001
998
|
);
|
|
1002
|
-
if (estimateGas) return smartNumber(gas);
|
|
1003
|
-
|
|
1004
|
-
await this.llamalend.updateFeeData();
|
|
1005
|
-
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1006
|
-
|
|
1007
|
-
return (await contract.repay_extended(
|
|
1008
|
-
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
1009
|
-
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
|
|
1010
|
-
zapCalldata,
|
|
1011
|
-
{ ...this.llamalend.options, gasLimit }
|
|
1012
|
-
)).hash
|
|
1013
999
|
}
|
|
1014
1000
|
|
|
1015
1001
|
public async leverageRepayEstimateGas({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
@@ -1018,7 +1004,7 @@ export class LeverageZapV2BaseModule {
|
|
|
1018
1004
|
userBorrowed: TAmount,
|
|
1019
1005
|
minRecv: TAmount,
|
|
1020
1006
|
router: string,
|
|
1021
|
-
calldata: string
|
|
1007
|
+
calldata: string,
|
|
1022
1008
|
}): Promise<number> {
|
|
1023
1009
|
this._checkLeverageZap();
|
|
1024
1010
|
if (!(await this.leverageRepayIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
|
|
@@ -1031,7 +1017,7 @@ export class LeverageZapV2BaseModule {
|
|
|
1031
1017
|
userBorrowed: TAmount,
|
|
1032
1018
|
minRecv: TAmount,
|
|
1033
1019
|
router: string,
|
|
1034
|
-
calldata: string
|
|
1020
|
+
calldata: string,
|
|
1035
1021
|
}): Promise<string> {
|
|
1036
1022
|
this._checkLeverageZap();
|
|
1037
1023
|
await this.leverageRepayApprove({ userCollateral, userBorrowed });
|
|
@@ -1,3 +1,156 @@
|
|
|
1
1
|
import { LeverageZapV2BaseModule } from "../common/leverageZapV2Base.js";
|
|
2
|
+
import type { TGas } from "../../../interfaces";
|
|
3
|
+
import {
|
|
4
|
+
parseUnits,
|
|
5
|
+
smartNumber,
|
|
6
|
+
_mulBy1_3,
|
|
7
|
+
DIGas,
|
|
8
|
+
buildCalldataForLeverageZapV2,
|
|
9
|
+
} from "../../../utils";
|
|
2
10
|
|
|
3
|
-
export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
|
|
11
|
+
export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
|
|
12
|
+
protected override _getLeverageZapAddress(): string {
|
|
13
|
+
return this.llamalend.constants.ALIASES.leverage_zap_v2;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
protected override async _getMaxAdditionalBorrowable(
|
|
17
|
+
_stateCollateral: bigint, _dCollateral: bigint, _N: bigint, _stateDebt: bigint
|
|
18
|
+
): Promise<bigint> {
|
|
19
|
+
const result = await this.llamalend.contracts[this.market.addresses.controller].contract.max_borrowable(
|
|
20
|
+
_stateCollateral + _dCollateral, _N, _stateDebt, this.llamalend.constantOptions
|
|
21
|
+
);
|
|
22
|
+
return result - _stateDebt;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected override async _calcDebtN1Call(_collateral: bigint, _debt: bigint, N: number | bigint): Promise<bigint> {
|
|
26
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.calculate_debt_n1(
|
|
27
|
+
_collateral, _debt, N, this.llamalend.constantOptions
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
protected override _calcDebtN1MulticallCall(_collateral: bigint, _debt: bigint, N: number | bigint): any {
|
|
32
|
+
return this.llamalend.contracts[this.market.addresses.controller].multicallContract.calculate_debt_n1(
|
|
33
|
+
_collateral, _debt, N
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected override async _calcCreateLoanHealthCall(
|
|
38
|
+
_collateral: bigint, _dDebt: bigint, N: number | bigint, full: boolean
|
|
39
|
+
): Promise<bigint> {
|
|
40
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.health_calculator(
|
|
41
|
+
this.llamalend.constants.ZERO_ADDRESS, _collateral, _dDebt, full, N, this.llamalend.constantOptions
|
|
42
|
+
) as bigint;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected override async _calcBorrowMoreHealthCall(
|
|
46
|
+
_collateral: bigint, _dDebt: bigint, N: number | bigint, user: string, full: boolean
|
|
47
|
+
): Promise<bigint> {
|
|
48
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.health_calculator(
|
|
49
|
+
user, _collateral, _dDebt, full, N, this.llamalend.constantOptions
|
|
50
|
+
) as bigint;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected override async _calcRepayHealthCall(
|
|
54
|
+
_dCollateral: bigint, _dDebt: bigint, N: number | bigint, user: string, full: boolean
|
|
55
|
+
): Promise<bigint> {
|
|
56
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.health_calculator(
|
|
57
|
+
user, _dCollateral, _dDebt, full, N, this.llamalend.constantOptions
|
|
58
|
+
) as bigint;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected override async _createLoanContractCall(
|
|
62
|
+
_userCollateral: bigint,
|
|
63
|
+
_userBorrowed: bigint,
|
|
64
|
+
_debt: bigint,
|
|
65
|
+
_minRecv: bigint,
|
|
66
|
+
range: number,
|
|
67
|
+
router: string,
|
|
68
|
+
exchangeCalldata: string,
|
|
69
|
+
estimateGas: boolean
|
|
70
|
+
): Promise<string | TGas> {
|
|
71
|
+
const zapCalldata = buildCalldataForLeverageZapV2(router, exchangeCalldata);
|
|
72
|
+
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
73
|
+
const gas = await contract.create_loan_extended.estimateGas(
|
|
74
|
+
_userCollateral,
|
|
75
|
+
_debt,
|
|
76
|
+
range,
|
|
77
|
+
this._getLeverageZapAddress(),
|
|
78
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
79
|
+
zapCalldata,
|
|
80
|
+
{ ...this.llamalend.constantOptions }
|
|
81
|
+
);
|
|
82
|
+
if (estimateGas) return smartNumber(gas);
|
|
83
|
+
|
|
84
|
+
await this.llamalend.updateFeeData();
|
|
85
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
86
|
+
return (await contract.create_loan_extended(
|
|
87
|
+
_userCollateral,
|
|
88
|
+
_debt,
|
|
89
|
+
range,
|
|
90
|
+
this._getLeverageZapAddress(),
|
|
91
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
92
|
+
zapCalldata,
|
|
93
|
+
{ ...this.llamalend.options, gasLimit }
|
|
94
|
+
)).hash;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
protected override async _borrowMoreContractCall(
|
|
98
|
+
_userCollateral: bigint,
|
|
99
|
+
_userBorrowed: bigint,
|
|
100
|
+
_debt: bigint,
|
|
101
|
+
_minRecv: bigint,
|
|
102
|
+
router: string,
|
|
103
|
+
exchangeCalldata: string,
|
|
104
|
+
estimateGas: boolean
|
|
105
|
+
): Promise<string | TGas> {
|
|
106
|
+
const zapCalldata = buildCalldataForLeverageZapV2(router, exchangeCalldata);
|
|
107
|
+
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
108
|
+
const gas = await contract.borrow_more_extended.estimateGas(
|
|
109
|
+
_userCollateral,
|
|
110
|
+
_debt,
|
|
111
|
+
this._getLeverageZapAddress(),
|
|
112
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
113
|
+
zapCalldata,
|
|
114
|
+
{ ...this.llamalend.constantOptions }
|
|
115
|
+
);
|
|
116
|
+
if (estimateGas) return smartNumber(gas);
|
|
117
|
+
|
|
118
|
+
await this.llamalend.updateFeeData();
|
|
119
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
120
|
+
return (await contract.borrow_more_extended(
|
|
121
|
+
_userCollateral,
|
|
122
|
+
_debt,
|
|
123
|
+
this._getLeverageZapAddress(),
|
|
124
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
125
|
+
zapCalldata,
|
|
126
|
+
{ ...this.llamalend.options, gasLimit }
|
|
127
|
+
)).hash;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
protected override async _repayContractCall(
|
|
131
|
+
_userCollateral: bigint,
|
|
132
|
+
_userBorrowed: bigint,
|
|
133
|
+
_minRecv: bigint,
|
|
134
|
+
router: string,
|
|
135
|
+
exchangeCalldata: string,
|
|
136
|
+
estimateGas: boolean
|
|
137
|
+
): Promise<string | TGas> {
|
|
138
|
+
const zapCalldata = buildCalldataForLeverageZapV2(router, exchangeCalldata);
|
|
139
|
+
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
140
|
+
const gas = await contract.repay_extended.estimateGas(
|
|
141
|
+
this._getLeverageZapAddress(),
|
|
142
|
+
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
|
|
143
|
+
zapCalldata
|
|
144
|
+
);
|
|
145
|
+
if (estimateGas) return smartNumber(gas);
|
|
146
|
+
|
|
147
|
+
await this.llamalend.updateFeeData();
|
|
148
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
149
|
+
return (await contract.repay_extended(
|
|
150
|
+
this._getLeverageZapAddress(),
|
|
151
|
+
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
|
|
152
|
+
zapCalldata,
|
|
153
|
+
{ ...this.llamalend.options, gasLimit }
|
|
154
|
+
)).hash;
|
|
155
|
+
}
|
|
156
|
+
}
|