@gearbox-protocol/sdk 14.12.0-next.72 → 14.12.0-next.74
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/dist/cjs/dev/AccountOpener.js +2 -5
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +49 -533
- package/dist/cjs/sdk/accounts/index.js +0 -4
- package/dist/cjs/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +1 -1
- package/dist/cjs/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +1 -1
- package/dist/cjs/sdk/accounts/intents/testing/sdk-mock.js +2 -2
- package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +3 -3
- package/dist/cjs/sdk/accounts/liquidations/constants.js +0 -2
- package/dist/cjs/sdk/accounts/liquidations/index.js +0 -2
- package/dist/cjs/sdk/constants/index.js +1 -0
- package/dist/cjs/sdk/constants/math.js +5 -0
- package/dist/cjs/sdk/index.js +8 -4
- package/dist/cjs/sdk/market/MarketRegister.js +10 -0
- package/dist/cjs/sdk/market/credit/CreditFacadeV310Contract.js +181 -0
- package/dist/cjs/sdk/market/credit/CreditSuite.js +107 -1
- package/dist/cjs/sdk/{accounts → market/credit}/dominantCollateral.js +18 -4
- package/dist/cjs/sdk/market/credit/index.js +3 -0
- package/dist/cjs/sdk/market/index.js +3 -0
- package/dist/cjs/sdk/market/math.js +59 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleV310Contract.js +2 -9
- package/dist/cjs/sdk/opportunities/index.js +4 -0
- package/dist/esm/dev/AccountOpener.js +2 -5
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +50 -534
- package/dist/esm/sdk/accounts/index.js +1 -3
- package/dist/esm/sdk/accounts/intents/operations/unwrap-rwa-collateral/index.js +1 -1
- package/dist/esm/sdk/accounts/intents/operations/wrap-rwa-collateral/index.js +1 -1
- package/dist/esm/sdk/accounts/intents/testing/sdk-mock.js +2 -2
- package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +2 -2
- package/dist/esm/sdk/accounts/liquidations/constants.js +1 -2
- package/dist/esm/sdk/accounts/liquidations/index.js +1 -2
- package/dist/esm/sdk/constants/index.js +2 -2
- package/dist/esm/sdk/constants/math.js +5 -1
- package/dist/esm/sdk/index.js +4 -5
- package/dist/esm/sdk/market/MarketRegister.js +10 -0
- package/dist/esm/sdk/market/credit/CreditFacadeV310Contract.js +181 -0
- package/dist/esm/sdk/market/credit/CreditSuite.js +108 -2
- package/dist/esm/sdk/{accounts → market/credit}/dominantCollateral.js +18 -5
- package/dist/esm/sdk/market/credit/index.js +2 -1
- package/dist/esm/sdk/market/index.js +2 -1
- package/dist/esm/sdk/market/math.js +56 -1
- package/dist/esm/sdk/market/oracle/PriceOracleV310Contract.js +3 -10
- package/dist/esm/sdk/opportunities/index.js +2 -2
- package/dist/types/sdk/OnchainSDK.d.ts +1 -1
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +11 -78
- package/dist/types/sdk/accounts/index.d.ts +2 -4
- package/dist/types/sdk/accounts/liquidations/constants.d.ts +1 -2
- package/dist/types/sdk/accounts/liquidations/index.d.ts +1 -2
- package/dist/types/sdk/accounts/types.d.ts +42 -346
- package/dist/types/sdk/constants/index.d.ts +2 -2
- package/dist/types/sdk/constants/math.d.ts +5 -1
- package/dist/types/sdk/index.d.ts +10 -11
- package/dist/types/sdk/market/MarketRegister.d.ts +7 -0
- package/dist/types/sdk/market/credit/CreditFacadeV310Contract.d.ts +47 -2
- package/dist/types/sdk/market/credit/CreditSuite.d.ts +24 -2
- package/dist/types/sdk/{accounts → market/credit}/dominantCollateral.d.ts +13 -6
- package/dist/types/sdk/market/credit/index.d.ts +3 -2
- package/dist/types/sdk/market/credit/types.d.ts +112 -2
- package/dist/types/sdk/market/index.d.ts +3 -2
- package/dist/types/sdk/market/math.d.ts +60 -1
- package/dist/types/sdk/market/oracle/PriceOracleV310Contract.d.ts +1 -0
- package/dist/types/sdk/market/rwa/securitize/SecuritizeRWAFactory.d.ts +1 -1
- package/dist/types/sdk/market/rwa/types.d.ts +1 -1
- package/dist/types/sdk/opportunities/index.d.ts +2 -2
- package/package.json +1 -1
- package/dist/cjs/sdk/accounts/constants.js +0 -12
- package/dist/esm/sdk/accounts/constants.js +0 -11
- package/dist/types/sdk/accounts/constants.d.ts +0 -11
|
@@ -8,7 +8,6 @@ const require_abi_compressors_rewardsCompressor = require("../../abi/compressors
|
|
|
8
8
|
const require_abi_rwa_iRWAFactory = require("../../abi/rwa/iRWAFactory.js");
|
|
9
9
|
const require_sdk_utils_AddressMap = require("../utils/AddressMap.js");
|
|
10
10
|
const require_sdk_utils_AddressSet = require("../utils/AddressSet.js");
|
|
11
|
-
const require_sdk_utils_AssetsMap = require("../utils/AssetsMap.js");
|
|
12
11
|
const require_sdk_constants_address_provider = require("../constants/address-provider.js");
|
|
13
12
|
const require_sdk_constants_addresses = require("../constants/addresses.js");
|
|
14
13
|
const require_sdk_constants_math = require("../constants/math.js");
|
|
@@ -19,12 +18,11 @@ require("../utils/index.js");
|
|
|
19
18
|
const require_sdk_base_SDKConstruct = require("../base/SDKConstruct.js");
|
|
20
19
|
require("../base/index.js");
|
|
21
20
|
const require_sdk_market_math = require("../market/math.js");
|
|
21
|
+
const require_sdk_market_credit_dominantCollateral = require("../market/credit/dominantCollateral.js");
|
|
22
22
|
const require_sdk_utils_viem_simulateWithPriceUpdates = require("../utils/viem/simulateWithPriceUpdates.js");
|
|
23
23
|
require("../utils/viem/index.js");
|
|
24
24
|
const require_sdk_market_pricefeeds_getRawPriceUpdates = require("../market/pricefeeds/getRawPriceUpdates.js");
|
|
25
25
|
require("../market/index.js");
|
|
26
|
-
require("./constants.js");
|
|
27
|
-
const require_sdk_accounts_dominantCollateral = require("./dominantCollateral.js");
|
|
28
26
|
const require_sdk_accounts_multicall_utils = require("./multicall-utils.js");
|
|
29
27
|
let viem = require("viem");
|
|
30
28
|
//#region src/sdk/accounts/CreditAccountsServiceV310.ts
|
|
@@ -247,7 +245,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
247
245
|
const { pool } = market.pool;
|
|
248
246
|
const token = this.sdk.tokensMeta.mustGetToken(market.unwrappedUnderlying);
|
|
249
247
|
const totalDebtValue = ca.debt + ca.accruedInterest + ca.accruedFees;
|
|
250
|
-
const collateral =
|
|
248
|
+
const collateral = require_sdk_market_credit_dominantCollateral.dominantCollateral(ca, market);
|
|
251
249
|
return {
|
|
252
250
|
kind: "strategy",
|
|
253
251
|
chainId: this.sdk.chainId,
|
|
@@ -437,212 +435,30 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
437
435
|
};
|
|
438
436
|
}
|
|
439
437
|
/**
|
|
440
|
-
* {@inheritDoc ICreditAccountsService.defaultPartialLiquidationParams}
|
|
441
|
-
*/
|
|
442
|
-
defaultPartialLiquidationParams(ca) {
|
|
443
|
-
const tokenOut = this.#getBestTokenOut(ca);
|
|
444
|
-
const optimalHF = this.getOptimalHFForPartialLiquidation(ca);
|
|
445
|
-
const repaidAmount = this.#calcOptimalRepaidAmount(ca, tokenOut, optimalHF);
|
|
446
|
-
return {
|
|
447
|
-
tokenOut,
|
|
448
|
-
optimalHF,
|
|
449
|
-
repaidAmount,
|
|
450
|
-
minSeizedAmount: this.#calcMinSeizedAmount(ca, tokenOut, repaidAmount)
|
|
451
|
-
};
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
438
|
* {@inheritDoc ICreditAccountsService.partiallyLiquidate}
|
|
455
439
|
*/
|
|
456
440
|
async partiallyLiquidate(props) {
|
|
457
441
|
const { account, to } = props;
|
|
458
|
-
const tokenOut = props.tokenOut ?? this.#getBestTokenOut(account);
|
|
459
|
-
const optimalHF = props.optimalHF ?? this.getOptimalHFForPartialLiquidation(account);
|
|
460
|
-
const repaidAmount = props.repaidAmount ?? this.#calcOptimalRepaidAmount(account, tokenOut, optimalHF);
|
|
461
|
-
const minSeizedAmount = props.minSeizedAmount ?? this.#calcMinSeizedAmount(account, tokenOut, repaidAmount);
|
|
462
442
|
const cm = this.sdk.marketRegister.findCreditManager(account.creditManager);
|
|
443
|
+
const { tokenOut, repaidAmount, minSeizedAmount } = cm.partialLiquidationParams(account, props);
|
|
463
444
|
const updates = await this.getOnDemandPriceUpdates(account, true);
|
|
464
445
|
return cm.creditFacade.partiallyLiquidateCreditAccount(account.creditAccount, tokenOut, repaidAmount, minSeizedAmount, to, updates);
|
|
465
446
|
}
|
|
466
447
|
/**
|
|
467
|
-
* Picks the most valuable enabled non-underlying collateral token on the
|
|
468
|
-
* credit account (by oracle value in underlying).
|
|
469
|
-
*
|
|
470
|
-
* Ported from solidity:
|
|
471
|
-
* https://github.com/Gearbox-protocol/router-v3/blob/main/contracts/liquidation/AbstractLiquidator.sol#L270
|
|
472
|
-
*/
|
|
473
|
-
#getBestTokenOut(account) {
|
|
474
|
-
const market = this.sdk.marketRegister.findByCreditManager(account.creditManager);
|
|
475
|
-
const underlying = market.underlying;
|
|
476
|
-
let bestVal = 0n;
|
|
477
|
-
let bestToken;
|
|
478
|
-
for (const t of account.tokens) {
|
|
479
|
-
if (t.token === underlying) continue;
|
|
480
|
-
if ((t.mask & account.enabledTokensMask) === 0n) continue;
|
|
481
|
-
if (t.balance === 0n) continue;
|
|
482
|
-
const val = market.priceOracle.convert(t.token, underlying, t.balance);
|
|
483
|
-
if (val > bestVal) {
|
|
484
|
-
bestVal = val;
|
|
485
|
-
bestToken = t.token;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
if (!bestToken) throw new Error(`cannot determine tokenOut for partial liquidation of ${this.sdk.labelAddress(account.creditAccount)}: no enabled non-underlying collateral with value`);
|
|
489
|
-
return bestToken;
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Returns the minimum amount of `token` collateral that must be seized when
|
|
493
|
-
* repaying `repaidAmount` of underlying. Mirrors the on-chain liquidation
|
|
494
|
-
* discount math, expired-aware.
|
|
495
|
-
*/
|
|
496
|
-
#calcMinSeizedAmount(account, token, repaidAmount) {
|
|
497
|
-
const market = this.sdk.marketRegister.findByCreditManager(account.creditManager);
|
|
498
|
-
const suite = this.sdk.marketRegister.findCreditManager(account.creditManager);
|
|
499
|
-
const fee = suite.isExpired ? suite.creditManager.liquidationDiscountExpired : suite.creditManager.liquidationDiscount;
|
|
500
|
-
return market.priceOracle.convert(market.underlying, token, repaidAmount) * 9990n / BigInt(fee);
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Computes the optimal `repaidAmount` (in underlying) that brings the credit
|
|
504
|
-
* account's health factor close to `optimalHF` after partial liquidation.
|
|
505
|
-
*
|
|
506
|
-
* Ported from solidity:
|
|
507
|
-
* https://github.com/Gearbox-protocol/router-v3/blob/56e2d515ec6d9bb1e324e71c3708e59710779b24/contracts/liquidation/AbstractLiquidator.sol#L292
|
|
508
|
-
*/
|
|
509
|
-
#calcOptimalRepaidAmount(account, token, optimalHF) {
|
|
510
|
-
const suite = this.sdk.marketRegister.findCreditManager(account.creditManager);
|
|
511
|
-
const market = this.sdk.marketRegister.findByCreditManager(account.creditManager);
|
|
512
|
-
const cm = suite.creditManager;
|
|
513
|
-
const feeLiquidation = suite.isExpired ? cm.feeLiquidationExpired : cm.feeLiquidation;
|
|
514
|
-
const liquidationDiscount = suite.isExpired ? cm.liquidationDiscountExpired : cm.liquidationDiscount;
|
|
515
|
-
const discount = BigInt(liquidationDiscount) - BigInt(feeLiquidation);
|
|
516
|
-
const ltTokenOut = cm.liquidationThresholds.get(token);
|
|
517
|
-
if (ltTokenOut === void 0) throw new Error(`token ${this.sdk.labelAddress(token)} is not a collateral token in credit manager ${this.sdk.labelAddress(account.creditManager)}`);
|
|
518
|
-
const totalDebt = account.debt + account.accruedInterest + account.accruedFees;
|
|
519
|
-
const twvUnderlying = market.priceOracle.convertFromUSD(market.underlying, account.twvUSD);
|
|
520
|
-
const denominator = discount * optimalHF / require_sdk_constants_math.PERCENTAGE_FACTOR - BigInt(ltTokenOut);
|
|
521
|
-
if (denominator <= 0n) throw new Error("cannot compute optimal repaid amount: invalid liquidation parameters (discount * hfOptimal <= ltTokenOut)");
|
|
522
|
-
const numerator = totalDebt * optimalHF - twvUnderlying * require_sdk_constants_math.PERCENTAGE_FACTOR;
|
|
523
|
-
if (numerator <= 0n) return 0n;
|
|
524
|
-
let repaidAmount = numerator / denominator * discount / require_sdk_constants_math.PERCENTAGE_FACTOR;
|
|
525
|
-
const minDebt = suite.creditFacade.minDebt;
|
|
526
|
-
if (totalDebt < minDebt) return 0n;
|
|
527
|
-
const surplusDebt = totalDebt - minDebt;
|
|
528
|
-
if (repaidAmount > surplusDebt) repaidAmount = surplusDebt * 999n / 1000n;
|
|
529
|
-
return repaidAmount;
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* {@inheritDoc ICreditAccountsService.closeCreditAccount}
|
|
533
|
-
**/
|
|
534
|
-
async closeCreditAccount({ operation, assetsToWithdraw, creditAccount: ca, to, slippage = 50n, closePath }) {
|
|
535
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
536
|
-
const routerCloseResult = closePath || await this.sdk.routerFor(ca).findBestClosePath({
|
|
537
|
-
creditAccount: ca,
|
|
538
|
-
creditManager: cm.creditManager,
|
|
539
|
-
slippage
|
|
540
|
-
});
|
|
541
|
-
const operationCalls = await this.assembleCloseCreditAccountCalls({
|
|
542
|
-
creditAccount: ca,
|
|
543
|
-
routerCalls: routerCloseResult.calls,
|
|
544
|
-
assetsToWithdraw,
|
|
545
|
-
to
|
|
546
|
-
});
|
|
547
|
-
const calls = operation === "close" ? operationCalls : await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
548
|
-
return {
|
|
549
|
-
tx: await this.#closeCreditAccountTx(cm, ca.creditAccount, calls, operation),
|
|
550
|
-
calls,
|
|
551
|
-
routerCloseResult,
|
|
552
|
-
creditFacade: cm.creditFacade
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
448
|
* {@inheritDoc ICreditAccountsService.assembleCloseCreditAccountCalls}
|
|
557
449
|
*/
|
|
558
450
|
async assembleCloseCreditAccountCalls({ creditAccount: ca, routerCalls, assetsToWithdraw, to }) {
|
|
559
|
-
const unwrapCalls = await this.
|
|
451
|
+
const unwrapCalls = await this.assembleRedeemDiffCalls(1n, ca.creditManager) ?? [];
|
|
452
|
+
const { creditFacade } = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
560
453
|
return [
|
|
561
454
|
...routerCalls,
|
|
562
|
-
...
|
|
563
|
-
...
|
|
455
|
+
...creditFacade.prepareDisableQuotas([...ca.tokens]),
|
|
456
|
+
...ca.debt > 0n ? [creditFacade.prepareDecreaseDebtFull()] : [],
|
|
564
457
|
...unwrapCalls,
|
|
565
|
-
...assetsToWithdraw.map((t) =>
|
|
458
|
+
...assetsToWithdraw.map((t) => creditFacade.prepareWithdrawCollateral(t, require_sdk_constants_math.MAX_UINT256, to))
|
|
566
459
|
];
|
|
567
460
|
}
|
|
568
461
|
/**
|
|
569
|
-
* {@inheritDoc ICreditAccountsService.updateQuotas}
|
|
570
|
-
**/
|
|
571
|
-
async updateQuotas({ minQuota, averageQuota, creditAccount }) {
|
|
572
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
573
|
-
const operationCalls = this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
574
|
-
minQuota,
|
|
575
|
-
averageQuota
|
|
576
|
-
});
|
|
577
|
-
const calls = await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
578
|
-
return {
|
|
579
|
-
tx: await this.#multicallTx(cm, creditAccount.creditAccount, calls),
|
|
580
|
-
calls,
|
|
581
|
-
creditFacade: cm.creditFacade
|
|
582
|
-
};
|
|
583
|
-
}
|
|
584
|
-
/**
|
|
585
|
-
* {@inheritDoc ICreditAccountsService.addCollateral}
|
|
586
|
-
**/
|
|
587
|
-
async addCollateral({ creditAccount, asset, permit, ethAmount, minQuota, averageQuota }) {
|
|
588
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
589
|
-
const operationCalls = [...this.prepareAddCollateral(creditAccount.creditFacade, [asset], permit ? { [asset.token]: permit } : {}), ...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
590
|
-
minQuota,
|
|
591
|
-
averageQuota
|
|
592
|
-
})];
|
|
593
|
-
const calls = await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
594
|
-
const tx = await this.#multicallTx(cm, creditAccount.creditAccount, calls);
|
|
595
|
-
tx.value = ethAmount.toString(10);
|
|
596
|
-
return {
|
|
597
|
-
tx,
|
|
598
|
-
calls,
|
|
599
|
-
creditFacade: cm.creditFacade
|
|
600
|
-
};
|
|
601
|
-
}
|
|
602
|
-
/**
|
|
603
|
-
* {@inheritDoc ICreditAccountsService.changeDebt}
|
|
604
|
-
**/
|
|
605
|
-
async changeDebt({ creditAccount, amount, collateral }) {
|
|
606
|
-
if (amount === 0n) throw new Error("debt increase or decrease must be non-zero");
|
|
607
|
-
const isDecrease = amount < 0n;
|
|
608
|
-
const change = amount > 0n ? amount : -amount;
|
|
609
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
610
|
-
const addCollateralCalls = collateral && isDecrease ? this.prepareAddCollateral(creditAccount.creditFacade, [{
|
|
611
|
-
token: collateral[0].token,
|
|
612
|
-
balance: collateral[0].balance
|
|
613
|
-
}], {}) : [];
|
|
614
|
-
const unwrapCalls = collateral && isDecrease ? await this.getRWAUnwrapCalls(collateral[0].balance, creditAccount.creditManager) || [] : [];
|
|
615
|
-
if (addCollateralCalls.length > 0 && unwrapCalls.length === 0 && collateral && collateral?.[0].token !== creditAccount.underlying) throw new Error("Can't use collateral other than underlying for non RWA market");
|
|
616
|
-
const operationCalls = [
|
|
617
|
-
...addCollateralCalls,
|
|
618
|
-
...unwrapCalls,
|
|
619
|
-
this.prepareChangeDebt(creditAccount.creditFacade, change, isDecrease)
|
|
620
|
-
];
|
|
621
|
-
const calls = await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
622
|
-
return {
|
|
623
|
-
tx: await this.#multicallTx(cm, creditAccount.creditAccount, calls),
|
|
624
|
-
calls,
|
|
625
|
-
creditFacade: cm.creditFacade
|
|
626
|
-
};
|
|
627
|
-
}
|
|
628
|
-
/**
|
|
629
|
-
* {@inheritDoc ICreditAccountsService.executeSwap}
|
|
630
|
-
**/
|
|
631
|
-
async executeSwap({ creditAccount, calls: swapCalls, minQuota, averageQuota }) {
|
|
632
|
-
if (swapCalls.length === 0) throw new Error("No path to execute");
|
|
633
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
634
|
-
const operationCalls = [...swapCalls, ...this.prepareUpdateQuotas(creditAccount.creditFacade, {
|
|
635
|
-
minQuota,
|
|
636
|
-
averageQuota
|
|
637
|
-
})];
|
|
638
|
-
const calls = await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
639
|
-
return {
|
|
640
|
-
tx: await this.#multicallTx(cm, creditAccount.creditAccount, calls),
|
|
641
|
-
calls,
|
|
642
|
-
creditFacade: cm.creditFacade
|
|
643
|
-
};
|
|
644
|
-
}
|
|
645
|
-
/**
|
|
646
462
|
* {@inheritDoc ICreditAccountsService.previewDelayedWithdrawal}
|
|
647
463
|
**/
|
|
648
464
|
async previewDelayedWithdrawal({ creditAccount, amount, token, withdrawalPhantomToken, intent }) {
|
|
@@ -665,25 +481,6 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
665
481
|
};
|
|
666
482
|
}
|
|
667
483
|
/**
|
|
668
|
-
* {@inheritDoc ICreditAccountsService.startDelayedWithdrawal}
|
|
669
|
-
**/
|
|
670
|
-
async startDelayedWithdrawal({ creditAccount, minQuota, averageQuota, preview }) {
|
|
671
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
672
|
-
const operationCalls = [...this.assembleStartDelayedWithdrawalCalls({
|
|
673
|
-
creditFacade: cm.creditFacade.address,
|
|
674
|
-
preview
|
|
675
|
-
}), ...this.prepareUpdateQuotas(cm.creditFacade.address, {
|
|
676
|
-
minQuota,
|
|
677
|
-
averageQuota
|
|
678
|
-
})];
|
|
679
|
-
const calls = await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
680
|
-
return {
|
|
681
|
-
tx: await this.#multicallTx(cm, creditAccount.creditAccount, calls),
|
|
682
|
-
calls,
|
|
683
|
-
creditFacade: cm.creditFacade
|
|
684
|
-
};
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
484
|
* {@inheritDoc ICreditAccountsService.assembleStartDelayedWithdrawalCalls}
|
|
688
485
|
**/
|
|
689
486
|
assembleStartDelayedWithdrawalCalls({ creditFacade, preview }) {
|
|
@@ -700,50 +497,14 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
700
497
|
token: preview.token,
|
|
701
498
|
amount: -preview.amountIn
|
|
702
499
|
});
|
|
703
|
-
const
|
|
704
|
-
target: creditFacade,
|
|
705
|
-
callData: (0, viem.encodeFunctionData)({
|
|
706
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
707
|
-
functionName: "storeExpectedBalances",
|
|
708
|
-
args: [deltas]
|
|
709
|
-
})
|
|
710
|
-
};
|
|
711
|
-
const compareBalances = {
|
|
712
|
-
target: creditFacade,
|
|
713
|
-
callData: (0, viem.encodeFunctionData)({
|
|
714
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
715
|
-
functionName: "compareBalances",
|
|
716
|
-
args: []
|
|
717
|
-
})
|
|
718
|
-
};
|
|
500
|
+
const facade = this.sdk.marketRegister.findCreditFacade(creditFacade);
|
|
719
501
|
return [
|
|
720
|
-
|
|
502
|
+
facade.prepareStoreExpectedBalances(deltas),
|
|
721
503
|
...preview.requestCalls,
|
|
722
|
-
|
|
504
|
+
facade.prepareCompareBalances()
|
|
723
505
|
];
|
|
724
506
|
}
|
|
725
507
|
/**
|
|
726
|
-
* {@inheritDoc ICreditAccountsService.claimDelayed}
|
|
727
|
-
**/
|
|
728
|
-
async claimDelayed({ creditAccount, minQuota, averageQuota, claimableNow }) {
|
|
729
|
-
const zeroDebt = creditAccount.debt === 0n;
|
|
730
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
731
|
-
const quotaCalls = zeroDebt ? [] : this.prepareUpdateQuotas(cm.creditFacade.address, {
|
|
732
|
-
minQuota,
|
|
733
|
-
averageQuota
|
|
734
|
-
});
|
|
735
|
-
const operationCalls = [...this.assembleClaimDelayedCalls({
|
|
736
|
-
creditFacade: cm.creditFacade.address,
|
|
737
|
-
claimableNow
|
|
738
|
-
}), ...quotaCalls];
|
|
739
|
-
const calls = zeroDebt ? operationCalls : await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
740
|
-
return {
|
|
741
|
-
tx: await this.#multicallTx(cm, creditAccount.creditAccount, calls),
|
|
742
|
-
calls,
|
|
743
|
-
creditFacade: cm.creditFacade
|
|
744
|
-
};
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
508
|
* {@inheritDoc ICreditAccountsService.assembleClaimDelayedCalls}
|
|
748
509
|
**/
|
|
749
510
|
assembleClaimDelayedCalls({ creditFacade, claimableNow }) {
|
|
@@ -760,26 +521,11 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
760
521
|
token: claimableNow.withdrawalPhantomToken,
|
|
761
522
|
amount: -claimableNow.withdrawalTokenSpent
|
|
762
523
|
});
|
|
763
|
-
const
|
|
764
|
-
target: creditFacade,
|
|
765
|
-
callData: (0, viem.encodeFunctionData)({
|
|
766
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
767
|
-
functionName: "storeExpectedBalances",
|
|
768
|
-
args: [deltas]
|
|
769
|
-
})
|
|
770
|
-
};
|
|
771
|
-
const compareBalances = {
|
|
772
|
-
target: creditFacade,
|
|
773
|
-
callData: (0, viem.encodeFunctionData)({
|
|
774
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
775
|
-
functionName: "compareBalances",
|
|
776
|
-
args: []
|
|
777
|
-
})
|
|
778
|
-
};
|
|
524
|
+
const facade = this.sdk.marketRegister.findCreditFacade(creditFacade);
|
|
779
525
|
return [
|
|
780
|
-
|
|
526
|
+
facade.prepareStoreExpectedBalances(deltas),
|
|
781
527
|
...claimableNow.claimCalls,
|
|
782
|
-
|
|
528
|
+
facade.prepareCompareBalances()
|
|
783
529
|
];
|
|
784
530
|
}
|
|
785
531
|
/**
|
|
@@ -810,12 +556,13 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
810
556
|
let tokenToWithdraw;
|
|
811
557
|
if (withdrawToken === true) tokenToWithdraw = cm.underlying;
|
|
812
558
|
else if (typeof withdrawToken === "string") tokenToWithdraw = withdrawToken;
|
|
559
|
+
const { creditFacade } = cmSuite;
|
|
813
560
|
const operationCalls = [
|
|
814
|
-
|
|
815
|
-
...
|
|
561
|
+
creditFacade.prepareIncreaseDebt(debt),
|
|
562
|
+
...creditFacade.prepareAddCollateral(collateral, permits),
|
|
816
563
|
...openPathCalls,
|
|
817
|
-
...tokenToWithdraw ? [
|
|
818
|
-
...
|
|
564
|
+
...tokenToWithdraw ? [creditFacade.prepareWithdrawCollateral(tokenToWithdraw, require_sdk_constants_math.MAX_UINT256, to)] : [],
|
|
565
|
+
...creditFacade.prepareUpdateQuotas({
|
|
819
566
|
minQuota,
|
|
820
567
|
averageQuota
|
|
821
568
|
}),
|
|
@@ -826,39 +573,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
826
573
|
if (reopenCreditAccount) tx = await this.#multicallTx(cmSuite, reopenCreditAccount, calls);
|
|
827
574
|
else tx = await this.#openCreditAccountTx(cmSuite, to, calls, referralCode, rwaOptions);
|
|
828
575
|
tx.value = ethAmount.toString(10);
|
|
829
|
-
return
|
|
830
|
-
calls,
|
|
831
|
-
tx,
|
|
832
|
-
creditFacade: cmSuite.creditFacade
|
|
833
|
-
};
|
|
834
|
-
}
|
|
835
|
-
/**
|
|
836
|
-
* {@inheritDoc ICreditAccountsService.getBorrowRate}
|
|
837
|
-
**/
|
|
838
|
-
getBorrowRate(ca) {
|
|
839
|
-
const { creditManager } = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
840
|
-
const { pool } = this.sdk.marketRegister.findByCreditManager(ca.creditManager);
|
|
841
|
-
const { feeInterest } = creditManager;
|
|
842
|
-
const { baseInterestRate } = pool.pool;
|
|
843
|
-
const baseRateWithFee = baseInterestRate * (BigInt(feeInterest) + require_sdk_constants_math.PERCENTAGE_FACTOR);
|
|
844
|
-
const totalDebt = ca.debt + ca.accruedInterest + ca.accruedFees;
|
|
845
|
-
const r = ca.debt * baseRateWithFee / (totalDebt * require_sdk_constants_math.RAY);
|
|
846
|
-
const caTokens = new require_sdk_utils_AddressMap.AddressMap(ca.tokens.map((t) => [t.token, t]));
|
|
847
|
-
let qr = 0n;
|
|
848
|
-
for (const t of creditManager.collateralTokens) {
|
|
849
|
-
const b = caTokens.get(t);
|
|
850
|
-
if (b) qr += b.quota * BigInt(pool.pqk.quotas.get(t)?.rate ?? 0);
|
|
851
|
-
}
|
|
852
|
-
qr = qr * (BigInt(feeInterest) + require_sdk_constants_math.PERCENTAGE_FACTOR) / require_sdk_constants_math.PERCENTAGE_FACTOR;
|
|
853
|
-
qr /= totalDebt;
|
|
854
|
-
return r + qr;
|
|
855
|
-
}
|
|
856
|
-
/**
|
|
857
|
-
* {@inheritDoc ICreditAccountsService.getOptimalHFForPartialLiquidation}
|
|
858
|
-
**/
|
|
859
|
-
getOptimalHFForPartialLiquidation(ca) {
|
|
860
|
-
const borrowRate = this.getBorrowRate(ca);
|
|
861
|
-
return require_sdk_constants_math.PERCENTAGE_FACTOR + (borrowRate < 100n ? borrowRate : 100n);
|
|
576
|
+
return tx;
|
|
862
577
|
}
|
|
863
578
|
/**
|
|
864
579
|
* Internal wrapper for CreditAccountCompressor.getCreditAccounts + price updates wrapped into multicall
|
|
@@ -902,7 +617,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
902
617
|
* @param creditManager - Credit manager address
|
|
903
618
|
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not RWA or no adapter is configured
|
|
904
619
|
*/
|
|
905
|
-
async
|
|
620
|
+
async assembleRWAUnwrapCalls(amount, creditManager) {
|
|
906
621
|
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
907
622
|
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
908
623
|
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
@@ -929,7 +644,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
929
644
|
* @param creditManager - Credit manager address
|
|
930
645
|
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not RWA or no adapter is configured
|
|
931
646
|
*/
|
|
932
|
-
async
|
|
647
|
+
async assembleRWAWrapCalls(amount, creditManager) {
|
|
933
648
|
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
934
649
|
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
935
650
|
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
@@ -952,7 +667,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
952
667
|
* @param creditManager - Credit manager address
|
|
953
668
|
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not RWA or no adapter is configured
|
|
954
669
|
*/
|
|
955
|
-
async
|
|
670
|
+
async assembleRedeemDiffCalls(amount, creditManager) {
|
|
956
671
|
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
957
672
|
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
958
673
|
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
@@ -975,7 +690,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
975
690
|
* @param creditManager - Credit manager address
|
|
976
691
|
* @returns Array of MultiCall to pass to credit facade multicall, or undefined if underlying is not RWA or no adapter is configured
|
|
977
692
|
*/
|
|
978
|
-
async
|
|
693
|
+
async assembleDepositDiffCalls(amount, creditManager) {
|
|
979
694
|
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
980
695
|
const meta = this.sdk.tokensMeta.mustGet(suite.underlying);
|
|
981
696
|
if (!this.sdk.tokensMeta.isRWAUnderlying(meta)) return;
|
|
@@ -1010,14 +725,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1010
725
|
stateMutability: "view"
|
|
1011
726
|
}]
|
|
1012
727
|
}).read.requiredPermissions();
|
|
1013
|
-
const addBotCall =
|
|
1014
|
-
target: cm.creditFacade.address,
|
|
1015
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1016
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1017
|
-
functionName: "setBotPermissions",
|
|
1018
|
-
args: [botAddress, permissions]
|
|
1019
|
-
})
|
|
1020
|
-
};
|
|
728
|
+
const addBotCall = cm.creditFacade.prepareSetBotPermissions(botAddress, permissions);
|
|
1021
729
|
const calls = targetContract.type === "creditAccount" ? await this.#prependPriceUpdates(targetContract.creditManager, [addBotCall], targetContract) : [addBotCall];
|
|
1022
730
|
return {
|
|
1023
731
|
tx: targetContract.type === "creditAccount" ? await this.#multicallTx(cm, targetContract.creditAccount, calls) : void 0,
|
|
@@ -1026,79 +734,26 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1026
734
|
};
|
|
1027
735
|
}
|
|
1028
736
|
/**
|
|
1029
|
-
* {@inheritDoc ICreditAccountsService.withdrawCollateral}
|
|
1030
|
-
*/
|
|
1031
|
-
async withdrawCollateral({ creditAccount, assetsToWithdraw, to, minQuota, averageQuota }) {
|
|
1032
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
1033
|
-
const operationCalls = [...assetsToWithdraw.map((a) => this.prepareWithdrawToken(cm.creditFacade.address, a.token, a.balance, to)), ...this.prepareUpdateQuotas(cm.creditFacade.address, {
|
|
1034
|
-
minQuota,
|
|
1035
|
-
averageQuota
|
|
1036
|
-
})];
|
|
1037
|
-
const calls = await this.#prependPriceUpdates(creditAccount.creditManager, operationCalls, creditAccount);
|
|
1038
|
-
return {
|
|
1039
|
-
tx: await this.#multicallTx(cm, creditAccount.creditAccount, calls),
|
|
1040
|
-
calls,
|
|
1041
|
-
creditFacade: cm.creditFacade
|
|
1042
|
-
};
|
|
1043
|
-
}
|
|
1044
|
-
/**
|
|
1045
|
-
* {@inheritDoc ICreditAccountsService.repayCreditAccount}
|
|
1046
|
-
*/
|
|
1047
|
-
async repayCreditAccount(props) {
|
|
1048
|
-
const { operation, creditAccount: ca } = props;
|
|
1049
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1050
|
-
const operationCalls = await this.assembleRepayCreditAccountCalls(props);
|
|
1051
|
-
const calls = operation === "close" ? operationCalls : await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
1052
|
-
return {
|
|
1053
|
-
tx: await this.#closeCreditAccountTx(cm, ca.creditAccount, calls, operation),
|
|
1054
|
-
calls,
|
|
1055
|
-
creditFacade: cm.creditFacade
|
|
1056
|
-
};
|
|
1057
|
-
}
|
|
1058
|
-
/**
|
|
1059
737
|
* {@inheritDoc ICreditAccountsService.assembleRepayCreditAccountCalls}
|
|
1060
738
|
*/
|
|
1061
739
|
async assembleRepayCreditAccountCalls({ collateralAssets, assetsToWithdraw, creditAccount: ca, permits, to, tokensToClaim, calls: wrapCalls = [] }) {
|
|
1062
740
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
1063
741
|
const router = this.sdk.routerFor(ca);
|
|
1064
|
-
const unwrapCalls = await this.
|
|
742
|
+
const unwrapCalls = await this.assembleRedeemDiffCalls(1n, ca.creditManager) ?? [];
|
|
1065
743
|
const claimPath = await router.findClaimAllRewards({
|
|
1066
744
|
tokensToClaim,
|
|
1067
745
|
creditAccount: ca
|
|
1068
746
|
});
|
|
747
|
+
const { creditFacade } = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1069
748
|
return [
|
|
1070
|
-
...
|
|
749
|
+
...creditFacade.prepareAddCollateral(addCollateral, permits),
|
|
1071
750
|
...wrapCalls,
|
|
1072
|
-
...
|
|
1073
|
-
...
|
|
751
|
+
...creditFacade.prepareDisableQuotas([...ca.tokens]),
|
|
752
|
+
...ca.debt > 0n ? [creditFacade.prepareDecreaseDebtFull()] : [],
|
|
1074
753
|
...unwrapCalls,
|
|
1075
754
|
...claimPath.calls,
|
|
1076
|
-
...assetsToWithdraw.map((t) =>
|
|
1077
|
-
];
|
|
1078
|
-
}
|
|
1079
|
-
/**
|
|
1080
|
-
* {@inheritDoc ICreditAccountsService.repayAndLiquidateCreditAccount}
|
|
1081
|
-
*/
|
|
1082
|
-
async repayAndLiquidateCreditAccount({ collateralAssets, assetsToWithdraw, creditAccount: ca, permits, to, tokensToClaim }) {
|
|
1083
|
-
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
|
|
1084
|
-
const claimPath = await this.sdk.routerFor(ca).findClaimAllRewards({
|
|
1085
|
-
tokensToClaim,
|
|
1086
|
-
creditAccount: ca
|
|
1087
|
-
});
|
|
1088
|
-
const wrapCalls = await this.getDepositDiffCalls(1n, ca.creditManager) ?? [];
|
|
1089
|
-
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
1090
|
-
const operationCalls = [
|
|
1091
|
-
...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
1092
|
-
...claimPath.calls,
|
|
1093
|
-
...wrapCalls,
|
|
1094
|
-
...assetsToWithdraw.map((t) => this.prepareWithdrawToken(ca.creditFacade, t.token, require_sdk_constants_math.MAX_UINT256, to))
|
|
755
|
+
...assetsToWithdraw.map((t) => creditFacade.prepareWithdrawCollateral(t.token, require_sdk_constants_math.MAX_UINT256, to))
|
|
1095
756
|
];
|
|
1096
|
-
const calls = await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
1097
|
-
return {
|
|
1098
|
-
tx: cm.creditFacade.liquidateCreditAccount(ca.creditAccount, to, calls),
|
|
1099
|
-
calls,
|
|
1100
|
-
creditFacade: cm.creditFacade
|
|
1101
|
-
};
|
|
1102
757
|
}
|
|
1103
758
|
/**
|
|
1104
759
|
* {@inheritDoc ICreditAccountsService.claimFarmRewards}
|
|
@@ -1113,16 +768,12 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1113
768
|
creditAccount: ca
|
|
1114
769
|
});
|
|
1115
770
|
if (claimPath.calls.length === 0) throw new Error("No path to execute");
|
|
1116
|
-
const operationCalls = [...claimPath.calls, ...
|
|
771
|
+
const operationCalls = [...claimPath.calls, ...cm.creditFacade.prepareUpdateQuotas({
|
|
1117
772
|
minQuota,
|
|
1118
773
|
averageQuota
|
|
1119
774
|
})];
|
|
1120
775
|
const calls = await this.#prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
1121
|
-
return
|
|
1122
|
-
tx: await this.#multicallTx(cm, ca.creditAccount, calls),
|
|
1123
|
-
calls,
|
|
1124
|
-
creditFacade: cm.creditFacade
|
|
1125
|
-
};
|
|
776
|
+
return await this.#multicallTx(cm, ca.creditAccount, calls);
|
|
1126
777
|
}
|
|
1127
778
|
/**
|
|
1128
779
|
* Returns raw txs that are needed to update all price feeds so that all credit accounts (possibly from different markets) compute
|
|
@@ -1152,7 +803,8 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1152
803
|
*/
|
|
1153
804
|
async #prependPriceUpdates(creditManager, calls, ca, options) {
|
|
1154
805
|
const market = this.sdk.marketRegister.findByCreditManager(creditManager);
|
|
1155
|
-
const
|
|
806
|
+
const suite = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
807
|
+
const cm = suite.creditManager;
|
|
1156
808
|
const { priceUpdates: existingUpdates, remainingCalls } = require_sdk_accounts_multicall_utils.extractPriceUpdates(calls);
|
|
1157
809
|
const tokens = new require_sdk_utils_AddressSet.AddressSet([cm.underlying, ...require_sdk_accounts_multicall_utils.extractQuotaTokens(calls)]);
|
|
1158
810
|
if (ca) for (const t of ca.tokens) {
|
|
@@ -1173,14 +825,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1173
825
|
const generatedUpdates = await this.sdk.priceFeeds.generatePriceFeedsUpdates(priceFeeds);
|
|
1174
826
|
const merged = require_sdk_accounts_multicall_utils.mergePriceUpdates(existingUpdates, generatedUpdates);
|
|
1175
827
|
if (merged.length === 0) return remainingCalls;
|
|
1176
|
-
return [
|
|
1177
|
-
target: cm.creditFacade,
|
|
1178
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1179
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1180
|
-
functionName: "onDemandPriceUpdates",
|
|
1181
|
-
args: [merged]
|
|
1182
|
-
})
|
|
1183
|
-
}, ...remainingCalls];
|
|
828
|
+
return [suite.creditFacade.prepareOnDemandPriceUpdates(merged), ...remainingCalls];
|
|
1184
829
|
}
|
|
1185
830
|
async #getUpdateForAccount(account, ignoreReservePrices) {
|
|
1186
831
|
const { creditManager, creditAccount, enabledTokensMask } = account;
|
|
@@ -1204,22 +849,6 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1204
849
|
return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(priceFeeds);
|
|
1205
850
|
}
|
|
1206
851
|
/**
|
|
1207
|
-
* {@inheritDoc ICreditAccountsService.multicall}
|
|
1208
|
-
*/
|
|
1209
|
-
async multicall(creditAccount, calls, options) {
|
|
1210
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
1211
|
-
const callsWithPrices = await this.#prependPriceUpdates(creditAccount.creditManager, calls, creditAccount, options);
|
|
1212
|
-
return cm.creditFacade.multicall(creditAccount.creditAccount, callsWithPrices);
|
|
1213
|
-
}
|
|
1214
|
-
/**
|
|
1215
|
-
* {@inheritDoc ICreditAccountsService.botMulticall}
|
|
1216
|
-
*/
|
|
1217
|
-
async botMulticall(creditAccount, calls, options) {
|
|
1218
|
-
const cm = this.sdk.marketRegister.findCreditManager(creditAccount.creditManager);
|
|
1219
|
-
const callsWithPrices = await this.#prependPriceUpdates(creditAccount.creditManager, calls, creditAccount, options);
|
|
1220
|
-
return cm.creditFacade.botMulticall(creditAccount.creditAccount, callsWithPrices);
|
|
1221
|
-
}
|
|
1222
|
-
/**
|
|
1223
852
|
* {@inheritDoc ICreditAccountsService.prependPriceUpdates}
|
|
1224
853
|
*/
|
|
1225
854
|
async prependPriceUpdates(creditManager, calls, creditAccount, options) {
|
|
@@ -1229,22 +858,23 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1229
858
|
* {@inheritDoc ICreditAccountsService.assembleCaOperations}
|
|
1230
859
|
*/
|
|
1231
860
|
assembleCaOperations({ operations, creditFacade }) {
|
|
861
|
+
const facade = this.sdk.marketRegister.findCreditFacade(creditFacade);
|
|
1232
862
|
const calls = [];
|
|
1233
863
|
for (const op of operations) switch (op.type) {
|
|
1234
864
|
case "increaseDebt":
|
|
1235
|
-
calls.push(
|
|
865
|
+
calls.push(facade.prepareIncreaseDebt(op.amount));
|
|
1236
866
|
break;
|
|
1237
867
|
case "decreaseDebt":
|
|
1238
|
-
calls.push(
|
|
868
|
+
calls.push(facade.prepareChangeDebt(op.amount, true));
|
|
1239
869
|
break;
|
|
1240
870
|
case "addCollateral":
|
|
1241
|
-
calls.push(...
|
|
871
|
+
calls.push(...facade.prepareAddCollateral([{
|
|
1242
872
|
token: op.token,
|
|
1243
873
|
balance: op.amount
|
|
1244
874
|
}], {}));
|
|
1245
875
|
break;
|
|
1246
876
|
case "withdrawCollateral":
|
|
1247
|
-
calls.push(
|
|
877
|
+
calls.push(facade.prepareWithdrawCollateral(op.token, op.amount, op.to));
|
|
1248
878
|
break;
|
|
1249
879
|
case "swap":
|
|
1250
880
|
case "wrapRwaCollateral":
|
|
@@ -1254,7 +884,7 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1254
884
|
case "changeQuota": {
|
|
1255
885
|
const quotaAssets = [...op.quotaIncrease, ...op.quotaDecrease];
|
|
1256
886
|
if (quotaAssets.length === 0) break;
|
|
1257
|
-
calls.push(...
|
|
887
|
+
calls.push(...facade.prepareUpdateQuotas({
|
|
1258
888
|
averageQuota: quotaAssets,
|
|
1259
889
|
minQuota: quotaAssets
|
|
1260
890
|
}));
|
|
@@ -1272,133 +902,37 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1272
902
|
const callsWithPrices = await this.#prependPriceUpdates(creditAccount.creditManager, calls, creditAccount, { ignoreReservePrices: options?.ignoreReservePrices });
|
|
1273
903
|
const tx = await this.#multicallTx(cm, creditAccount.creditAccount, callsWithPrices);
|
|
1274
904
|
if (options?.ethAmount && options.ethAmount > 0n) tx.value = options.ethAmount.toString(10);
|
|
1275
|
-
return
|
|
1276
|
-
tx,
|
|
1277
|
-
calls: callsWithPrices
|
|
1278
|
-
};
|
|
1279
|
-
}
|
|
1280
|
-
#prepareDisableQuotas(ca) {
|
|
1281
|
-
const calls = [];
|
|
1282
|
-
for (const { token, quota } of ca.tokens) if (quota > 0n) calls.push({
|
|
1283
|
-
target: ca.creditFacade,
|
|
1284
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1285
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1286
|
-
functionName: "updateQuota",
|
|
1287
|
-
args: [
|
|
1288
|
-
token,
|
|
1289
|
-
require_sdk_constants_math.MIN_INT96,
|
|
1290
|
-
0n
|
|
1291
|
-
]
|
|
1292
|
-
})
|
|
1293
|
-
});
|
|
1294
|
-
return calls;
|
|
905
|
+
return tx;
|
|
1295
906
|
}
|
|
1296
907
|
/**
|
|
1297
908
|
* {@inheritDoc ICreditAccountsService.prepareUpdateQuotas}
|
|
1298
909
|
*/
|
|
1299
|
-
prepareUpdateQuotas(creditFacade,
|
|
1300
|
-
|
|
1301
|
-
return averageQuota.map((q) => {
|
|
1302
|
-
const minBalance = minRecord.get(q.token);
|
|
1303
|
-
const min = minBalance && minBalance > 0n ? minBalance : 0n;
|
|
1304
|
-
return {
|
|
1305
|
-
target: creditFacade,
|
|
1306
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1307
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1308
|
-
functionName: "updateQuota",
|
|
1309
|
-
args: [
|
|
1310
|
-
q.token,
|
|
1311
|
-
q.balance,
|
|
1312
|
-
min
|
|
1313
|
-
]
|
|
1314
|
-
})
|
|
1315
|
-
};
|
|
1316
|
-
});
|
|
1317
|
-
}
|
|
1318
|
-
#prepareDecreaseDebt(ca) {
|
|
1319
|
-
if (ca.debt > 0n) return [{
|
|
1320
|
-
target: ca.creditFacade,
|
|
1321
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1322
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1323
|
-
functionName: "decreaseDebt",
|
|
1324
|
-
args: [require_sdk_constants_math.MAX_UINT256]
|
|
1325
|
-
})
|
|
1326
|
-
}];
|
|
1327
|
-
return [];
|
|
910
|
+
prepareUpdateQuotas(creditFacade, props) {
|
|
911
|
+
return this.sdk.marketRegister.findCreditFacade(creditFacade).prepareUpdateQuotas(props);
|
|
1328
912
|
}
|
|
1329
913
|
/**
|
|
1330
914
|
* {@inheritDoc ICreditAccountsService.prepareWithdrawToken}
|
|
1331
915
|
*/
|
|
1332
916
|
prepareWithdrawToken(creditFacade, token, amount, to) {
|
|
1333
|
-
return
|
|
1334
|
-
target: creditFacade,
|
|
1335
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1336
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1337
|
-
functionName: "withdrawCollateral",
|
|
1338
|
-
args: [
|
|
1339
|
-
token,
|
|
1340
|
-
amount,
|
|
1341
|
-
to
|
|
1342
|
-
]
|
|
1343
|
-
})
|
|
1344
|
-
};
|
|
917
|
+
return this.sdk.marketRegister.findCreditFacade(creditFacade).prepareWithdrawCollateral(token, amount, to);
|
|
1345
918
|
}
|
|
1346
919
|
/**
|
|
1347
920
|
* {@inheritDoc ICreditAccountsService.prepareIncreaseDebt}
|
|
1348
921
|
*/
|
|
1349
922
|
prepareIncreaseDebt(creditFacade, debt) {
|
|
1350
|
-
return
|
|
1351
|
-
target: creditFacade,
|
|
1352
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1353
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1354
|
-
functionName: "increaseDebt",
|
|
1355
|
-
args: [debt]
|
|
1356
|
-
})
|
|
1357
|
-
};
|
|
923
|
+
return this.sdk.marketRegister.findCreditFacade(creditFacade).prepareIncreaseDebt(debt);
|
|
1358
924
|
}
|
|
1359
925
|
/**
|
|
1360
926
|
* {@inheritDoc ICreditAccountsService.prepareChangeDebt}
|
|
1361
927
|
*/
|
|
1362
928
|
prepareChangeDebt(creditFacade, change, isDecrease) {
|
|
1363
|
-
return
|
|
1364
|
-
target: creditFacade,
|
|
1365
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1366
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1367
|
-
functionName: isDecrease ? "decreaseDebt" : "increaseDebt",
|
|
1368
|
-
args: [change]
|
|
1369
|
-
})
|
|
1370
|
-
};
|
|
929
|
+
return this.sdk.marketRegister.findCreditFacade(creditFacade).prepareChangeDebt(change, isDecrease);
|
|
1371
930
|
}
|
|
1372
931
|
/**
|
|
1373
932
|
* {@inheritDoc ICreditAccountsService.prepareAddCollateral}
|
|
1374
933
|
*/
|
|
1375
934
|
prepareAddCollateral(creditFacade, assets, permits) {
|
|
1376
|
-
return
|
|
1377
|
-
const p = permits[token];
|
|
1378
|
-
if (p) return {
|
|
1379
|
-
target: creditFacade,
|
|
1380
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1381
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1382
|
-
functionName: "addCollateralWithPermit",
|
|
1383
|
-
args: [
|
|
1384
|
-
token,
|
|
1385
|
-
balance,
|
|
1386
|
-
p.deadline,
|
|
1387
|
-
p.v,
|
|
1388
|
-
p.r,
|
|
1389
|
-
p.s
|
|
1390
|
-
]
|
|
1391
|
-
})
|
|
1392
|
-
};
|
|
1393
|
-
return {
|
|
1394
|
-
target: creditFacade,
|
|
1395
|
-
callData: (0, viem.encodeFunctionData)({
|
|
1396
|
-
abi: require_abi_310_generated.iCreditFacadeMulticallV310Abi,
|
|
1397
|
-
functionName: "addCollateral",
|
|
1398
|
-
args: [token, balance]
|
|
1399
|
-
})
|
|
1400
|
-
};
|
|
1401
|
-
});
|
|
935
|
+
return this.sdk.marketRegister.findCreditFacade(creditFacade).prepareAddCollateral(assets, permits);
|
|
1402
936
|
}
|
|
1403
937
|
/**
|
|
1404
938
|
* Returns addresses of market configurators
|
|
@@ -1447,24 +981,6 @@ var CreditAccountsServiceV310 = class extends require_sdk_base_SDKConstruct.SDKC
|
|
|
1447
981
|
return suite.creditFacade.multicall(creditAccount, calls);
|
|
1448
982
|
}
|
|
1449
983
|
/**
|
|
1450
|
-
* Wrapper that selects between credit facade and RWA factory
|
|
1451
|
-
* @param suite
|
|
1452
|
-
* @param creditAccount
|
|
1453
|
-
* @param calls
|
|
1454
|
-
* @param operation
|
|
1455
|
-
* @param rwaOptions
|
|
1456
|
-
* @returns
|
|
1457
|
-
*/
|
|
1458
|
-
async #closeCreditAccountTx(suite, creditAccount, calls, operation, rwaOptions) {
|
|
1459
|
-
const factory = this.sdk.marketRegister.findByCreditManager(suite.creditManager.address).rwaFactory;
|
|
1460
|
-
if (operation === "close") {
|
|
1461
|
-
if (factory) throw new Error("CloseOptions=close is not supported for RWA underlying credit accounts");
|
|
1462
|
-
return suite.creditFacade.closeCreditAccount(creditAccount, calls);
|
|
1463
|
-
}
|
|
1464
|
-
if (factory) return factory.multicall(creditAccount, calls, rwaOptions);
|
|
1465
|
-
return suite.creditFacade.multicall(creditAccount, calls);
|
|
1466
|
-
}
|
|
1467
|
-
/**
|
|
1468
984
|
* Withdrawal compressor of the current chain.
|
|
1469
985
|
* @throws If no withdrawal compressor is supported on the current chain.
|
|
1470
986
|
**/
|