@curvefi/llamalend-api 1.1.7 → 1.1.9
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/README.md +36 -8
- package/lib/constants/aliases.js +1 -1
- package/lib/interfaces.d.ts +6 -0
- package/lib/lendMarkets/LendMarketTemplate.js +1 -0
- package/lib/lendMarkets/interfaces/leverageZapV2.d.ts +13 -6
- package/lib/lendMarkets/modules/leverageZapV2.d.ts +13 -6
- package/lib/lendMarkets/modules/leverageZapV2.js +29 -21
- package/lib/mintMarkets/MintMarketTemplate.d.ts +8 -7
- package/lib/mintMarkets/MintMarketTemplate.js +15 -5
- package/package.json +1 -1
- package/src/constants/aliases.ts +1 -1
- package/src/interfaces.ts +8 -1
- package/src/lendMarkets/LendMarketTemplate.ts +4 -3
- package/src/lendMarkets/interfaces/leverageZapV2.ts +13 -5
- package/src/lendMarkets/modules/leverageZapV2.ts +36 -18
- package/src/mintMarkets/MintMarketTemplate.ts +20 -9
package/README.md
CHANGED
|
@@ -986,15 +986,15 @@ interface ILeverageMetrics {
|
|
|
986
986
|
}
|
|
987
987
|
```
|
|
988
988
|
|
|
989
|
-
**2. Final methods now require `router` and `
|
|
989
|
+
**2. Final methods now require `router`, `calldata` and `minRecv` parameters**
|
|
990
990
|
- **Before**: `createLoan(userCollateral, userBorrowed, debt, range)`
|
|
991
|
-
- **Now**: `createLoan(userCollateral, userBorrowed, debt, range, router, calldata)`
|
|
991
|
+
- **Now**: `createLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata)`
|
|
992
992
|
- **Before**: `borrowMore(userCollateral, userBorrowed, debt, slippage)`
|
|
993
|
-
- **Now**: `borrowMore(userCollateral, userBorrowed, debt, router, calldata)`
|
|
993
|
+
- **Now**: `borrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata)`
|
|
994
994
|
- **Before**: `repay(stateCollateral, userCollateral, userBorrowed, slippage)`
|
|
995
|
-
- **Now**: `repay(stateCollateral, userCollateral, userBorrowed, router, calldata)`
|
|
995
|
+
- **Now**: `repay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata)`
|
|
996
996
|
|
|
997
|
-
where `router` - address of router, `calldata` - calldata byte code
|
|
997
|
+
where `router` - address of router, `calldata` - calldata byte code, `minRecv` - minimum amount of tokens to receive (see [Security Improvements](#security-improvements))
|
|
998
998
|
|
|
999
999
|
#### Understanding Callback Functions and Quotes
|
|
1000
1000
|
|
|
@@ -1037,6 +1037,24 @@ interface IQuote {
|
|
|
1037
1037
|
- Example: For stateCollateral=2 and userCollateral=1, get quote for swapping 3 collateral tokens to borrowed tokens
|
|
1038
1038
|
|
|
1039
1039
|
|
|
1040
|
+
#### Security Improvements
|
|
1041
|
+
|
|
1042
|
+
Execution methods (`createLoan`, `borrowMore`, `repay`) accept a `minRecv` parameter that guarantees the minimum amount of tokens received after the swap. This protects the user from slippage and frontrunning.
|
|
1043
|
+
|
|
1044
|
+
Use `calcMinRecv(expected, slippage)` to compute the minimum acceptable amount:
|
|
1045
|
+
|
|
1046
|
+
**Formula:** `minRecv = expected * (100 - slippage) / 100`
|
|
1047
|
+
|
|
1048
|
+
- `expected` — expected amount of tokens from the swap
|
|
1049
|
+
- `slippage` — allowed slippage in percent (e.g. `0.5` for 0.5%)
|
|
1050
|
+
|
|
1051
|
+
```ts
|
|
1052
|
+
// quote.outAmount — the expected amount from your router (computed on your side)
|
|
1053
|
+
const minRecv = lendMarket.leverageZapV2.calcMinRecv(quote.outAmount, 0.5); // 0.5% slippage
|
|
1054
|
+
await lendMarket.leverageZapV2.createLoan({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata });
|
|
1055
|
+
```
|
|
1056
|
+
|
|
1057
|
+
|
|
1040
1058
|
#### Leverage operations with routers
|
|
1041
1059
|
|
|
1042
1060
|
```ts
|
|
@@ -1120,8 +1138,12 @@ interface IQuote {
|
|
|
1120
1138
|
// ]
|
|
1121
1139
|
|
|
1122
1140
|
|
|
1141
|
+
// Calculate minRecv with slippage protection (see Security Improvements)
|
|
1142
|
+
// expected — amount from your router quote
|
|
1143
|
+
const minRecv = lendMarket.leverageZapV2.calcMinRecv(quote.outAmount, 0.5); // 0.5% slippage
|
|
1144
|
+
|
|
1123
1145
|
// Create loan, passing router address and calldata from router
|
|
1124
|
-
await lendMarket.leverageZapV2.createLoan({ userCollateral, userBorrowed, debt, range, router, calldata });
|
|
1146
|
+
await lendMarket.leverageZapV2.createLoan({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata });
|
|
1125
1147
|
// 0xeb1b7a92bcb02598f00dc8bbfe8fa3a554e7a2b1ca764e0ee45e2bf583edf731
|
|
1126
1148
|
|
|
1127
1149
|
await lendMarket.wallet.balances();
|
|
@@ -1207,8 +1229,11 @@ interface IQuote {
|
|
|
1207
1229
|
await lendMarket.leverageZapV2.borrowMoreApprove({ userCollateral, userBorrowed });
|
|
1208
1230
|
// []
|
|
1209
1231
|
|
|
1232
|
+
// Calculate minRecv with slippage protection (see Security Improvements)
|
|
1233
|
+
const minRecvBM = lendMarket.leverageZapV2.calcMinRecv(quote.outAmount, 0.5); // 0.5% slippage
|
|
1234
|
+
|
|
1210
1235
|
// Execute borrowMore, passing router address and calldata from router
|
|
1211
|
-
await lendMarket.leverageZapV2.borrowMore({ userCollateral, userBorrowed, debt, router, calldata });
|
|
1236
|
+
await lendMarket.leverageZapV2.borrowMore({ userCollateral, userBorrowed, debt, minRecv: minRecvBM, router, calldata });
|
|
1212
1237
|
// 0x6357dd6ea7250d7adb2344cd9295f8255fd8fbbe85f00120fbcd1ebf139e057c
|
|
1213
1238
|
|
|
1214
1239
|
await lendMarket.wallet.balances();
|
|
@@ -1292,8 +1317,11 @@ interface IQuote {
|
|
|
1292
1317
|
await lendMarket.leverageZapV2.repayApprove({ userCollateral, userBorrowed });
|
|
1293
1318
|
// ['0xd8a8d3b3f67395e1a4f4d4f95b041edcaf1c9f7bab5eb8a8a767467678295498']
|
|
1294
1319
|
|
|
1320
|
+
// Calculate minRecv with slippage protection (see Security Improvements)
|
|
1321
|
+
const minRecvRepay = lendMarket.leverageZapV2.calcMinRecv(quote.outAmount, 0.5); // 0.5% slippage
|
|
1322
|
+
|
|
1295
1323
|
// Execute repay, passing router address and calldata from router
|
|
1296
|
-
await lendMarket.leverageZapV2.repay({ stateCollateral, userCollateral, userBorrowed, router, calldata });
|
|
1324
|
+
await lendMarket.leverageZapV2.repay({ stateCollateral, userCollateral, userBorrowed, minRecv: minRecvRepay, router, calldata });
|
|
1297
1325
|
// 0xe48a97fef1c54180a2c7d104d210a95ac1a516fdd22109682179f1582da23a82
|
|
1298
1326
|
|
|
1299
1327
|
await lendMarket.wallet.balances();
|
package/lib/constants/aliases.js
CHANGED
|
@@ -43,7 +43,7 @@ export const ALIASES_ARBITRUM = lowerCaseValues({
|
|
|
43
43
|
"gauge_factory": "0xabC000d88f23Bb45525E447528DBF656A9D55bf5",
|
|
44
44
|
// "leverage_zap": "0x61C404B60ee9c5fB09F70F9A645DD38fE5b3A956", // 1inch
|
|
45
45
|
"leverage_zap": "0xFE02553d3Ba4c3f39F36a4632F91404DF94b9AE2", // odos v3
|
|
46
|
-
"leverage_zap_v2": "
|
|
46
|
+
"leverage_zap_v2": "0x9577086c6E38d38359872F903Da201f1bdCc0323",
|
|
47
47
|
"leverage_markets_start_id": "9",
|
|
48
48
|
});
|
|
49
49
|
export const ALIASES_OPTIMISM = lowerCaseValues({
|
package/lib/interfaces.d.ts
CHANGED
|
@@ -229,4 +229,10 @@ export interface ILeverageMetrics {
|
|
|
229
229
|
health: string;
|
|
230
230
|
}
|
|
231
231
|
export type GetExpectedFn = (fromToken: string, toToken: string, amountIn: bigint, blacklist: string | string[]) => Promise<IQuote>;
|
|
232
|
+
export interface IRates {
|
|
233
|
+
borrowApr: string;
|
|
234
|
+
lendApr: string;
|
|
235
|
+
borrowApy: string;
|
|
236
|
+
lendApy: string;
|
|
237
|
+
}
|
|
232
238
|
export {};
|
|
@@ -635,6 +635,7 @@ export class LendMarketTemplate {
|
|
|
635
635
|
createLoanIsApproved: leverageZapV2.leverageCreateLoanIsApproved.bind(leverageZapV2),
|
|
636
636
|
createLoanApprove: leverageZapV2.leverageCreateLoanApprove.bind(leverageZapV2),
|
|
637
637
|
createLoanExpectedMetrics: leverageZapV2.leverageCreateLoanExpectedMetrics.bind(leverageZapV2),
|
|
638
|
+
calcMinRecv: leverageZapV2.calcMinRecv.bind(leverageZapV2),
|
|
638
639
|
createLoan: leverageZapV2.leverageCreateLoan.bind(leverageZapV2),
|
|
639
640
|
borrowMoreMaxRecv: leverageZapV2.leverageBorrowMoreMaxRecv.bind(leverageZapV2),
|
|
640
641
|
borrowMoreExpectedCollateral: leverageZapV2.leverageBorrowMoreExpectedCollateral.bind(leverageZapV2),
|
|
@@ -78,11 +78,13 @@ export interface ILeverageZapV2 {
|
|
|
78
78
|
userCollateral: TAmount;
|
|
79
79
|
userBorrowed: TAmount;
|
|
80
80
|
}) => Promise<string[]>;
|
|
81
|
-
|
|
81
|
+
calcMinRecv: (expected: TAmount, slippage: number) => string;
|
|
82
|
+
createLoan: ({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata, }: {
|
|
82
83
|
userCollateral: TAmount;
|
|
83
84
|
userBorrowed: TAmount;
|
|
84
85
|
debt: TAmount;
|
|
85
86
|
range: number;
|
|
87
|
+
minRecv: TAmount;
|
|
86
88
|
router: string;
|
|
87
89
|
calldata: string;
|
|
88
90
|
}) => Promise<string>;
|
|
@@ -128,10 +130,11 @@ export interface ILeverageZapV2 {
|
|
|
128
130
|
userCollateral: TAmount;
|
|
129
131
|
userBorrowed: TAmount;
|
|
130
132
|
}) => Promise<string[]>;
|
|
131
|
-
borrowMore: ({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
133
|
+
borrowMore: ({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
132
134
|
userCollateral: TAmount;
|
|
133
135
|
userBorrowed: TAmount;
|
|
134
136
|
debt: TAmount;
|
|
137
|
+
minRecv: TAmount;
|
|
135
138
|
router: string;
|
|
136
139
|
calldata: string;
|
|
137
140
|
}) => Promise<string>;
|
|
@@ -184,10 +187,11 @@ export interface ILeverageZapV2 {
|
|
|
184
187
|
userCollateral: TAmount;
|
|
185
188
|
userBorrowed: TAmount;
|
|
186
189
|
}) => Promise<string[]>;
|
|
187
|
-
repay: ({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
190
|
+
repay: ({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
188
191
|
stateCollateral: TAmount;
|
|
189
192
|
userCollateral: TAmount;
|
|
190
193
|
userBorrowed: TAmount;
|
|
194
|
+
minRecv: TAmount;
|
|
191
195
|
router: string;
|
|
192
196
|
calldata: string;
|
|
193
197
|
}) => Promise<string>;
|
|
@@ -203,11 +207,12 @@ export interface ILeverageZapV2 {
|
|
|
203
207
|
userCollateral: TAmount;
|
|
204
208
|
userBorrowed: TAmount;
|
|
205
209
|
}) => Promise<TGas>;
|
|
206
|
-
createLoan: ({ userCollateral, userBorrowed, debt, range, router, calldata }: {
|
|
210
|
+
createLoan: ({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
207
211
|
userCollateral: TAmount;
|
|
208
212
|
userBorrowed: TAmount;
|
|
209
213
|
debt: TAmount;
|
|
210
214
|
range: number;
|
|
215
|
+
minRecv: TAmount;
|
|
211
216
|
router: string;
|
|
212
217
|
calldata: string;
|
|
213
218
|
}) => Promise<number>;
|
|
@@ -215,10 +220,11 @@ export interface ILeverageZapV2 {
|
|
|
215
220
|
userCollateral: TAmount;
|
|
216
221
|
userBorrowed: TAmount;
|
|
217
222
|
}) => Promise<TGas>;
|
|
218
|
-
borrowMore: ({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
223
|
+
borrowMore: ({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
219
224
|
userCollateral: TAmount;
|
|
220
225
|
userBorrowed: TAmount;
|
|
221
226
|
debt: TAmount;
|
|
227
|
+
minRecv: TAmount;
|
|
222
228
|
router: string;
|
|
223
229
|
calldata: string;
|
|
224
230
|
}) => Promise<number>;
|
|
@@ -226,10 +232,11 @@ export interface ILeverageZapV2 {
|
|
|
226
232
|
userCollateral: TAmount;
|
|
227
233
|
userBorrowed: TAmount;
|
|
228
234
|
}) => Promise<TGas>;
|
|
229
|
-
repay: ({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
235
|
+
repay: ({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
230
236
|
stateCollateral: TAmount;
|
|
231
237
|
userCollateral: TAmount;
|
|
232
238
|
userBorrowed: TAmount;
|
|
239
|
+
minRecv: TAmount;
|
|
233
240
|
router: string;
|
|
234
241
|
calldata: string;
|
|
235
242
|
}) => Promise<number>;
|
|
@@ -14,6 +14,7 @@ export declare class LeverageZapV2Module {
|
|
|
14
14
|
_checkLeverageZap(): void;
|
|
15
15
|
_get_k_effective_BN(N: number): Promise<BigNumber>;
|
|
16
16
|
maxLeverage(N: number): Promise<string>;
|
|
17
|
+
calcMinRecv(expected: TAmount, slippage: number): string;
|
|
17
18
|
leverageCreateLoanMaxRecv({ userCollateral, userBorrowed, range, getExpected }: {
|
|
18
19
|
userCollateral: TAmount;
|
|
19
20
|
userBorrowed: TAmount;
|
|
@@ -113,19 +114,21 @@ export declare class LeverageZapV2Module {
|
|
|
113
114
|
userBorrowed: TAmount;
|
|
114
115
|
}): Promise<string[]>;
|
|
115
116
|
private _leverageCreateLoan;
|
|
116
|
-
leverageCreateLoanEstimateGas({ userCollateral, userBorrowed, debt, range, router, calldata }: {
|
|
117
|
+
leverageCreateLoanEstimateGas({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
117
118
|
userCollateral: TAmount;
|
|
118
119
|
userBorrowed: TAmount;
|
|
119
120
|
debt: TAmount;
|
|
120
121
|
range: number;
|
|
122
|
+
minRecv: TAmount;
|
|
121
123
|
router: string;
|
|
122
124
|
calldata: string;
|
|
123
125
|
}): Promise<number>;
|
|
124
|
-
leverageCreateLoan({ userCollateral, userBorrowed, debt, range, router, calldata }: {
|
|
126
|
+
leverageCreateLoan({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
125
127
|
userCollateral: TAmount;
|
|
126
128
|
userBorrowed: TAmount;
|
|
127
129
|
debt: TAmount;
|
|
128
130
|
range: number;
|
|
131
|
+
minRecv: TAmount;
|
|
129
132
|
router: string;
|
|
130
133
|
calldata: string;
|
|
131
134
|
}): Promise<string>;
|
|
@@ -172,17 +175,19 @@ export declare class LeverageZapV2Module {
|
|
|
172
175
|
userCollateral: TAmount;
|
|
173
176
|
userBorrowed: TAmount;
|
|
174
177
|
}): Promise<string[]>;
|
|
175
|
-
leverageBorrowMoreEstimateGas({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
178
|
+
leverageBorrowMoreEstimateGas({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
176
179
|
userCollateral: TAmount;
|
|
177
180
|
userBorrowed: TAmount;
|
|
178
181
|
debt: TAmount;
|
|
182
|
+
minRecv: TAmount;
|
|
179
183
|
router: string;
|
|
180
184
|
calldata: string;
|
|
181
185
|
}): Promise<number>;
|
|
182
|
-
leverageBorrowMore({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
186
|
+
leverageBorrowMore({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
183
187
|
userCollateral: TAmount;
|
|
184
188
|
userBorrowed: TAmount;
|
|
185
189
|
debt: TAmount;
|
|
190
|
+
minRecv: TAmount;
|
|
186
191
|
router: string;
|
|
187
192
|
calldata: string;
|
|
188
193
|
}): Promise<string>;
|
|
@@ -243,17 +248,19 @@ export declare class LeverageZapV2Module {
|
|
|
243
248
|
userBorrowed: TAmount;
|
|
244
249
|
}): Promise<string[]>;
|
|
245
250
|
private _leverageRepay;
|
|
246
|
-
leverageRepayEstimateGas({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
251
|
+
leverageRepayEstimateGas({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
247
252
|
stateCollateral: TAmount;
|
|
248
253
|
userCollateral: TAmount;
|
|
249
254
|
userBorrowed: TAmount;
|
|
255
|
+
minRecv: TAmount;
|
|
250
256
|
router: string;
|
|
251
257
|
calldata: string;
|
|
252
258
|
}): Promise<number>;
|
|
253
|
-
leverageRepay({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
259
|
+
leverageRepay({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
254
260
|
stateCollateral: TAmount;
|
|
255
261
|
userCollateral: TAmount;
|
|
256
262
|
userBorrowed: TAmount;
|
|
263
|
+
minRecv: TAmount;
|
|
257
264
|
router: string;
|
|
258
265
|
calldata: string;
|
|
259
266
|
}): Promise<string>;
|
|
@@ -211,6 +211,11 @@ export class LeverageZapV2Module {
|
|
|
211
211
|
return BN(1).div(BN(1).minus(k_effective_BN)).toString();
|
|
212
212
|
});
|
|
213
213
|
}
|
|
214
|
+
calcMinRecv(expected, slippage) {
|
|
215
|
+
if (slippage < 0 || slippage > 100)
|
|
216
|
+
throw Error("Slippage must be between 0 and 100");
|
|
217
|
+
return BN(expected).times(BN(100).minus(slippage)).div(100).toString();
|
|
218
|
+
}
|
|
214
219
|
leverageCreateLoanMaxRecv(_a) {
|
|
215
220
|
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, range, getExpected }) {
|
|
216
221
|
// max_borrowable = userCollateral / (1 / (k_effective * max_p_base) - 1 / p_avg)
|
|
@@ -400,7 +405,7 @@ export class LeverageZapV2Module {
|
|
|
400
405
|
return [...collateralApproveTx, ...borrowedApproveTx];
|
|
401
406
|
});
|
|
402
407
|
}
|
|
403
|
-
_leverageCreateLoan(userCollateral, userBorrowed, debt, range, router, calldata, estimateGas) {
|
|
408
|
+
_leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, estimateGas) {
|
|
404
409
|
return __awaiter(this, void 0, void 0, function* () {
|
|
405
410
|
if (yield this.market.userLoanExists())
|
|
406
411
|
throw Error("Loan already created");
|
|
@@ -408,29 +413,30 @@ export class LeverageZapV2Module {
|
|
|
408
413
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
409
414
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
410
415
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
416
|
+
const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
|
|
411
417
|
const zapCalldata = buildCalldataForLeverageZapV2(router, calldata);
|
|
412
418
|
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
413
|
-
const gas = yield contract.create_loan_extended.estimateGas(_userCollateral, _debt, range, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed], zapCalldata, Object.assign({}, this.llamalend.constantOptions));
|
|
419
|
+
const gas = yield contract.create_loan_extended.estimateGas(_userCollateral, _debt, range, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv], zapCalldata, Object.assign({}, this.llamalend.constantOptions));
|
|
414
420
|
if (estimateGas)
|
|
415
421
|
return smartNumber(gas);
|
|
416
422
|
yield this.llamalend.updateFeeData();
|
|
417
423
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
418
|
-
return (yield contract.create_loan_extended(_userCollateral, _debt, range, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed], zapCalldata, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
424
|
+
return (yield contract.create_loan_extended(_userCollateral, _debt, range, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv], zapCalldata, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
419
425
|
});
|
|
420
426
|
}
|
|
421
427
|
leverageCreateLoanEstimateGas(_a) {
|
|
422
|
-
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, range, router, calldata }) {
|
|
428
|
+
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }) {
|
|
423
429
|
this._checkLeverageZap();
|
|
424
430
|
if (!(yield this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed })))
|
|
425
431
|
throw Error("Approval is needed for gas estimation");
|
|
426
|
-
return yield this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, router, calldata, true);
|
|
432
|
+
return yield this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, true);
|
|
427
433
|
});
|
|
428
434
|
}
|
|
429
435
|
leverageCreateLoan(_a) {
|
|
430
|
-
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, range, router, calldata }) {
|
|
436
|
+
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }) {
|
|
431
437
|
this._checkLeverageZap();
|
|
432
438
|
yield this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
|
|
433
|
-
return yield this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, router, calldata, false);
|
|
439
|
+
return yield this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, false);
|
|
434
440
|
});
|
|
435
441
|
}
|
|
436
442
|
// ---------------- LEVERAGE BORROW MORE ----------------
|
|
@@ -515,21 +521,22 @@ export class LeverageZapV2Module {
|
|
|
515
521
|
};
|
|
516
522
|
});
|
|
517
523
|
}
|
|
518
|
-
_leverageBorrowMore(userCollateral, userBorrowed, debt, router, calldata, estimateGas) {
|
|
524
|
+
_leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, estimateGas) {
|
|
519
525
|
return __awaiter(this, void 0, void 0, function* () {
|
|
520
526
|
if (!(yield this.market.userLoanExists()))
|
|
521
527
|
throw Error("Loan does not exist");
|
|
522
528
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
523
529
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
524
530
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
531
|
+
const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
|
|
525
532
|
const zapCalldata = buildCalldataForLeverageZapV2(router, calldata);
|
|
526
533
|
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
527
|
-
const gas = yield contract.borrow_more_extended.estimateGas(_userCollateral, _debt, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed], zapCalldata, Object.assign({}, this.llamalend.constantOptions));
|
|
534
|
+
const gas = yield contract.borrow_more_extended.estimateGas(_userCollateral, _debt, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv], zapCalldata, Object.assign({}, this.llamalend.constantOptions));
|
|
528
535
|
if (estimateGas)
|
|
529
536
|
return smartNumber(gas);
|
|
530
537
|
yield this.llamalend.updateFeeData();
|
|
531
538
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
532
|
-
return (yield contract.borrow_more_extended(_userCollateral, _debt, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed], zapCalldata, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
539
|
+
return (yield contract.borrow_more_extended(_userCollateral, _debt, this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv], zapCalldata, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
533
540
|
});
|
|
534
541
|
}
|
|
535
542
|
leverageBorrowMoreIsApproved(_a) {
|
|
@@ -543,18 +550,18 @@ export class LeverageZapV2Module {
|
|
|
543
550
|
});
|
|
544
551
|
}
|
|
545
552
|
leverageBorrowMoreEstimateGas(_a) {
|
|
546
|
-
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, router, calldata }) {
|
|
553
|
+
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, minRecv, router, calldata }) {
|
|
547
554
|
this._checkLeverageZap();
|
|
548
555
|
if (!(yield this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed })))
|
|
549
556
|
throw Error("Approval is needed for gas estimation");
|
|
550
|
-
return yield this._leverageBorrowMore(userCollateral, userBorrowed, debt, router, calldata, true);
|
|
557
|
+
return yield this._leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, true);
|
|
551
558
|
});
|
|
552
559
|
}
|
|
553
560
|
leverageBorrowMore(_a) {
|
|
554
|
-
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, router, calldata }) {
|
|
561
|
+
return __awaiter(this, arguments, void 0, function* ({ userCollateral, userBorrowed, debt, minRecv, router, calldata }) {
|
|
555
562
|
this._checkLeverageZap();
|
|
556
563
|
yield this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
|
|
557
|
-
return yield this._leverageBorrowMore(userCollateral, userBorrowed, debt, router, calldata, false);
|
|
564
|
+
return yield this._leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, false);
|
|
558
565
|
});
|
|
559
566
|
}
|
|
560
567
|
leverageBorrowMoreFutureLeverage(_a) {
|
|
@@ -660,39 +667,40 @@ export class LeverageZapV2Module {
|
|
|
660
667
|
return yield ensureAllowance.call(this.llamalend, [this.market.collateral_token.address, this.market.borrowed_token.address], [userCollateral, userBorrowed], this.llamalend.constants.ALIASES.leverage_zap_v2);
|
|
661
668
|
});
|
|
662
669
|
}
|
|
663
|
-
_leverageRepay(stateCollateral, userCollateral, userBorrowed, router, calldata, estimateGas) {
|
|
670
|
+
_leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, estimateGas) {
|
|
664
671
|
return __awaiter(this, void 0, void 0, function* () {
|
|
665
672
|
if (!(yield this.market.userLoanExists()))
|
|
666
673
|
throw Error("Loan does not exist");
|
|
667
674
|
const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
|
|
668
675
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
669
676
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
677
|
+
const _minRecv = parseUnits(minRecv, this.market.borrowed_token.decimals);
|
|
670
678
|
let zapCalldata = buildCalldataForLeverageZapV2(router, "0x");
|
|
671
679
|
if (_stateCollateral + _userCollateral > BigInt(0)) {
|
|
672
680
|
zapCalldata = buildCalldataForLeverageZapV2(router, calldata);
|
|
673
681
|
}
|
|
674
682
|
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
675
|
-
const gas = yield contract.repay_extended.estimateGas(this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed], zapCalldata);
|
|
683
|
+
const gas = yield contract.repay_extended.estimateGas(this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv], zapCalldata);
|
|
676
684
|
if (estimateGas)
|
|
677
685
|
return smartNumber(gas);
|
|
678
686
|
yield this.llamalend.updateFeeData();
|
|
679
687
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
680
|
-
return (yield contract.repay_extended(this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed], zapCalldata, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
688
|
+
return (yield contract.repay_extended(this.llamalend.constants.ALIASES.leverage_zap_v2, [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv], zapCalldata, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
681
689
|
});
|
|
682
690
|
}
|
|
683
691
|
leverageRepayEstimateGas(_a) {
|
|
684
|
-
return __awaiter(this, arguments, void 0, function* ({ stateCollateral, userCollateral, userBorrowed, router, calldata }) {
|
|
692
|
+
return __awaiter(this, arguments, void 0, function* ({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }) {
|
|
685
693
|
this._checkLeverageZap();
|
|
686
694
|
if (!(yield this.leverageRepayIsApproved({ userCollateral, userBorrowed })))
|
|
687
695
|
throw Error("Approval is needed for gas estimation");
|
|
688
|
-
return yield this._leverageRepay(stateCollateral, userCollateral, userBorrowed, router, calldata, true);
|
|
696
|
+
return yield this._leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, true);
|
|
689
697
|
});
|
|
690
698
|
}
|
|
691
699
|
leverageRepay(_a) {
|
|
692
|
-
return __awaiter(this, arguments, void 0, function* ({ stateCollateral, userCollateral, userBorrowed, router, calldata }) {
|
|
700
|
+
return __awaiter(this, arguments, void 0, function* ({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }) {
|
|
693
701
|
this._checkLeverageZap();
|
|
694
702
|
yield this.leverageRepayApprove({ userCollateral, userBorrowed });
|
|
695
|
-
return yield this._leverageRepay(stateCollateral, userCollateral, userBorrowed, router, calldata, false);
|
|
703
|
+
return yield this._leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, false);
|
|
696
704
|
});
|
|
697
705
|
}
|
|
698
706
|
leverageRepayFutureLeverage(_a) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import memoize from "memoizee";
|
|
2
2
|
import type { Llamalend } from "../llamalend.js";
|
|
3
|
-
import { IDict, ILlamma, TGas } from "../interfaces.js";
|
|
3
|
+
import { IDict, ILlamma, TGas, IRates } from "../interfaces.js";
|
|
4
4
|
import { ILeverageV2 } from "./interfaces/leverage.js";
|
|
5
5
|
export declare class MintMarketTemplate {
|
|
6
6
|
private llamalend;
|
|
@@ -46,8 +46,8 @@ export declare class MintMarketTemplate {
|
|
|
46
46
|
parameters: () => Promise<{
|
|
47
47
|
fee: string;
|
|
48
48
|
admin_fee: string;
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
rates: IRates;
|
|
50
|
+
future_rates: IRates;
|
|
51
51
|
liquidation_discount: string;
|
|
52
52
|
loan_discount: string;
|
|
53
53
|
}>;
|
|
@@ -134,18 +134,19 @@ export declare class MintMarketTemplate {
|
|
|
134
134
|
};
|
|
135
135
|
};
|
|
136
136
|
constructor(id: string, llammaData: ILlamma, llamalend: Llamalend);
|
|
137
|
+
private _buildRatesObject;
|
|
137
138
|
statsParameters: (() => Promise<{
|
|
138
139
|
fee: string;
|
|
139
140
|
admin_fee: string;
|
|
140
|
-
|
|
141
|
-
|
|
141
|
+
rates: IRates;
|
|
142
|
+
future_rates: IRates;
|
|
142
143
|
liquidation_discount: string;
|
|
143
144
|
loan_discount: string;
|
|
144
145
|
}>) & memoize.Memoized<() => Promise<{
|
|
145
146
|
fee: string;
|
|
146
147
|
admin_fee: string;
|
|
147
|
-
|
|
148
|
-
|
|
148
|
+
rates: IRates;
|
|
149
|
+
future_rates: IRates;
|
|
149
150
|
liquidation_discount: string;
|
|
150
151
|
loan_discount: string;
|
|
151
152
|
}>>;
|
|
@@ -15,7 +15,6 @@ import { LeverageV2Module } from "./modules/index.js";
|
|
|
15
15
|
export class MintMarketTemplate {
|
|
16
16
|
constructor(id, llammaData, llamalend) {
|
|
17
17
|
var _a;
|
|
18
|
-
// ---------------- STATS ----------------
|
|
19
18
|
this.statsParameters = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
20
19
|
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
21
20
|
const controllerContract = this.llamalend.contracts[this.controller].multicallContract;
|
|
@@ -31,10 +30,9 @@ export class MintMarketTemplate {
|
|
|
31
30
|
const [_fee, _admin_fee, _rate, _mp_rate, _liquidation_discount, _loan_discount] = yield this.llamalend.multicallProvider.all(calls);
|
|
32
31
|
const [fee, admin_fee, liquidation_discount, loan_discount] = [_fee, _admin_fee, _liquidation_discount, _loan_discount]
|
|
33
32
|
.map((x) => formatUnits(x * BigInt(100)));
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
return { fee, admin_fee, rate, future_rate, liquidation_discount, loan_discount };
|
|
33
|
+
const rates = this._buildRatesObject(_rate);
|
|
34
|
+
const future_rates = this._buildRatesObject(_mp_rate);
|
|
35
|
+
return { fee, admin_fee, rates, future_rates, liquidation_discount, loan_discount };
|
|
38
36
|
}), {
|
|
39
37
|
promise: true,
|
|
40
38
|
maxAge: 5 * 60 * 1000, // 5m
|
|
@@ -378,6 +376,18 @@ export class MintMarketTemplate {
|
|
|
378
376
|
},
|
|
379
377
|
};
|
|
380
378
|
}
|
|
379
|
+
// ---------------- STATS ----------------
|
|
380
|
+
_buildRatesObject(rate) {
|
|
381
|
+
const borrowApr = toBN(rate).times(365).times(86400).times(100).toString();
|
|
382
|
+
// borrowApy = e**(rate*365*86400) - 1
|
|
383
|
+
const borrowApy = String(((Math.pow(2.718281828459, (toBN(rate).times(365).times(86400)).toNumber())) - 1) * 100);
|
|
384
|
+
return {
|
|
385
|
+
borrowApr,
|
|
386
|
+
borrowApy,
|
|
387
|
+
lendApr: "0",
|
|
388
|
+
lendApy: "0",
|
|
389
|
+
};
|
|
390
|
+
}
|
|
381
391
|
statsBalances() {
|
|
382
392
|
return __awaiter(this, void 0, void 0, function* () {
|
|
383
393
|
const crvusdContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
package/package.json
CHANGED
package/src/constants/aliases.ts
CHANGED
|
@@ -49,7 +49,7 @@ export const ALIASES_ARBITRUM = lowerCaseValues({
|
|
|
49
49
|
"gauge_factory": "0xabC000d88f23Bb45525E447528DBF656A9D55bf5",
|
|
50
50
|
// "leverage_zap": "0x61C404B60ee9c5fB09F70F9A645DD38fE5b3A956", // 1inch
|
|
51
51
|
"leverage_zap": "0xFE02553d3Ba4c3f39F36a4632F91404DF94b9AE2", // odos v3
|
|
52
|
-
"leverage_zap_v2": "
|
|
52
|
+
"leverage_zap_v2": "0x9577086c6E38d38359872F903Da201f1bdCc0323",
|
|
53
53
|
"leverage_markets_start_id": "9",
|
|
54
54
|
});
|
|
55
55
|
|
package/src/interfaces.ts
CHANGED
|
@@ -248,4 +248,11 @@ export type GetExpectedFn = (
|
|
|
248
248
|
toToken: string,
|
|
249
249
|
amountIn: bigint,
|
|
250
250
|
blacklist: string | string[],
|
|
251
|
-
) => Promise<IQuote>;
|
|
251
|
+
) => Promise<IQuote>;
|
|
252
|
+
|
|
253
|
+
export interface IRates {
|
|
254
|
+
borrowApr: string;
|
|
255
|
+
lendApr: string;
|
|
256
|
+
borrowApy: string;
|
|
257
|
+
lendApy: string;
|
|
258
|
+
}
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
smartNumber,
|
|
24
24
|
calculateFutureLeverage,
|
|
25
25
|
} from "../utils.js";
|
|
26
|
-
import {IDict, TGas, TAmount, IReward, IQuoteOdos, IOneWayMarket, IPartialFrac} from "../interfaces.js";
|
|
26
|
+
import {IDict, TGas, TAmount, IReward, IQuoteOdos, IOneWayMarket, IPartialFrac, IRates} from "../interfaces.js";
|
|
27
27
|
import { _getExpectedOdos, _getQuoteOdos, _assembleTxOdos, _getUserCollateral, _getUserCollateralForce, _getMarketsData } from "../external-api.js";
|
|
28
28
|
import ERC20Abi from '../constants/abis/ERC20.json' with {type: 'json'};
|
|
29
29
|
import {cacheKey, cacheStats} from "../cache/index.js";
|
|
@@ -410,6 +410,7 @@ export class LendMarketTemplate {
|
|
|
410
410
|
createLoanIsApproved: leverageZapV2.leverageCreateLoanIsApproved.bind(leverageZapV2),
|
|
411
411
|
createLoanApprove: leverageZapV2.leverageCreateLoanApprove.bind(leverageZapV2),
|
|
412
412
|
createLoanExpectedMetrics: leverageZapV2.leverageCreateLoanExpectedMetrics.bind(leverageZapV2),
|
|
413
|
+
calcMinRecv: leverageZapV2.calcMinRecv.bind(leverageZapV2),
|
|
413
414
|
createLoan: leverageZapV2.leverageCreateLoan.bind(leverageZapV2),
|
|
414
415
|
|
|
415
416
|
borrowMoreMaxRecv: leverageZapV2.leverageBorrowMoreMaxRecv.bind(leverageZapV2),
|
|
@@ -1016,7 +1017,7 @@ export class LendMarketTemplate {
|
|
|
1016
1017
|
return await mpContract.future_rate(this.addresses.controller, _dReserves, _dDebt);
|
|
1017
1018
|
}
|
|
1018
1019
|
|
|
1019
|
-
private async statsRates(isGetter = true, useAPI = false): Promise<
|
|
1020
|
+
private async statsRates(isGetter = true, useAPI = false): Promise<IRates> {
|
|
1020
1021
|
if(useAPI) {
|
|
1021
1022
|
const response = await _getMarketsData(this.llamalend.constants.NETWORK_NAME);
|
|
1022
1023
|
|
|
@@ -1052,7 +1053,7 @@ export class LendMarketTemplate {
|
|
|
1052
1053
|
}
|
|
1053
1054
|
}
|
|
1054
1055
|
|
|
1055
|
-
private async statsFutureRates(dReserves: TAmount, dDebt: TAmount, useAPI = true): Promise<
|
|
1056
|
+
private async statsFutureRates(dReserves: TAmount, dDebt: TAmount, useAPI = true): Promise<IRates> {
|
|
1056
1057
|
const _dReserves = parseUnits(dReserves, this.borrowed_token.decimals);
|
|
1057
1058
|
const _dDebt = parseUnits(dDebt, this.borrowed_token.decimals);
|
|
1058
1059
|
const _rate = await this._getFutureRate(_dReserves, _dDebt);
|
|
@@ -125,11 +125,13 @@ export interface ILeverageZapV2 {
|
|
|
125
125
|
userCollateral: TAmount,
|
|
126
126
|
userBorrowed: TAmount
|
|
127
127
|
}) => Promise<string[]>,
|
|
128
|
+
calcMinRecv: (expected: TAmount, slippage: number) => string,
|
|
128
129
|
createLoan: ({
|
|
129
130
|
userCollateral,
|
|
130
131
|
userBorrowed,
|
|
131
132
|
debt,
|
|
132
133
|
range,
|
|
134
|
+
minRecv,
|
|
133
135
|
router,
|
|
134
136
|
calldata,
|
|
135
137
|
}: {
|
|
@@ -137,6 +139,7 @@ export interface ILeverageZapV2 {
|
|
|
137
139
|
userBorrowed: TAmount,
|
|
138
140
|
debt: TAmount,
|
|
139
141
|
range: number,
|
|
142
|
+
minRecv: TAmount,
|
|
140
143
|
router: string,
|
|
141
144
|
calldata: string
|
|
142
145
|
}) => Promise<string>,
|
|
@@ -183,10 +186,11 @@ export interface ILeverageZapV2 {
|
|
|
183
186
|
userCollateral: TAmount,
|
|
184
187
|
userBorrowed: TAmount
|
|
185
188
|
}) => Promise<string[]>,
|
|
186
|
-
borrowMore: ({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
189
|
+
borrowMore: ({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
187
190
|
userCollateral: TAmount,
|
|
188
191
|
userBorrowed: TAmount,
|
|
189
192
|
debt: TAmount,
|
|
193
|
+
minRecv: TAmount,
|
|
190
194
|
router: string,
|
|
191
195
|
calldata: string
|
|
192
196
|
}) => Promise<string>,
|
|
@@ -240,10 +244,11 @@ export interface ILeverageZapV2 {
|
|
|
240
244
|
userCollateral: TAmount,
|
|
241
245
|
userBorrowed: TAmount
|
|
242
246
|
}) => Promise<string[]>,
|
|
243
|
-
repay: ({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
247
|
+
repay: ({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
244
248
|
stateCollateral: TAmount,
|
|
245
249
|
userCollateral: TAmount,
|
|
246
250
|
userBorrowed: TAmount,
|
|
251
|
+
minRecv: TAmount,
|
|
247
252
|
router: string,
|
|
248
253
|
calldata: string
|
|
249
254
|
}) => Promise<string>,
|
|
@@ -260,11 +265,12 @@ export interface ILeverageZapV2 {
|
|
|
260
265
|
userCollateral: TAmount,
|
|
261
266
|
userBorrowed: TAmount
|
|
262
267
|
}) => Promise<TGas>,
|
|
263
|
-
createLoan: ({ userCollateral, userBorrowed, debt, range, router, calldata }: {
|
|
268
|
+
createLoan: ({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
264
269
|
userCollateral: TAmount,
|
|
265
270
|
userBorrowed: TAmount,
|
|
266
271
|
debt: TAmount,
|
|
267
272
|
range: number,
|
|
273
|
+
minRecv: TAmount,
|
|
268
274
|
router: string,
|
|
269
275
|
calldata: string
|
|
270
276
|
}) => Promise<number>,
|
|
@@ -273,10 +279,11 @@ export interface ILeverageZapV2 {
|
|
|
273
279
|
userCollateral: TAmount,
|
|
274
280
|
userBorrowed: TAmount
|
|
275
281
|
}) => Promise<TGas>,
|
|
276
|
-
borrowMore: ({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
282
|
+
borrowMore: ({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
277
283
|
userCollateral: TAmount,
|
|
278
284
|
userBorrowed: TAmount,
|
|
279
285
|
debt: TAmount,
|
|
286
|
+
minRecv: TAmount,
|
|
280
287
|
router: string,
|
|
281
288
|
calldata: string
|
|
282
289
|
}) => Promise<number>,
|
|
@@ -285,10 +292,11 @@ export interface ILeverageZapV2 {
|
|
|
285
292
|
userCollateral: TAmount,
|
|
286
293
|
userBorrowed: TAmount
|
|
287
294
|
}) => Promise<TGas>,
|
|
288
|
-
repay: ({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
295
|
+
repay: ({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
289
296
|
stateCollateral: TAmount,
|
|
290
297
|
userCollateral: TAmount,
|
|
291
298
|
userBorrowed: TAmount,
|
|
299
|
+
minRecv: TAmount,
|
|
292
300
|
router: string,
|
|
293
301
|
calldata: string
|
|
294
302
|
}) => Promise<number>,
|
|
@@ -70,6 +70,11 @@ export class LeverageZapV2Module {
|
|
|
70
70
|
return BN(1).div(BN(1).minus(k_effective_BN)).toString()
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
public calcMinRecv(expected: TAmount, slippage: number): string {
|
|
74
|
+
if (slippage < 0 || slippage > 100) throw Error("Slippage must be between 0 and 100");
|
|
75
|
+
return BN(expected).times(BN(100).minus(slippage)).div(100).toString();
|
|
76
|
+
}
|
|
77
|
+
|
|
73
78
|
public async leverageCreateLoanMaxRecv({ userCollateral, userBorrowed, range, getExpected }: {
|
|
74
79
|
userCollateral: TAmount,
|
|
75
80
|
userBorrowed: TAmount,
|
|
@@ -484,6 +489,7 @@ export class LeverageZapV2Module {
|
|
|
484
489
|
userBorrowed: TAmount,
|
|
485
490
|
debt: TAmount,
|
|
486
491
|
range: number,
|
|
492
|
+
minRecv: TAmount,
|
|
487
493
|
router: string,
|
|
488
494
|
calldata: string,
|
|
489
495
|
estimateGas: boolean
|
|
@@ -494,6 +500,7 @@ export class LeverageZapV2Module {
|
|
|
494
500
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
495
501
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
496
502
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
503
|
+
const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
|
|
497
504
|
|
|
498
505
|
|
|
499
506
|
const zapCalldata = buildCalldataForLeverageZapV2(router, calldata)
|
|
@@ -503,7 +510,7 @@ export class LeverageZapV2Module {
|
|
|
503
510
|
_debt,
|
|
504
511
|
range,
|
|
505
512
|
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
506
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
513
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
507
514
|
zapCalldata,
|
|
508
515
|
{ ...this.llamalend.constantOptions }
|
|
509
516
|
);
|
|
@@ -516,36 +523,38 @@ export class LeverageZapV2Module {
|
|
|
516
523
|
_debt,
|
|
517
524
|
range,
|
|
518
525
|
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
519
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
526
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
520
527
|
zapCalldata,
|
|
521
528
|
{ ...this.llamalend.options, gasLimit }
|
|
522
529
|
)).hash
|
|
523
530
|
}
|
|
524
531
|
|
|
525
|
-
public async leverageCreateLoanEstimateGas({ userCollateral, userBorrowed, debt, range, router, calldata }: {
|
|
532
|
+
public async leverageCreateLoanEstimateGas({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
526
533
|
userCollateral: TAmount,
|
|
527
534
|
userBorrowed: TAmount,
|
|
528
535
|
debt: TAmount,
|
|
529
536
|
range: number,
|
|
537
|
+
minRecv: TAmount,
|
|
530
538
|
router: string,
|
|
531
539
|
calldata: string
|
|
532
540
|
}): Promise<number> {
|
|
533
541
|
this._checkLeverageZap();
|
|
534
542
|
if (!(await this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
|
|
535
|
-
return await this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, router, calldata, true) as number;
|
|
543
|
+
return await this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, true) as number;
|
|
536
544
|
}
|
|
537
545
|
|
|
538
|
-
public async leverageCreateLoan({ userCollateral, userBorrowed, debt, range, router, calldata }: {
|
|
546
|
+
public async leverageCreateLoan({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
|
|
539
547
|
userCollateral: TAmount,
|
|
540
548
|
userBorrowed: TAmount,
|
|
541
549
|
debt: TAmount,
|
|
542
550
|
range: number,
|
|
551
|
+
minRecv: TAmount,
|
|
543
552
|
router: string,
|
|
544
553
|
calldata: string
|
|
545
554
|
}): Promise<string> {
|
|
546
555
|
this._checkLeverageZap();
|
|
547
556
|
await this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
|
|
548
|
-
return await this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, router, calldata, false) as string;
|
|
557
|
+
return await this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, false) as string;
|
|
549
558
|
}
|
|
550
559
|
|
|
551
560
|
// ---------------- LEVERAGE BORROW MORE ----------------
|
|
@@ -667,6 +676,7 @@ export class LeverageZapV2Module {
|
|
|
667
676
|
userCollateral: TAmount,
|
|
668
677
|
userBorrowed: TAmount,
|
|
669
678
|
debt: TAmount,
|
|
679
|
+
minRecv: TAmount,
|
|
670
680
|
router: string,
|
|
671
681
|
calldata: string,
|
|
672
682
|
estimateGas: boolean
|
|
@@ -675,6 +685,7 @@ export class LeverageZapV2Module {
|
|
|
675
685
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
676
686
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
677
687
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
688
|
+
const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
|
|
678
689
|
|
|
679
690
|
const zapCalldata = buildCalldataForLeverageZapV2(router, calldata);
|
|
680
691
|
|
|
@@ -683,7 +694,7 @@ export class LeverageZapV2Module {
|
|
|
683
694
|
_userCollateral,
|
|
684
695
|
_debt,
|
|
685
696
|
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
686
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
697
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
687
698
|
zapCalldata,
|
|
688
699
|
{ ...this.llamalend.constantOptions }
|
|
689
700
|
);
|
|
@@ -696,7 +707,7 @@ export class LeverageZapV2Module {
|
|
|
696
707
|
_userCollateral,
|
|
697
708
|
_debt,
|
|
698
709
|
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
699
|
-
[0, parseUnits(this._getMarketId(), 0), _userBorrowed],
|
|
710
|
+
[0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
|
|
700
711
|
zapCalldata,
|
|
701
712
|
{ ...this.llamalend.options, gasLimit }
|
|
702
713
|
)).hash
|
|
@@ -716,28 +727,30 @@ export class LeverageZapV2Module {
|
|
|
716
727
|
return await this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
|
|
717
728
|
}
|
|
718
729
|
|
|
719
|
-
public async leverageBorrowMoreEstimateGas({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
730
|
+
public async leverageBorrowMoreEstimateGas({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
720
731
|
userCollateral: TAmount,
|
|
721
732
|
userBorrowed: TAmount,
|
|
722
733
|
debt: TAmount,
|
|
734
|
+
minRecv: TAmount,
|
|
723
735
|
router: string,
|
|
724
736
|
calldata: string
|
|
725
737
|
}): Promise<number> {
|
|
726
738
|
this._checkLeverageZap();
|
|
727
739
|
if (!(await this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
|
|
728
|
-
return await this._leverageBorrowMore(userCollateral, userBorrowed, debt, router, calldata, true) as number;
|
|
740
|
+
return await this._leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, true) as number;
|
|
729
741
|
}
|
|
730
742
|
|
|
731
|
-
public async leverageBorrowMore({ userCollateral, userBorrowed, debt, router, calldata }: {
|
|
743
|
+
public async leverageBorrowMore({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
|
|
732
744
|
userCollateral: TAmount,
|
|
733
745
|
userBorrowed: TAmount,
|
|
734
746
|
debt: TAmount,
|
|
747
|
+
minRecv: TAmount,
|
|
735
748
|
router: string,
|
|
736
749
|
calldata: string
|
|
737
750
|
}): Promise<string> {
|
|
738
751
|
this._checkLeverageZap();
|
|
739
752
|
await this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
|
|
740
|
-
return await this._leverageBorrowMore(userCollateral, userBorrowed, debt, router, calldata, false) as string;
|
|
753
|
+
return await this._leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, false) as string;
|
|
741
754
|
}
|
|
742
755
|
|
|
743
756
|
public async leverageBorrowMoreFutureLeverage({ userCollateral, userBorrowed, debt, quote, address = "" }: {
|
|
@@ -959,6 +972,7 @@ export class LeverageZapV2Module {
|
|
|
959
972
|
stateCollateral: TAmount,
|
|
960
973
|
userCollateral: TAmount,
|
|
961
974
|
userBorrowed: TAmount,
|
|
975
|
+
minRecv: TAmount,
|
|
962
976
|
router: string,
|
|
963
977
|
calldata: string,
|
|
964
978
|
estimateGas: boolean
|
|
@@ -967,6 +981,8 @@ export class LeverageZapV2Module {
|
|
|
967
981
|
const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
|
|
968
982
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
969
983
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
984
|
+
const _minRecv = parseUnits(minRecv, this.market.borrowed_token.decimals);
|
|
985
|
+
|
|
970
986
|
let zapCalldata = buildCalldataForLeverageZapV2(router, "0x");
|
|
971
987
|
if (_stateCollateral + _userCollateral > BigInt(0)) {
|
|
972
988
|
zapCalldata = buildCalldataForLeverageZapV2(router, calldata)
|
|
@@ -975,7 +991,7 @@ export class LeverageZapV2Module {
|
|
|
975
991
|
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
976
992
|
const gas = await contract.repay_extended.estimateGas(
|
|
977
993
|
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
978
|
-
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed],
|
|
994
|
+
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
|
|
979
995
|
zapCalldata
|
|
980
996
|
);
|
|
981
997
|
if (estimateGas) return smartNumber(gas);
|
|
@@ -985,34 +1001,36 @@ export class LeverageZapV2Module {
|
|
|
985
1001
|
|
|
986
1002
|
return (await contract.repay_extended(
|
|
987
1003
|
this.llamalend.constants.ALIASES.leverage_zap_v2,
|
|
988
|
-
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed],
|
|
1004
|
+
[0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
|
|
989
1005
|
zapCalldata,
|
|
990
1006
|
{ ...this.llamalend.options, gasLimit }
|
|
991
1007
|
)).hash
|
|
992
1008
|
}
|
|
993
1009
|
|
|
994
|
-
public async leverageRepayEstimateGas({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
1010
|
+
public async leverageRepayEstimateGas({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
995
1011
|
stateCollateral: TAmount,
|
|
996
1012
|
userCollateral: TAmount,
|
|
997
1013
|
userBorrowed: TAmount,
|
|
1014
|
+
minRecv: TAmount,
|
|
998
1015
|
router: string,
|
|
999
1016
|
calldata: string
|
|
1000
1017
|
}): Promise<number> {
|
|
1001
1018
|
this._checkLeverageZap();
|
|
1002
1019
|
if (!(await this.leverageRepayIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
|
|
1003
|
-
return await this._leverageRepay(stateCollateral, userCollateral, userBorrowed, router, calldata, true) as number;
|
|
1020
|
+
return await this._leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, true) as number;
|
|
1004
1021
|
}
|
|
1005
1022
|
|
|
1006
|
-
public async leverageRepay({ stateCollateral, userCollateral, userBorrowed, router, calldata }: {
|
|
1023
|
+
public async leverageRepay({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
|
|
1007
1024
|
stateCollateral: TAmount,
|
|
1008
1025
|
userCollateral: TAmount,
|
|
1009
1026
|
userBorrowed: TAmount,
|
|
1027
|
+
minRecv: TAmount,
|
|
1010
1028
|
router: string,
|
|
1011
1029
|
calldata: string
|
|
1012
1030
|
}): Promise<string> {
|
|
1013
1031
|
this._checkLeverageZap();
|
|
1014
1032
|
await this.leverageRepayApprove({ userCollateral, userBorrowed });
|
|
1015
|
-
return await this._leverageRepay(stateCollateral, userCollateral, userBorrowed, router, calldata, false) as string;
|
|
1033
|
+
return await this._leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, false) as string;
|
|
1016
1034
|
}
|
|
1017
1035
|
|
|
1018
1036
|
public async leverageRepayFutureLeverage({ stateCollateral, userCollateral, userBorrowed, address = "" }: {
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
DIGas,
|
|
22
22
|
calculateFutureLeverage,
|
|
23
23
|
} from "../utils.js";
|
|
24
|
-
import {IDict, ILlamma, TGas} from "../interfaces.js";
|
|
24
|
+
import {IDict, ILlamma, TGas, IRates} from "../interfaces.js";
|
|
25
25
|
import {_getUserCollateralCrvUsd, _getUserCollateralCrvUsdFull} from "../external-api.js";
|
|
26
26
|
import { ILeverageV2 } from "./interfaces/leverage.js";
|
|
27
27
|
import { LeverageV2Module } from "./modules/index.js";
|
|
@@ -71,8 +71,8 @@ export class MintMarketTemplate {
|
|
|
71
71
|
parameters: () => Promise<{
|
|
72
72
|
fee: string, // %
|
|
73
73
|
admin_fee: string, // %
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
rates: IRates,
|
|
75
|
+
future_rates: IRates,
|
|
76
76
|
liquidation_discount: string, // %
|
|
77
77
|
loan_discount: string, // %
|
|
78
78
|
}>,
|
|
@@ -282,11 +282,23 @@ export class MintMarketTemplate {
|
|
|
282
282
|
|
|
283
283
|
// ---------------- STATS ----------------
|
|
284
284
|
|
|
285
|
+
private _buildRatesObject(rate: bigint): IRates {
|
|
286
|
+
const borrowApr = toBN(rate).times(365).times(86400).times(100).toString();
|
|
287
|
+
// borrowApy = e**(rate*365*86400) - 1
|
|
288
|
+
const borrowApy = String(((2.718281828459 ** (toBN(rate).times(365).times(86400)).toNumber()) - 1) * 100);
|
|
289
|
+
return {
|
|
290
|
+
borrowApr,
|
|
291
|
+
borrowApy,
|
|
292
|
+
lendApr: "0",
|
|
293
|
+
lendApy: "0",
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
285
297
|
public statsParameters = memoize(async (): Promise<{
|
|
286
298
|
fee: string, // %
|
|
287
299
|
admin_fee: string, // %
|
|
288
|
-
|
|
289
|
-
|
|
300
|
+
rates: IRates,
|
|
301
|
+
future_rates: IRates,
|
|
290
302
|
liquidation_discount: string, // %
|
|
291
303
|
loan_discount: string, // %
|
|
292
304
|
}> => {
|
|
@@ -307,11 +319,10 @@ export class MintMarketTemplate {
|
|
|
307
319
|
const [fee, admin_fee, liquidation_discount, loan_discount] = [_fee, _admin_fee, _liquidation_discount, _loan_discount]
|
|
308
320
|
.map((x) => formatUnits(x * BigInt(100)));
|
|
309
321
|
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
const future_rate = String(((2.718281828459 ** Number((toBN(_mp_rate).times(365).times(86400)))) - 1) * 100);
|
|
322
|
+
const rates = this._buildRatesObject(_rate);
|
|
323
|
+
const future_rates = this._buildRatesObject(_mp_rate);
|
|
313
324
|
|
|
314
|
-
return { fee, admin_fee,
|
|
325
|
+
return { fee, admin_fee, rates, future_rates, liquidation_discount, loan_discount }
|
|
315
326
|
},
|
|
316
327
|
{
|
|
317
328
|
promise: true,
|