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