@curvefi/llamalend-api 2.0.8 → 2.0.10

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.
@@ -370,14 +370,49 @@ On the frontend, the value that we now return as **`totalAssets`** corresponds t
370
370
 
371
371
  | Method | v1 | v2 | Logic Unchanged | Parameters Unchanged | Return Type Unchanged |
372
372
  |--------|----|----|-----------------|----------------------|-----------------------|
373
- | repayBands() | ✅ | ✅ | | | ✅ |
374
- | repayPrices() | ✅ | ✅ | | | ✅ |
373
+ | repayBands() | ✅ | ✅ | | | ✅ |
374
+ | repayPrices() | ✅ | ✅ | | | ✅ |
375
375
  | repayIsApproved() | ✅ | ✅ | ✅ | ✅ | ✅ |
376
376
  | repayApprove() | ✅ | ✅ | ✅ | ✅ | ✅ |
377
- | repayHealth() | ✅ | ✅ | | | ✅ |
378
- | repay() | ✅ | ✅ | ❌ | ❌ | |
377
+ | repayHealth() | ✅ | ✅ | | | ✅ |
378
+ | repay() | ✅ | ✅ | ❌ | ❌ | |
379
379
  | repayFutureLeverage() | ✅ | ✅ | ✅ | ✅ | ✅ |
380
380
 
381
+ #### Shrink mechanism (v2 only)
382
+
383
+ In v2, the `repay` operation supports a new **shrink** mode that allows users to exit soft-liquidation by cutting the converted part of their position.
384
+
385
+ When a user is in soft-liquidation, some bands are converted from collateral to borrowed asset. With `shrink = true`, all borrowed assets within the position are used for debt repayment, and the position shrinks to only the unconverted bands.
386
+
387
+ **Example:**
388
+ A position with 10 bands where 3 are fully converted and 1 is partially converted (active band):
389
+
390
+ Before shrink: `| c | c | c | c | c | c | c_a + b_a | b | b | b |`
391
+ After shrink (6 bands): `| c + c_a/6 | c + c_a/6 | c + c_a/6 | c + c_a/6 | c + c_a/6 | c + c_a/6 |`
392
+
393
+ **Constraints:**
394
+ - User must have at least 4 unconverted bands to shrink
395
+ - Additional borrowed tokens may be required (use `tokens_to_shrink` on the controller contract to check)
396
+
397
+ **Updated method signatures (v2):**
398
+
399
+ ```ts
400
+ // Preview bands/prices after repay (with optional shrink)
401
+ market.loan.repayBands({ debt, address?, shrink? }) // params: { debt: TAmount, address?: string, shrink?: boolean }
402
+ market.loan.repayPrices({ debt, address?, shrink? }) // params: { debt: TAmount, address?: string, shrink?: boolean }
403
+
404
+ // Preview health after repay (with optional shrink)
405
+ market.loan.repayHealth({ debt, shrink?, full?, address? }) // params: { debt: TAmount, shrink?: boolean, full?: boolean, address?: string }
406
+
407
+ // Execute repay (with optional shrink)
408
+ market.loan.repay({ debt, address?, shrink? }) // params: { debt: TAmount, address?: string, shrink?: boolean }
409
+
410
+ // Estimate gas (with optional shrink)
411
+ market.loan.estimateGas.repay({ debt, address?, shrink? }) // params: { debt: TAmount, address?: string, shrink?: boolean }
412
+ ```
413
+
414
+ **Note:** When `shrink = false` (default), v2 repay behaves identically to v1. The `shrink` parameter is only relevant for v2 markets and has no effect on v1.
415
+
381
416
  ### Repay (Full)
382
417
 
383
418
  | Method | v1 | v2 | Logic Unchanged | Parameters Unchanged | Return Type Unchanged |
@@ -839,31 +839,6 @@
839
839
  }
840
840
  ]
841
841
  },
842
- {
843
- "stateMutability": "view",
844
- "type": "function",
845
- "name": "calculate_debt_n1",
846
- "inputs": [
847
- {
848
- "name": "_collateral",
849
- "type": "uint256"
850
- },
851
- {
852
- "name": "_debt",
853
- "type": "uint256"
854
- },
855
- {
856
- "name": "_N",
857
- "type": "uint256"
858
- }
859
- ],
860
- "outputs": [
861
- {
862
- "name": "",
863
- "type": "int256"
864
- }
865
- ]
866
- },
867
842
  {
868
843
  "stateMutability": "view",
869
844
  "type": "function",
@@ -38,12 +38,26 @@ export interface ILoanV1 {
38
38
  removeCollateralHealth: (collateral: TAmount, full?: boolean, address?: string) => Promise<string>;
39
39
  removeCollateral: (collateral: TAmount) => Promise<string>;
40
40
  removeCollateralFutureLeverage: (collateral: TAmount, userAddress?: string) => Promise<string>;
41
- repayBands: (debt: TAmount, address?: string) => Promise<[number, number]>;
42
- repayPrices: (debt: TAmount, address?: string) => Promise<string[]>;
41
+ repayBands: (params: {
42
+ debt: TAmount;
43
+ address?: string;
44
+ }) => Promise<[number, number]>;
45
+ repayPrices: (params: {
46
+ debt: TAmount;
47
+ address?: string;
48
+ }) => Promise<string[]>;
43
49
  repayIsApproved: (debt: TAmount) => Promise<boolean>;
44
50
  repayApprove: (debt: TAmount) => Promise<string[]>;
45
- repayHealth: (debt: TAmount, full?: boolean, address?: string) => Promise<string>;
46
- repay: (debt: TAmount, address?: string) => Promise<string>;
51
+ repayHealth: (params: {
52
+ debt: TAmount;
53
+ full?: boolean;
54
+ address?: string;
55
+ }) => Promise<string>;
56
+ repay: (params: {
57
+ debt: TAmount;
58
+ address?: string;
59
+ shrink?: boolean;
60
+ }) => Promise<string>;
47
61
  repayFutureLeverage: (debt: TAmount, userAddress?: string) => Promise<string>;
48
62
  fullRepayIsApproved: (address?: string) => Promise<boolean>;
49
63
  fullRepayApprove: (address?: string) => Promise<string[]>;
@@ -68,7 +82,11 @@ export interface ILoanV1 {
68
82
  addCollateral: (collateral: TAmount, address?: string) => Promise<TGas>;
69
83
  removeCollateral: (collateral: TAmount) => Promise<TGas>;
70
84
  repayApprove: (debt: TAmount) => Promise<TGas>;
71
- repay: (debt: TAmount, address?: string) => Promise<TGas>;
85
+ repay: (params: {
86
+ debt: TAmount;
87
+ address?: string;
88
+ shrink?: boolean;
89
+ }) => Promise<TGas>;
72
90
  fullRepayApprove: (address?: string) => Promise<TGas>;
73
91
  fullRepay: (address?: string) => Promise<TGas>;
74
92
  liquidateApprove: (address?: string) => Promise<TGas>;
@@ -38,12 +38,29 @@ export interface ILoanV2 {
38
38
  removeCollateralHealth: (collateral: TAmount, full?: boolean, address?: string) => Promise<string>;
39
39
  removeCollateral: (collateral: TAmount) => Promise<string>;
40
40
  removeCollateralFutureLeverage: (collateral: TAmount, userAddress?: string) => Promise<string>;
41
- repayBands: (debt: TAmount, address?: string) => Promise<[number, number]>;
42
- repayPrices: (debt: TAmount, address?: string) => Promise<string[]>;
41
+ repayBands: (params: {
42
+ debt: TAmount;
43
+ address?: string;
44
+ shrink?: boolean;
45
+ }) => Promise<[number, number]>;
46
+ repayPrices: (params: {
47
+ debt: TAmount;
48
+ address?: string;
49
+ shrink?: boolean;
50
+ }) => Promise<string[]>;
43
51
  repayIsApproved: (debt: TAmount) => Promise<boolean>;
44
52
  repayApprove: (debt: TAmount) => Promise<string[]>;
45
- repayHealth: (debt: TAmount, shrink?: boolean, full?: boolean, address?: string) => Promise<string>;
46
- repay: (debt: TAmount, address?: string) => Promise<string>;
53
+ repayHealth: (params: {
54
+ debt: TAmount;
55
+ shrink?: boolean;
56
+ full?: boolean;
57
+ address?: string;
58
+ }) => Promise<string>;
59
+ repay: (params: {
60
+ debt: TAmount;
61
+ address?: string;
62
+ shrink?: boolean;
63
+ }) => Promise<string>;
47
64
  repayFutureLeverage: (debt: TAmount, userAddress?: string) => Promise<string>;
48
65
  fullRepayIsApproved: (address?: string) => Promise<boolean>;
49
66
  fullRepayApprove: (address?: string) => Promise<string[]>;
@@ -68,7 +85,11 @@ export interface ILoanV2 {
68
85
  addCollateral: (collateral: TAmount, address?: string) => Promise<TGas>;
69
86
  removeCollateral: (collateral: TAmount) => Promise<TGas>;
70
87
  repayApprove: (debt: TAmount) => Promise<TGas>;
71
- repay: (debt: TAmount, address?: string) => Promise<TGas>;
88
+ repay: (params: {
89
+ debt: TAmount;
90
+ address?: string;
91
+ shrink?: boolean;
92
+ }) => Promise<TGas>;
72
93
  fullRepayApprove: (address?: string) => Promise<TGas>;
73
94
  fullRepay: (address?: string) => Promise<TGas>;
74
95
  liquidateApprove: (address?: string) => Promise<TGas>;
@@ -47,7 +47,7 @@ export class LeverageZapV2BaseModule {
47
47
  const j = N - this.market.minBands;
48
48
  calls.push(contract.max_borrowable(this.market.addresses.controller, _userEffectiveCollateral, _maxLeverageCollateral[j], N, fromBN(pBN)));
49
49
  }
50
- _maxBorrowable = (yield this.llamalend.multicallProvider.all(calls)).map((_mb) => _mb * BigInt(998) / BigInt(1000));
50
+ _maxBorrowable = (yield this.llamalend.multicallProvider.all(calls)).map((_mb) => _mb * BigInt(970) / BigInt(1000));
51
51
  maxBorrowableBN = _maxBorrowable.map((_mb) => toBN(_mb, this.market.borrowed_token.decimals));
52
52
  const deltaBN = maxBorrowableBN.map((mb, l) => mb.minus(maxBorrowablePrevBN[l]).abs().div(mb));
53
53
  if (BigNumber.max(...deltaBN).lt(0.0005)) {
@@ -239,7 +239,7 @@ export class LeverageZapV2BaseModule {
239
239
  maxBorrowablePrevBN = maxBorrowableBN;
240
240
  _userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.market.collateral_token.decimals);
241
241
  let _maxBorrowable = yield contract.max_borrowable(this.market.addresses.controller, _userEffectiveCollateral, _maxLeverageCollateral, range, fromBN(pAvgBN));
242
- _maxBorrowable = _maxBorrowable * BigInt(998) / BigInt(1000);
242
+ _maxBorrowable = _maxBorrowable * BigInt(970) / BigInt(1000);
243
243
  if (_maxBorrowable === BigInt(0))
244
244
  break;
245
245
  maxBorrowableBN = toBN(_maxBorrowable, this.market.borrowed_token.decimals);
@@ -468,7 +468,7 @@ export class LeverageZapV2BaseModule {
468
468
  maxBorrowablePrevBN = maxBorrowableBN;
469
469
  _userEffectiveCollateral = _userCollateral + fromBN(BN(userBorrowed).div(pAvgBN), this.market.collateral_token.decimals);
470
470
  let _maxBorrowable = yield contract.max_borrowable(this.market.addresses.controller, _userEffectiveCollateral, _maxLeverageCollateral, _N, fromBN(pAvgBN));
471
- _maxBorrowable = _maxBorrowable * BigInt(998) / BigInt(1000);
471
+ _maxBorrowable = _maxBorrowable * BigInt(970) / BigInt(1000);
472
472
  if (_maxBorrowable === BigInt(0))
473
473
  break;
474
474
  maxBorrowableBN = toBN(_maxBorrowable, this.market.borrowed_token.decimals);
@@ -485,7 +485,7 @@ export class LeverageZapV2BaseModule {
485
485
  _userEffectiveCollateral = BigInt(0);
486
486
  const _maxTotalCollateral = _userEffectiveCollateral + _maxLeverageCollateral;
487
487
  let _maxBorrowable = (yield controllerContract.max_borrowable(_stateCollateral + _maxTotalCollateral, _N, _stateDebt, this.llamalend.constantOptions)) - _stateDebt;
488
- _maxBorrowable = _maxBorrowable * BigInt(998) / BigInt(1000);
488
+ _maxBorrowable = _maxBorrowable * BigInt(970) / BigInt(1000);
489
489
  return {
490
490
  maxDebt: formatUnits(_maxBorrowable, this.market.borrowed_token.decimals),
491
491
  maxTotalCollateral: formatUnits(_maxTotalCollateral, this.market.collateral_token.decimals),
@@ -2,20 +2,51 @@ import memoize from "memoizee";
2
2
  import { TAmount, TGas, IPartialFrac } from "../../../interfaces";
3
3
  import type { LendMarketTemplate } from "../../LendMarketTemplate";
4
4
  import { Llamalend } from "../../../llamalend";
5
- export declare class LoanBaseModule {
5
+ export declare abstract class LoanBaseModule {
6
6
  protected market: LendMarketTemplate;
7
7
  protected llamalend: Llamalend;
8
8
  constructor(market: LendMarketTemplate);
9
+ protected abstract _calcN1(_collateral: bigint, _debt: bigint, range: number): Promise<bigint>;
10
+ protected abstract _calcN1AllRanges(_collateral: bigint, _debt: bigint, maxN: number): Promise<bigint[]>;
11
+ protected abstract _maxBorrowable(collateralAmount: TAmount, range?: number): Promise<bigint>;
12
+ protected abstract _getMaxBorrowableCall(_collateral: bigint, N: number): any;
13
+ protected abstract _createLoanContractCall(_collateral: bigint, _debt: bigint, range: number, estimateGas: boolean): Promise<string | TGas>;
14
+ protected abstract _borrowMoreContractCall(_collateral: bigint, _debt: bigint, estimateGas: boolean): Promise<string | TGas>;
15
+ protected abstract _repayContractCall(params: {
16
+ _debt: bigint;
17
+ address: string;
18
+ n: number | bigint;
19
+ estimateGas: boolean;
20
+ shrink?: boolean;
21
+ }): Promise<string | TGas>;
22
+ protected abstract _partialLiquidateContractCall(address: string, _minAmount: bigint, frac: string, estimateGas: boolean): Promise<string | TGas>;
23
+ protected abstract _repayBands(params: {
24
+ debt: number | string;
25
+ address: string;
26
+ shrink?: boolean;
27
+ }): Promise<[bigint, bigint]>;
28
+ abstract repayBands(params: {
29
+ debt: number | string;
30
+ address?: string;
31
+ shrink?: boolean;
32
+ }): Promise<[number, number]>;
33
+ abstract repayPrices(params: {
34
+ debt: number | string;
35
+ address?: string;
36
+ shrink?: boolean;
37
+ }): Promise<string[]>;
38
+ abstract createLoanHealth(collateral: number | string, debt: number | string, range: number, full?: boolean): Promise<string>;
39
+ abstract borrowMoreHealth(collateral: number | string, debt: number | string, full?: boolean, address?: string): Promise<string>;
40
+ abstract addCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
41
+ abstract removeCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
9
42
  _checkRange(range: number): void;
10
- createLoanMaxRecv(collateral: number | string, range: number): Promise<string>;
43
+ createLoanMaxRecv(collateral: TAmount, range: number): Promise<string>;
11
44
  createLoanMaxRecvAllRanges: ((collateral: number | string) => Promise<{
12
45
  [index: number]: string;
13
46
  }>) & memoize.Memoized<(collateral: number | string) => Promise<{
14
47
  [index: number]: string;
15
48
  }>>;
16
49
  getMaxRange(collateral: number | string, debt: number | string): Promise<number>;
17
- private _calcN1;
18
- private _calcN1AllRanges;
19
50
  private _createLoanBands;
20
51
  private _createLoanBandsAllRanges;
21
52
  createLoanBands(collateral: number | string, debt: number | string, range: number): Promise<[number, number]>;
@@ -26,7 +57,6 @@ export declare class LoanBaseModule {
26
57
  createLoanPricesAllRanges(collateral: number | string, debt: number | string): Promise<{
27
58
  [index: number]: [string, string] | null;
28
59
  }>;
29
- createLoanHealth(collateral: number | string, debt: number | string, range: number, full?: boolean): Promise<string>;
30
60
  createLoanIsApproved(collateral: number | string): Promise<boolean>;
31
61
  private createLoanApproveEstimateGas;
32
62
  createLoanApprove(collateral: number | string): Promise<string[]>;
@@ -37,7 +67,6 @@ export declare class LoanBaseModule {
37
67
  private _borrowMoreBands;
38
68
  borrowMoreBands(collateral: number | string, debt: number | string): Promise<[number, number]>;
39
69
  borrowMorePrices(collateral: number | string, debt: number | string): Promise<string[]>;
40
- borrowMoreHealth(collateral: number | string, debt: number | string, full?: boolean, address?: string): Promise<string>;
41
70
  borrowMoreIsApproved(collateral: number | string): Promise<boolean>;
42
71
  private borrowMoreApproveEstimateGas;
43
72
  borrowMoreApprove(collateral: number | string): Promise<string[]>;
@@ -48,7 +77,6 @@ export declare class LoanBaseModule {
48
77
  private _addCollateralBands;
49
78
  addCollateralBands(collateral: number | string, address?: string): Promise<[number, number]>;
50
79
  addCollateralPrices(collateral: number | string, address?: string): Promise<string[]>;
51
- addCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
52
80
  addCollateralIsApproved(collateral: number | string): Promise<boolean>;
53
81
  private addCollateralApproveEstimateGas;
54
82
  addCollateralApprove(collateral: number | string): Promise<string[]>;
@@ -60,20 +88,24 @@ export declare class LoanBaseModule {
60
88
  private _removeCollateralBands;
61
89
  removeCollateralBands(collateral: number | string): Promise<[number, number]>;
62
90
  removeCollateralPrices(collateral: number | string): Promise<string[]>;
63
- removeCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
64
91
  private _removeCollateral;
65
92
  removeCollateralEstimateGas(collateral: number | string): Promise<TGas>;
66
93
  removeCollateral(collateral: number | string): Promise<string>;
67
94
  removeCollateralFutureLeverage(collateral: number | string, userAddress?: string): Promise<string>;
68
- private _repayBands;
69
- repayBands(debt: number | string, address?: string): Promise<[number, number]>;
70
- repayPrices(debt: number | string, address?: string): Promise<string[]>;
71
95
  repayIsApproved(debt: number | string): Promise<boolean>;
72
96
  private repayApproveEstimateGas;
73
97
  repayApprove(debt: number | string): Promise<string[]>;
74
98
  private _repay;
75
- repayEstimateGas(debt: number | string, address?: string): Promise<TGas>;
76
- repay(debt: number | string, address?: string): Promise<string>;
99
+ repayEstimateGas({ debt, address, shrink }: {
100
+ debt: number | string;
101
+ address?: string;
102
+ shrink?: boolean;
103
+ }): Promise<TGas>;
104
+ repay({ debt, address, shrink }: {
105
+ debt: number | string;
106
+ address?: string;
107
+ shrink?: boolean;
108
+ }): Promise<string>;
77
109
  repayFutureLeverage(debt: number | string, userAddress?: string): Promise<string>;
78
110
  private _fullRepayAmount;
79
111
  fullRepayIsApproved(address?: string): Promise<boolean>;
@@ -109,7 +141,11 @@ export declare class LoanBaseModule {
109
141
  addCollateral: (collateral: number | string, address?: string) => Promise<TGas>;
110
142
  removeCollateral: (collateral: number | string) => Promise<TGas>;
111
143
  repayApprove: (debt: number | string) => Promise<TGas>;
112
- repay: (debt: number | string, address?: string) => Promise<TGas>;
144
+ repay: ({ debt, address, shrink }: {
145
+ debt: number | string;
146
+ address?: string;
147
+ shrink?: boolean;
148
+ }) => Promise<TGas>;
113
149
  fullRepayApprove: (address?: string) => Promise<TGas>;
114
150
  fullRepay: (address?: string) => Promise<TGas>;
115
151
  liquidateApprove: (address?: string) => Promise<TGas>;
@@ -16,8 +16,7 @@ export class LoanBaseModule {
16
16
  const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
17
17
  const calls = [];
18
18
  for (let N = this.market.minBands; N <= this.market.maxBands; N++) {
19
- // TODO: currentDebt is no longer passed - now we pass user address or zero address, currentDebt is calculated under the hood
20
- calls.push(this.llamalend.contracts[this.market.addresses.controller].multicallContract.max_borrowable(_collateral, N, 0));
19
+ calls.push(this._getMaxBorrowableCall(_collateral, N));
21
20
  }
22
21
  const _amounts = yield this.llamalend.multicallProvider.all(calls);
23
22
  const res = {};
@@ -60,10 +59,7 @@ export class LoanBaseModule {
60
59
  createLoanMaxRecv(collateral, range) {
61
60
  return __awaiter(this, void 0, void 0, function* () {
62
61
  this._checkRange(range);
63
- const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
64
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
65
- // TODO: currentDebt is no longer passed - now we pass user address or zero address, currentDebt is calculated under the hood
66
- return formatUnits(yield contract.max_borrowable(_collateral, range, 0, this.llamalend.constantOptions), this.market.borrowed_token.decimals);
62
+ return formatUnits(yield this._maxBorrowable(collateral, range), this.market.borrowed_token.decimals);
67
63
  });
68
64
  }
69
65
  getMaxRange(collateral, debt) {
@@ -76,23 +72,6 @@ export class LoanBaseModule {
76
72
  return this.market.maxBands;
77
73
  });
78
74
  }
79
- _calcN1(_collateral, _debt, range) {
80
- return __awaiter(this, void 0, void 0, function* () {
81
- this._checkRange(range);
82
- // TODO: add 4th parameter - user address in calculate_debt_n1
83
- return yield this.llamalend.contracts[this.market.addresses.controller].contract.calculate_debt_n1(_collateral, _debt, range, this.llamalend.constantOptions);
84
- });
85
- }
86
- _calcN1AllRanges(_collateral, _debt, maxN) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- const calls = [];
89
- // TODO: add 4th parameter - user address in calculate_debt_n1
90
- for (let N = this.market.minBands; N <= maxN; N++) {
91
- calls.push(this.llamalend.contracts[this.market.addresses.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
92
- }
93
- return yield this.llamalend.multicallProvider.all(calls);
94
- });
95
- }
96
75
  _createLoanBands(collateral, debt, range) {
97
76
  return __awaiter(this, void 0, void 0, function* () {
98
77
  const _n1 = yield this._calcN1(parseUnits(collateral, this.market.collateral_token.decimals), parseUnits(debt, this.market.borrowed_token.decimals), range);
@@ -157,15 +136,6 @@ export class LoanBaseModule {
157
136
  return pricesAllRanges;
158
137
  });
159
138
  }
160
- createLoanHealth(collateral_1, debt_1, range_1) {
161
- return __awaiter(this, arguments, void 0, function* (collateral, debt, range, full = true) {
162
- const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
163
- const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
164
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
165
- const _health = yield contract.health_calculator(this.llamalend.constants.ZERO_ADDRESS, _collateral, _debt, full, range, this.llamalend.constantOptions);
166
- return formatUnits(_health * BigInt(100));
167
- });
168
- }
169
139
  createLoanIsApproved(collateral) {
170
140
  return __awaiter(this, void 0, void 0, function* () {
171
141
  return yield hasAllowance.call(this.llamalend, [this.market.collateral_token.address], [collateral], this.llamalend.signerAddress, this.market.addresses.controller);
@@ -188,14 +158,7 @@ export class LoanBaseModule {
188
158
  this._checkRange(range);
189
159
  const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
190
160
  const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
191
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
192
- const gas = yield contract.create_loan.estimateGas(_collateral, _debt, range, Object.assign({}, this.llamalend.constantOptions));
193
- if (estimateGas)
194
- return smartNumber(gas);
195
- yield this.llamalend.updateFeeData();
196
- const gasLimit = _mulBy1_3(DIGas(gas));
197
- // TODO: ABI has changed (see docs)
198
- return (yield contract.create_loan(_collateral, _debt, range, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
161
+ return this._createLoanContractCall(_collateral, _debt, range, estimateGas);
199
162
  });
200
163
  }
201
164
  createLoanEstimateGas(collateral, debt, range) {
@@ -214,12 +177,7 @@ export class LoanBaseModule {
214
177
  // ---------------- BORROW MORE ----------------
215
178
  borrowMoreMaxRecv(collateralAmount) {
216
179
  return __awaiter(this, void 0, void 0, function* () {
217
- const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = yield this.market.userPosition.userStateBigInt();
218
- const _collateral = _currentCollateral + parseUnits(collateralAmount, this.market.collateral_token.decimals);
219
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
220
- const _debt = yield contract.max_borrowable(_collateral, _N, _currentDebt, this.llamalend.constantOptions);
221
- // TODO: max_borrowable(deltaCollateral, _N, user) - debt is now calculated under the hood, return as is
222
- return formatUnits(_debt - _currentDebt, this.market.borrowed_token.decimals);
180
+ return formatUnits(yield this._maxBorrowable(collateralAmount), this.market.borrowed_token.decimals);
223
181
  });
224
182
  }
225
183
  _borrowMoreBands(collateral, debt) {
@@ -246,16 +204,6 @@ export class LoanBaseModule {
246
204
  return yield this.market.prices.getPrices(_n2, _n1);
247
205
  });
248
206
  }
249
- borrowMoreHealth(collateral_1, debt_1) {
250
- return __awaiter(this, arguments, void 0, function* (collateral, debt, full = true, address = "") {
251
- address = _getAddress.call(this.llamalend, address);
252
- const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
253
- const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
254
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
255
- const _health = yield contract.health_calculator(address, _collateral, _debt, full, 0, this.llamalend.constantOptions);
256
- return formatUnits(_health * BigInt(100));
257
- });
258
- }
259
207
  borrowMoreIsApproved(collateral) {
260
208
  return __awaiter(this, void 0, void 0, function* () {
261
209
  return yield hasAllowance.call(this.llamalend, [this.market.addresses.collateral_token], [collateral], this.llamalend.signerAddress, this.market.addresses.controller);
@@ -280,13 +228,7 @@ export class LoanBaseModule {
280
228
  throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
281
229
  const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
282
230
  const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
283
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
284
- const gas = yield contract.borrow_more.estimateGas(_collateral, _debt, Object.assign({}, this.llamalend.constantOptions));
285
- if (estimateGas)
286
- return smartNumber(gas);
287
- yield this.llamalend.updateFeeData();
288
- const gasLimit = _mulBy1_3(DIGas(gas));
289
- return (yield contract.borrow_more(_collateral, _debt, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
231
+ return this._borrowMoreContractCall(_collateral, _debt, estimateGas);
290
232
  });
291
233
  }
292
234
  borrowMoreEstimateGas(collateral, debt) {
@@ -337,15 +279,6 @@ export class LoanBaseModule {
337
279
  return yield this.market.prices.getPrices(_n2, _n1);
338
280
  });
339
281
  }
340
- addCollateralHealth(collateral_1) {
341
- return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
342
- address = _getAddress.call(this.llamalend, address);
343
- const _collateral = parseUnits(collateral, this.market.collateral_token.decimals);
344
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
345
- const _health = yield contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions);
346
- return formatUnits(_health * BigInt(100));
347
- });
348
- }
349
282
  addCollateralIsApproved(collateral) {
350
283
  return __awaiter(this, void 0, void 0, function* () {
351
284
  return yield hasAllowance.call(this.llamalend, [this.market.addresses.collateral_token], [collateral], this.llamalend.signerAddress, this.market.addresses.controller);
@@ -435,15 +368,6 @@ export class LoanBaseModule {
435
368
  return yield this.market.prices.getPrices(_n2, _n1);
436
369
  });
437
370
  }
438
- removeCollateralHealth(collateral_1) {
439
- return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
440
- address = _getAddress.call(this.llamalend, address);
441
- const _collateral = parseUnits(collateral, this.market.collateral_token.decimals) * BigInt(-1);
442
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
443
- const _health = yield contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions);
444
- return formatUnits(_health * BigInt(100));
445
- });
446
- }
447
371
  _removeCollateral(collateral, estimateGas) {
448
372
  return __awaiter(this, void 0, void 0, function* () {
449
373
  const { borrowed, debt: currentDebt } = yield this.market.userPosition.userState();
@@ -483,35 +407,6 @@ export class LoanBaseModule {
483
407
  });
484
408
  }
485
409
  // ---------------- REPAY ----------------
486
- // TODO: add shrink: bool parameter
487
- // If bool is true, _N = n2 - n1 + 1 -> _N = n2 - activeBand
488
- _repayBands(debt, address) {
489
- return __awaiter(this, void 0, void 0, function* () {
490
- const { _collateral: _currentCollateral, _borrowed, _debt: _currentDebt, _N } = yield this.market.userPosition.userStateBigInt(address);
491
- if (_currentDebt === BigInt(0))
492
- throw Error(`Loan for ${address} does not exist`);
493
- // && shrink === false
494
- if (_borrowed > BigInt(0))
495
- return yield this.market.userPosition.userBandsBigInt(address);
496
- // If shrink == true, then _debt = _currentDebt - parseUnits(debt, this.market.borrowed_token.decimals) - _borrowed
497
- const _debt = _currentDebt - parseUnits(debt, this.market.borrowed_token.decimals);
498
- const _n1 = yield this._calcN1(_currentCollateral, _debt, Number(_N));
499
- const _n2 = _n1 + _N - BigInt(1);
500
- return [_n2, _n1];
501
- });
502
- }
503
- repayBands(debt_1) {
504
- return __awaiter(this, arguments, void 0, function* (debt, address = "") {
505
- const [_n2, _n1] = yield this._repayBands(debt, address);
506
- return [Number(_n2), Number(_n1)];
507
- });
508
- }
509
- repayPrices(debt_1) {
510
- return __awaiter(this, arguments, void 0, function* (debt, address = "") {
511
- const [_n2, _n1] = yield this._repayBands(debt, address);
512
- return yield this.market.prices.getPrices(_n2, _n1);
513
- });
514
- }
515
410
  repayIsApproved(debt) {
516
411
  return __awaiter(this, void 0, void 0, function* () {
517
412
  return yield hasAllowance.call(this.llamalend, [this.market.borrowed_token.address], [debt], this.llamalend.signerAddress, this.market.addresses.controller);
@@ -527,37 +422,30 @@ export class LoanBaseModule {
527
422
  return yield ensureAllowance.call(this.llamalend, [this.market.borrowed_token.address], [debt], this.market.addresses.controller);
528
423
  });
529
424
  }
530
- _repay(debt, address, estimateGas) {
531
- return __awaiter(this, void 0, void 0, function* () {
425
+ _repay(debt_1, address_1, estimateGas_1) {
426
+ return __awaiter(this, arguments, void 0, function* (debt, address, estimateGas, shrink = false) {
532
427
  address = _getAddress.call(this.llamalend, address);
533
428
  const { debt: currentDebt } = yield this.market.userPosition.userState(address);
534
429
  if (Number(currentDebt) === 0)
535
430
  throw Error(`Loan for ${address} does not exist`);
536
431
  const _debt = parseUnits(debt);
537
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
538
432
  const [, n1] = yield this.market.userPosition.userBands(address);
539
433
  const { borrowed } = yield this.market.userPosition.userState(address);
540
- const n = (BN(borrowed).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1; // In liquidation mode it doesn't matter if active band moves
541
- const gas = yield contract.repay.estimateGas(_debt, address, n, this.llamalend.constantOptions);
542
- if (estimateGas)
543
- return smartNumber(gas);
544
- yield this.llamalend.updateFeeData();
545
- const gasLimit = _mulBy1_3(DIGas(gas));
546
- // TODO: shrink parameter is added, which changes bands calculation
547
- return (yield contract.repay(_debt, address, n, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
434
+ const n = (BN(borrowed).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1;
435
+ return this._repayContractCall({ _debt, address, n, estimateGas, shrink });
548
436
  });
549
437
  }
550
- repayEstimateGas(debt_1) {
551
- return __awaiter(this, arguments, void 0, function* (debt, address = "") {
438
+ repayEstimateGas(_a) {
439
+ return __awaiter(this, arguments, void 0, function* ({ debt, address = "", shrink = false }) {
552
440
  if (!(yield this.repayIsApproved(debt)))
553
441
  throw Error("Approval is needed for gas estimation");
554
- return yield this._repay(debt, address, true);
442
+ return yield this._repay(debt, address, true, shrink);
555
443
  });
556
444
  }
557
- repay(debt_1) {
558
- return __awaiter(this, arguments, void 0, function* (debt, address = "") {
445
+ repay(_a) {
446
+ return __awaiter(this, arguments, void 0, function* ({ debt, address = "", shrink = false }) {
559
447
  yield this.repayApprove(debt);
560
- return yield this._repay(debt, address, false);
448
+ return yield this._repay(debt, address, false, shrink);
561
449
  });
562
450
  }
563
451
  repayFutureLeverage(debt_1) {
@@ -703,13 +591,7 @@ export class LoanBaseModule {
703
591
  const expectedBorrowedBN = borrowedBN.times(fracBN);
704
592
  const minAmountBN = expectedBorrowedBN.times(100 - slippage).div(100);
705
593
  const _minAmount = fromBN(minAmountBN);
706
- const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
707
- const gas = (yield contract.liquidate_extended.estimateGas(address, _minAmount, frac, this.llamalend.constants.ZERO_ADDRESS, [], this.llamalend.constantOptions));
708
- if (estimateGas)
709
- return smartNumber(gas);
710
- yield this.llamalend.updateFeeData();
711
- const gasLimit = _mulBy1_3(DIGas(gas));
712
- return (yield contract.liquidate_extended(address, _minAmount, frac, this.llamalend.constants.ZERO_ADDRESS, [], Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
594
+ return this._partialLiquidateContractCall(address, _minAmount, frac, estimateGas);
713
595
  });
714
596
  }
715
597
  liquidateEstimateGas(address_1) {
@@ -1,5 +1,41 @@
1
1
  import { LoanBaseModule } from "../common/loanBase.js";
2
2
  import { ILoanV1 } from "../../interfaces/v1/loanV1";
3
+ import { TGas, TAmount } from "../../../interfaces";
3
4
  export declare class LoanV1Module extends LoanBaseModule implements ILoanV1 {
4
- repayHealth(debt: number | string, full?: boolean, address?: string): Promise<string>;
5
+ protected _maxBorrowable: (collateralAmount: TAmount, range?: number) => Promise<bigint>;
6
+ protected _getMaxBorrowableCall(_collateral: bigint, N: number): any;
7
+ protected _calcN1(_collateral: bigint, _debt: bigint, range: number): Promise<bigint>;
8
+ protected _calcN1AllRanges(_collateral: bigint, _debt: bigint, maxN: number): Promise<bigint[]>;
9
+ createLoanHealth(collateral: number | string, debt: number | string, range: number, full?: boolean): Promise<string>;
10
+ borrowMoreHealth(collateral: number | string, debt: number | string, full?: boolean, address?: string): Promise<string>;
11
+ addCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
12
+ removeCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
13
+ protected _repayBands({ debt, address }: {
14
+ debt: number | string;
15
+ address: string;
16
+ shrink?: boolean;
17
+ }): Promise<[bigint, bigint]>;
18
+ repayBands({ debt, address }: {
19
+ debt: number | string;
20
+ address?: string;
21
+ }): Promise<[number, number]>;
22
+ repayPrices({ debt, address }: {
23
+ debt: number | string;
24
+ address?: string;
25
+ }): Promise<string[]>;
26
+ protected _repayContractCall({ _debt, address, n, estimateGas }: {
27
+ _debt: bigint;
28
+ address: string;
29
+ n: number | bigint;
30
+ estimateGas: boolean;
31
+ shrink?: boolean;
32
+ }): Promise<string | TGas>;
33
+ protected _createLoanContractCall(_collateral: bigint, _debt: bigint, range: number, estimateGas: boolean): Promise<string | TGas>;
34
+ protected _borrowMoreContractCall(_collateral: bigint, _debt: bigint, estimateGas: boolean): Promise<string | TGas>;
35
+ protected _partialLiquidateContractCall(address: string, _minAmount: bigint, frac: string, estimateGas: boolean): Promise<string | TGas>;
36
+ repayHealth({ debt, full, address }: {
37
+ debt: number | string;
38
+ full?: boolean;
39
+ address?: string;
40
+ }): Promise<string>;
5
41
  }