@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
@@ -18,24 +18,13 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var get_price_update_tx_exports = {};
20
20
  __export(get_price_update_tx_exports, {
21
- getPriceUpdateTx: () => getPriceUpdateTx,
22
- getUpdateCalldata: () => getUpdateCalldata
21
+ getPriceUpdateTx: () => getPriceUpdateTx
23
22
  });
24
23
  module.exports = __toCommonJS(get_price_update_tx_exports);
25
24
  var import_viem = require("viem");
26
25
  var import_sdk = require("../../../sdk/index.js");
27
26
  var import_bindings = require("../../bindings/index.js");
28
27
  var import_addresses = require("../../deployment/addresses.js");
29
- function getUpdateCalldata(tx) {
30
- const data = (0, import_viem.decodeFunctionData)({
31
- abi: (0, import_viem.parseAbi)(["function updatePrice(bytes calldata data) external"]),
32
- data: tx.raw.callData
33
- });
34
- return {
35
- priceFeed: tx.raw.to,
36
- data: data.args[0]
37
- };
38
- }
39
28
  async function getPriceUpdateTx({
40
29
  client,
41
30
  priceFeeds,
@@ -72,7 +61,7 @@ async function getPriceUpdateTx({
72
61
  });
73
62
  return multicallTx;
74
63
  }
75
- const priceUpdates = updateTxs.txs.map((tx) => getUpdateCalldata(tx));
64
+ const priceUpdates = (0, import_sdk.getRawPriceUpdates)(updateTxs);
76
65
  if (priceUpdates.length === 0) {
77
66
  return void 0;
78
67
  }
@@ -80,6 +69,5 @@ async function getPriceUpdateTx({
80
69
  }
81
70
  // Annotate the CommonJS export names for ESM import in node:
82
71
  0 && (module.exports = {
83
- getPriceUpdateTx,
84
- getUpdateCalldata
72
+ getPriceUpdateTx
85
73
  });
@@ -76,24 +76,50 @@ async function attachClient(options, network) {
76
76
  }
77
77
  class GearboxSDK extends import_base.ChainContractsRegister {
78
78
  #hooks = new import_internal.Hooks();
79
+ /**
80
+ * Registered plugin instances, keyed by plugin name.
81
+ **/
79
82
  plugins;
80
- // Block which was use for data query
81
83
  #currentBlock;
82
84
  #timestamp;
83
85
  #syncing = false;
84
- // Collection of core singleton contracts
85
86
  #addressProvider;
86
87
  #attachConfig;
87
- // Collection of markets
88
88
  #marketRegister;
89
89
  #priceFeeds;
90
+ /**
91
+ * Gas limit applied to read-only `eth_call` requests.
92
+ * `undefined` means that gas limit will not be set on read-only calls,
93
+ * leaving it to rpc provider to decide.
94
+ **/
90
95
  gasLimit;
91
96
  /**
92
- * Will throw an error if contract type is not supported, otherwise will try to use generic contract first, if possible
93
- */
97
+ * When `true`, the SDK throws on unrecognised contract types instead of
98
+ * falling back to a generic contract wrapper.
99
+ **/
94
100
  strictContractTypes;
101
+ /**
102
+ * Registers a callback for an SDK lifecycle event.
103
+ *
104
+ * @see {@link SDKHooks} for available event names.
105
+ **/
95
106
  addHook = this.#hooks.addHook.bind(this.#hooks);
107
+ /**
108
+ * Removes a previously registered lifecycle callback.
109
+ *
110
+ * @see {@link SDKHooks} for available event names.
111
+ **/
96
112
  removeHook = this.#hooks.removeHook.bind(this.#hooks);
113
+ /**
114
+ * Creates and initialises a new SDK instance by reading live on-chain state.
115
+ *
116
+ * This is the primary way to bootstrap the SDK. The method connects to the
117
+ * chain, discovers the address provider and all configured markets, and
118
+ * attaches any supplied plugins.
119
+ *
120
+ * @param options - Combined SDK, client, and (optional) network options.
121
+ * @returns A fully initialised SDK instance.
122
+ **/
97
123
  static async attach(options) {
98
124
  const {
99
125
  logger,
@@ -129,6 +155,19 @@ class GearboxSDK extends import_base.ChainContractsRegister {
129
155
  pyth
130
156
  });
131
157
  }
158
+ /**
159
+ * Creates a new SDK instance from a previously serialised {@link GearboxState}
160
+ * snapshot, without making any on-chain calls.
161
+ *
162
+ * Use this for fast startup when a recent state snapshot is available
163
+ * (e.g. loaded from a file or received from a backend service).
164
+ *
165
+ * @param options - SDK and client options (block number and address provider
166
+ * are taken from the snapshot).
167
+ * @param state - Serialised state obtained from {@link GearboxSDK.state}.
168
+ * @returns A fully initialised SDK instance.
169
+ * @throws If the snapshot's {@link STATE_VERSION} does not match.
170
+ **/
132
171
  static hydrate(options, state) {
133
172
  const { logger, plugins, strictContractTypes, gasLimit, ...rest } = options;
134
173
  const client = createClient(options, {
@@ -281,9 +320,15 @@ class GearboxSDK extends import_base.ChainContractsRegister {
281
320
  return this;
282
321
  }
283
322
  /**
284
- * Reattach SDK with the same config as before, without re-creating instance. Will load all state from scratch
285
- * Be mindful of block number, for example
286
- */
323
+ * Re-attaches the SDK using the same configuration, discarding all cached
324
+ * state and re-reading everything from the chain.
325
+ *
326
+ * Useful when the SDK needs a full refresh (e.g. after a protocol upgrade).
327
+ * Note that if the original `blockNumber` was pinned, the same block is
328
+ * re-used — call {@link syncState} instead if you want to advance.
329
+ *
330
+ * @throws If the SDK has not been attached yet.
331
+ **/
287
332
  async reattach() {
288
333
  if (!this.#attachConfig) {
289
334
  throw new Error("cannot reattach, attach config is not set");
@@ -291,8 +336,15 @@ class GearboxSDK extends import_base.ChainContractsRegister {
291
336
  await this.#attach(this.#attachConfig);
292
337
  }
293
338
  /**
294
- * Rehydrate existing SDK from new state without re-creating instance
295
- */
339
+ * Replaces the SDK's in-memory state with a new serialised snapshot
340
+ * without re-creating the instance.
341
+ *
342
+ * After hydration the `rehydrate` hook is triggered so that listeners
343
+ * can react to the state change.
344
+ *
345
+ * @param state - Serialised state obtained from {@link GearboxSDK.state}.
346
+ * @throws If the SDK has not been attached or hydrated yet.
347
+ **/
296
348
  async rehydrate(state) {
297
349
  if (!this.#attachConfig) {
298
350
  throw new Error("cannot rehydrate, attach config is not set");
@@ -308,9 +360,20 @@ class GearboxSDK extends import_base.ChainContractsRegister {
308
360
  timestamp: state.timestamp
309
361
  });
310
362
  }
363
+ /**
364
+ * Gearbox network type the SDK is connected to (e.g. `"Mainnet"`, `"Arbitrum"`).
365
+ **/
311
366
  get networkType() {
312
367
  return this.client.chain.network;
313
368
  }
369
+ /**
370
+ * Returns a human-readable snapshot of the entire SDK state, suitable
371
+ * for logging or diagnostic inspection.
372
+ *
373
+ * @param raw - When `true`, include raw numeric values alongside
374
+ * formatted ones.
375
+ * @default true
376
+ **/
314
377
  stateHuman(raw = true) {
315
378
  return {
316
379
  block: Number(this.currentBlock),
@@ -328,6 +391,13 @@ class GearboxSDK extends import_base.ChainContractsRegister {
328
391
  ...this.marketRegister.stateHuman(raw)
329
392
  };
330
393
  }
394
+ /**
395
+ * Serialisable snapshot of the current SDK state.
396
+ *
397
+ * The returned object can be persisted (e.g. written to a file) and later
398
+ * passed to {@link GearboxSDK.hydrate} for instant restoration without
399
+ * on-chain reads.
400
+ **/
331
401
  get state() {
332
402
  return {
333
403
  version: STATE_VERSION,
@@ -346,10 +416,20 @@ class GearboxSDK extends import_base.ChainContractsRegister {
346
416
  };
347
417
  }
348
418
  /**
349
- * Reloads markets states based on events from last processed block to new block (defaults to latest block)
350
- * @param opts
351
- * @returns true if successful, false if was skipped or failed
352
- */
419
+ * Incrementally updates the SDK state by replaying on-chain events from the
420
+ * last processed block up to the target block (defaults to `latest`).
421
+ *
422
+ * Use the to periodically update sdk state on cron, or subscribe to new blocks
423
+ * using viem's `watchBlocks`
424
+ *
425
+ * On failure the SDK reverts to the previous block/timestamp so that
426
+ * subsequent calls can retry.
427
+ *
428
+ * @param opts - Target block and sync behaviour. When omitted the latest
429
+ * block is fetched automatically.
430
+ * @returns `true` if the sync completed successfully, `false` if it was
431
+ * skipped or failed.
432
+ **/
353
433
  async syncState(opts) {
354
434
  let {
355
435
  blockNumber,
@@ -451,12 +531,22 @@ class GearboxSDK extends import_base.ChainContractsRegister {
451
531
  }
452
532
  return success;
453
533
  }
534
+ /**
535
+ * Block number that the SDK state corresponds to.
536
+ *
537
+ * @throws If the SDK has not been attached or hydrated yet.
538
+ **/
454
539
  get currentBlock() {
455
540
  if (this.#currentBlock === void 0) {
456
541
  throw ERR_NOT_ATTACHED;
457
542
  }
458
543
  return this.#currentBlock;
459
544
  }
545
+ /**
546
+ * Block timestamp (Unix epoch seconds) corresponding to {@link currentBlock}.
547
+ *
548
+ * @throws If the SDK has not been attached or hydrated yet.
549
+ **/
460
550
  get timestamp() {
461
551
  if (this.#timestamp === void 0) {
462
552
  throw ERR_NOT_ATTACHED;
@@ -464,14 +554,23 @@ class GearboxSDK extends import_base.ChainContractsRegister {
464
554
  return this.#timestamp;
465
555
  }
466
556
  /**
467
- * All price feeds known to sdk, without oracle-related data (stalenessPeriod, main/reserve, etc.)
468
- */
557
+ * Global registry of all price feeds known to the SDK (on all markets).
558
+ *
559
+ * Unlike per-oracle price feed references, this register does not carry
560
+ * oracle-specific metadata (staleness period, main/reserve designation, etc.).
561
+ *
562
+ * @throws If the SDK has not been attached or hydrated yet.
563
+ **/
469
564
  get priceFeeds() {
470
565
  if (this.#priceFeeds === void 0) {
471
566
  throw ERR_NOT_ATTACHED;
472
567
  }
473
568
  return this.#priceFeeds;
474
569
  }
570
+ /**
571
+ * Address of the GEAR governance token on this chain, or `undefined`
572
+ * if the address provider does not list it.
573
+ **/
475
574
  get gear() {
476
575
  try {
477
576
  const g = this.addressProvider.getAddress(import_constants.AP_GEAR_TOKEN, import_constants.NO_VERSION);
@@ -481,12 +580,23 @@ class GearboxSDK extends import_base.ChainContractsRegister {
481
580
  return void 0;
482
581
  }
483
582
  }
583
+ /**
584
+ * The chain's address provider contract, the central directory for all
585
+ * protocol-wide Gearbox protocol addresses.
586
+ *
587
+ * @throws If the SDK has not been attached or hydrated yet.
588
+ **/
484
589
  get addressProvider() {
485
590
  if (this.#addressProvider === void 0) {
486
591
  throw ERR_NOT_ATTACHED;
487
592
  }
488
593
  return this.#addressProvider;
489
594
  }
595
+ /**
596
+ * Registry of all loaded markets (pools, credit managers, oracles, etc.).
597
+ *
598
+ * @throws If the SDK has not been attached or hydrated yet.
599
+ **/
490
600
  get marketRegister() {
491
601
  if (this.#marketRegister === void 0) {
492
602
  throw ERR_NOT_ATTACHED;
@@ -494,10 +604,15 @@ class GearboxSDK extends import_base.ChainContractsRegister {
494
604
  return this.#marketRegister;
495
605
  }
496
606
  /**
497
- * Returns router contract that will work for given credit manager or credit facade, or simply version range
498
- * @param params
499
- * @returns
500
- */
607
+ * Resolves the appropriate router contract for a given credit manager,
608
+ * credit facade, or explicit version range.
609
+ *
610
+ * @param params - Identifies the context: a credit manager address/state,
611
+ * a credit facade address/state, or a {@link VersionRange}.
612
+ * @returns The matching router contract instance.
613
+ * @throws If the credit facade version is unsupported or no router is
614
+ * registered for the resolved version range.
615
+ **/
501
616
  routerFor(params) {
502
617
  let routerRange;
503
618
  if (Array.isArray(params)) {