@curvefi/llamalend-api 2.2.5 → 2.3.0

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.
Files changed (34) hide show
  1. package/README.md +23 -26
  2. package/lib/constants/abis/ControllerV2.json +0 -17
  3. package/lib/constants/aliases.js +5 -5
  4. package/lib/lendMarkets/LendMarketTemplate.js +1 -0
  5. package/lib/lendMarkets/interfaces/leverageZapV2.d.ts +31 -63
  6. package/lib/lendMarkets/interfaces/v1/loanV1.d.ts +1 -0
  7. package/lib/lendMarkets/interfaces/v2/loanV2.d.ts +1 -0
  8. package/lib/lendMarkets/modules/common/leverageZapV2Base.d.ts +34 -65
  9. package/lib/lendMarkets/modules/common/leverageZapV2Base.js +107 -127
  10. package/lib/lendMarkets/modules/common/loanBase.d.ts +1 -0
  11. package/lib/lendMarkets/modules/v1/leverageV1ZapV2.d.ts +3 -3
  12. package/lib/lendMarkets/modules/v1/leverageV1ZapV2.js +12 -12
  13. package/lib/lendMarkets/modules/v1/loanV1.d.ts +1 -0
  14. package/lib/lendMarkets/modules/v1/loanV1.js +5 -0
  15. package/lib/lendMarkets/modules/v2/leverageV2ZapV2.d.ts +3 -3
  16. package/lib/lendMarkets/modules/v2/leverageV2ZapV2.js +10 -17
  17. package/lib/lendMarkets/modules/v2/loanV2.d.ts +1 -0
  18. package/lib/lendMarkets/modules/v2/loanV2.js +9 -0
  19. package/lib/utils.d.ts +3 -11
  20. package/lib/utils.js +2 -10
  21. package/package.json +1 -1
  22. package/src/constants/abis/ControllerV2.json +0 -17
  23. package/src/constants/aliases.ts +5 -5
  24. package/src/lendMarkets/LendMarketTemplate.ts +1 -0
  25. package/src/lendMarkets/interfaces/leverageZapV2.ts +30 -72
  26. package/src/lendMarkets/interfaces/v1/loanV1.ts +2 -0
  27. package/src/lendMarkets/interfaces/v2/loanV2.ts +2 -0
  28. package/src/lendMarkets/modules/common/leverageZapV2Base.ts +112 -175
  29. package/src/lendMarkets/modules/common/loanBase.ts +2 -0
  30. package/src/lendMarkets/modules/v1/leverageV1ZapV2.ts +9 -12
  31. package/src/lendMarkets/modules/v1/loanV1.ts +4 -0
  32. package/src/lendMarkets/modules/v2/leverageV2ZapV2.ts +7 -17
  33. package/src/lendMarkets/modules/v2/loanV2.ts +10 -0
  34. package/src/utils.ts +5 -27
@@ -46,7 +46,6 @@ export abstract class LeverageZapV2BaseModule {
46
46
 
47
47
  protected abstract _createLoanContractCall(
48
48
  _userCollateral: bigint,
49
- _userBorrowed: bigint,
50
49
  _debt: bigint,
51
50
  _minRecv: bigint,
52
51
  range: number,
@@ -57,7 +56,6 @@ export abstract class LeverageZapV2BaseModule {
57
56
 
58
57
  protected abstract _borrowMoreContractCall(
59
58
  _userCollateral: bigint,
60
- _userBorrowed: bigint,
61
59
  _debt: bigint,
62
60
  _minRecv: bigint,
63
61
  router: string,
@@ -67,7 +65,6 @@ export abstract class LeverageZapV2BaseModule {
67
65
 
68
66
  protected abstract _repayContractCall(
69
67
  _userCollateral: bigint,
70
- _userBorrowed: bigint,
71
68
  _minRecv: bigint,
72
69
  router: string,
73
70
  exchangeCalldata: string,
@@ -125,9 +122,8 @@ export abstract class LeverageZapV2BaseModule {
125
122
  return BN(expected).times(BN(100).minus(slippage)).div(100).toString();
126
123
  }
127
124
 
128
- public async leverageCreateLoanMaxRecv({ userCollateral, userBorrowed, range, getExpected }: {
125
+ public async leverageCreateLoanMaxRecv({ userCollateral, range, getExpected }: {
129
126
  userCollateral: TAmount,
130
- userBorrowed: TAmount,
131
127
  range: number,
132
128
  getExpected: GetExpectedFn
133
129
  }): Promise<{
@@ -143,7 +139,6 @@ export abstract class LeverageZapV2BaseModule {
143
139
  this._checkLeverageZap();
144
140
  if (range > 0) this.market.prices.checkRange(range);
145
141
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
146
- const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
147
142
 
148
143
  const oraclePriceBand = await this.market.prices.oraclePriceBand();
149
144
  let pAvgBN = BN(await this.market.prices.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
@@ -155,7 +150,7 @@ export abstract class LeverageZapV2BaseModule {
155
150
  const contract = this.llamalend.contracts[this._getLeverageZapAddress()].contract;
156
151
  for (let i = 0; i < 5; i++) {
157
152
  maxBorrowablePrevBN = maxBorrowableBN;
158
- _userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.market.collateral_token.decimals);
153
+ _userEffectiveCollateral = _userCollateral;
159
154
  let _maxBorrowable = await contract.max_borrowable(this.market.addresses.controller, _userEffectiveCollateral, _maxLeverageCollateral, range, fromBN(pAvgBN));
160
155
  _maxBorrowable = _maxBorrowable * BigInt(970) / BigInt(1000)
161
156
  if (_maxBorrowable === BigInt(0)) break;
@@ -166,16 +161,15 @@ export abstract class LeverageZapV2BaseModule {
166
161
  break;
167
162
  }
168
163
 
169
- // additionalCollateral = (userBorrowed / p) + leverageCollateral
170
164
  const _maxAdditionalCollateral = BigInt((await getExpected(
171
165
  this.market.addresses.borrowed_token,
172
166
  this.market.addresses.collateral_token,
173
- _maxBorrowable + _userBorrowed,
167
+ _maxBorrowable,
174
168
  this.market.addresses.amm
175
169
  )).outAmount);
176
170
 
177
- pAvgBN = maxBorrowableBN.plus(userBorrowed).div(toBN(_maxAdditionalCollateral, this.market.collateral_token.decimals));
178
- _maxLeverageCollateral = _maxAdditionalCollateral - fromBN(BN(userBorrowed).div(pAvgBN), this.market.collateral_token.decimals);
171
+ pAvgBN = maxBorrowableBN.div(toBN(_maxAdditionalCollateral, this.market.collateral_token.decimals));
172
+ _maxLeverageCollateral = _maxAdditionalCollateral;
179
173
  }
180
174
 
181
175
  const userEffectiveCollateralBN = maxBorrowableBN.gt(0) ? toBN(_userEffectiveCollateral, this.market.collateral_token.decimals) : BN(0);
@@ -186,16 +180,15 @@ export abstract class LeverageZapV2BaseModule {
186
180
  maxDebt: formatNumber(maxBorrowableBN.toString(), this.market.borrowed_token.decimals),
187
181
  maxTotalCollateral: formatNumber(maxLeverageCollateralBN.plus(userEffectiveCollateralBN).toString(), this.market.collateral_token.decimals),
188
182
  userCollateral: formatNumber(userCollateral, this.market.collateral_token.decimals),
189
- collateralFromUserBorrowed: formatNumber(BN(userBorrowed).div(pAvgBN).toString(), this.market.collateral_token.decimals),
183
+ collateralFromUserBorrowed: formatNumber(BN(0).toString(), this.market.collateral_token.decimals),
190
184
  collateralFromMaxDebt: formatNumber(maxLeverageCollateralBN.toString(), this.market.collateral_token.decimals),
191
185
  maxLeverage: maxLeverageCollateralBN.plus(userEffectiveCollateralBN).div(userEffectiveCollateralBN).toString(),
192
186
  avgPrice: pAvgBN.toString(),
193
187
  };
194
188
  }
195
189
 
196
- public leverageCreateLoanMaxRecvAllRanges = memoize(async ({ userCollateral, userBorrowed, getExpected }: {
190
+ public leverageCreateLoanMaxRecvAllRanges = memoize(async ({ userCollateral, getExpected }: {
197
191
  userCollateral: TAmount,
198
- userBorrowed: TAmount,
199
192
  getExpected: GetExpectedFn
200
193
  }): Promise<IDict<{
201
194
  maxDebt: string,
@@ -223,7 +216,7 @@ export abstract class LeverageZapV2BaseModule {
223
216
  for (let i = 0; i < 5; i++) {
224
217
  const pBN = pAvgBN ?? pAvgApproxBN;
225
218
  maxBorrowablePrevBN = maxBorrowableBN;
226
- const _userEffectiveCollateral: bigint = _userCollateral + fromBN(BN(userBorrowed).div(pBN), this.market.collateral_token.decimals);
219
+ const _userEffectiveCollateral: bigint = _userCollateral;
227
220
  const calls = [];
228
221
  for (let N = this.market.minBands; N <= this.market.maxBands; N++) {
229
222
  const j = N - this.market.minBands;
@@ -253,7 +246,7 @@ export abstract class LeverageZapV2BaseModule {
253
246
  _maxLeverageCollateral = maxLeverageCollateralBN.map((mlc) => fromBN(mlc, this.market.collateral_token.decimals));
254
247
  }
255
248
 
256
- const userEffectiveCollateralBN = BN(userCollateral).plus(BN(userBorrowed).div(pAvgBN as BigNumber));
249
+ const userEffectiveCollateralBN = BN(userCollateral);
257
250
 
258
251
  const res: IDict<{
259
252
  maxDebt: string,
@@ -270,7 +263,7 @@ export abstract class LeverageZapV2BaseModule {
270
263
  maxDebt: formatNumber(maxBorrowableBN[j].toString(), this.market.borrowed_token.decimals),
271
264
  maxTotalCollateral: formatNumber(maxLeverageCollateralBN[j].plus(userEffectiveCollateralBN).toString(), this.market.collateral_token.decimals),
272
265
  userCollateral: formatNumber(userCollateral, this.market.collateral_token.decimals),
273
- collateralFromUserBorrowed: formatNumber(BN(userBorrowed).div(pAvgBN as BigNumber).toString(), this.market.collateral_token.decimals),
266
+ collateralFromUserBorrowed: formatNumber(BN(0).toString(), this.market.collateral_token.decimals),
274
267
  collateralFromMaxDebt: formatNumber(maxLeverageCollateralBN[j].toString(), this.market.collateral_token.decimals),
275
268
  maxLeverage: maxLeverageCollateralBN[j].plus(userEffectiveCollateralBN).div(userEffectiveCollateralBN).toString(),
276
269
  avgPrice: (pAvgBN as BigNumber).toString(),
@@ -284,16 +277,14 @@ export abstract class LeverageZapV2BaseModule {
284
277
  maxAge: 60 * 1000, // 1m
285
278
  });
286
279
 
287
- private _leverageExpectedCollateral = async (userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, quote: IQuote, user?: string):
280
+ private _leverageExpectedCollateral = async (userCollateral: TAmount, debt: TAmount, quote: IQuote, user?: string):
288
281
  Promise<{ _futureStateCollateral: bigint, _totalCollateral: bigint, _userCollateral: bigint,
289
282
  _collateralFromUserBorrowed: bigint, _collateralFromDebt: bigint, avgPrice: string }> => {
290
283
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
291
284
  const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
292
- const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
293
- // additionalCollateral = (userBorrowed / p) + leverageCollateral
294
285
  const _additionalCollateral = BigInt(quote.outAmount);
295
- const _collateralFromDebt = _debt * BigInt(10**18) / (_debt + _userBorrowed) * _additionalCollateral / BigInt(10**18);
296
- const _collateralFromUserBorrowed = _additionalCollateral - _collateralFromDebt;
286
+ const _collateralFromDebt = _additionalCollateral;
287
+ const _collateralFromUserBorrowed = BigInt(0);
297
288
  let _stateCollateral = BigInt(0);
298
289
  if (user) {
299
290
  const { _collateral, _borrowed } = await this.market.userPosition.userStateBigInt(user);
@@ -302,21 +293,20 @@ export abstract class LeverageZapV2BaseModule {
302
293
  }
303
294
  const _totalCollateral = _userCollateral + _additionalCollateral;
304
295
  const _futureStateCollateral = _stateCollateral + _totalCollateral;
305
- const avgPrice = toBN(_debt + _userBorrowed, this.market.borrowed_token.decimals).div(toBN(_additionalCollateral, this.market.collateral_token.decimals)).toString();
296
+ const avgPrice = toBN(_debt, this.market.borrowed_token.decimals).div(toBN(_additionalCollateral, this.market.collateral_token.decimals)).toString();
306
297
 
307
298
  return { _futureStateCollateral, _totalCollateral, _userCollateral, _collateralFromUserBorrowed, _collateralFromDebt, avgPrice };
308
299
  };
309
300
 
310
- public async leverageCreateLoanExpectedCollateral({ userCollateral, userBorrowed, debt, quote }: {
301
+ public async leverageCreateLoanExpectedCollateral({ userCollateral, debt, quote }: {
311
302
  userCollateral: TAmount,
312
- userBorrowed: TAmount,
313
303
  debt: TAmount,
314
304
  quote: IQuote
315
305
  }): Promise<{ totalCollateral: string, userCollateral: string, collateralFromUserBorrowed: string, collateralFromDebt: string, leverage: string, avgPrice: string }> {
316
306
  this._checkLeverageZap();
317
307
 
318
308
  const { _totalCollateral, _userCollateral, _collateralFromUserBorrowed, _collateralFromDebt, avgPrice } =
319
- await this._leverageExpectedCollateral(userCollateral, userBorrowed, debt, quote);
309
+ await this._leverageExpectedCollateral(userCollateral, debt, quote);
320
310
  return {
321
311
  totalCollateral: formatUnits(_totalCollateral, this.market.collateral_token.decimals),
322
312
  userCollateral: formatUnits(_userCollateral, this.market.collateral_token.decimals),
@@ -328,9 +318,8 @@ export abstract class LeverageZapV2BaseModule {
328
318
  }
329
319
  }
330
320
 
331
- public async leverageCreateLoanExpectedMetrics({ userCollateral, userBorrowed, debt, range, quote, healthIsFull = true }: {
321
+ public async leverageCreateLoanExpectedMetrics({ userCollateral, debt, range, quote, healthIsFull = true }: {
332
322
  userCollateral: TAmount,
333
- userBorrowed: TAmount,
334
323
  debt: TAmount,
335
324
  range: number,
336
325
  quote: IQuote,
@@ -338,10 +327,10 @@ export abstract class LeverageZapV2BaseModule {
338
327
  }): Promise<ILeverageMetrics> {
339
328
  this._checkLeverageZap();
340
329
 
341
- const [_n2, _n1] = await this._leverageBands(userCollateral, userBorrowed, debt, range, quote);
330
+ const [_n2, _n1] = await this._leverageBands(userCollateral, debt, range, quote);
342
331
 
343
332
  const prices = await this.market.prices.getPrices(_n2, _n1);
344
- const health = await this._leverageHealth(userCollateral, userBorrowed, debt, range, quote, healthIsFull);
333
+ const health = await this._leverageHealth(userCollateral, debt, range, quote, healthIsFull);
345
334
 
346
335
  return {
347
336
  priceImpact: quote.priceImpact,
@@ -351,14 +340,13 @@ export abstract class LeverageZapV2BaseModule {
351
340
  }
352
341
  }
353
342
 
354
- public async leverageCreateLoanMaxRange({ userCollateral, userBorrowed, debt, getExpected }: {
343
+ public async leverageCreateLoanMaxRange({ userCollateral, debt, getExpected }: {
355
344
  userCollateral: TAmount,
356
- userBorrowed: TAmount,
357
345
  debt: TAmount,
358
346
  getExpected: GetExpectedFn
359
347
  }): Promise<number> {
360
348
  this._checkLeverageZap();
361
- const maxRecv = await this.leverageCreateLoanMaxRecvAllRanges({ userCollateral, userBorrowed, getExpected });
349
+ const maxRecv = await this.leverageCreateLoanMaxRecvAllRanges({ userCollateral, getExpected });
362
350
  for (let N = this.market.minBands; N <= this.market.maxBands; N++) {
363
351
  if (BN(debt).gt(maxRecv[N].maxDebt)) return N - 1;
364
352
  }
@@ -366,7 +354,7 @@ export abstract class LeverageZapV2BaseModule {
366
354
  return this.market.maxBands;
367
355
  }
368
356
 
369
- private _leverageCalcN1 = memoize(async (userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, range: number, quote: IQuote, user?: string): Promise<bigint> => {
357
+ private _leverageCalcN1 = memoize(async (userCollateral: TAmount, debt: TAmount, range: number, quote: IQuote, user?: string): Promise<bigint> => {
370
358
  if (range > 0) this.market.prices.checkRange(range);
371
359
  let _stateDebt = BigInt(0);
372
360
  if (user) {
@@ -375,7 +363,7 @@ export abstract class LeverageZapV2BaseModule {
375
363
  _stateDebt = _debt;
376
364
  if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
377
365
  }
378
- const { _futureStateCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, debt, quote, user);
366
+ const { _futureStateCollateral } = await this._leverageExpectedCollateral(userCollateral, debt, quote, user);
379
367
  const _debt = _stateDebt + parseUnits(debt, this.market.borrowed_token.decimals);
380
368
  return await this._calcDebtN1Call(_futureStateCollateral, _debt, range);
381
369
  },
@@ -384,8 +372,8 @@ export abstract class LeverageZapV2BaseModule {
384
372
  maxAge: 60 * 1000, // 1m
385
373
  });
386
374
 
387
- private _leverageCalcN1AllRanges = memoize(async (userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, maxN: number, quote: IQuote): Promise<bigint[]> => {
388
- const { _futureStateCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, debt, quote);
375
+ private _leverageCalcN1AllRanges = memoize(async (userCollateral: TAmount, debt: TAmount, maxN: number, quote: IQuote): Promise<bigint[]> => {
376
+ const { _futureStateCollateral } = await this._leverageExpectedCollateral(userCollateral, debt, quote);
389
377
  const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
390
378
  const calls = [];
391
379
  for (let N = this.market.minBands; N <= maxN; N++) {
@@ -398,8 +386,8 @@ export abstract class LeverageZapV2BaseModule {
398
386
  maxAge: 60 * 1000, // 1m
399
387
  });
400
388
 
401
- private async _leverageBands(userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, range: number, quote: IQuote, user?: string): Promise<[bigint, bigint]> {
402
- const _n1 = await this._leverageCalcN1(userCollateral, userBorrowed, debt, range, quote, user);
389
+ private async _leverageBands(userCollateral: TAmount, debt: TAmount, range: number, quote: IQuote, user?: string): Promise<[bigint, bigint]> {
390
+ const _n1 = await this._leverageCalcN1(userCollateral, debt, range, quote, user);
403
391
  if (range < 0) {
404
392
  const { N } = await this.market.userPosition.userState(user);
405
393
  range = Number(N);
@@ -409,9 +397,9 @@ export abstract class LeverageZapV2BaseModule {
409
397
  return [_n2, _n1];
410
398
  }
411
399
 
412
- private async _leverageCreateLoanBandsAllRanges(userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, getExpected: GetExpectedFn, quote: IQuote): Promise<IDict<[bigint, bigint]>> {
413
- const maxN = await this.leverageCreateLoanMaxRange({ userCollateral, userBorrowed, debt, getExpected });
414
- const _n1_arr = await this._leverageCalcN1AllRanges(userCollateral, userBorrowed, debt, maxN, quote);
400
+ private async _leverageCreateLoanBandsAllRanges(userCollateral: TAmount, debt: TAmount, getExpected: GetExpectedFn, quote: IQuote): Promise<IDict<[bigint, bigint]>> {
401
+ const maxN = await this.leverageCreateLoanMaxRange({ userCollateral, debt, getExpected });
402
+ const _n1_arr = await this._leverageCalcN1AllRanges(userCollateral, debt, maxN, quote);
415
403
  const _n2_arr: bigint[] = [];
416
404
  for (let N = this.market.minBands; N <= maxN; N++) {
417
405
  _n2_arr.push(_n1_arr[N - this.market.minBands] + BigInt(N - 1));
@@ -425,15 +413,14 @@ export abstract class LeverageZapV2BaseModule {
425
413
  return _bands;
426
414
  }
427
415
 
428
- public async leverageCreateLoanBandsAllRanges({ userCollateral, userBorrowed, debt, getExpected, quote }: {
416
+ public async leverageCreateLoanBandsAllRanges({ userCollateral, debt, getExpected, quote }: {
429
417
  userCollateral: TAmount,
430
- userBorrowed: TAmount,
431
418
  debt: TAmount,
432
419
  getExpected: GetExpectedFn,
433
420
  quote: IQuote
434
421
  }): Promise<IDict<[number, number] | null>> {
435
422
  this._checkLeverageZap();
436
- const _bands = await this._leverageCreateLoanBandsAllRanges(userCollateral, userBorrowed, debt, getExpected, quote);
423
+ const _bands = await this._leverageCreateLoanBandsAllRanges(userCollateral, debt, getExpected, quote);
437
424
 
438
425
  const bands: { [index: number]: [number, number] | null } = {};
439
426
  for (let N = this.market.minBands; N <= this.market.maxBands; N++) {
@@ -447,15 +434,14 @@ export abstract class LeverageZapV2BaseModule {
447
434
  return bands;
448
435
  }
449
436
 
450
- public async leverageCreateLoanPricesAllRanges({ userCollateral, userBorrowed, debt, getExpected, quote }: {
437
+ public async leverageCreateLoanPricesAllRanges({ userCollateral, debt, getExpected, quote }: {
451
438
  userCollateral: TAmount,
452
- userBorrowed: TAmount,
453
439
  debt: TAmount,
454
440
  getExpected: GetExpectedFn,
455
441
  quote: IQuote
456
442
  }): Promise<IDict<[string, string] | null>> {
457
443
  this._checkLeverageZap();
458
- const _bands = await this._leverageCreateLoanBandsAllRanges(userCollateral, userBorrowed, debt, getExpected, quote);
444
+ const _bands = await this._leverageCreateLoanBandsAllRanges(userCollateral, debt, getExpected, quote);
459
445
 
460
446
  const prices: { [index: number]: [string, string] | null } = {};
461
447
  for (let N = this.market.minBands; N <= this.market.maxBands; N++) {
@@ -471,7 +457,6 @@ export abstract class LeverageZapV2BaseModule {
471
457
 
472
458
  private async _leverageHealth(
473
459
  userCollateral: TAmount,
474
- userBorrowed: TAmount,
475
460
  dDebt: TAmount,
476
461
  range: number,
477
462
  quote: IQuote,
@@ -479,7 +464,7 @@ export abstract class LeverageZapV2BaseModule {
479
464
  user = this.llamalend.constants.ZERO_ADDRESS
480
465
  ): Promise<string> {
481
466
  if (range > 0) this.market.prices.checkRange(range);
482
- const { _totalCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, dDebt, quote, user);
467
+ const { _totalCollateral } = await this._leverageExpectedCollateral(userCollateral, dDebt, quote, user);
483
468
  const { _borrowed, _N } = await this.market.userPosition.userStateBigInt(user);
484
469
  if (_borrowed > BigInt(0)) throw Error(`User ${user} is already in liquidation mode`);
485
470
  if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
@@ -494,52 +479,32 @@ export abstract class LeverageZapV2BaseModule {
494
479
  return formatUnits(_health);
495
480
  }
496
481
 
497
- public async leverageCreateLoanIsApproved({ userCollateral, userBorrowed }: {
498
- userCollateral: TAmount,
499
- userBorrowed: TAmount
482
+ public async leverageCreateLoanIsApproved({ userCollateral }: {
483
+ userCollateral: TAmount
500
484
  }): Promise<boolean> {
501
485
  this._checkLeverageZap();
502
- const collateralAllowance = await hasAllowance.call(this.llamalend,
486
+ return await hasAllowance.call(this.llamalend,
503
487
  [this.market.collateral_token.address], [userCollateral], this.llamalend.signerAddress, this.market.addresses.controller);
504
- const borrowedAllowance = await hasAllowance.call(this.llamalend,
505
- [this.market.borrowed_token.address], [userBorrowed], this.llamalend.signerAddress, this._getLeverageZapAddress());
506
-
507
- return collateralAllowance && borrowedAllowance
508
488
  }
509
489
 
510
- public async leverageCreateLoanApproveEstimateGas ({ userCollateral, userBorrowed }: {
511
- userCollateral: TAmount,
512
- userBorrowed: TAmount
490
+ public async leverageCreateLoanApproveEstimateGas ({ userCollateral }: {
491
+ userCollateral: TAmount
513
492
  }): Promise<TGas> {
514
493
  this._checkLeverageZap();
515
- const collateralGas = await ensureAllowanceEstimateGas.call(this.llamalend,
494
+ return await ensureAllowanceEstimateGas.call(this.llamalend,
516
495
  [this.market.collateral_token.address], [userCollateral], this.market.addresses.controller);
517
- const borrowedGas = await ensureAllowanceEstimateGas.call(this.llamalend,
518
- [this.market.borrowed_token.address], [userBorrowed], this._getLeverageZapAddress());
519
-
520
- if(Array.isArray(collateralGas) && Array.isArray(borrowedGas)) {
521
- return [collateralGas[0] + borrowedGas[0], collateralGas[1] + borrowedGas[1]]
522
- } else {
523
- return (collateralGas as number) + (borrowedGas as number)
524
- }
525
496
  }
526
497
 
527
- public async leverageCreateLoanApprove({ userCollateral, userBorrowed }: {
528
- userCollateral: TAmount,
529
- userBorrowed: TAmount
498
+ public async leverageCreateLoanApprove({ userCollateral }: {
499
+ userCollateral: TAmount
530
500
  }): Promise<string[]> {
531
501
  this._checkLeverageZap();
532
- const collateralApproveTx = await ensureAllowance.call(this.llamalend,
502
+ return await ensureAllowance.call(this.llamalend,
533
503
  [this.market.collateral_token.address], [userCollateral], this.market.addresses.controller);
534
- const borrowedApproveTx = await ensureAllowance.call(this.llamalend,
535
- [this.market.borrowed_token.address], [userBorrowed], this._getLeverageZapAddress());
536
-
537
- return [...collateralApproveTx, ...borrowedApproveTx]
538
504
  }
539
505
 
540
506
  private async _leverageCreateLoan(
541
507
  userCollateral: TAmount,
542
- userBorrowed: TAmount,
543
508
  debt: TAmount,
544
509
  range: number,
545
510
  minRecv: TAmount,
@@ -551,18 +516,16 @@ export abstract class LeverageZapV2BaseModule {
551
516
  this.market.prices.checkRange(range);
552
517
 
553
518
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
554
- const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
555
519
  const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
556
520
  const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
557
521
 
558
522
  return await this._createLoanContractCall(
559
- _userCollateral, _userBorrowed, _debt, _minRecv, range, router, calldata, estimateGas
523
+ _userCollateral, _debt, _minRecv, range, router, calldata, estimateGas
560
524
  );
561
525
  }
562
526
 
563
- public async leverageCreateLoanEstimateGas({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
527
+ public async leverageCreateLoanEstimateGas({ userCollateral, debt, range, minRecv, router, calldata }: {
564
528
  userCollateral: TAmount,
565
- userBorrowed: TAmount,
566
529
  debt: TAmount,
567
530
  range: number,
568
531
  minRecv: TAmount,
@@ -570,13 +533,12 @@ export abstract class LeverageZapV2BaseModule {
570
533
  calldata: string
571
534
  }): Promise<number> {
572
535
  this._checkLeverageZap();
573
- if (!(await this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
574
- return await this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, true) as number;
536
+ if (!(await this.leverageCreateLoanIsApproved({ userCollateral }))) throw Error("Approval is needed for gas estimation");
537
+ return await this._leverageCreateLoan(userCollateral, debt, range, minRecv, router, calldata, true) as number;
575
538
  }
576
539
 
577
- public async leverageCreateLoan({ userCollateral, userBorrowed, debt, range, minRecv, router, calldata }: {
540
+ public async leverageCreateLoan({ userCollateral, debt, range, minRecv, router, calldata }: {
578
541
  userCollateral: TAmount,
579
- userBorrowed: TAmount,
580
542
  debt: TAmount,
581
543
  range: number,
582
544
  minRecv: TAmount,
@@ -584,15 +546,14 @@ export abstract class LeverageZapV2BaseModule {
584
546
  calldata: string
585
547
  }): Promise<string> {
586
548
  this._checkLeverageZap();
587
- await this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
588
- return await this._leverageCreateLoan(userCollateral, userBorrowed, debt, range, minRecv, router, calldata, false) as string;
549
+ await this.leverageCreateLoanApprove({ userCollateral });
550
+ return await this._leverageCreateLoan(userCollateral, debt, range, minRecv, router, calldata, false) as string;
589
551
  }
590
552
 
591
553
  // ---------------- LEVERAGE BORROW MORE ----------------
592
554
 
593
- public async leverageBorrowMoreMaxRecv({ userCollateral, userBorrowed, getExpected, address = "" }: {
555
+ public async leverageBorrowMoreMaxRecv({ userCollateral, getExpected, address = "" }: {
594
556
  userCollateral: TAmount,
595
- userBorrowed: TAmount,
596
557
  getExpected: GetExpectedFn,
597
558
  address?: string
598
559
  }): Promise<{
@@ -610,8 +571,8 @@ export abstract class LeverageZapV2BaseModule {
610
571
  if (_stateBorrowed > BigInt(0)) throw Error(`User ${address} is already in liquidation mode`);
611
572
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
612
573
  const _borrowedFromStateCollateral = await this._getMaxAdditionalBorrowable(_stateCollateral, BigInt(0), _N, _stateDebt, address);
613
- const _userBorrowed = _borrowedFromStateCollateral + parseUnits(userBorrowed, this.market.borrowed_token.decimals);
614
- userBorrowed = formatUnits(_userBorrowed, this.market.borrowed_token.decimals);
574
+ const _userBorrowed = _borrowedFromStateCollateral;
575
+ const userBorrowed = formatUnits(_userBorrowed, this.market.borrowed_token.decimals);
615
576
 
616
577
  const oraclePriceBand = await this.market.prices.oraclePriceBand();
617
578
  let pAvgBN = BN(await this.market.prices.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
@@ -656,9 +617,8 @@ export abstract class LeverageZapV2BaseModule {
656
617
  };
657
618
  }
658
619
 
659
- public async leverageBorrowMoreExpectedCollateral({ userCollateral, userBorrowed, dDebt, quote, address = "" }: {
620
+ public async leverageBorrowMoreExpectedCollateral({ userCollateral, dDebt, quote, address = "" }: {
660
621
  userCollateral: TAmount,
661
- userBorrowed: TAmount,
662
622
  dDebt: TAmount,
663
623
  quote: IQuote,
664
624
  address?: string
@@ -667,7 +627,7 @@ export abstract class LeverageZapV2BaseModule {
667
627
  address = _getAddress.call(this.llamalend, address);
668
628
 
669
629
  const { _totalCollateral, _userCollateral, _collateralFromUserBorrowed, _collateralFromDebt, avgPrice } =
670
- await this._leverageExpectedCollateral(userCollateral, userBorrowed, dDebt, quote, address);
630
+ await this._leverageExpectedCollateral(userCollateral, dDebt, quote, address);
671
631
  return {
672
632
  totalCollateral: formatUnits(_totalCollateral, this.market.collateral_token.decimals),
673
633
  userCollateral: formatUnits(_userCollateral, this.market.collateral_token.decimals),
@@ -677,9 +637,8 @@ export abstract class LeverageZapV2BaseModule {
677
637
  }
678
638
  }
679
639
 
680
- public async leverageBorrowMoreExpectedMetrics({ userCollateral, userBorrowed, debt, quote, healthIsFull = true, address = "" }: {
640
+ public async leverageBorrowMoreExpectedMetrics({ userCollateral, debt, quote, healthIsFull = true, address = "" }: {
681
641
  userCollateral: TAmount,
682
- userBorrowed: TAmount,
683
642
  debt: TAmount,
684
643
  quote: IQuote,
685
644
  healthIsFull?: boolean,
@@ -689,10 +648,10 @@ export abstract class LeverageZapV2BaseModule {
689
648
  address = _getAddress.call(this.llamalend, address);
690
649
 
691
650
 
692
- const [_n2, _n1] = await this._leverageBands(userCollateral, userBorrowed, debt, -1, quote, address);
651
+ const [_n2, _n1] = await this._leverageBands(userCollateral, debt, -1, quote, address);
693
652
 
694
653
  const prices = await this.market.prices.getPrices(_n2, _n1);
695
- const health = await this._leverageHealth(userCollateral, userBorrowed, debt, -1, quote, healthIsFull, address);
654
+ const health = await this._leverageHealth(userCollateral, debt, -1, quote, healthIsFull, address);
696
655
 
697
656
  return {
698
657
  priceImpact: quote.priceImpact,
@@ -704,7 +663,6 @@ export abstract class LeverageZapV2BaseModule {
704
663
 
705
664
  private async _leverageBorrowMore(
706
665
  userCollateral: TAmount,
707
- userBorrowed: TAmount,
708
666
  debt: TAmount,
709
667
  minRecv: TAmount,
710
668
  router: string,
@@ -713,58 +671,52 @@ export abstract class LeverageZapV2BaseModule {
713
671
  ): Promise<string | TGas> {
714
672
  if (!(await this.market.userPosition.userLoanExists())) throw Error("Loan does not exist");
715
673
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
716
- const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
717
674
  const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
718
675
  const _minRecv = parseUnits(minRecv, this.market.collateral_token.decimals);
719
676
 
720
677
  return await this._borrowMoreContractCall(
721
- _userCollateral, _userBorrowed, _debt, _minRecv, router, calldata, estimateGas
678
+ _userCollateral, _debt, _minRecv, router, calldata, estimateGas
722
679
  );
723
680
  }
724
681
 
725
- public async leverageBorrowMoreIsApproved({ userCollateral, userBorrowed }: {
726
- userCollateral: TAmount,
727
- userBorrowed: TAmount
682
+ public async leverageBorrowMoreIsApproved({ userCollateral }: {
683
+ userCollateral: TAmount
728
684
  }): Promise<boolean> {
729
- return await this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed });
685
+ return await this.leverageCreateLoanIsApproved({ userCollateral });
730
686
  }
731
687
 
732
- public async leverageBorrowMoreApprove({ userCollateral, userBorrowed }: {
733
- userCollateral: TAmount,
734
- userBorrowed: TAmount
688
+ public async leverageBorrowMoreApprove({ userCollateral }: {
689
+ userCollateral: TAmount
735
690
  }): Promise<string[]> {
736
- return await this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
691
+ return await this.leverageCreateLoanApprove({ userCollateral });
737
692
  }
738
693
 
739
- public async leverageBorrowMoreEstimateGas({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
694
+ public async leverageBorrowMoreEstimateGas({ userCollateral, debt, minRecv, router, calldata }: {
740
695
  userCollateral: TAmount,
741
- userBorrowed: TAmount,
742
696
  debt: TAmount,
743
697
  minRecv: TAmount,
744
698
  router: string,
745
699
  calldata: string
746
700
  }): Promise<number> {
747
701
  this._checkLeverageZap();
748
- if (!(await this.leverageCreateLoanIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
749
- return await this._leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, true) as number;
702
+ if (!(await this.leverageCreateLoanIsApproved({ userCollateral }))) throw Error("Approval is needed for gas estimation");
703
+ return await this._leverageBorrowMore(userCollateral, debt, minRecv, router, calldata, true) as number;
750
704
  }
751
705
 
752
- public async leverageBorrowMore({ userCollateral, userBorrowed, debt, minRecv, router, calldata }: {
706
+ public async leverageBorrowMore({ userCollateral, debt, minRecv, router, calldata }: {
753
707
  userCollateral: TAmount,
754
- userBorrowed: TAmount,
755
708
  debt: TAmount,
756
709
  minRecv: TAmount,
757
710
  router: string,
758
711
  calldata: string
759
712
  }): Promise<string> {
760
713
  this._checkLeverageZap();
761
- await this.leverageCreateLoanApprove({ userCollateral, userBorrowed });
762
- return await this._leverageBorrowMore(userCollateral, userBorrowed, debt, minRecv, router, calldata, false) as string;
714
+ await this.leverageCreateLoanApprove({ userCollateral });
715
+ return await this._leverageBorrowMore(userCollateral, debt, minRecv, router, calldata, false) as string;
763
716
  }
764
717
 
765
- public async leverageBorrowMoreFutureLeverage({ userCollateral, userBorrowed, debt, quote, address = "" }: {
718
+ public async leverageBorrowMoreFutureLeverage({ userCollateral, debt, quote, address = "" }: {
766
719
  userCollateral: TAmount,
767
- userBorrowed: TAmount,
768
720
  debt: TAmount,
769
721
  quote: IQuote,
770
722
  address?: string
@@ -776,7 +728,6 @@ export abstract class LeverageZapV2BaseModule {
776
728
 
777
729
  const expected = await this.leverageBorrowMoreExpectedCollateral({
778
730
  userCollateral,
779
- userBorrowed,
780
731
  dDebt: debt,
781
732
  quote,
782
733
  address,
@@ -790,7 +741,7 @@ export abstract class LeverageZapV2BaseModule {
790
741
 
791
742
  // ---------------- LEVERAGE REPAY ----------------
792
743
 
793
- private _leverageRepayExpectedBorrowed = (stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, quote: IQuote):
744
+ private _leverageRepayExpectedBorrowed = (stateCollateral: TAmount, userCollateral: TAmount, quote: IQuote):
794
745
  { _totalBorrowed: bigint, _borrowedFromStateCollateral: bigint, _borrowedFromUserCollateral: bigint, avgPrice: string } => {
795
746
  this._checkLeverageZap();
796
747
  const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
@@ -803,51 +754,48 @@ export abstract class LeverageZapV2BaseModule {
803
754
  _borrowedFromStateCollateral = _stateCollateral * BigInt(10 ** 18) / (_stateCollateral + _userCollateral) * _borrowedExpected / BigInt(10 ** 18);
804
755
  _borrowedFromUserCollateral = _borrowedExpected - _borrowedFromStateCollateral;
805
756
  }
806
- const _totalBorrowed = _borrowedExpected + parseUnits(userBorrowed, this.market.borrowed_token.decimals);
757
+ const _totalBorrowed = _borrowedExpected;
807
758
  const avgPrice = toBN(_borrowedExpected, this.market.borrowed_token.decimals).div(toBN(_stateCollateral + _userCollateral, this.market.collateral_token.decimals)).toString();
808
759
 
809
760
  return { _totalBorrowed, _borrowedFromStateCollateral, _borrowedFromUserCollateral, avgPrice }
810
761
  };
811
762
 
812
- public leverageRepayExpectedBorrowed = async ({ stateCollateral, userCollateral, userBorrowed, quote }: {
763
+ public leverageRepayExpectedBorrowed = async ({ stateCollateral, userCollateral, quote }: {
813
764
  stateCollateral: TAmount,
814
765
  userCollateral: TAmount,
815
- userBorrowed: TAmount,
816
766
  quote: IQuote
817
767
  }): Promise<{ totalBorrowed: string, borrowedFromStateCollateral: string, borrowedFromUserCollateral: string, userBorrowed: string, avgPrice: string }> => {
818
768
  this._checkLeverageZap();
819
769
 
820
770
  const { _totalBorrowed, _borrowedFromStateCollateral, _borrowedFromUserCollateral, avgPrice } =
821
- this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed, quote);
771
+ this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, quote);
822
772
 
823
773
  return {
824
774
  totalBorrowed: formatUnits(_totalBorrowed, this.market.borrowed_token.decimals),
825
775
  borrowedFromStateCollateral: formatUnits(_borrowedFromStateCollateral, this.market.borrowed_token.decimals),
826
776
  borrowedFromUserCollateral: formatUnits(_borrowedFromUserCollateral, this.market.borrowed_token.decimals),
827
- userBorrowed: formatNumber(userBorrowed, this.market.borrowed_token.decimals),
777
+ userBorrowed: formatNumber(0, this.market.borrowed_token.decimals),
828
778
  avgPrice,
829
779
  }
830
780
  };
831
781
 
832
- public async leverageRepayIsFull({ stateCollateral, userCollateral, userBorrowed, quote, address = "" }: {
782
+ public async leverageRepayIsFull({ stateCollateral, userCollateral, quote, address = "" }: {
833
783
  stateCollateral: TAmount,
834
784
  userCollateral: TAmount,
835
- userBorrowed: TAmount,
836
785
  quote: IQuote,
837
786
  address?: string
838
787
  }): Promise<boolean> {
839
788
  this._checkLeverageZap();
840
789
  address = _getAddress.call(this.llamalend, address);
841
790
  const { _borrowed: _stateBorrowed, _debt } = await this.market.userPosition.userStateBigInt(address);
842
- const { _totalBorrowed } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed, quote);
791
+ const { _totalBorrowed } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, quote);
843
792
 
844
793
  return _stateBorrowed + _totalBorrowed > _debt;
845
794
  }
846
795
 
847
- public async leverageRepayIsAvailable({ stateCollateral, userCollateral, userBorrowed, quote, address = "" }: {
796
+ public async leverageRepayIsAvailable({ stateCollateral, userCollateral, quote, address = "" }: {
848
797
  stateCollateral: TAmount,
849
798
  userCollateral: TAmount,
850
- userBorrowed: TAmount,
851
799
  quote: IQuote,
852
800
  address?: string
853
801
  }): Promise<boolean> {
@@ -863,15 +811,14 @@ export abstract class LeverageZapV2BaseModule {
863
811
  // Can't spend more than user has
864
812
  if (BN(stateCollateral).gt(collateral)) return false;
865
813
  // Only full repayment and closing the position is available if user is underwater+
866
- if (BN(borrowed).gt(0)) return await this.leverageRepayIsFull({ stateCollateral, userCollateral, userBorrowed, quote, address });
814
+ if (BN(borrowed).gt(0)) return await this.leverageRepayIsFull({ stateCollateral, userCollateral, quote, address });
867
815
 
868
816
  return true;
869
817
  }
870
818
 
871
- public async leverageRepayExpectedMetrics({ stateCollateral, userCollateral, userBorrowed, healthIsFull, quote, address }: {
819
+ public async leverageRepayExpectedMetrics({ stateCollateral, userCollateral, healthIsFull, quote, address }: {
872
820
  stateCollateral: TAmount,
873
821
  userCollateral: TAmount,
874
- userBorrowed: TAmount,
875
822
  healthIsFull: boolean,
876
823
  quote: IQuote,
877
824
  address: string
@@ -879,9 +826,9 @@ export abstract class LeverageZapV2BaseModule {
879
826
  this._checkLeverageZap();
880
827
  address = _getAddress.call(this.llamalend, address);
881
828
 
882
- const [_n2, _n1] = await this._leverageRepayBands(stateCollateral, userCollateral, userBorrowed, quote, address);
829
+ const [_n2, _n1] = await this._leverageRepayBands(stateCollateral, userCollateral, quote, address);
883
830
  const prices = await this.market.prices.getPrices(_n2, _n1);
884
- const health = await this._leverageRepayHealth(stateCollateral, userCollateral, userBorrowed, quote, healthIsFull, address);
831
+ const health = await this._leverageRepayHealth(stateCollateral, userCollateral, quote, healthIsFull, address);
885
832
 
886
833
  const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
887
834
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
@@ -895,9 +842,9 @@ export abstract class LeverageZapV2BaseModule {
895
842
  }
896
843
  }
897
844
 
898
- private _leverageRepayBands = memoize( async (stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, quote: IQuote, address: string): Promise<[bigint, bigint]> => {
845
+ private _leverageRepayBands = memoize( async (stateCollateral: TAmount, userCollateral: TAmount, quote: IQuote, address: string): Promise<[bigint, bigint]> => {
899
846
  address = _getAddress.call(this.llamalend, address);
900
- if (!(await this.leverageRepayIsAvailable({ stateCollateral, userCollateral, userBorrowed, quote, address }))) return [parseUnits(0, 0), parseUnits(0, 0)];
847
+ if (!(await this.leverageRepayIsAvailable({ stateCollateral, userCollateral, quote, address }))) return [parseUnits(0, 0), parseUnits(0, 0)];
901
848
 
902
849
  const _stateRepayCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
903
850
  const { _collateral: _stateCollateral, _debt: _stateDebt, _N } = await this.market.userPosition.userStateBigInt(address);
@@ -906,7 +853,7 @@ export abstract class LeverageZapV2BaseModule {
906
853
 
907
854
  let _n1 = parseUnits(0, 0);
908
855
  let _n2 = parseUnits(0, 0);
909
- const { _totalBorrowed: _repayExpected } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed, quote);
856
+ const { _totalBorrowed: _repayExpected } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, quote);
910
857
  try {
911
858
  _n1 = await this._calcDebtN1Call(_stateCollateral - _stateRepayCollateral, _stateDebt - _repayExpected, _N);
912
859
  _n2 = _n1 + (_N - BigInt(1));
@@ -921,14 +868,14 @@ export abstract class LeverageZapV2BaseModule {
921
868
  });
922
869
 
923
870
 
924
- private async _leverageRepayHealth(stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, quote: IQuote, full = true, address = ""): Promise<string> {
871
+ private async _leverageRepayHealth(stateCollateral: TAmount, userCollateral: TAmount, quote: IQuote, full = true, address = ""): Promise<string> {
925
872
  this._checkLeverageZap();
926
873
  address = _getAddress.call(this.llamalend, address);
927
874
  const { _borrowed: _stateBorrowed, _debt, _N } = await this.market.userPosition.userStateBigInt(address);
928
875
  if (_stateBorrowed > BigInt(0)) return "0.0";
929
- if (!(await this.leverageRepayIsAvailable({ stateCollateral, userCollateral, userBorrowed, quote, address }))) return "0.0";
876
+ if (!(await this.leverageRepayIsAvailable({ stateCollateral, userCollateral, quote, address }))) return "0.0";
930
877
 
931
- const { _totalBorrowed } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed, quote);
878
+ const { _totalBorrowed } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, quote);
932
879
  const _dCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals) * BigInt(-1);
933
880
  const _dDebt = _totalBorrowed * BigInt(-1);
934
881
 
@@ -939,39 +886,36 @@ export abstract class LeverageZapV2BaseModule {
939
886
  return this.llamalend.formatUnits(_health);
940
887
  }
941
888
 
942
- public async leverageRepayIsApproved({ userCollateral, userBorrowed }: {
943
- userCollateral: TAmount,
944
- userBorrowed: TAmount
889
+ public async leverageRepayIsApproved({ userCollateral }: {
890
+ userCollateral: TAmount
945
891
  }): Promise<boolean> {
946
892
  this._checkLeverageZap();
947
893
  return await hasAllowance.call(this.llamalend,
948
- [this.market.collateral_token.address, this.market.borrowed_token.address],
949
- [userCollateral, userBorrowed],
894
+ [this.market.collateral_token.address],
895
+ [userCollateral],
950
896
  this.llamalend.signerAddress,
951
897
  this._getLeverageZapAddress()
952
898
  );
953
899
  }
954
900
 
955
- public async leverageRepayApproveEstimateGas ({ userCollateral, userBorrowed }: {
956
- userCollateral: TAmount,
957
- userBorrowed: TAmount
901
+ public async leverageRepayApproveEstimateGas ({ userCollateral }: {
902
+ userCollateral: TAmount
958
903
  }): Promise<TGas> {
959
904
  this._checkLeverageZap();
960
905
  return await ensureAllowanceEstimateGas.call(this.llamalend,
961
- [this.market.collateral_token.address, this.market.borrowed_token.address],
962
- [userCollateral, userBorrowed],
906
+ [this.market.collateral_token.address],
907
+ [userCollateral],
963
908
  this._getLeverageZapAddress()
964
909
  );
965
910
  }
966
911
 
967
- public async leverageRepayApprove({ userCollateral, userBorrowed }: {
968
- userCollateral: TAmount,
969
- userBorrowed: TAmount
912
+ public async leverageRepayApprove({ userCollateral }: {
913
+ userCollateral: TAmount
970
914
  }): Promise<string[]> {
971
915
  this._checkLeverageZap();
972
916
  return await ensureAllowance.call(this.llamalend,
973
- [this.market.collateral_token.address, this.market.borrowed_token.address],
974
- [userCollateral, userBorrowed],
917
+ [this.market.collateral_token.address],
918
+ [userCollateral],
975
919
  this._getLeverageZapAddress()
976
920
  );
977
921
  }
@@ -979,7 +923,6 @@ export abstract class LeverageZapV2BaseModule {
979
923
  private async _leverageRepay(
980
924
  stateCollateral: TAmount,
981
925
  userCollateral: TAmount,
982
- userBorrowed: TAmount,
983
926
  minRecv: TAmount,
984
927
  router: string,
985
928
  calldata: string,
@@ -988,46 +931,42 @@ export abstract class LeverageZapV2BaseModule {
988
931
  if (!(await this.market.userPosition.userLoanExists())) throw Error("Loan does not exist");
989
932
  const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
990
933
  const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
991
- const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
992
934
  const _minRecv = parseUnits(minRecv, this.market.borrowed_token.decimals);
993
935
 
994
936
  const exchangeCalldata = _stateCollateral + _userCollateral > BigInt(0) ? calldata : "0x";
995
937
 
996
938
  return await this._repayContractCall(
997
- _userCollateral, _userBorrowed, _minRecv, router, exchangeCalldata, estimateGas
939
+ _userCollateral, _minRecv, router, exchangeCalldata, estimateGas
998
940
  );
999
941
  }
1000
942
 
1001
- public async leverageRepayEstimateGas({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
943
+ public async leverageRepayEstimateGas({ stateCollateral, userCollateral, minRecv, router, calldata }: {
1002
944
  stateCollateral: TAmount,
1003
945
  userCollateral: TAmount,
1004
- userBorrowed: TAmount,
1005
946
  minRecv: TAmount,
1006
947
  router: string,
1007
948
  calldata: string,
1008
949
  }): Promise<number> {
1009
950
  this._checkLeverageZap();
1010
- if (!(await this.leverageRepayIsApproved({ userCollateral, userBorrowed }))) throw Error("Approval is needed for gas estimation");
1011
- return await this._leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, true) as number;
951
+ if (!(await this.leverageRepayIsApproved({ userCollateral }))) throw Error("Approval is needed for gas estimation");
952
+ return await this._leverageRepay(stateCollateral, userCollateral, minRecv, router, calldata, true) as number;
1012
953
  }
1013
954
 
1014
- public async leverageRepay({ stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata }: {
955
+ public async leverageRepay({ stateCollateral, userCollateral, minRecv, router, calldata }: {
1015
956
  stateCollateral: TAmount,
1016
957
  userCollateral: TAmount,
1017
- userBorrowed: TAmount,
1018
958
  minRecv: TAmount,
1019
959
  router: string,
1020
960
  calldata: string,
1021
961
  }): Promise<string> {
1022
962
  this._checkLeverageZap();
1023
- await this.leverageRepayApprove({ userCollateral, userBorrowed });
1024
- return await this._leverageRepay(stateCollateral, userCollateral, userBorrowed, minRecv, router, calldata, false) as string;
963
+ await this.leverageRepayApprove({ userCollateral });
964
+ return await this._leverageRepay(stateCollateral, userCollateral, minRecv, router, calldata, false) as string;
1025
965
  }
1026
966
 
1027
- public async leverageRepayFutureLeverage({ stateCollateral, userCollateral, userBorrowed, address = "" }: {
967
+ public async leverageRepayFutureLeverage({ stateCollateral, userCollateral, address = "" }: {
1028
968
  stateCollateral: TAmount,
1029
969
  userCollateral: TAmount,
1030
- userBorrowed: TAmount,
1031
970
  address?: string
1032
971
  }): Promise<string> {
1033
972
  address = _getAddress.call(this.llamalend, address);
@@ -1035,10 +974,8 @@ export abstract class LeverageZapV2BaseModule {
1035
974
 
1036
975
  const { stateCollateral: currentStateCollateral, totalDepositFromUser } = await this.market.userPosition.getCurrentLeverageParams(address);
1037
976
 
1038
- const collateralFromUserBorrowed = await this.market.amm.swapExpected(0, 1, userBorrowed);
1039
-
1040
977
  const futureCollateralState = BN(currentStateCollateral).minus(stateCollateral);
1041
- const futureTotalDepositFromUserPrecise = BN(totalDepositFromUser).plus(userCollateral).plus(collateralFromUserBorrowed);
978
+ const futureTotalDepositFromUserPrecise = BN(totalDepositFromUser).plus(userCollateral);
1042
979
 
1043
980
  return futureCollateralState.div(futureTotalDepositFromUserPrecise).toString();
1044
981
  }