@curvefi/llamalend-api 1.1.9 → 2.0.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.
- package/docs/MIGRATION.md +325 -0
- package/docs/SUPPORT_LLv2.md +409 -0
- package/lib/constants/abis/OneWayLendingFactoryV2ABI.json +543 -0
- package/lib/constants/aliases.d.ts +0 -11
- package/lib/constants/aliases.js +1 -86
- package/lib/constants/coins.d.ts +0 -11
- package/lib/constants/coins.js +0 -14
- package/lib/constants/utils.d.ts +2 -0
- package/lib/constants/utils.js +2 -0
- package/lib/index.d.ts +10 -4
- package/lib/interfaces.d.ts +3 -2
- package/lib/lendMarkets/LendMarketTemplate.d.ts +13 -501
- package/lib/lendMarkets/LendMarketTemplate.js +237 -2854
- package/lib/lendMarkets/fetch/fetchFactoryData.d.ts +13 -0
- package/lib/lendMarkets/fetch/fetchFactoryData.js +101 -0
- package/lib/lendMarkets/fetch/fetchLendMarkets.d.ts +3 -0
- package/lib/lendMarkets/fetch/fetchLendMarkets.js +94 -0
- package/lib/lendMarkets/interfaces/common/amm.d.ts +10 -0
- package/lib/lendMarkets/interfaces/common/amm.js +1 -0
- package/lib/lendMarkets/interfaces/common/index.d.ts +5 -0
- package/lib/lendMarkets/interfaces/common/index.js +5 -0
- package/lib/lendMarkets/interfaces/common/prices.d.ts +13 -0
- package/lib/lendMarkets/interfaces/common/prices.js +1 -0
- package/lib/lendMarkets/interfaces/common/userPosition.d.ts +39 -0
- package/lib/lendMarkets/interfaces/common/userPosition.js +1 -0
- package/lib/lendMarkets/interfaces/common/vault.d.ts +55 -0
- package/lib/lendMarkets/interfaces/common/vault.js +1 -0
- package/lib/lendMarkets/interfaces/common/wallet.d.ts +8 -0
- package/lib/lendMarkets/interfaces/common/wallet.js +1 -0
- package/lib/lendMarkets/interfaces/v1/index.d.ts +4 -0
- package/lib/lendMarkets/interfaces/v1/index.js +4 -0
- package/lib/lendMarkets/interfaces/v1/leverageV1.d.ts +90 -0
- package/lib/lendMarkets/interfaces/v1/leverageV1.js +1 -0
- package/lib/lendMarkets/interfaces/v1/loanV1.d.ts +73 -0
- package/lib/lendMarkets/interfaces/v1/loanV1.js +1 -0
- package/lib/lendMarkets/interfaces/v1/statsV1.d.ts +49 -0
- package/lib/lendMarkets/interfaces/v1/statsV1.js +1 -0
- package/lib/lendMarkets/interfaces/v2/index.d.ts +3 -0
- package/lib/lendMarkets/interfaces/v2/index.js +3 -0
- package/lib/lendMarkets/interfaces/v2/leverageV2.d.ts +90 -0
- package/lib/lendMarkets/interfaces/v2/leverageV2.js +1 -0
- package/lib/lendMarkets/interfaces/v2/loanV2.d.ts +73 -0
- package/lib/lendMarkets/interfaces/v2/loanV2.js +1 -0
- package/lib/lendMarkets/interfaces/v2/statsV2.d.ts +49 -0
- package/lib/lendMarkets/interfaces/v2/statsV2.js +1 -0
- package/lib/lendMarkets/lendMarketConstructor.js +1 -1
- package/lib/lendMarkets/modules/common/amm.d.ts +19 -0
- package/lib/lendMarkets/modules/common/amm.js +137 -0
- package/lib/lendMarkets/modules/common/index.d.ts +9 -0
- package/lib/lendMarkets/modules/common/index.js +9 -0
- package/lib/lendMarkets/modules/common/leverageZapV1Base.d.ts +118 -0
- package/lib/lendMarkets/modules/common/leverageZapV1Base.js +770 -0
- package/lib/lendMarkets/modules/{leverageZapV2.d.ts → common/leverageZapV2Base.d.ts} +10 -5
- package/lib/lendMarkets/modules/{leverageZapV2.js → common/leverageZapV2Base.js} +36 -32
- package/lib/lendMarkets/modules/common/loanBase.d.ts +115 -0
- package/lib/lendMarkets/modules/common/loanBase.js +793 -0
- package/lib/lendMarkets/modules/common/prices.d.ts +19 -0
- package/lib/lendMarkets/modules/common/prices.js +104 -0
- package/lib/lendMarkets/modules/common/statsBase.d.ts +69 -0
- package/lib/lendMarkets/modules/common/statsBase.js +291 -0
- package/lib/lendMarkets/modules/common/userPosition.d.ts +46 -0
- package/lib/lendMarkets/modules/common/userPosition.js +223 -0
- package/lib/lendMarkets/modules/common/vault.d.ts +69 -0
- package/lib/lendMarkets/modules/common/vault.js +535 -0
- package/lib/lendMarkets/modules/common/wallet.d.ts +13 -0
- package/lib/lendMarkets/modules/common/wallet.js +28 -0
- package/lib/lendMarkets/modules/index.d.ts +1 -1
- package/lib/lendMarkets/modules/index.js +1 -1
- package/lib/lendMarkets/modules/v1/index.d.ts +4 -0
- package/lib/lendMarkets/modules/v1/index.js +4 -0
- package/lib/lendMarkets/modules/v1/leverageV1ZapV1.d.ts +3 -0
- package/lib/lendMarkets/modules/v1/leverageV1ZapV1.js +3 -0
- package/lib/lendMarkets/modules/v1/leverageV1ZapV2.d.ts +3 -0
- package/lib/lendMarkets/modules/v1/leverageV1ZapV2.js +3 -0
- package/lib/lendMarkets/modules/v1/loanV1.d.ts +4 -0
- package/lib/lendMarkets/modules/v1/loanV1.js +3 -0
- package/lib/lendMarkets/modules/v1/statsV1.d.ts +3 -0
- package/lib/lendMarkets/modules/v1/statsV1.js +3 -0
- package/lib/lendMarkets/modules/v2/index.d.ts +4 -0
- package/lib/lendMarkets/modules/v2/index.js +4 -0
- package/lib/lendMarkets/modules/v2/leverageV2ZapV1.d.ts +3 -0
- package/lib/lendMarkets/modules/v2/leverageV2ZapV1.js +3 -0
- package/lib/lendMarkets/modules/v2/leverageV2ZapV2.d.ts +3 -0
- package/lib/lendMarkets/modules/v2/leverageV2ZapV2.js +3 -0
- package/lib/lendMarkets/modules/v2/loanV2.d.ts +4 -0
- package/lib/lendMarkets/modules/v2/loanV2.js +3 -0
- package/lib/lendMarkets/modules/v2/statsV2.d.ts +3 -0
- package/lib/lendMarkets/modules/v2/statsV2.js +3 -0
- package/lib/llamalend.d.ts +6 -6
- package/lib/llamalend.js +46 -210
- package/lib/utils.js +2 -33
- package/package.json +1 -1
- package/src/constants/abis/OneWayLendingFactoryV2ABI.json +543 -0
- package/src/constants/aliases.ts +1 -97
- package/src/constants/coins.ts +0 -25
- package/src/constants/utils.ts +4 -0
- package/src/interfaces.ts +3 -2
- package/src/lendMarkets/LendMarketTemplate.ts +318 -3214
- package/src/lendMarkets/fetch/fetchFactoryData.ts +113 -0
- package/src/lendMarkets/fetch/fetchLendMarkets.ts +108 -0
- package/src/lendMarkets/interfaces/common/amm.ts +11 -0
- package/src/lendMarkets/interfaces/common/index.ts +5 -0
- package/src/lendMarkets/interfaces/common/prices.ts +13 -0
- package/src/lendMarkets/interfaces/common/userPosition.ts +24 -0
- package/src/lendMarkets/interfaces/common/vault.ts +48 -0
- package/src/lendMarkets/interfaces/common/wallet.ts +3 -0
- package/src/lendMarkets/interfaces/v1/index.ts +4 -0
- package/src/lendMarkets/interfaces/v1/leverageV1.ts +84 -0
- package/src/lendMarkets/interfaces/v1/loanV1.ts +77 -0
- package/src/lendMarkets/interfaces/v1/statsV1.ts +21 -0
- package/src/lendMarkets/interfaces/v2/index.ts +3 -0
- package/src/lendMarkets/interfaces/v2/leverageV2.ts +84 -0
- package/src/lendMarkets/interfaces/v2/loanV2.ts +77 -0
- package/src/lendMarkets/interfaces/v2/statsV2.ts +21 -0
- package/src/lendMarkets/lendMarketConstructor.ts +1 -1
- package/src/lendMarkets/modules/common/amm.ts +132 -0
- package/src/lendMarkets/modules/common/index.ts +9 -0
- package/src/lendMarkets/modules/common/leverageZapV1Base.ts +912 -0
- package/src/lendMarkets/modules/{leverageZapV2.ts → common/leverageZapV2Base.ts} +41 -37
- package/src/lendMarkets/modules/common/loanBase.ts +773 -0
- package/src/lendMarkets/modules/common/prices.ts +111 -0
- package/src/lendMarkets/modules/common/statsBase.ts +313 -0
- package/src/lendMarkets/modules/common/userPosition.ts +243 -0
- package/src/lendMarkets/modules/common/vault.ts +551 -0
- package/src/lendMarkets/modules/common/wallet.ts +29 -0
- package/src/lendMarkets/modules/index.ts +1 -1
- package/src/lendMarkets/modules/v1/index.ts +4 -0
- package/src/lendMarkets/modules/v1/leverageV1ZapV1.ts +3 -0
- package/src/lendMarkets/modules/v1/leverageV1ZapV2.ts +3 -0
- package/src/lendMarkets/modules/v1/loanV1.ts +4 -0
- package/src/lendMarkets/modules/v1/statsV1.ts +3 -0
- package/src/lendMarkets/modules/v2/index.ts +4 -0
- package/src/lendMarkets/modules/v2/leverageV2ZapV1.ts +3 -0
- package/src/lendMarkets/modules/v2/leverageV2ZapV2.ts +3 -0
- package/src/lendMarkets/modules/v2/loanV2.ts +4 -0
- package/src/lendMarkets/modules/v2/statsV2.ts +3 -0
- package/src/llamalend.ts +47 -244
- package/src/utils.ts +2 -35
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import memoize from "memoizee";
|
|
2
|
-
import type { TAmount, TGas, IDict, IQuote, ILeverageMetrics, GetExpectedFn } from "
|
|
3
|
-
import type { LendMarketTemplate } from "
|
|
2
|
+
import type { TAmount, TGas, IDict, IQuote, ILeverageMetrics, GetExpectedFn } from "../../../interfaces";
|
|
3
|
+
import type { LendMarketTemplate } from "../../LendMarketTemplate";
|
|
4
4
|
import {
|
|
5
5
|
_getAddress,
|
|
6
6
|
parseUnits,
|
|
@@ -16,16 +16,20 @@ import {
|
|
|
16
16
|
_mulBy1_3,
|
|
17
17
|
DIGas,
|
|
18
18
|
buildCalldataForLeverageZapV2,
|
|
19
|
-
} from "
|
|
20
|
-
import {Llamalend} from "
|
|
19
|
+
} from "../../../utils";
|
|
20
|
+
import {Llamalend} from "../../../llamalend";
|
|
21
21
|
import BigNumber from "bignumber.js";
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* LeverageZapV2 module for LendMarketTemplate
|
|
25
|
+
*
|
|
26
|
+
* DEPENDENCIES:
|
|
27
|
+
* - prices
|
|
28
|
+
* - userPosition
|
|
25
29
|
*/
|
|
26
|
-
export class
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
export class LeverageZapV2BaseModule {
|
|
31
|
+
protected market: LendMarketTemplate;
|
|
32
|
+
protected llamalend: Llamalend;
|
|
29
33
|
|
|
30
34
|
constructor(market: LendMarketTemplate) {
|
|
31
35
|
this.market = market;
|
|
@@ -91,12 +95,12 @@ export class LeverageZapV2Module {
|
|
|
91
95
|
}> {
|
|
92
96
|
// max_borrowable = userCollateral / (1 / (k_effective * max_p_base) - 1 / p_avg)
|
|
93
97
|
this._checkLeverageZap();
|
|
94
|
-
if (range > 0) this.market.
|
|
98
|
+
if (range > 0) this.market.prices.checkRange(range);
|
|
95
99
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
96
100
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
97
101
|
|
|
98
|
-
const oraclePriceBand = await this.market.oraclePriceBand();
|
|
99
|
-
let pAvgBN = BN(await this.market.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
102
|
+
const oraclePriceBand = await this.market.prices.oraclePriceBand();
|
|
103
|
+
let pAvgBN = BN(await this.market.prices.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
100
104
|
let maxBorrowablePrevBN = BN(0);
|
|
101
105
|
let maxBorrowableBN = BN(0);
|
|
102
106
|
let _userEffectiveCollateral = BigInt(0);
|
|
@@ -159,8 +163,8 @@ export class LeverageZapV2Module {
|
|
|
159
163
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
160
164
|
const contract = this.llamalend.contracts[this.llamalend.constants.ALIASES.leverage_zap_v2].multicallContract;
|
|
161
165
|
|
|
162
|
-
const oraclePriceBand = await this.market.oraclePriceBand();
|
|
163
|
-
const pAvgApproxBN = BN(await this.market.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
166
|
+
const oraclePriceBand = await this.market.prices.oraclePriceBand();
|
|
167
|
+
const pAvgApproxBN = BN(await this.market.prices.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
164
168
|
let pAvgBN: BigNumber | null = null;
|
|
165
169
|
const arrLength = this.market.maxBands - this.market.minBands + 1;
|
|
166
170
|
let maxLeverageCollateralBN: BigNumber[] = new Array(arrLength).fill(BN(0));
|
|
@@ -245,7 +249,7 @@ export class LeverageZapV2Module {
|
|
|
245
249
|
const _collateralFromUserBorrowed = _additionalCollateral - _collateralFromDebt;
|
|
246
250
|
let _stateCollateral = BigInt(0);
|
|
247
251
|
if (user) {
|
|
248
|
-
const { _collateral, _borrowed } = await this.market.
|
|
252
|
+
const { _collateral, _borrowed } = await this.market.userPosition.userStateBigInt(user);
|
|
249
253
|
if (_borrowed > BigInt(0)) throw Error(`User ${user} is already in liquidation mode`);
|
|
250
254
|
_stateCollateral = _collateral;
|
|
251
255
|
}
|
|
@@ -289,7 +293,7 @@ export class LeverageZapV2Module {
|
|
|
289
293
|
|
|
290
294
|
const [_n2, _n1] = await this._leverageBands(userCollateral, userBorrowed, debt, range, quote);
|
|
291
295
|
|
|
292
|
-
const prices = await this.market.
|
|
296
|
+
const prices = await this.market.prices.getPrices(_n2, _n1);
|
|
293
297
|
const health = await this._leverageHealth(userCollateral, userBorrowed, debt, range, quote, healthIsFull);
|
|
294
298
|
|
|
295
299
|
return {
|
|
@@ -316,10 +320,10 @@ export class LeverageZapV2Module {
|
|
|
316
320
|
}
|
|
317
321
|
|
|
318
322
|
private _leverageCalcN1 = memoize(async (userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, range: number, quote: IQuote, user?: string): Promise<bigint> => {
|
|
319
|
-
if (range > 0) this.market.
|
|
323
|
+
if (range > 0) this.market.prices.checkRange(range);
|
|
320
324
|
let _stateDebt = BigInt(0);
|
|
321
325
|
if (user) {
|
|
322
|
-
const { _debt, _borrowed, _N } = await this.market.
|
|
326
|
+
const { _debt, _borrowed, _N } = await this.market.userPosition.userStateBigInt(user);
|
|
323
327
|
if (_borrowed > BigInt(0)) throw Error(`User ${user} is already in liquidation mode`);
|
|
324
328
|
_stateDebt = _debt;
|
|
325
329
|
if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
|
|
@@ -350,7 +354,7 @@ export class LeverageZapV2Module {
|
|
|
350
354
|
private async _leverageBands(userCollateral: TAmount, userBorrowed: TAmount, debt: TAmount, range: number, quote: IQuote, user?: string): Promise<[bigint, bigint]> {
|
|
351
355
|
const _n1 = await this._leverageCalcN1(userCollateral, userBorrowed, debt, range, quote, user);
|
|
352
356
|
if (range < 0) {
|
|
353
|
-
const { N } = await this.market.userState(user);
|
|
357
|
+
const { N } = await this.market.userPosition.userState(user);
|
|
354
358
|
range = Number(N);
|
|
355
359
|
}
|
|
356
360
|
const _n2 = _n1 + BigInt(range - 1);
|
|
@@ -409,7 +413,7 @@ export class LeverageZapV2Module {
|
|
|
409
413
|
const prices: { [index: number]: [string, string] | null } = {};
|
|
410
414
|
for (let N = this.market.minBands; N <= this.market.maxBands; N++) {
|
|
411
415
|
if (_bands[N]) {
|
|
412
|
-
prices[N] = await this.market.
|
|
416
|
+
prices[N] = await this.market.prices.calcPrices(..._bands[N]);
|
|
413
417
|
} else {
|
|
414
418
|
prices[N] = null
|
|
415
419
|
}
|
|
@@ -427,9 +431,9 @@ export class LeverageZapV2Module {
|
|
|
427
431
|
full: boolean,
|
|
428
432
|
user = this.llamalend.constants.ZERO_ADDRESS
|
|
429
433
|
): Promise<string> {
|
|
430
|
-
if (range > 0) this.market.
|
|
434
|
+
if (range > 0) this.market.prices.checkRange(range);
|
|
431
435
|
const { _totalCollateral } = await this._leverageExpectedCollateral(userCollateral, userBorrowed, dDebt, quote, user);
|
|
432
|
-
const { _borrowed, _N } = await this.market.
|
|
436
|
+
const { _borrowed, _N } = await this.market.userPosition.userStateBigInt(user);
|
|
433
437
|
if (_borrowed > BigInt(0)) throw Error(`User ${user} is already in liquidation mode`);
|
|
434
438
|
if (range < 0) range = Number(this.llamalend.formatUnits(_N, 0));
|
|
435
439
|
const _dDebt = parseUnits(dDebt, this.market.borrowed_token.decimals);
|
|
@@ -494,8 +498,8 @@ export class LeverageZapV2Module {
|
|
|
494
498
|
calldata: string,
|
|
495
499
|
estimateGas: boolean
|
|
496
500
|
): Promise<string | TGas> {
|
|
497
|
-
if (await this.market.userLoanExists()) throw Error("Loan already created");
|
|
498
|
-
this.market.
|
|
501
|
+
if (await this.market.userPosition.userLoanExists()) throw Error("Loan already created");
|
|
502
|
+
this.market.prices.checkRange(range);
|
|
499
503
|
|
|
500
504
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
501
505
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
@@ -575,7 +579,7 @@ export class LeverageZapV2Module {
|
|
|
575
579
|
// max_borrowable = userCollateral / (1 / (k_effective * max_p_base) - 1 / p_avg)
|
|
576
580
|
this._checkLeverageZap();
|
|
577
581
|
address = _getAddress.call(this.llamalend, address);
|
|
578
|
-
const { _collateral: _stateCollateral, _borrowed: _stateBorrowed, _debt: _stateDebt, _N } = await this.market.
|
|
582
|
+
const { _collateral: _stateCollateral, _borrowed: _stateBorrowed, _debt: _stateDebt, _N } = await this.market.userPosition.userStateBigInt(address);
|
|
579
583
|
if (_stateBorrowed > BigInt(0)) throw Error(`User ${address} is already in liquidation mode`);
|
|
580
584
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
581
585
|
const controllerContract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
@@ -583,8 +587,8 @@ export class LeverageZapV2Module {
|
|
|
583
587
|
const _userBorrowed = _borrowedFromStateCollateral + parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
584
588
|
userBorrowed = formatUnits(_userBorrowed, this.market.borrowed_token.decimals);
|
|
585
589
|
|
|
586
|
-
const oraclePriceBand = await this.market.oraclePriceBand();
|
|
587
|
-
let pAvgBN = BN(await this.market.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
590
|
+
const oraclePriceBand = await this.market.prices.oraclePriceBand();
|
|
591
|
+
let pAvgBN = BN(await this.market.prices.calcTickPrice(oraclePriceBand)); // upper tick of oracle price band
|
|
588
592
|
let maxBorrowablePrevBN = BN(0);
|
|
589
593
|
let maxBorrowableBN = BN(0);
|
|
590
594
|
let _userEffectiveCollateral = BigInt(0);
|
|
@@ -661,7 +665,7 @@ export class LeverageZapV2Module {
|
|
|
661
665
|
|
|
662
666
|
const [_n2, _n1] = await this._leverageBands(userCollateral, userBorrowed, debt, -1, quote, address);
|
|
663
667
|
|
|
664
|
-
const prices = await this.market.
|
|
668
|
+
const prices = await this.market.prices.getPrices(_n2, _n1);
|
|
665
669
|
const health = await this._leverageHealth(userCollateral, userBorrowed, debt, -1, quote, healthIsFull, address);
|
|
666
670
|
|
|
667
671
|
return {
|
|
@@ -681,7 +685,7 @@ export class LeverageZapV2Module {
|
|
|
681
685
|
calldata: string,
|
|
682
686
|
estimateGas: boolean
|
|
683
687
|
): Promise<string | TGas> {
|
|
684
|
-
if (!(await this.market.userLoanExists())) throw Error("Loan does not exist");
|
|
688
|
+
if (!(await this.market.userPosition.userLoanExists())) throw Error("Loan does not exist");
|
|
685
689
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
686
690
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
687
691
|
const _debt = parseUnits(debt, this.market.borrowed_token.decimals);
|
|
@@ -763,7 +767,7 @@ export class LeverageZapV2Module {
|
|
|
763
767
|
address = _getAddress.call(this.llamalend, address);
|
|
764
768
|
this._checkLeverageZap();
|
|
765
769
|
|
|
766
|
-
const { stateCollateral, totalDepositFromUser } = await this.market.
|
|
770
|
+
const { stateCollateral, totalDepositFromUser } = await this.market.userPosition.getCurrentLeverageParams(address);
|
|
767
771
|
|
|
768
772
|
const expected = await this.leverageBorrowMoreExpectedCollateral({
|
|
769
773
|
userCollateral,
|
|
@@ -829,7 +833,7 @@ export class LeverageZapV2Module {
|
|
|
829
833
|
}): Promise<boolean> {
|
|
830
834
|
this._checkLeverageZap();
|
|
831
835
|
address = _getAddress.call(this.llamalend, address);
|
|
832
|
-
const { _borrowed: _stateBorrowed, _debt } = await this.market.
|
|
836
|
+
const { _borrowed: _stateBorrowed, _debt } = await this.market.userPosition.userStateBigInt(address);
|
|
833
837
|
const { _totalBorrowed } = this._leverageRepayExpectedBorrowed(stateCollateral, userCollateral, userBorrowed, quote);
|
|
834
838
|
|
|
835
839
|
return _stateBorrowed + _totalBorrowed > _debt;
|
|
@@ -842,13 +846,13 @@ export class LeverageZapV2Module {
|
|
|
842
846
|
quote: IQuote,
|
|
843
847
|
address?: string
|
|
844
848
|
}): Promise<boolean> {
|
|
845
|
-
// 0. const { collateral, stablecoin, debt } = await this.market.userState(address);
|
|
849
|
+
// 0. const { collateral, stablecoin, debt } = await this.market.userPosition.userState(address);
|
|
846
850
|
// 1. maxCollateral for deleverage is collateral from line above.
|
|
847
851
|
// 2. If user is underwater (stablecoin > 0), only full repayment is available:
|
|
848
852
|
// await this.deleverageRepayStablecoins(deleverageCollateral) + stablecoin > debt
|
|
849
853
|
this._checkLeverageZap();
|
|
850
854
|
address = _getAddress.call(this.llamalend, address);
|
|
851
|
-
const { collateral, borrowed, debt } = await this.market.userState(address);
|
|
855
|
+
const { collateral, borrowed, debt } = await this.market.userPosition.userState(address);
|
|
852
856
|
// Loan does not exist
|
|
853
857
|
if (BN(debt).eq(0)) return false;
|
|
854
858
|
// Can't spend more than user has
|
|
@@ -871,7 +875,7 @@ export class LeverageZapV2Module {
|
|
|
871
875
|
address = _getAddress.call(this.llamalend, address);
|
|
872
876
|
|
|
873
877
|
const [_n2, _n1] = await this._leverageRepayBands(stateCollateral, userCollateral, userBorrowed, quote, address);
|
|
874
|
-
const prices = await this.market.
|
|
878
|
+
const prices = await this.market.prices.getPrices(_n2, _n1);
|
|
875
879
|
const health = await this._leverageRepayHealth(stateCollateral, userCollateral, userBorrowed, quote, healthIsFull, address);
|
|
876
880
|
|
|
877
881
|
const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
|
|
@@ -891,7 +895,7 @@ export class LeverageZapV2Module {
|
|
|
891
895
|
if (!(await this.leverageRepayIsAvailable({ stateCollateral, userCollateral, userBorrowed, quote, address }))) return [parseUnits(0, 0), parseUnits(0, 0)];
|
|
892
896
|
|
|
893
897
|
const _stateRepayCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
|
|
894
|
-
const { _collateral: _stateCollateral, _debt: _stateDebt, _N } = await this.market.
|
|
898
|
+
const { _collateral: _stateCollateral, _debt: _stateDebt, _N } = await this.market.userPosition.userStateBigInt(address);
|
|
895
899
|
if (_stateDebt == BigInt(0)) throw Error(`Loan for ${address} does not exist`);
|
|
896
900
|
if (_stateCollateral < _stateRepayCollateral) throw Error(`Can't use more collateral than user's position has (${_stateRepayCollateral}) > ${_stateCollateral})`);
|
|
897
901
|
|
|
@@ -915,7 +919,7 @@ export class LeverageZapV2Module {
|
|
|
915
919
|
private async _leverageRepayHealth(stateCollateral: TAmount, userCollateral: TAmount, userBorrowed: TAmount, quote: IQuote, full = true, address = ""): Promise<string> {
|
|
916
920
|
this._checkLeverageZap();
|
|
917
921
|
address = _getAddress.call(this.llamalend, address);
|
|
918
|
-
const { _borrowed: _stateBorrowed, _debt, _N } = await this.market.
|
|
922
|
+
const { _borrowed: _stateBorrowed, _debt, _N } = await this.market.userPosition.userStateBigInt(address);
|
|
919
923
|
if (_stateBorrowed > BigInt(0)) return "0.0";
|
|
920
924
|
if (!(await this.leverageRepayIsAvailable({ stateCollateral, userCollateral, userBorrowed, quote, address }))) return "0.0";
|
|
921
925
|
|
|
@@ -977,7 +981,7 @@ export class LeverageZapV2Module {
|
|
|
977
981
|
calldata: string,
|
|
978
982
|
estimateGas: boolean
|
|
979
983
|
): Promise<string | TGas> {
|
|
980
|
-
if (!(await this.market.userLoanExists())) throw Error("Loan does not exist");
|
|
984
|
+
if (!(await this.market.userPosition.userLoanExists())) throw Error("Loan does not exist");
|
|
981
985
|
const _stateCollateral = parseUnits(stateCollateral, this.market.collateral_token.decimals);
|
|
982
986
|
const _userCollateral = parseUnits(userCollateral, this.market.collateral_token.decimals);
|
|
983
987
|
const _userBorrowed = parseUnits(userBorrowed, this.market.borrowed_token.decimals);
|
|
@@ -1042,9 +1046,9 @@ export class LeverageZapV2Module {
|
|
|
1042
1046
|
address = _getAddress.call(this.llamalend, address);
|
|
1043
1047
|
this._checkLeverageZap();
|
|
1044
1048
|
|
|
1045
|
-
const { stateCollateral: currentStateCollateral, totalDepositFromUser } = await this.market.
|
|
1049
|
+
const { stateCollateral: currentStateCollateral, totalDepositFromUser } = await this.market.userPosition.getCurrentLeverageParams(address);
|
|
1046
1050
|
|
|
1047
|
-
const collateralFromUserBorrowed = await this.market.swapExpected(0, 1, userBorrowed);
|
|
1051
|
+
const collateralFromUserBorrowed = await this.market.amm.swapExpected(0, 1, userBorrowed);
|
|
1048
1052
|
|
|
1049
1053
|
const futureCollateralState = BN(currentStateCollateral).minus(stateCollateral);
|
|
1050
1054
|
const futureTotalDepositFromUserPrecise = BN(totalDepositFromUser).plus(userCollateral).plus(collateralFromUserBorrowed);
|