@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
@@ -56,6 +56,8 @@ export abstract class LoanBaseModule {
56
56
 
57
57
  public abstract removeCollateralHealth(collateral: number | string, full?: boolean, address?: string): Promise<string>;
58
58
 
59
+ public abstract tokensToShrink(dCollateral?: number | string, address?: string): Promise<string>;
60
+
59
61
  public _checkRange(range: number): void {
60
62
  if (range < this.market.minBands) throw Error(`range must be >= ${this.market.minBands}`);
61
63
  if (range > this.market.maxBands) throw Error(`range must be <= ${this.market.maxBands}`);
@@ -60,7 +60,6 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
60
60
 
61
61
  protected override async _createLoanContractCall(
62
62
  _userCollateral: bigint,
63
- _userBorrowed: bigint,
64
63
  _debt: bigint,
65
64
  _minRecv: bigint,
66
65
  range: number,
@@ -68,14 +67,14 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
68
67
  exchangeCalldata: string,
69
68
  estimateGas: boolean
70
69
  ): Promise<string | TGas> {
71
- const zapCalldata = buildCalldataForLeverageZapV2(router, exchangeCalldata);
70
+ const zapCalldata = buildCalldataForLeverageZapV2({controllerId: parseUnits(this._getMarketId(), 0), _minRecv ,router, exchangeCalldata});
72
71
  const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
73
72
  const gas = await contract.create_loan_extended.estimateGas(
74
73
  _userCollateral,
75
74
  _debt,
76
75
  range,
77
76
  this._getLeverageZapAddress(),
78
- [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
77
+ [],
79
78
  zapCalldata,
80
79
  { ...this.llamalend.constantOptions }
81
80
  );
@@ -88,7 +87,7 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
88
87
  _debt,
89
88
  range,
90
89
  this._getLeverageZapAddress(),
91
- [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
90
+ [],
92
91
  zapCalldata,
93
92
  { ...this.llamalend.options, gasLimit }
94
93
  )).hash;
@@ -96,20 +95,19 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
96
95
 
97
96
  protected override async _borrowMoreContractCall(
98
97
  _userCollateral: bigint,
99
- _userBorrowed: bigint,
100
98
  _debt: bigint,
101
99
  _minRecv: bigint,
102
100
  router: string,
103
101
  exchangeCalldata: string,
104
102
  estimateGas: boolean
105
103
  ): Promise<string | TGas> {
106
- const zapCalldata = buildCalldataForLeverageZapV2(router, exchangeCalldata);
104
+ const zapCalldata = buildCalldataForLeverageZapV2({controllerId: parseUnits(this._getMarketId(), 0), _minRecv,router, exchangeCalldata});
107
105
  const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
108
106
  const gas = await contract.borrow_more_extended.estimateGas(
109
107
  _userCollateral,
110
108
  _debt,
111
109
  this._getLeverageZapAddress(),
112
- [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
110
+ [],
113
111
  zapCalldata,
114
112
  { ...this.llamalend.constantOptions }
115
113
  );
@@ -121,7 +119,7 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
121
119
  _userCollateral,
122
120
  _debt,
123
121
  this._getLeverageZapAddress(),
124
- [0, parseUnits(this._getMarketId(), 0), _userBorrowed, _minRecv],
122
+ [],
125
123
  zapCalldata,
126
124
  { ...this.llamalend.options, gasLimit }
127
125
  )).hash;
@@ -129,17 +127,16 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
129
127
 
130
128
  protected override async _repayContractCall(
131
129
  _userCollateral: bigint,
132
- _userBorrowed: bigint,
133
130
  _minRecv: bigint,
134
131
  router: string,
135
132
  exchangeCalldata: string,
136
133
  estimateGas: boolean
137
134
  ): Promise<string | TGas> {
138
- const zapCalldata = buildCalldataForLeverageZapV2(router, exchangeCalldata);
135
+ const zapCalldata = buildCalldataForLeverageZapV2({controllerId: parseUnits(this._getMarketId(), 0), _minRecv ,router, exchangeCalldata});
139
136
  const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
140
137
  const gas = await contract.repay_extended.estimateGas(
141
138
  this._getLeverageZapAddress(),
142
- [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
139
+ [],
143
140
  zapCalldata
144
141
  );
145
142
  if (estimateGas) return smartNumber(gas);
@@ -148,7 +145,7 @@ export class LeverageV1ZapV2Module extends LeverageZapV2BaseModule {
148
145
  const gasLimit = _mulBy1_3(DIGas(gas));
149
146
  return (await contract.repay_extended(
150
147
  this._getLeverageZapAddress(),
151
- [0, parseUnits(this._getMarketId(), 0), _userCollateral, _userBorrowed, _minRecv],
148
+ [],
152
149
  zapCalldata,
153
150
  { ...this.llamalend.options, gasLimit }
154
151
  )).hash;
@@ -80,6 +80,10 @@ export class LoanV1Module extends LoanBaseModule implements ILoanV1 {
80
80
  return formatUnits(_health * BigInt(100));
81
81
  }
82
82
 
83
+ public async tokensToShrink(): Promise<string> {
84
+ throw Error("tokensToShrink is not supported for v1 markets");
85
+ }
86
+
83
87
  protected async _repayBands({ debt, address }: { debt: number | string, address: string, shrink?: boolean }): Promise<[bigint, bigint]> {
84
88
  const { _collateral: _currentCollateral, _borrowed, _debt: _currentDebt, _N } = await this.market.userPosition.userStateBigInt(address);
85
89
  if (_currentDebt === BigInt(0)) throw Error(`Loan for ${address} does not exist`);
@@ -6,7 +6,7 @@ import {
6
6
  _mulBy1_3,
7
7
  DIGas,
8
8
  MAX_ACTIVE_BAND,
9
- buildCalldataForLeverageZapV2Llv2,
9
+ buildCalldataForLeverageZapV2,
10
10
  } from "../../../utils";
11
11
 
12
12
  export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
@@ -66,7 +66,6 @@ export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
66
66
 
67
67
  protected override async _createLoanContractCall(
68
68
  _userCollateral: bigint,
69
- _userBorrowed: bigint,
70
69
  _debt: bigint,
71
70
  _minRecv: bigint,
72
71
  range: number,
@@ -77,11 +76,9 @@ export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
77
76
  const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
78
77
  const _for = _getAddress.call(this.llamalend, '');
79
78
  const _callbacker = this._getLeverageZapAddress();
80
- const zapCalldata = buildCalldataForLeverageZapV2Llv2({
81
- op: 'create_loan',
79
+ const zapCalldata = buildCalldataForLeverageZapV2({
82
80
  controllerId: this._getMarketId(),
83
- userBorrowed: _userBorrowed,
84
- minRecv: _minRecv,
81
+ _minRecv,
85
82
  router,
86
83
  exchangeCalldata,
87
84
  });
@@ -112,7 +109,6 @@ export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
112
109
 
113
110
  protected override async _borrowMoreContractCall(
114
111
  _userCollateral: bigint,
115
- _userBorrowed: bigint,
116
112
  _debt: bigint,
117
113
  _minRecv: bigint,
118
114
  router: string,
@@ -122,11 +118,9 @@ export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
122
118
  const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
123
119
  const _for = _getAddress.call(this.llamalend, '');
124
120
  const _callbacker = this._getLeverageZapAddress();
125
- const zapCalldata = buildCalldataForLeverageZapV2Llv2({
126
- op: 'borrow_more',
121
+ const zapCalldata = buildCalldataForLeverageZapV2({
127
122
  controllerId: this._getMarketId(),
128
- userBorrowed: _userBorrowed,
129
- minRecv: _minRecv,
123
+ _minRecv,
130
124
  router,
131
125
  exchangeCalldata,
132
126
  });
@@ -155,7 +149,6 @@ export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
155
149
 
156
150
  protected override async _repayContractCall(
157
151
  _userCollateral: bigint,
158
- _userBorrowed: bigint,
159
152
  _minRecv: bigint,
160
153
  router: string,
161
154
  exchangeCalldata: string,
@@ -164,12 +157,9 @@ export class LeverageV2ZapV2Module extends LeverageZapV2BaseModule {
164
157
  const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
165
158
  const _for = _getAddress.call(this.llamalend, '');
166
159
  const _callbacker = this._getLeverageZapAddress();
167
- const zapCalldata = buildCalldataForLeverageZapV2Llv2({
168
- op: 'repay',
160
+ const zapCalldata = buildCalldataForLeverageZapV2({
169
161
  controllerId: this._getMarketId(),
170
- userCollateral: _userCollateral,
171
- userBorrowed: _userBorrowed,
172
- minRecv: _minRecv,
162
+ _minRecv,
173
163
  router,
174
164
  exchangeCalldata,
175
165
  });
@@ -43,6 +43,16 @@ export class LoanV2Module extends LoanBaseModule implements ILoanV2 {
43
43
  return formatUnits(_health * BigInt(100));
44
44
  }
45
45
 
46
+ public async tokensToShrink(dCollateral: number | string = 0, address = ""): Promise<string> {
47
+ address = _getAddress.call(this.llamalend, address);
48
+ const _dCollateral = parseUnits(dCollateral, this.market.collateral_token.decimals);
49
+
50
+ const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
51
+ const _tokens = await contract.tokens_to_shrink(address, _dCollateral, this.llamalend.constantOptions) as bigint;
52
+
53
+ return formatUnits(_tokens, this.market.borrowed_token.decimals);
54
+ }
55
+
46
56
  public async maxRemovable(): Promise<string> {
47
57
  const address = _getAddress.call(this.llamalend, '');
48
58
  const { _collateral: _currentCollateral, _debt: _currentDebt, _N } = await this.market.userPosition.userStateBigInt();
package/src/utils.ts CHANGED
@@ -484,43 +484,21 @@ export const calculateFutureLeverage = (
484
484
  }
485
485
  };
486
486
 
487
- export const buildCalldataForLeverageZapV2 = (routerAddress: string, exchangeCalldata: string): string => {
488
- const cleanCalldata = exchangeCalldata.startsWith('0x') ? exchangeCalldata.slice(2) : exchangeCalldata;
489
-
490
- const abiCoder = ethers.AbiCoder.defaultAbiCoder();
491
- return abiCoder.encode(
492
- ['address', 'bytes'],
493
- [routerAddress, '0x' + cleanCalldata]
494
- );
495
- };
496
-
497
- // - create_loan / borrow_more: (uint256 controllerId, uint256 userBorrowed, uint256 minRecv, address router, bytes exchangeCalldata)
498
- // - repay: (uint256 controllerId, uint256 userCollateral, uint256 userBorrowed, uint256 minRecv, address router, bytes exchangeCalldata)
499
487
  export type LeverageZapV2LLv2CalldataParams = {
500
488
  controllerId: number | string | bigint;
501
- minRecv: bigint;
489
+ _minRecv: bigint;
502
490
  router: string;
503
491
  exchangeCalldata: string;
504
- } & (
505
- { op: 'create_loan' | 'borrow_more'; userBorrowed: bigint }
506
- | {op: 'repay'; userCollateral: bigint; userBorrowed: bigint }
507
- );
492
+ }
508
493
 
509
- export const buildCalldataForLeverageZapV2Llv2 = (params: LeverageZapV2LLv2CalldataParams): string => {
494
+ export const buildCalldataForLeverageZapV2 = (params: LeverageZapV2LLv2CalldataParams): string => {
510
495
  const cleanCalldata = params.exchangeCalldata.startsWith('0x') ? params.exchangeCalldata.slice(2) : params.exchangeCalldata;
511
496
  const exchangeBytes = '0x' + cleanCalldata;
512
497
  const abiCoder = ethers.AbiCoder.defaultAbiCoder();
513
498
 
514
- if (params.op === 'repay') {
515
- return abiCoder.encode(
516
- ['uint256', 'uint256', 'uint256', 'uint256', 'address', 'bytes'],
517
- [BigInt(params.controllerId), params.userCollateral, params.userBorrowed, params.minRecv, params.router, exchangeBytes]
518
- );
519
- }
520
-
521
499
  return abiCoder.encode(
522
- ['uint256', 'uint256', 'uint256', 'address', 'bytes'],
523
- [BigInt(params.controllerId), params.userBorrowed, params.minRecv, params.router, exchangeBytes]
500
+ ['uint256', 'uint256', 'address', 'bytes'],
501
+ [BigInt(params.controllerId), params._minRecv, params.router, exchangeBytes]
524
502
  );
525
503
  };
526
504