@gearbox-protocol/sdk 13.4.0-beta.1 → 13.4.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/cjs/permissionless/utils/price-update/get-price-update-tx.js +3 -15
  2. package/dist/cjs/sdk/GearboxSDK.js +135 -20
  3. package/dist/cjs/sdk/accounts/AbstractCreditAccountsService.js +66 -187
  4. package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +5 -5
  5. package/dist/cjs/sdk/base/BaseContract.js +26 -6
  6. package/dist/cjs/sdk/base/ChainContractsRegister.js +39 -2
  7. package/dist/cjs/sdk/base/Construct.js +15 -3
  8. package/dist/cjs/sdk/base/TokensMeta.js +23 -0
  9. package/dist/cjs/sdk/constants/address-provider.js +0 -22
  10. package/dist/cjs/sdk/core/AbstractAddressProviderContract.js +15 -0
  11. package/dist/cjs/sdk/market/MarketRegister.js +74 -3
  12. package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
  13. package/dist/cjs/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
  14. package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
  15. package/dist/cjs/sdk/options.js +30 -24
  16. package/dist/cjs/sdk/plugins/BasePlugin.js +24 -0
  17. package/dist/cjs/sdk/pools/AbstractPoolService.js +6 -0
  18. package/dist/cjs/sdk/router/AbstractRouterContract.js +4 -1
  19. package/dist/cjs/sdk/router/RouterV310Contract.js +20 -15
  20. package/dist/cjs/sdk/utils/AddressMap.js +53 -17
  21. package/dist/cjs/sdk/utils/AddressSet.js +9 -0
  22. package/dist/esm/permissionless/utils/price-update/get-price-update-tx.js +6 -20
  23. package/dist/esm/sdk/GearboxSDK.js +135 -20
  24. package/dist/esm/sdk/accounts/AbstractCreditAccountsService.js +69 -188
  25. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +5 -5
  26. package/dist/esm/sdk/base/BaseContract.js +26 -6
  27. package/dist/esm/sdk/base/ChainContractsRegister.js +39 -2
  28. package/dist/esm/sdk/base/Construct.js +15 -3
  29. package/dist/esm/sdk/base/TokensMeta.js +23 -0
  30. package/dist/esm/sdk/constants/address-provider.js +0 -21
  31. package/dist/esm/sdk/core/AbstractAddressProviderContract.js +15 -0
  32. package/dist/esm/sdk/market/MarketRegister.js +74 -3
  33. package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
  34. package/dist/esm/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
  35. package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
  36. package/dist/esm/sdk/options.js +30 -24
  37. package/dist/esm/sdk/plugins/BasePlugin.js +24 -0
  38. package/dist/esm/sdk/pools/AbstractPoolService.js +6 -0
  39. package/dist/esm/sdk/router/AbstractRouterContract.js +4 -1
  40. package/dist/esm/sdk/router/RouterV310Contract.js +20 -15
  41. package/dist/esm/sdk/utils/AddressMap.js +53 -17
  42. package/dist/esm/sdk/utils/AddressSet.js +9 -0
  43. package/dist/types/permissionless/bindings/price-feed-store.d.ts +1 -2
  44. package/dist/types/permissionless/bindings/types.d.ts +0 -4
  45. package/dist/types/permissionless/utils/price-update/get-price-update-tx.d.ts +1 -3
  46. package/dist/types/sdk/GearboxSDK.d.ts +236 -34
  47. package/dist/types/sdk/accounts/AbstractCreditAccountsService.d.ts +55 -132
  48. package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +14 -5
  49. package/dist/types/sdk/accounts/multicall-utils.d.ts +3 -3
  50. package/dist/types/sdk/accounts/types.d.ts +215 -28
  51. package/dist/types/sdk/base/BaseContract.d.ts +67 -6
  52. package/dist/types/sdk/base/ChainContractsRegister.d.ts +51 -2
  53. package/dist/types/sdk/base/Construct.d.ts +31 -0
  54. package/dist/types/sdk/base/PlaceholderContract.d.ts +3 -0
  55. package/dist/types/sdk/base/SDKConstruct.d.ts +10 -0
  56. package/dist/types/sdk/base/TokensMeta.d.ts +59 -2
  57. package/dist/types/sdk/base/types.d.ts +185 -25
  58. package/dist/types/sdk/chain/chains.d.ts +78 -18
  59. package/dist/types/sdk/chain/detectNetwork.d.ts +7 -0
  60. package/dist/types/sdk/constants/address-provider.d.ts +4 -3
  61. package/dist/types/sdk/core/AbstractAddressProviderContract.d.ts +23 -0
  62. package/dist/types/sdk/core/types.d.ts +46 -0
  63. package/dist/types/sdk/market/MarketRegister.d.ts +81 -0
  64. package/dist/types/sdk/market/adapters/PlaceholderAdapterContracts.d.ts +3 -0
  65. package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +40 -50
  66. package/dist/types/sdk/market/oracle/types.d.ts +78 -59
  67. package/dist/types/sdk/market/pricefeeds/AbstractLPPriceFeed.d.ts +3 -0
  68. package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +3 -0
  69. package/dist/types/sdk/market/pricefeeds/PriceFeedRef.d.ts +22 -2
  70. package/dist/types/sdk/market/pricefeeds/PriceFeedsRegister.d.ts +80 -16
  71. package/dist/types/sdk/market/pricefeeds/getRawPriceUpdates.d.ts +2 -2
  72. package/dist/types/sdk/market/pricefeeds/types.d.ts +75 -11
  73. package/dist/types/sdk/options.d.ts +13 -4
  74. package/dist/types/sdk/plugins/BasePlugin.d.ts +39 -0
  75. package/dist/types/sdk/plugins/types.d.ts +73 -43
  76. package/dist/types/sdk/pools/AbstractPoolService.d.ts +12 -0
  77. package/dist/types/sdk/pools/types.d.ts +75 -6
  78. package/dist/types/sdk/router/AbstractRouterContract.d.ts +21 -2
  79. package/dist/types/sdk/router/RouterV310Contract.d.ts +27 -15
  80. package/dist/types/sdk/router/types.d.ts +47 -70
  81. package/dist/types/sdk/types/state.d.ts +32 -3
  82. package/dist/types/sdk/utils/AddressMap.d.ts +61 -17
  83. package/dist/types/sdk/utils/AddressSet.d.ts +15 -0
  84. package/package.json +3 -2
@@ -32,6 +32,7 @@ var import_iBaseRewardPool = require("../../abi/iBaseRewardPool.js");
32
32
  var import_base = require("../base/index.js");
33
33
  var import_chains = require("../chain/chains.js");
34
34
  var import_constants = require("../constants/index.js");
35
+ var import_market = require("../market/index.js");
35
36
  var import_router = require("../router/index.js");
36
37
  var import_utils = require("../utils/index.js");
37
38
  var import_viem2 = require("../utils/viem/index.js");
@@ -58,12 +59,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
58
59
  );
59
60
  }
60
61
  /**
61
- * Returns single credit account data, or undefined if it's not found
62
- * Performs all necessary price feed updates under the hood
63
- * @param account
64
- * @param blockNumber
65
- * @returns
66
- */
62
+ * {@inheritDoc ICreditAccountsService.getCreditAccountData}
63
+ **/
67
64
  async getCreditAccountData(account, blockNumber) {
68
65
  let raw;
69
66
  try {
@@ -82,10 +79,7 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
82
79
  if (raw.success) {
83
80
  return raw;
84
81
  }
85
- const { txs: priceUpdateTxs } = await this.getUpdateForAccount({
86
- creditManager: raw.creditManager,
87
- creditAccount: raw
88
- });
82
+ const { txs: priceUpdateTxs } = await this.getUpdateForAccount(raw);
89
83
  const [cad] = await (0, import_viem2.simulateWithPriceUpdates)(this.client, {
90
84
  priceUpdates: priceUpdateTxs,
91
85
  contracts: [
@@ -102,13 +96,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
102
96
  return cad;
103
97
  }
104
98
  /**
105
- * Methods to get all credit accounts with some optional filtering
106
- * Performs all necessary price feed updates under the hood
107
- *
108
- * @param options
109
- * @param blockNumber
110
- * @returns returned credit accounts are sorted by health factor in ascending order
111
- */
99
+ * {@inheritDoc ICreditAccountsService.getCreditAccounts}
100
+ **/
112
101
  async getCreditAccounts(options, blockNumber) {
113
102
  const {
114
103
  creditManager,
@@ -161,11 +150,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
161
150
  return allCAs.sort((a, b) => Number(a.healthFactor - b.healthFactor));
162
151
  }
163
152
  /**
164
- * Method to get all claimable rewards for credit account (ex. stkUSDS SKY rewards)
165
- Assosiates rewards by adapter + stakedPhantomToken
166
- * @param {Address} creditAccount - address of credit account to get rewards for
167
- * @returns {Array<Rewards>} list of {@link Rewards} that can be claimed
168
- */
153
+ * {@inheritDoc ICreditAccountsService.getRewards}
154
+ **/
169
155
  async getRewards(creditAccount) {
170
156
  const rewards = await this.client.readContract({
171
157
  abi: import_rewardsCompressor.rewardsCompressorAbi,
@@ -205,11 +191,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
205
191
  return Object.values(r);
206
192
  }
207
193
  /**
208
- * Method to get all connected bots for credit account
209
- * @param {Array<AccountToCheck>} accountsToCheck - list of credit accounts
210
- and their credit managers to check connected bots on
211
- * @returns call result of getConnectedBots for each credit account
212
- */
194
+ * {@inheritDoc ICreditAccountsService.getConnectedBots}
195
+ **/
213
196
  async getConnectedBots(accountsToCheck, legacyMigrationBot, additionalBots) {
214
197
  const allResp = await this.client.multicall({
215
198
  contracts: [
@@ -313,10 +296,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
313
296
  return void 0;
314
297
  }
315
298
  /**
316
- * Generates transaction to liquidate credit account
317
- * @param props - {@link FullyLiquidateProps}
318
- * @returns
319
- */
299
+ * {@inheritDoc ICreditAccountsService.fullyLiquidate}
300
+ **/
320
301
  async fullyLiquidate(props) {
321
302
  const {
322
303
  account,
@@ -366,21 +347,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
366
347
  };
367
348
  }
368
349
  /**
369
- * Closes credit account or closes credit account and keeps it open with zero debt.
370
- - Ca is closed in the following order: price update -> close path to swap all tokens into underlying ->
371
- -> disable quotas of exiting tokens -> decrease debt -> disable exiting tokens tokens -> withdraw underlying tokenz
372
- * @param {CloseOptions} operation - {@link CloseOptions}: close or zeroDebt
373
- * @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
374
- * @param {Array<Address>} assetsToWithdraw - tokens to withdraw from credit account.
375
- For credit account closing this is the underlying token, because during the closure,
376
- all tokens on account are swapped into the underlying,
377
- and only the underlying token will remain on the credit account
378
- * @param {Address} to - Wallet address to withdraw underlying to
379
- * @param {number} slippage - Slippage in PERCENTAGE_FORMAT (100% = 10_000) per operation
380
- * @default 50n
381
- * @param {RouterCloseResult | undefined} closePath - result of findBestClosePath method from router; if omited, calls marketRegister.findCreditManager {@link RouterCloseResult}
382
- * @returns All necessary data to execute the transaction (call, credit facade)
383
- */
350
+ * {@inheritDoc ICreditAccountsService.closeCreditAccount}
351
+ **/
384
352
  async closeCreditAccount({
385
353
  operation,
386
354
  assetsToWithdraw,
@@ -408,13 +376,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
408
376
  return { tx, calls, routerCloseResult, creditFacade: cm.creditFacade };
409
377
  }
410
378
  /**
411
- * Updates quota of credit account.
412
- - CA quota updated in the following order: price update -> update quotas
413
- * @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
414
- * @param {Array<Asset>} averageQuota - average quota for desired tokens {@link Asset}
415
- * @param {Array<Asset>} minQuota - minimum quota for desired tokens {@link Asset}
416
- * @returns All necessary data to execute the transaction (call, credit facade)
417
- */
379
+ * {@inheritDoc ICreditAccountsService.updateQuotas}
380
+ **/
418
381
  async updateQuotas({
419
382
  minQuota,
420
383
  averageQuota,
@@ -436,16 +399,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
436
399
  return { tx, calls, creditFacade: cm.creditFacade };
437
400
  }
438
401
  /**
439
- * Adds a single collateral to credit account and updates quotas
440
- - Collateral is added in the following order: price update -> add collateral (with permit) -> update quotas
441
- * @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
442
- * @param {Array<Asset>} averageQuota - average quota for desired token {@link Asset}
443
- * @param {Array<Asset>} minQuota - minimum quota for desired token {@link Asset}
444
- * @param {Asset} asset - asset to add as collateral {@link Asset}
445
- * @param {PermitResult | undefined} permits - permits of collateral asset if it is permittable {@link PermitResult}
446
- * @param {bigint} ethAmount - native token amount to attach to tx
447
- * @returns All necessary data to execute the transaction (call, credit facade)
448
- */
402
+ * {@inheritDoc ICreditAccountsService.addCollateral}
403
+ **/
449
404
  async addCollateral({
450
405
  creditAccount,
451
406
  asset,
@@ -478,15 +433,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
478
433
  return { tx, calls, creditFacade: cm.creditFacade };
479
434
  }
480
435
  /**
481
- * Increases or decreases debt of credit account; debt decrease uses token ON CREDIT ACCOUNT
482
- - Debt is changed in the following order: price update -> (enables underlying if it was disabled) -> change debt
483
- * @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
484
- * @param {bigint} amount - amount to change debt by;
485
- 0 - prohibited value;
486
- negative value for debt decrease;
487
- positive value for debt increase.
488
- * @returns All necessary data to execute the transaction (call, credit facade)
489
- */
436
+ * {@inheritDoc ICreditAccountsService.changeDebt}
437
+ **/
490
438
  async changeDebt({
491
439
  creditAccount,
492
440
  amount,
@@ -523,14 +471,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
523
471
  return { tx, calls, creditFacade: cm.creditFacade };
524
472
  }
525
473
  /**
526
- * Executes swap specified by given calls, update quotas of affected tokens
527
- - Swap is executed in the following order: price update -> execute swap path -> update quotas
528
- * @param {RouterCASlice} creditAccount - minimal credit account data {@link RouterCASlice} on which operation is performed
529
- * @param {Array<Asset>} averageQuota - average quota for desired token {@link Asset}
530
- * @param {Array<Asset>} minQuota - minimum quota for desired token {@link Asset}
531
- * @param {Array<MultiCall>} calls - array of MultiCall from router methods getSingleSwap or getAllSwaps {@link MultiCall}
532
- * @returns All necessary data to execute the transaction (call, credit facade)
533
- */
474
+ * {@inheritDoc ICreditAccountsService.executeSwap}
475
+ **/
534
476
  async executeSwap({
535
477
  creditAccount,
536
478
  calls: swapCalls,
@@ -557,10 +499,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
557
499
  return { tx, calls, creditFacade: cm.creditFacade };
558
500
  }
559
501
  /**
560
- * Preview delayed withdrawal for given token
561
- * @param props - {@link PreviewDelayedWithdrawalProps}
562
- * @returns
563
- */
502
+ * {@inheritDoc ICreditAccountsService.previewDelayedWithdrawal}
503
+ **/
564
504
  async previewDelayedWithdrawal({
565
505
  creditAccount,
566
506
  amount,
@@ -584,10 +524,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
584
524
  return resp;
585
525
  }
586
526
  /**
587
- * Get claimable and pending withdrawals of an account
588
- * @param props - {@link GetPendingWithdrawalsProps}
589
- * @returns
590
- */
527
+ * {@inheritDoc ICreditAccountsService.getPendingWithdrawals}
528
+ **/
591
529
  async getPendingWithdrawals({
592
530
  creditAccount
593
531
  }) {
@@ -613,11 +551,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
613
551
  return respResult;
614
552
  }
615
553
  /**
616
- * Start delayed withdrawal for given token
617
- - Withdrawal is executed in the following order: price update -> execute withdraw calls -> update quotas
618
- * @param props - {@link StartDelayedWithdrawalProps}
619
- * @returns
620
- */
554
+ * {@inheritDoc ICreditAccountsService.startDelayedWithdrawal}
555
+ **/
621
556
  async startDelayedWithdrawal({
622
557
  creditAccount,
623
558
  minQuota,
@@ -672,11 +607,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
672
607
  return { tx, calls, creditFacade: cm.creditFacade };
673
608
  }
674
609
  /**
675
- * Claim tokens with delayed withdrawal
676
- - Claim is executed in the following order: price update -> execute claim calls -> update quotas
677
- * @param props - {@link ClaimDelayedProps}
678
- * @returns
679
- */
610
+ * {@inheritDoc ICreditAccountsService.claimDelayed}
611
+ **/
680
612
  async claimDelayed({
681
613
  creditAccount,
682
614
  minQuota,
@@ -736,29 +668,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
736
668
  return { tx, calls, creditFacade: cm.creditFacade };
737
669
  }
738
670
  /**
739
- * Executes swap specified by given calls, update quotas of affected tokens
740
- - Open credit account is executed in the following order: price update -> increase debt -> add collateral ->
741
- -> update quotas -> (optionally: execute swap path for trading/strategy) ->
742
- -> (optionally: withdraw debt for lending)
743
- - Basic open credit account: price update -> increase debt -> add collateral -> update quotas
744
- - Lending: price update -> increase debt -> add collateral -> update quotas -> withdraw debt
745
- - Strategy/trading: price update -> increase debt -> add collateral -> update quotas -> execute swap path
746
- - In strategy is possible situation when collateral is added, but not swapped; the only swapped value in this case will be debt
747
- * @param {bigint} ethAmount - native token amount to attach to tx
748
- * @param {Address} creditManager - address of credit manager to open credit account on
749
- * @param {Array<Asset>} collateral - array of collateral which can be just directly added or swapped using the path {@link Asset}
750
- * @param {Record<Address, PermitResult>} permits - permits of collateral tokens (in any permittable token is present) {@link PermitResult}
751
- * @param {bigint} debt - debt to open credit account with
752
- * @param {boolean} withdrawDebt - flag to withdraw debt to wallet after opening credit account;
753
- used for borrowing functionality
754
- * @param {bigint} referralCode - referral code to open credit account with
755
- * @param {Address} to - wallet address to transfer credit account to\
756
- * @param {Array<MultiCall>} calls - array of MultiCall from router methods findOpenStrategyPath {@link MultiCall}.
757
- Used for trading and strategy functionality
758
- * @param {Array<Asset>} averageQuota - average quota for tokens after open {@link Asset}
759
- * @param {Array<Asset>} minQuota - minimum quota for tokens after open {@link Asset}
760
- * @returns All necessary data to execute the transaction (call, credit facade)
761
- */
671
+ * {@inheritDoc ICreditAccountsService.openCA}
672
+ **/
762
673
  async openCA({
763
674
  ethAmount,
764
675
  creditManager,
@@ -790,10 +701,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
790
701
  return { calls, tx, creditFacade: cmSuite.creditFacade };
791
702
  }
792
703
  /**
793
- * Returns borrow rate with 4 digits precision (10000 = 100%)
794
- * @param ca
795
- * @returns
796
- */
704
+ * {@inheritDoc ICreditAccountsService.getBorrowRate}
705
+ **/
797
706
  getBorrowRate(ca) {
798
707
  const { creditManager } = this.sdk.marketRegister.findCreditManager(
799
708
  ca.creditManager
@@ -819,9 +728,8 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
819
728
  return r + qr;
820
729
  }
821
730
  /**
822
- * Returns optimal HF for partial liquidation with 4 digits precision (10000 = 100%)
823
- * @param ca
824
- */
731
+ * {@inheritDoc ICreditAccountsService.getOptimalHFForPartialLiquidation}
732
+ **/
825
733
  getOptimalHFForPartialLiquidation(ca) {
826
734
  const borrowRate = this.getBorrowRate(ca);
827
735
  return import_constants.PERCENTAGE_FACTOR + (borrowRate < 100n ? borrowRate : 100n);
@@ -869,70 +777,18 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
869
777
  );
870
778
  return resp;
871
779
  }
872
- async getUpdateForAccount(options) {
873
- const { creditManager, creditAccount, desiredQuotas, ignoreReservePrices } = options;
874
- const quotaRecord = desiredQuotas ? (0, import_router.assetsMap)(desiredQuotas) : desiredQuotas;
875
- const caBalancesRecord = creditAccount ? (0, import_router.assetsMap)(creditAccount.tokens) : creditAccount;
876
- const market = this.sdk.marketRegister.findByCreditManager(creditManager);
877
- const cm = this.sdk.marketRegister.findCreditManager(creditManager).creditManager;
878
- const tokens = /* @__PURE__ */ new Set([(0, import_viem.getAddress)(cm.underlying)]);
879
- for (const t of cm.collateralTokens) {
880
- if (creditAccount && caBalancesRecord && quotaRecord) {
881
- const balanceAsset = caBalancesRecord.get(t);
882
- const balance = balanceAsset?.balance || 0n;
883
- const mask = balanceAsset?.mask || 0n;
884
- const isEnabled = (mask & creditAccount.enabledTokensMask) !== 0n;
885
- const quotaAsset = quotaRecord.get(t);
886
- const quotaBalance = quotaAsset?.balance || 0n;
887
- if (balance > 10n && isEnabled || quotaBalance > 0) {
888
- tokens.add((0, import_viem.getAddress)(t));
889
- }
890
- } else if (creditAccount && caBalancesRecord) {
891
- const balanceAsset = caBalancesRecord.get(t);
892
- const balance = balanceAsset?.balance || 0n;
893
- const mask = balanceAsset?.mask || 0n;
894
- const isEnabled = (mask & creditAccount.enabledTokensMask) !== 0n;
895
- if (balance > 10n && isEnabled) {
896
- tokens.add((0, import_viem.getAddress)(t));
897
- }
898
- } else if (quotaRecord) {
899
- const quotaAsset = quotaRecord.get(t);
900
- const quotaBalance = quotaAsset?.balance || 0n;
901
- if (quotaBalance > 0) {
902
- tokens.add((0, import_viem.getAddress)(t));
903
- }
904
- }
905
- }
906
- const priceFeeds = market.priceOracle.priceFeedsForTokens(Array.from(tokens), {
907
- main: true,
908
- reserve: !ignoreReservePrices
909
- });
910
- const tStr = Array.from(tokens).map((t) => this.labelAddress(t)).join(", ");
911
- const remark = ignoreReservePrices ? " main" : "";
912
- this.logger?.debug(
913
- { account: creditAccount?.creditAccount, manager: cm.name },
914
- `generating price feed updates for ${tStr} from ${priceFeeds.length}${remark} price feeds`
915
- );
916
- return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(priceFeeds);
917
- }
918
780
  /**
919
- * Returns account price updates that can be used in credit facade multicall or liquidator calls
920
- * @param options
921
- * @returns
922
- */
923
- async getOnDemandPriceUpdates(options) {
924
- const { creditManager, creditAccount } = options;
925
- const market = this.sdk.marketRegister.findByCreditManager(creditManager);
781
+ * {@inheritDoc ICreditAccountsService.getOnDemandPriceUpdates}
782
+ **/
783
+ async getOnDemandPriceUpdates(account, ignoreReservePrices) {
784
+ const { creditManager, creditAccount } = account;
926
785
  const cm = this.sdk.marketRegister.findCreditManager(creditManager);
927
- const update = await this.getUpdateForAccount(options);
786
+ const update = await this.getUpdateForAccount(account, ignoreReservePrices);
928
787
  this.logger?.debug(
929
- { account: creditAccount?.creditAccount, manager: cm.name },
788
+ { account: creditAccount, manager: cm.name },
930
789
  `getting on demand price updates from ${update.txs.length} txs`
931
790
  );
932
- return market.priceOracle.onDemandPriceUpdates(
933
- cm.creditFacade.address,
934
- update
935
- ).raw;
791
+ return (0, import_market.getRawPriceUpdates)(update);
936
792
  }
937
793
  /**
938
794
  * Analyzes a multicall array and prepends necessary on-demand price feed updates.
@@ -992,6 +848,29 @@ class AbstractCreditAccountService extends import_base.SDKConstruct {
992
848
  ...remainingCalls
993
849
  ];
994
850
  }
851
+ async getUpdateForAccount(account, ignoreReservePrices) {
852
+ const { creditManager, creditAccount, enabledTokensMask } = account;
853
+ const market = this.sdk.marketRegister.findByCreditManager(creditManager);
854
+ const cm = this.sdk.marketRegister.findCreditManager(creditManager).creditManager;
855
+ const tokens = new import_utils.AddressSet([cm.underlying]);
856
+ for (const t of account.tokens) {
857
+ const isEnabled = (t.mask & enabledTokensMask) !== 0n;
858
+ if (t.balance > 10n && isEnabled) {
859
+ tokens.add(t.token);
860
+ }
861
+ }
862
+ const priceFeeds = market.priceOracle.priceFeedsForTokens(Array.from(tokens), {
863
+ main: true,
864
+ reserve: !ignoreReservePrices
865
+ });
866
+ const tStr = tokens.map((t) => this.labelAddress(t)).join(", ");
867
+ const remark = ignoreReservePrices ? " main" : "";
868
+ this.logger?.debug(
869
+ { account: creditAccount, manager: cm.name },
870
+ `generating price feed updates for ${tStr} from ${priceFeeds.length}${remark} price feeds`
871
+ );
872
+ return this.sdk.priceFeeds.generatePriceFeedsUpdateTxs(priceFeeds);
873
+ }
995
874
  /**
996
875
  * Executes a multicall on a credit account, automatically prepending
997
876
  * necessary on-demand price feed updates.
@@ -27,7 +27,7 @@ var import_math = require("../constants/math.js");
27
27
  var import_AbstractCreditAccountsService = require("./AbstractCreditAccountsService.js");
28
28
  class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.AbstractCreditAccountService {
29
29
  /**
30
- * Implements {@link ICreditAccountsService.setBot}
30
+ * {@inheritDoc ICreditAccountsService.setBot}
31
31
  */
32
32
  async setBot({
33
33
  botAddress,
@@ -69,7 +69,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
69
69
  return { tx, calls, creditFacade: cm.creditFacade };
70
70
  }
71
71
  /**
72
- * Implements {@link ICreditAccountsService.withdrawCollateral}
72
+ * {@inheritDoc ICreditAccountsService.withdrawCollateral}
73
73
  */
74
74
  async withdrawCollateral({
75
75
  creditAccount,
@@ -104,7 +104,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
104
104
  return { tx, calls, creditFacade: cm.creditFacade };
105
105
  }
106
106
  /**
107
- * Implements {@link ICreditAccountsService.repayCreditAccount}
107
+ * {@inheritDoc ICreditAccountsService.repayCreditAccount}
108
108
  */
109
109
  async repayCreditAccount({
110
110
  operation,
@@ -136,7 +136,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
136
136
  return { tx, calls, creditFacade: cm.creditFacade };
137
137
  }
138
138
  /**
139
- * Implements {@link ICreditAccountsService.repayAndLiquidateCreditAccount}
139
+ * {@inheritDoc ICreditAccountsService.repayAndLiquidateCreditAccount}
140
140
  */
141
141
  async repayAndLiquidateCreditAccount({
142
142
  collateralAssets,
@@ -173,7 +173,7 @@ class CreditAccountServiceV310 extends import_AbstractCreditAccountsService.Abst
173
173
  return { tx, calls, creditFacade: cm.creditFacade };
174
174
  }
175
175
  /**
176
- * Implements {@link ICreditAccountsService.claimFarmRewards}
176
+ * {@inheritDoc ICreditAccountsService.claimFarmRewards}
177
177
  */
178
178
  async claimFarmRewards({
179
179
  calls: externalCalls,
@@ -45,11 +45,30 @@ class ContractParseError extends import_viem.BaseError {
45
45
  }
46
46
  }
47
47
  class BaseContract extends import_Construct.Construct {
48
+ /**
49
+ * Viem contract instance for direct read calls
50
+ **/
48
51
  contract;
52
+ /**
53
+ * Contract ABI
54
+ **/
49
55
  abi;
56
+ /**
57
+ * Gearbox contract type identifier (e.g. `"CREDIT_MANAGER"`), or empty string if unknown.
58
+ **/
50
59
  contractType;
60
+ /**
61
+ * Contract version number.
62
+ * @default 0
63
+ **/
51
64
  version;
65
+ /**
66
+ * On-chain address of the contract.
67
+ **/
52
68
  address;
69
+ /**
70
+ * Display name for the contract.
71
+ **/
53
72
  name;
54
73
  constructor(options, args) {
55
74
  super(options);
@@ -73,6 +92,7 @@ class BaseContract extends import_Construct.Construct {
73
92
  register.setAddressLabel(this.address, this.name);
74
93
  }
75
94
  }
95
+ /** {@inheritDoc IBaseContract.stateHuman} */
76
96
  stateHuman(_ = true) {
77
97
  return {
78
98
  address: this.labelAddress(this.address),
@@ -81,9 +101,10 @@ class BaseContract extends import_Construct.Construct {
81
101
  };
82
102
  }
83
103
  /**
84
- * Updates (or not) contract's internal state from event
85
- * @param _log
86
- */
104
+ * Applies an on-chain event to update this contract's local state.
105
+ *
106
+ * @param _log - Decoded event log emitted by this contract.
107
+ **/
87
108
  processLog(_log) {
88
109
  }
89
110
  /**
@@ -158,9 +179,8 @@ class BaseContract extends import_Construct.Construct {
158
179
  }
159
180
  }
160
181
  /**
161
- * Same as {@link stingifyFunctionData}, but throws if error occurs
162
- * @param calldata
163
- * @returns
182
+ * Same as {@link stringifyFunctionData}, but throws if error occurs.
183
+ * @param calldata - Raw ABI-encoded calldata.
164
184
  */
165
185
  mustStringifyFunctionData(calldata) {
166
186
  const decoded = (0, import_viem.decodeFunctionData)({
@@ -32,8 +32,8 @@ class ChainContractsRegister {
32
32
  labels = new import_AddressMap.AddressMap([], "labels");
33
33
  client;
34
34
  /**
35
- * Token metadata such as symbol and decimals
36
- */
35
+ * Shared token metadata (symbol, decimals) for all tokens known on this chain.
36
+ **/
37
37
  tokensMeta;
38
38
  logger;
39
39
  constructor(client, logger) {
@@ -41,6 +41,9 @@ class ChainContractsRegister {
41
41
  this.tokensMeta = new import_TokensMeta.TokensMeta(client);
42
42
  this.logger = logger;
43
43
  }
44
+ /**
45
+ * Clears all registered contracts, address labels, and token metadata.
46
+ **/
44
47
  resetContracts() {
45
48
  this.logger?.debug(
46
49
  `resetting contacts register with ${this.contracts.size} contracts`
@@ -49,9 +52,19 @@ class ChainContractsRegister {
49
52
  this.contracts.clear();
50
53
  this.tokensMeta.reset();
51
54
  }
55
+ /**
56
+ * Looks up a contract by address.
57
+ * @param address - On-chain address.
58
+ * @returns The contract wrapper, or `undefined` if not registered.
59
+ */
52
60
  getContract(address) {
53
61
  return this.contracts.get(address);
54
62
  }
63
+ /**
64
+ * Looks up a contract by address, throwing if not found.
65
+ * @param address - On-chain address.
66
+ * @throws If no contract is registered at this address.
67
+ */
55
68
  mustGetContract(address) {
56
69
  const contract = this.contracts.mustGet(address);
57
70
  if (!contract) {
@@ -59,9 +72,21 @@ class ChainContractsRegister {
59
72
  }
60
73
  return contract;
61
74
  }
75
+ /**
76
+ * Registers (or replaces) a contract at the given address.
77
+ * @param address - On-chain address.
78
+ * @param contract - Contract wrapper instance.
79
+ */
62
80
  setContract(address, contract) {
63
81
  this.contracts.upsert(address, contract);
64
82
  }
83
+ /**
84
+ * Assigns a human-readable label to an address for use in logging and
85
+ * parsed call output.
86
+ * @param address - On-chain address.
87
+ * @param label - Static label string, or a function that receives the
88
+ * current label and returns a new one.
89
+ */
65
90
  setAddressLabel(address, label) {
66
91
  if (address === import_addresses.NOT_DEPLOYED) {
67
92
  return;
@@ -72,13 +97,25 @@ class ChainContractsRegister {
72
97
  this.labels.upsert(address, label(this.labels.get(address)));
73
98
  }
74
99
  }
100
+ /**
101
+ * Returns a display string for an address, incorporating its label if one exists.
102
+ * @param address - On-chain address.
103
+ * @param omitAddress - When `true`, returns only the label (no address prefix).
104
+ * Falls back to the raw address when no label is set.
105
+ */
75
106
  labelAddress(address, omitAddress) {
76
107
  const label = this.labels.get(address);
77
108
  return label ? omitAddress ? label : `${address} [${label}]` : address;
78
109
  }
110
+ /**
111
+ * The viem {@link Chain} object for this register's connected client.
112
+ **/
79
113
  get chain() {
80
114
  return this.client.chain;
81
115
  }
116
+ /**
117
+ * Numeric chain ID (e.g. `1` for Ethereum mainnet).
118
+ **/
82
119
  get chainId() {
83
120
  return this.client.chain.id;
84
121
  }
@@ -27,9 +27,6 @@ class Construct {
27
27
  logger;
28
28
  client;
29
29
  #register;
30
- /**
31
- * Indicates that contract state needs to be updated
32
- */
33
30
  #dirty = false;
34
31
  constructor(options) {
35
32
  if (options instanceof import_ChainContractsRegister.ChainContractsRegister) {
@@ -61,12 +58,22 @@ class Construct {
61
58
  safeGetRegister() {
62
59
  return this.#register;
63
60
  }
61
+ /**
62
+ * The viem {@link Chain} object associated with the connected client.
63
+ **/
64
64
  get chain() {
65
65
  return this.client.chain;
66
66
  }
67
+ /**
68
+ * Numeric chain ID (e.g. `1` for Ethereum mainnet).
69
+ **/
67
70
  get chainId() {
68
71
  return this.client.chain.id;
69
72
  }
73
+ /**
74
+ * Gearbox network type for this chain (e.g. `"Mainnet"`, `"Arbitrum"`).
75
+ * @throws If the chain was not created by the Gearbox SDK.
76
+ */
70
77
  get networkType() {
71
78
  if ("network" in this.chain) {
72
79
  return this.chain.network;
@@ -74,6 +81,7 @@ class Construct {
74
81
  throw new Error(`chain ${this.chain.id} is not a Gearbox SDK chain`);
75
82
  }
76
83
  /**
84
+ * @internal
77
85
  * Indicates that contract state diverged from onchain state and needs to be updated
78
86
  */
79
87
  get dirty() {
@@ -82,6 +90,9 @@ class Construct {
82
90
  set dirty(value) {
83
91
  this.#dirty = value;
84
92
  }
93
+ /**
94
+ * Information about tokens known on this chain
95
+ */
85
96
  get tokensMeta() {
86
97
  return this.register.tokensMeta;
87
98
  }
@@ -89,6 +100,7 @@ class Construct {
89
100
  return this.#register?.labelAddress(address, omitAddress) ?? address;
90
101
  }
91
102
  /**
103
+ * @internal
92
104
  * Returns list of addresses that should be watched for events to sync state
93
105
  */
94
106
  get watchAddresses() {