@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
|
@@ -1,3 +1,202 @@
|
|
|
1
1
|
import { LeverageZapV2BaseModule } from "../common/leverageZapV2Base.js";
|
|
2
|
+
import type { TGas } from "../../../interfaces";
|
|
3
|
+
import {
|
|
4
|
+
_getAddress,
|
|
5
|
+
smartNumber,
|
|
6
|
+
_mulBy1_3,
|
|
7
|
+
DIGas,
|
|
8
|
+
MAX_ACTIVE_BAND,
|
|
9
|
+
buildCalldataForLeverageZapV2Llv2,
|
|
10
|
+
} from "../../../utils";
|
|
2
11
|
|
|
3
|
-
export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
|
|
12
|
+
export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
|
|
13
|
+
protected override _getLeverageZapAddress(): string {
|
|
14
|
+
return this.llamalend.constants.ALIASES.leverage_zap_v2_llv2;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
protected override async _getMaxAdditionalBorrowable(
|
|
18
|
+
_stateCollateral: bigint, _dCollateral: bigint, _N: bigint, _stateDebt: bigint, address: string
|
|
19
|
+
): Promise<bigint> {
|
|
20
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.max_borrowable(
|
|
21
|
+
_dCollateral, _N, address, this.llamalend.constantOptions
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected override async _calcDebtN1Call(_collateral: bigint, _debt: bigint, N: number | bigint): Promise<bigint> {
|
|
26
|
+
const address = _getAddress.call(this.llamalend, '');
|
|
27
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.calculate_debt_n1(
|
|
28
|
+
_collateral, _debt, N, address, this.llamalend.constantOptions
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected override _calcDebtN1MulticallCall(_collateral: bigint, _debt: bigint, N: number | bigint): any {
|
|
33
|
+
const address = _getAddress.call(this.llamalend, '');
|
|
34
|
+
return this.llamalend.contracts[this.market.addresses.controller].multicallContract.calculate_debt_n1(
|
|
35
|
+
_collateral, _debt, N, address
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected override async _calcCreateLoanHealthCall(
|
|
40
|
+
_collateral: bigint, _dDebt: bigint, N: number | bigint, full: boolean
|
|
41
|
+
): Promise<bigint> {
|
|
42
|
+
const _for = _getAddress.call(this.llamalend, '');
|
|
43
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.create_loan_health_preview(
|
|
44
|
+
_collateral, _dDebt, N, _for, full, this.llamalend.constantOptions
|
|
45
|
+
) as bigint;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected override async _calcBorrowMoreHealthCall(
|
|
49
|
+
_collateral: bigint, _dDebt: bigint, _N: number | bigint, user: string, full: boolean
|
|
50
|
+
): Promise<bigint> {
|
|
51
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.borrow_more_health_preview(
|
|
52
|
+
_collateral, _dDebt, user, full, this.llamalend.constantOptions
|
|
53
|
+
) as bigint;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
protected override async _calcRepayHealthCall(
|
|
57
|
+
_dCollateral: bigint, _dDebt: bigint, _N: number | bigint, user: string, full: boolean
|
|
58
|
+
): Promise<bigint> {
|
|
59
|
+
const _dCollateralAbs = _dCollateral < BigInt(0) ? -_dCollateral : _dCollateral;
|
|
60
|
+
const _dDebtAbs = _dDebt < BigInt(0) ? -_dDebt : _dDebt;
|
|
61
|
+
const _shrink = false;
|
|
62
|
+
return await this.llamalend.contracts[this.market.addresses.controller].contract.repay_health_preview(
|
|
63
|
+
_dCollateralAbs, _dDebtAbs, user, _shrink, full, this.llamalend.constantOptions
|
|
64
|
+
) as bigint;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected override async _createLoanContractCall(
|
|
68
|
+
_userCollateral: bigint,
|
|
69
|
+
_userBorrowed: bigint,
|
|
70
|
+
_debt: bigint,
|
|
71
|
+
_minRecv: bigint,
|
|
72
|
+
range: number,
|
|
73
|
+
router: string,
|
|
74
|
+
exchangeCalldata: string,
|
|
75
|
+
estimateGas: boolean
|
|
76
|
+
): Promise<string | TGas> {
|
|
77
|
+
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
78
|
+
const _for = _getAddress.call(this.llamalend, '');
|
|
79
|
+
const _callbacker = this._getLeverageZapAddress();
|
|
80
|
+
const zapCalldata = buildCalldataForLeverageZapV2Llv2({
|
|
81
|
+
op: 'create_loan',
|
|
82
|
+
controllerId: this._getMarketId(),
|
|
83
|
+
userBorrowed: _userBorrowed,
|
|
84
|
+
minRecv: _minRecv,
|
|
85
|
+
router,
|
|
86
|
+
exchangeCalldata,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const gas = await contract.create_loan.estimateGas(
|
|
90
|
+
_userCollateral,
|
|
91
|
+
_debt,
|
|
92
|
+
range,
|
|
93
|
+
_for,
|
|
94
|
+
_callbacker,
|
|
95
|
+
zapCalldata,
|
|
96
|
+
{ ...this.llamalend.constantOptions }
|
|
97
|
+
);
|
|
98
|
+
if (estimateGas) return smartNumber(gas);
|
|
99
|
+
|
|
100
|
+
await this.llamalend.updateFeeData();
|
|
101
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
102
|
+
return (await contract.create_loan(
|
|
103
|
+
_userCollateral,
|
|
104
|
+
_debt,
|
|
105
|
+
range,
|
|
106
|
+
_for,
|
|
107
|
+
_callbacker,
|
|
108
|
+
zapCalldata,
|
|
109
|
+
{ ...this.llamalend.options, gasLimit }
|
|
110
|
+
)).hash;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
protected override async _borrowMoreContractCall(
|
|
114
|
+
_userCollateral: bigint,
|
|
115
|
+
_userBorrowed: bigint,
|
|
116
|
+
_debt: bigint,
|
|
117
|
+
_minRecv: bigint,
|
|
118
|
+
router: string,
|
|
119
|
+
exchangeCalldata: string,
|
|
120
|
+
estimateGas: boolean
|
|
121
|
+
): Promise<string | TGas> {
|
|
122
|
+
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
123
|
+
const _for = _getAddress.call(this.llamalend, '');
|
|
124
|
+
const _callbacker = this._getLeverageZapAddress();
|
|
125
|
+
const zapCalldata = buildCalldataForLeverageZapV2Llv2({
|
|
126
|
+
op: 'borrow_more',
|
|
127
|
+
controllerId: this._getMarketId(),
|
|
128
|
+
userBorrowed: _userBorrowed,
|
|
129
|
+
minRecv: _minRecv,
|
|
130
|
+
router,
|
|
131
|
+
exchangeCalldata,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const gas = await contract.borrow_more.estimateGas(
|
|
135
|
+
_userCollateral,
|
|
136
|
+
_debt,
|
|
137
|
+
_for,
|
|
138
|
+
_callbacker,
|
|
139
|
+
zapCalldata,
|
|
140
|
+
{ ...this.llamalend.constantOptions }
|
|
141
|
+
);
|
|
142
|
+
if (estimateGas) return smartNumber(gas);
|
|
143
|
+
|
|
144
|
+
await this.llamalend.updateFeeData();
|
|
145
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
146
|
+
return (await contract.borrow_more(
|
|
147
|
+
_userCollateral,
|
|
148
|
+
_debt,
|
|
149
|
+
_for,
|
|
150
|
+
_callbacker,
|
|
151
|
+
zapCalldata,
|
|
152
|
+
{ ...this.llamalend.options, gasLimit }
|
|
153
|
+
)).hash;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
protected override async _repayContractCall(
|
|
157
|
+
_userCollateral: bigint,
|
|
158
|
+
_userBorrowed: bigint,
|
|
159
|
+
_minRecv: bigint,
|
|
160
|
+
router: string,
|
|
161
|
+
exchangeCalldata: string,
|
|
162
|
+
estimateGas: boolean
|
|
163
|
+
): Promise<string | TGas> {
|
|
164
|
+
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
165
|
+
const _for = _getAddress.call(this.llamalend, '');
|
|
166
|
+
const _callbacker = this._getLeverageZapAddress();
|
|
167
|
+
const zapCalldata = buildCalldataForLeverageZapV2Llv2({
|
|
168
|
+
op: 'repay',
|
|
169
|
+
controllerId: this._getMarketId(),
|
|
170
|
+
userCollateral: _userCollateral,
|
|
171
|
+
userBorrowed: _userBorrowed,
|
|
172
|
+
minRecv: _minRecv,
|
|
173
|
+
router,
|
|
174
|
+
exchangeCalldata,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const _walletDDebt = BigInt(0);
|
|
178
|
+
|
|
179
|
+
const gas = await contract.repay.estimateGas(
|
|
180
|
+
_walletDDebt,
|
|
181
|
+
_for,
|
|
182
|
+
MAX_ACTIVE_BAND,
|
|
183
|
+
_callbacker,
|
|
184
|
+
zapCalldata,
|
|
185
|
+
false,
|
|
186
|
+
{ ...this.llamalend.constantOptions }
|
|
187
|
+
);
|
|
188
|
+
if (estimateGas) return smartNumber(gas);
|
|
189
|
+
|
|
190
|
+
await this.llamalend.updateFeeData();
|
|
191
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
192
|
+
return (await contract.repay(
|
|
193
|
+
_walletDDebt,
|
|
194
|
+
_for,
|
|
195
|
+
MAX_ACTIVE_BAND,
|
|
196
|
+
_callbacker,
|
|
197
|
+
zapCalldata,
|
|
198
|
+
false,
|
|
199
|
+
{ ...this.llamalend.options, gasLimit }
|
|
200
|
+
)).hash;
|
|
201
|
+
}
|
|
202
|
+
}
|
package/src/llamalend.ts
CHANGED
|
@@ -288,8 +288,10 @@ class Llamalend implements ILlamalend {
|
|
|
288
288
|
this.setContract(this.constants.ALIASES['one_way_factory_v2'], OneWayLendingFactoryV2ABI);
|
|
289
289
|
}
|
|
290
290
|
this.setContract(this.constants.ALIASES['gauge_controller'], GaugeControllerABI);
|
|
291
|
-
this.setContract(this.constants.ALIASES['
|
|
291
|
+
this.setContract(this.constants.ALIASES['leverage_zap_deprecated'], LeverageZapABI);
|
|
292
292
|
this.setContract(this.constants.ALIASES['leverage_zap_v2'], LeverageZapABI);
|
|
293
|
+
this.setContract(this.constants.ALIASES['leverage_zap_v2_llv2'], LeverageZapABI);
|
|
294
|
+
|
|
293
295
|
if (this.chainId === 1) {
|
|
294
296
|
this.setContract(this.constants.ALIASES.minter, MinterABI);
|
|
295
297
|
this.setContract(this.constants.ALIASES.gauge_factory, GaugeFactoryMainnetABI);
|
|
@@ -1136,6 +1136,7 @@ export class MintMarketTemplate {
|
|
|
1136
1136
|
// ---------------- REPAY ----------------
|
|
1137
1137
|
|
|
1138
1138
|
private async _repayBands(debt: number | string, address: string): Promise<[bigint, bigint]> {
|
|
1139
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1139
1140
|
const { _collateral: _currentCollateral, _debt: _currentDebt, _stablecoin: _currentStablecoin } = await this._userState(address);
|
|
1140
1141
|
if (_currentDebt === BigInt(0)) throw Error(`Loan for ${address} does not exist`);
|
|
1141
1142
|
|
|
@@ -3,7 +3,7 @@ import { _getCrvUsdMarketsData } from "../../external-api.js";
|
|
|
3
3
|
import ERC20ABI from '../../constants/abis/ERC20.json' with {type: 'json'};
|
|
4
4
|
import llammaABI from "../../constants/abis/crvUSD/llamma.json" with {type: 'json'};
|
|
5
5
|
import controllerABI from "../../constants/abis/crvUSD/controller.json" with {type: 'json'};
|
|
6
|
-
import controllerV2ABI from "../../constants/abis/crvUSD/controller_v2.json";
|
|
6
|
+
import controllerV2ABI from "../../constants/abis/crvUSD/controller_v2.json" with {type: 'json'};
|
|
7
7
|
import FactoryABI from "../../constants/abis/crvUSD/Factory.json" with {type: 'json'};
|
|
8
8
|
import {extractDecimals} from "../../constants/utils.js";
|
|
9
9
|
import {handleMultiCallResponse} from "../../utils.js";
|
package/src/utils.ts
CHANGED
|
@@ -486,7 +486,7 @@ export const calculateFutureLeverage = (
|
|
|
486
486
|
|
|
487
487
|
export const buildCalldataForLeverageZapV2 = (routerAddress: string, exchangeCalldata: string): string => {
|
|
488
488
|
const cleanCalldata = exchangeCalldata.startsWith('0x') ? exchangeCalldata.slice(2) : exchangeCalldata;
|
|
489
|
-
|
|
489
|
+
|
|
490
490
|
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
|
|
491
491
|
return abiCoder.encode(
|
|
492
492
|
['address', 'bytes'],
|
|
@@ -494,3 +494,33 @@ export const buildCalldataForLeverageZapV2 = (routerAddress: string, exchangeCal
|
|
|
494
494
|
);
|
|
495
495
|
};
|
|
496
496
|
|
|
497
|
+
// - create_loan / borrow_more: (uint256 controllerId, uint256 userBorrowed, uint256 minRecv, address router, bytes exchangeCalldata)
|
|
498
|
+
// - repay: (uint256 controllerId, uint256 userCollateral, uint256 userBorrowed, uint256 minRecv, address router, bytes exchangeCalldata)
|
|
499
|
+
export type LeverageZapV2LLv2CalldataParams = {
|
|
500
|
+
controllerId: number | string | bigint;
|
|
501
|
+
minRecv: bigint;
|
|
502
|
+
router: string;
|
|
503
|
+
exchangeCalldata: string;
|
|
504
|
+
} & (
|
|
505
|
+
{ op: 'create_loan' | 'borrow_more'; userBorrowed: bigint }
|
|
506
|
+
| {op: 'repay'; userCollateral: bigint; userBorrowed: bigint }
|
|
507
|
+
);
|
|
508
|
+
|
|
509
|
+
export const buildCalldataForLeverageZapV2Llv2 = (params: LeverageZapV2LLv2CalldataParams): string => {
|
|
510
|
+
const cleanCalldata = params.exchangeCalldata.startsWith('0x') ? params.exchangeCalldata.slice(2) : params.exchangeCalldata;
|
|
511
|
+
const exchangeBytes = '0x' + cleanCalldata;
|
|
512
|
+
const abiCoder = ethers.AbiCoder.defaultAbiCoder();
|
|
513
|
+
|
|
514
|
+
if (params.op === 'repay') {
|
|
515
|
+
return abiCoder.encode(
|
|
516
|
+
['uint256', 'uint256', 'uint256', 'uint256', 'address', 'bytes'],
|
|
517
|
+
[BigInt(params.controllerId), params.userCollateral, params.userBorrowed, params.minRecv, params.router, exchangeBytes]
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return abiCoder.encode(
|
|
522
|
+
['uint256', 'uint256', 'uint256', 'address', 'bytes'],
|
|
523
|
+
[BigInt(params.controllerId), params.userBorrowed, params.minRecv, params.router, exchangeBytes]
|
|
524
|
+
);
|
|
525
|
+
};
|
|
526
|
+
|