@tuwaio/pulsar-core 0.0.3 → 0.0.5

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/index.d.ts CHANGED
@@ -9,108 +9,107 @@ import { PersistOptions } from 'zustand/middleware';
9
9
  */
10
10
 
11
11
  /**
12
- * A utility type for creating modular Zustand store slices.
13
- * @template T The state slice type.
14
- * @template E The full store state type, defaults to T.
12
+ * A utility type for creating modular Zustand store slices, enabling composable state management.
13
+ * @template T - The type of the state slice.
14
+ * @template S - The type of the full store state, defaulting to T.
15
15
  */
16
- type StoreSlice<T extends object, E extends object = T> = (set: StoreApi<E extends T ? E : E & T>['setState'], get: StoreApi<E extends T ? E : E & T>['getState']) => T;
16
+ type StoreSlice<T extends object, S extends object = T> = (set: StoreApi<S extends T ? S : S & T>['setState'], get: StoreApi<S extends T ? S : S & T>['getState']) => T;
17
17
  /**
18
- * Represents the blockchain adapter for a transaction.
18
+ * Defines the supported blockchain adapters. Each adapter corresponds to a specific chain architecture.
19
19
  */
20
20
  declare enum TransactionAdapter {
21
- /** EVM adapter. */
21
+ /** For Ethereum Virtual Machine (EVM) compatible chains like Ethereum, Polygon, etc. */
22
22
  EVM = "evm",
23
- /** Solana adapter. */
23
+ /** For the Solana blockchain. */
24
24
  SOLANA = "solana",
25
- /** Starknet adapter. */
26
- Starknet = "Starknet"
25
+ /** For the Starknet L2 network. */
26
+ Starknet = "starknet"
27
27
  }
28
28
  /**
29
- * Represents the final status of a transaction.
29
+ * Represents the terminal status of a transaction after it has been processed.
30
30
  */
31
31
  declare enum TransactionStatus {
32
- /** The transaction failed to execute. */
32
+ /** The transaction failed to execute due to an on-chain error or rejection. */
33
33
  Failed = "Failed",
34
34
  /** The transaction was successfully mined and executed. */
35
35
  Success = "Success",
36
- /** The transaction was replaced by another (e.g., speed-up). */
36
+ /** The transaction was replaced by another with the same nonce (e.g., a speed-up or cancel). */
37
37
  Replaced = "Replaced"
38
38
  }
39
39
  /**
40
- * The base structure for any transaction being tracked.
41
- * @template T The type of the tracker identifier (e.g., 'evm', 'safe').
40
+ * The fundamental structure for any transaction being tracked by Pulsar.
41
+ * This forms the base upon which chain-specific transaction types are built.
42
+ * @template T - The type of the tracker identifier (e.g., 'ethereum', 'gelato', 'safe').
42
43
  */
43
44
  type BaseTransaction<T> = {
44
- /** A key identifying the retry logic for this transaction from the actions registry. */
45
+ /** A unique key identifying a re-executable action from the `TxActions` registry. */
45
46
  actionKey?: string;
46
47
  /** The chain identifier (e.g., 1 for Ethereum Mainnet, 'SN_MAIN' for Starknet). */
47
48
  chainId: number | string;
48
- /** A description for the transaction, with states for [pending, success, error, replaced]. */
49
+ /** A user-facing description. Can be a single string or an array for [pending, success, error, replaced] states. */
49
50
  description?: string | [string, string, string, string];
50
- /** An error message if the transaction failed. */
51
+ /** The error message if the transaction failed. */
51
52
  errorMessage?: string;
52
- /** The timestamp (in seconds) when the transaction was finalized on-chain. */
53
+ /** The on-chain timestamp (in seconds) when the transaction was finalized. */
53
54
  finishedTimestamp?: number;
54
- /** The sender's address. */
55
+ /** The sender's wallet address. */
55
56
  from: string;
56
57
  /** A flag indicating if the transaction is in a failed state. */
57
58
  isError?: boolean;
58
- /** A flag indicating if the detailed tracking modal should be open for this transaction. For UI purposes. */
59
+ /** A UI flag to control the visibility of a detailed tracking modal for this transaction. */
59
60
  isTrackedModalOpen?: boolean;
60
- /** The local timestamp (in seconds) when the transaction was initiated. */
61
+ /** The local timestamp (in seconds) when the transaction was initiated by the user. */
61
62
  localTimestamp: number;
62
- /** Any additional data associated with the transaction. */
63
+ /** Any additional, custom data associated with the transaction. */
63
64
  payload?: object;
64
- /** Indicates if the transaction is still pending confirmation. */
65
+ /** A flag indicating if the transaction is still awaiting on-chain confirmation. */
65
66
  pending: boolean;
66
- /** The final status of the transaction. */
67
+ /** The final on-chain status of the transaction. */
67
68
  status?: TransactionStatus;
68
- /** A title for the transaction, with states for [pending, success, error, replaced]. */
69
+ /** A user-facing title. Can be a single string or an array for [pending, success, error, replaced] states. */
69
70
  title?: string | [string, string, string, string];
70
- /** The specific tracker responsible for monitoring this transaction (e.g., 'evm', 'safe', 'gelato', etc.). */
71
+ /** The specific tracker responsible for monitoring this transaction's status. */
71
72
  tracker: T;
72
- /** The unique key for the transaction within its tracker (e.g., EVM hash, Gelato task ID). */
73
+ /** The unique identifier for the transaction (e.g., EVM hash, Gelato task ID). */
73
74
  txKey: string;
74
- /** The type or category of the transaction (e.g., 'increment', 'approve'). */
75
+ /** The application-specific type or category of the transaction (e.g., 'SWAP', 'APPROVE'). */
75
76
  type: string;
76
- /** The type of wallet used for the transaction. */
77
+ /** The type of wallet used to sign the transaction (e.g., 'injected', 'walletConnect'). */
77
78
  walletType: string;
78
79
  };
79
80
  /**
80
- * Represents an EVM-specific transaction, extending the base properties.
81
- * @template T The type of the tracker identifier.
81
+ * Represents an EVM-specific transaction, extending the base properties with EVM fields.
82
+ * @template T - The type of the tracker identifier.
82
83
  */
83
84
  type EvmTransaction<T> = BaseTransaction<T> & {
84
- /** The transaction adapter type. */
85
85
  adapter: TransactionAdapter.EVM;
86
86
  /** The on-chain transaction hash, available after submission. */
87
- hash?: string;
88
- /** The data payload for the transaction, typically for contract interactions. */
89
- input?: string;
90
- /** The maximum fee per gas for the transaction (EIP-1559). */
87
+ hash?: `0x${string}`;
88
+ /** The data payload for the transaction, typically for smart contract interactions. */
89
+ input?: `0x${string}`;
90
+ /** The maximum fee per gas for an EIP-1559 transaction (in wei). */
91
91
  maxFeePerGas?: string;
92
- /** The maximum priority fee per gas for the transaction (EIP-1559). */
92
+ /** The maximum priority fee per gas for an EIP-1559 transaction (in wei). */
93
93
  maxPriorityFeePerGas?: string;
94
- /** The transaction nonce. */
94
+ /** The transaction nonce, a sequential number for the sender's account. */
95
95
  nonce?: number;
96
- /** The hash of a transaction that this one replaced (e.g., for speed-up). */
97
- replacedTxHash?: string;
98
- /** The recipient's address. */
99
- to?: string;
100
- /** The value (in wei) being sent with the transaction. */
96
+ /** The hash of a transaction that this one replaced. */
97
+ replacedTxHash?: `0x${string}`;
98
+ /** The recipient's address or contract address. */
99
+ to?: `0x${string}`;
100
+ /** The amount of native currency (in wei) being sent. */
101
101
  value?: string;
102
102
  };
103
103
  /**
104
104
  * Represents a Solana-specific transaction, extending the base properties.
105
- * @template T The type of the tracker identifier.
105
+ * @template T - The type of the tracker identifier.
106
106
  */
107
107
  type SolanaTransaction<T> = BaseTransaction<T> & {
108
- /** The transaction adapter type. */
109
108
  adapter: TransactionAdapter.SOLANA;
110
- /** The transaction fee. */
109
+ /** The transaction fee in lamports. */
111
110
  fee?: number;
112
111
  /** The instructions included in the transaction. */
113
- instructions?: any[];
112
+ instructions?: unknown[];
114
113
  /** The recent blockhash used for the transaction. */
115
114
  recentBlockhash?: string;
116
115
  /** The slot in which the transaction was processed. */
@@ -118,9 +117,10 @@ type SolanaTransaction<T> = BaseTransaction<T> & {
118
117
  };
119
118
  /**
120
119
  * Represents a Starknet-specific transaction, extending the base properties.
121
- * @template T The type of the tracker identifier.
120
+ * @template T - The type of the tracker identifier.
122
121
  */
123
122
  type StarknetTransaction<T> = BaseTransaction<T> & {
123
+ adapter: TransactionAdapter.Starknet;
124
124
  /** The actual fee paid for the transaction. */
125
125
  actualFee?: {
126
126
  amount: string;
@@ -128,86 +128,118 @@ type StarknetTransaction<T> = BaseTransaction<T> & {
128
128
  };
129
129
  /** The address of the contract being interacted with. */
130
130
  contractAddress?: string;
131
- /** The transaction adapter type. */
132
- adapter: TransactionAdapter.Starknet;
133
131
  /** The reason for transaction failure, if applicable. */
134
132
  revertReason?: string;
135
133
  };
136
- /** A union type representing any possible transaction structure. */
134
+ /** A union type representing any possible transaction structure that Pulsar can handle. */
137
135
  type Transaction<T> = EvmTransaction<T> | SolanaTransaction<T> | StarknetTransaction<T>;
138
136
  /**
139
- * Represents the parameters required to initiate a new transaction.
137
+ * A registry of functions that can be re-executed, keyed by `actionKey`.
138
+ * Used for implementing "Retry" functionality.
139
+ */
140
+ type TxActions = Record<string, (...args: any[]) => Promise<unknown>>;
141
+ /**
142
+ * Represents the parameters required to initiate a new transaction tracking flow.
140
143
  */
141
144
  type InitialTransactionParams = {
142
- /** The transaction adapter type. */
143
145
  adapter: TransactionAdapter;
144
- /** A key identifying the retry logic from the actions registry. */
146
+ /** A key to identify the re-executable action from the `TxActions` registry. */
145
147
  actionKey?: string;
146
- /** A description for the transaction, with states for [pending, success, error, replaced]. */
148
+ /** A user-facing description for the transaction. */
147
149
  description?: string | [string, string, string, string];
148
- /** The ID of the desired blockchain network. */
150
+ /** The target chain ID for the transaction. */
149
151
  desiredChainID: number | string;
150
- /** Any additional data to be associated with the transaction. */
152
+ /** Any custom data to associate with the transaction. */
151
153
  payload?: object;
152
- /** A title for the transaction, with states for [pending, success, error, replaced]. */
154
+ /** A user-facing title for the transaction. */
153
155
  title?: string | [string, string, string, string];
154
- /** The type or category of the transaction (e.g., 'increment', 'approve'). */
156
+ /** The application-specific type of the transaction. */
155
157
  type: string;
156
- /** If true, the detailed tracking modal will open automatically for this transaction. */
158
+ /** If true, the detailed tracking modal will open automatically upon initiation. */
157
159
  withTrackedModal?: boolean;
158
- /** Wallet address from. */
159
- from: string;
160
- /** Type of the wallet. (injected, wallet connect, etc.) */
161
- walletType: string;
162
160
  };
163
161
  /**
164
- * Represents a transaction in its initial, pre-submission state within the store.
162
+ * Represents a transaction in its temporary, pre-submission state.
165
163
  * This is used for UI feedback while the transaction is being signed and sent.
166
164
  */
167
165
  type InitialTransaction = InitialTransactionParams & {
168
- /** An error message if the initialization process fails (e.g., user rejects signature). */
166
+ /** An error message if the initialization fails (e.g., user rejects signature). */
169
167
  errorMessage?: string;
170
- /** True if the transaction is currently being processed (e.g., waiting for user signature). */
168
+ /** A flag indicating if the transaction is being processed (e.g., waiting for signature). */
171
169
  isInitializing: boolean;
172
- /** The key of the on-chain transaction that this action produced, used for linking. */
170
+ /** The `txKey` of the on-chain transaction that this action produced, used for linking the states. */
173
171
  lastTxKey?: string;
174
172
  /** The local timestamp when the user initiated the action. */
175
173
  localTimestamp: number;
176
174
  };
175
+ /**
176
+ * Defines the interface for a transaction adapter, which provides chain-specific logic and utilities.
177
+ * @template TR - The type of the tracker identifier (e.g., a string enum).
178
+ * @template T - The specific transaction type, extending `Transaction<TR>`.
179
+ * @template A - The type of the key returned by the `actionFunction` (e.g., a transaction hash).
180
+ */
177
181
  type TxAdapter<TR, T extends Transaction<TR>, A> = {
182
+ /** The unique key identifying this adapter. */
178
183
  key: TransactionAdapter;
184
+ /** Returns information about the currently connected wallet. */
185
+ getWalletInfo: () => {
186
+ walletAddress: string;
187
+ walletType: string;
188
+ };
189
+ /** Ensures the connected wallet is on the correct network for the transaction. */
179
190
  checkChainForTx: (chainId: string | number) => Promise<void>;
191
+ /** Determines the appropriate tracker and final `txKey` based on the result of an action. */
180
192
  checkTransactionsTracker: (actionTxKey: A, walletType: string) => {
181
193
  txKey: string;
182
194
  tracker: TR;
183
195
  };
184
- checkAndInitializeTrackerInStore: ({ tx, ...rest }: {
196
+ /** Initializes the correct background tracker for a given transaction. */
197
+ checkAndInitializeTrackerInStore: (params: {
185
198
  tx: T;
186
199
  } & Pick<ITxTrackingStore<TR, T, A>, 'transactionsPool' | 'updateTxParams' | 'onSucceedCallbacks' | 'removeTxFromPool'>) => Promise<void>;
200
+ /** Returns the base URL for the blockchain explorer. */
201
+ getExplorerUrl: () => string | undefined;
202
+ /** Optional: Fetches a name from a chain-specific name service (e.g., ENS). */
203
+ getName?: (address: string) => Promise<string | null>;
204
+ /** Optional: Fetches an avatar URL from a chain-specific name service. */
205
+ getAvatar?: (name: string) => Promise<string | null>;
206
+ /** Optional: Logic to cancel a pending EVM transaction. */
207
+ cancelTxAction?: (tx: T) => Promise<string>;
208
+ /** Optional: Logic to speed up a pending EVM transaction. */
209
+ speedUpTxAction?: (tx: T) => Promise<string>;
210
+ /** Optional: Logic to retry a failed transaction. */
211
+ retryTxAction?: (params: {
212
+ txKey: string;
213
+ tx: InitialTransactionParams;
214
+ actions?: TxActions;
215
+ onClose: (txKey?: string) => void;
216
+ } & Partial<Pick<ITxTrackingStore<TR, T, A>, 'handleTransaction'>>) => Promise<void>;
217
+ /** Optional: Constructs a full explorer URL for a specific transaction. */
218
+ getExplorerTxUrl?: (transactionsPool: TransactionPool<TR, T>, txKey: string, replacedTxHash?: string) => string;
187
219
  };
188
220
  /**
189
- * Interface for the complete transaction tracking store.
190
- * @template TR The type of the tracker identifier.
191
- * @template T The transaction type.
192
- * @template C The configuration object type (e.g., wagmi config).
193
- * @template A The return type of the action function being wrapped.
221
+ * The complete interface for the Pulsar transaction tracking store.
222
+ * @template TR - The type of the tracker identifier.
223
+ * @template T - The transaction type.
224
+ * @template A - The return type of the `actionFunction`.
194
225
  */
195
226
  type ITxTrackingStore<TR, T extends Transaction<TR>, A> = IInitializeTxTrackingStore<TR, T> & {
196
227
  /**
197
- * A wrapper function that handles the entire lifecycle of a transaction.
198
- * It creates an `InitialTransaction`, executes the on-chain action, and tracks its status.
228
+ * The core function that handles the entire lifecycle of a new transaction.
229
+ * It manages UI state, executes the on-chain action, and initiates background tracking.
199
230
  * @param params - The parameters for handling the transaction.
200
231
  */
201
232
  handleTransaction: (params: {
202
- /** The async function to execute (e.g., a smart contract write call). */
233
+ /** The async function to execute (e.g., a smart contract write call). Must return a unique key or undefined. */
203
234
  actionFunction: () => Promise<A | undefined>;
204
- /** The metadata for the transaction to be created, of type `InitialTransactionParams`. */
235
+ /** The metadata for the transaction. */
205
236
  params: InitialTransactionParams;
237
+ /** The default tracker to use if it cannot be determined automatically. */
206
238
  defaultTracker?: TR;
207
239
  }) => Promise<void>;
208
240
  /**
209
- * Initializes all active trackers for pending transactions in the pool.
210
- * This is useful for resuming tracking after a page reload.
241
+ * Initializes trackers for all pending transactions in the pool.
242
+ * This is essential for resuming tracking after a page reload.
211
243
  */
212
244
  initializeTransactionsPool: () => Promise<void>;
213
245
  };
@@ -228,14 +260,14 @@ type TransactionPool<TR, T extends Transaction<TR>> = Record<string, T>;
228
260
  * that are updatable via the `updateTxParams` action.
229
261
  * @template TR - The type of the tracker identifier.
230
262
  */
231
- type UpdatedParamsFields<TR> = Pick<EvmTransaction<TR>, 'to' | 'nonce' | 'txKey' | 'pending' | 'hash' | 'status' | 'replacedTxHash' | 'errorMessage' | 'finishedTimestamp' | 'isTrackedModalOpen' | 'isError' | 'maxPriorityFeePerGas' | 'maxFeePerGas' | 'input' | 'value'>;
263
+ type UpdatableTransactionFields<TR> = Partial<Pick<EvmTransaction<TR>, 'to' | 'nonce' | 'txKey' | 'pending' | 'hash' | 'status' | 'replacedTxHash' | 'errorMessage' | 'finishedTimestamp' | 'isTrackedModalOpen' | 'isError' | 'maxPriorityFeePerGas' | 'maxFeePerGas' | 'input' | 'value'>>;
232
264
  /**
233
265
  * Defines the interface for the base transaction tracking store slice.
234
266
  * It includes the state and actions for managing transactions.
235
267
  * @template TR - The type of the tracker identifier.
236
268
  * @template T - The transaction type.
237
269
  */
238
- type IInitializeTxTrackingStore<TR, T extends Transaction<TR>> = {
270
+ interface IInitializeTxTrackingStore<TR, T extends Transaction<TR>> {
239
271
  /** An optional callback function to be executed when a transaction successfully completes. */
240
272
  onSucceedCallbacks?: (tx: T) => Promise<void> | void;
241
273
  /** A pool of all transactions currently being tracked, indexed by their `txKey`. */
@@ -245,18 +277,16 @@ type IInitializeTxTrackingStore<TR, T extends Transaction<TR>> = {
245
277
  /** The state of a transaction that is currently being initiated but not yet submitted. */
246
278
  initialTx?: InitialTransaction;
247
279
  /** Adds a new transaction to the tracking pool. */
248
- addTxToPool: ({ tx }: {
249
- tx: T;
250
- }) => void;
280
+ addTxToPool: (tx: T) => void;
251
281
  /** Updates one or more parameters of an existing transaction in the pool. */
252
- updateTxParams: (fields: UpdatedParamsFields<TR>) => void;
282
+ updateTxParams: (txKey: string, fields: UpdatableTransactionFields<TR>) => void;
253
283
  /** Removes a transaction from the tracking pool using its key. */
254
284
  removeTxFromPool: (txKey: string) => void;
255
285
  /** Closes the tracking modal for a specific transaction. */
256
286
  closeTxTrackedModal: (txKey?: string) => void;
257
287
  /** Returns the key of the last transaction that was added to the pool. */
258
288
  getLastTxKey: () => string | undefined;
259
- };
289
+ }
260
290
  /**
261
291
  * Creates a Zustand store slice containing the core logic for transaction tracking.
262
292
  * This function is a slice creator and is meant to be used within `createStore` from Zustand.
@@ -270,91 +300,118 @@ declare function initializeTxTrackingStore<TR, T extends Transaction<TR>>({ onSu
270
300
 
271
301
  /**
272
302
  * @file This file contains selector functions for deriving state from the transaction tracking store.
273
- * Selectors help abstract away the shape of the state and provide memoized, efficient access to computed data.
303
+ * Selectors help abstract the state's shape and provide efficient, memoized access to computed data.
274
304
  */
275
305
 
276
306
  /**
277
- * Selects all transactions from the pool and sorts them by their creation timestamp.
307
+ * Selects all transactions from the pool and sorts them by their creation timestamp in ascending order.
278
308
  * @template TR - The type of the tracker identifier.
279
309
  * @template T - The transaction type.
280
- * @param {TransactionPool<TR, T>} transactionsPool - The entire pool of transactions from the store.
310
+ * @param {TransactionPool<TR, T>} transactionsPool - The entire transaction pool from the store.
281
311
  * @returns {T[]} An array of all transactions, sorted chronologically.
282
312
  */
283
313
  declare const selectAllTransactions: <TR, T extends Transaction<TR>>(transactionsPool: TransactionPool<TR, T>) => T[];
284
314
  /**
285
- * Selects all transactions that are currently in a pending state.
315
+ * Selects all transactions that are currently in a pending state, sorted chronologically.
286
316
  * @template TR - The type of the tracker identifier.
287
317
  * @template T - The transaction type.
288
- * @param {TransactionPool<TR, T>} transactionsPool - The entire pool of transactions from the store.
318
+ * @param {TransactionPool<TR, T>} transactionsPool - The entire transaction pool from the store.
289
319
  * @returns {T[]} An array of pending transactions.
290
320
  */
291
321
  declare const selectPendingTransactions: <TR, T extends Transaction<TR>>(transactionsPool: TransactionPool<TR, T>) => T[];
292
322
  /**
293
- * Selects a single transaction from the pool by its unique transaction key (`txKey`).
294
- * This is the most direct way to retrieve a transaction.
323
+ * Selects a single transaction from the pool by its unique key (`txKey`).
295
324
  * @template TR - The type of the tracker identifier.
296
325
  * @template T - The transaction type.
297
- * @param {TransactionPool<TR, T>} transactionsPool - The entire pool of transactions from the store.
326
+ * @param {TransactionPool<TR, T>} transactionsPool - The entire transaction pool from the store.
298
327
  * @param {string} key - The `txKey` of the transaction to retrieve.
299
328
  * @returns {T | undefined} The transaction object if found, otherwise undefined.
300
329
  */
301
- declare const selectTXByKey: <TR, T extends Transaction<TR>>(transactionsPool: TransactionPool<TR, T>, key: string) => T | undefined;
330
+ declare const selectTxByKey: <TR, T extends Transaction<TR>>(transactionsPool: TransactionPool<TR, T>, key: string) => T | undefined;
302
331
  /**
303
- * Selects all transactions initiated by a specific wallet address.
332
+ * Selects all transactions initiated by a specific wallet address, sorted chronologically.
304
333
  * @template TR - The type of the tracker identifier.
305
334
  * @template T - The transaction type.
306
- * @param {TransactionPool<TR, T>} transactionsPool - The entire pool of transactions from the store.
335
+ * @param {TransactionPool<TR, T>} transactionsPool - The entire transaction pool from the store.
307
336
  * @param {string} from - The wallet address (`from` address) to filter transactions by.
308
337
  * @returns {T[]} An array of transactions associated with the given wallet.
309
338
  */
310
339
  declare const selectAllTransactionsByActiveWallet: <TR, T extends Transaction<TR>>(transactionsPool: TransactionPool<TR, T>, from: string) => T[];
311
340
  /**
312
- * Selects all pending transactions initiated by a specific wallet address.
341
+ * Selects all pending transactions for a specific wallet address, sorted chronologically.
313
342
  * @template TR - The type of the tracker identifier.
314
343
  * @template T - The transaction type.
315
- * @param {TransactionPool<TR, T>} transactionsPool - The entire pool of transactions from the store.
344
+ * @param {TransactionPool<TR, T>} transactionsPool - The entire transaction pool from the store.
316
345
  * @param {string} from - The wallet address (`from` address) to filter transactions by.
317
- * @returns {T[]} An array of pending transactions associated with the given wallet.
346
+ * @returns {T[]} An array of pending transactions for the given wallet.
318
347
  */
319
348
  declare const selectPendingTransactionsByActiveWallet: <TR, T extends Transaction<TR>>(transactionsPool: TransactionPool<TR, T>, from: string) => T[];
320
349
 
350
+ /**
351
+ * Creates the main Pulsar store for transaction tracking.
352
+ *
353
+ * This function sets up a Zustand store with persistence, combining the core
354
+ * transaction slice with adapter-specific logic to handle the entire lifecycle
355
+ * of a transaction.
356
+ *
357
+ * @template TR - The type of the tracker identifier (e.g., a string enum).
358
+ * @template T - The specific transaction type, extending the base `Transaction`.
359
+ * @template A - The type for the adapter-specific context or API.
360
+ *
361
+ * @param {object} config - Configuration object for creating the store.
362
+ * @param {function} [config.onSucceedCallbacks] - Optional async callback executed on transaction success.
363
+ * @param {TxAdapter<TR, T, A>[]} config.adapters - An array of adapters for different transaction types or chains.
364
+ * @param {PersistOptions<ITxTrackingStore<TR, T, A>>} [options] - Configuration for the Zustand persist middleware.
365
+ * @returns A fully configured Zustand store instance.
366
+ */
321
367
  declare function createPulsarStore<TR, T extends Transaction<TR>, A>({ onSucceedCallbacks, adapters, ...options }: {
322
368
  onSucceedCallbacks?: (tx: T) => Promise<void> | void;
323
369
  adapters: TxAdapter<TR, T, A>[];
324
- } & PersistOptions<ITxTrackingStore<TR, T, A>>): Omit<zustand.StoreApi<ITxTrackingStore<TR, T, A>>, "persist"> & {
370
+ } & PersistOptions<ITxTrackingStore<TR, T, A>>): Omit<zustand.StoreApi<ITxTrackingStore<TR, T, A>>, "setState" | "persist"> & {
371
+ setState(partial: ITxTrackingStore<TR, T, A> | Partial<ITxTrackingStore<TR, T, A>> | ((state: ITxTrackingStore<TR, T, A>) => ITxTrackingStore<TR, T, A> | Partial<ITxTrackingStore<TR, T, A>>), replace?: false | undefined): unknown;
372
+ setState(state: ITxTrackingStore<TR, T, A> | ((state: ITxTrackingStore<TR, T, A>) => ITxTrackingStore<TR, T, A>), replace: true): unknown;
325
373
  persist: {
326
- setOptions: (options: Partial<PersistOptions<ITxTrackingStore<TR, T, A>, ITxTrackingStore<TR, T, A>>>) => void;
374
+ setOptions: (options: Partial<PersistOptions<ITxTrackingStore<TR, T, A>, ITxTrackingStore<TR, T, A>, unknown>>) => void;
327
375
  clearStorage: () => void;
328
376
  rehydrate: () => Promise<void> | void;
329
377
  hasHydrated: () => boolean;
330
378
  onHydrate: (fn: (state: ITxTrackingStore<TR, T, A>) => void) => () => void;
331
379
  onFinishHydration: (fn: (state: ITxTrackingStore<TR, T, A>) => void) => () => void;
332
- getOptions: () => Partial<PersistOptions<ITxTrackingStore<TR, T, A>, ITxTrackingStore<TR, T, A>>>;
380
+ getOptions: () => Partial<PersistOptions<ITxTrackingStore<TR, T, A>, ITxTrackingStore<TR, T, A>, unknown>>;
333
381
  };
334
382
  };
335
383
 
336
384
  /**
337
- * @file This file provides a utility for creating a bounded Zustand hook from a vanilla store.
338
- * This is a recommended pattern for using vanilla stores with React to ensure full type safety.
385
+ * @file This file provides a utility for creating a type-safe, bounded Zustand hook from a vanilla store.
386
+ * This pattern is recommended by the official Zustand documentation to ensure full type
387
+ * safety when integrating vanilla stores with React.
339
388
  *
340
- * @see https://zustand.docs.pmnd.rs/guides/typescript#bounded-usestore-hook-for-vanilla-stores
389
+ * @see https://docs.pmnd.rs/zustand/guides/typescript#bounded-usestore-hook-for-vanilla-stores
341
390
  */
342
391
 
343
392
  /**
344
- * A utility type to infer the state shape from a Zustand store type.
345
- * @template S The type of the Zustand store (`StoreApi`).
393
+ * A utility type that infers the state shape from a Zustand `StoreApi`.
394
+ * It extracts the return type of the `getState` method.
395
+ * @template S - The type of the Zustand store (`StoreApi`).
346
396
  */
347
397
  type ExtractState<S> = S extends {
348
- getState: () => infer X;
349
- } ? X : never;
398
+ getState: () => infer T;
399
+ } ? T : never;
350
400
  /**
351
- * Creates a bounded `useStore` hook from a vanilla Zustand store instance.
352
- * The returned hook is fully typed and can be used with or without a selector.
401
+ * Creates a bounded `useStore` hook from a vanilla Zustand store.
402
+ *
403
+ * This function takes a vanilla Zustand store instance and returns a React hook
404
+ * that is pre-bound to that store. This approach provides a cleaner API and
405
+ * enhances type inference, eliminating the need to pass the store instance
406
+ * on every use.
407
+ *
408
+ * The returned hook supports two signatures:
409
+ * 1. `useBoundedStore()`: Selects the entire state.
410
+ * 2. `useBoundedStore(selector)`: Selects a slice of the state, returning only what the selector function specifies.
353
411
  *
354
- * @template S The type of the Zustand store (`StoreApi`).
355
- * @param {S} store - The vanilla Zustand store instance.
356
- * @returns {function} A hook that can be called with an optional selector function.
357
- * - When called with a selector (`useBoundedStore(state => state.someValue)`), it returns the selected slice of the state.
412
+ * @template S - The type of the Zustand store (`StoreApi`).
413
+ * @param {S} store - The vanilla Zustand store instance to bind the hook to.
414
+ * @returns {function} A fully typed React hook for accessing the store's state.
358
415
  */
359
416
  declare const createBoundedUseStore: <S extends StoreApi<unknown>>(store: S) => {
360
417
  (): ExtractState<S>;
@@ -362,63 +419,99 @@ declare const createBoundedUseStore: <S extends StoreApi<unknown>>(store: S) =>
362
419
  };
363
420
 
364
421
  /**
365
- * @file This file contains a generic utility for creating a polling mechanism to track asynchronous tasks,
366
- * such as API-based transaction status checks (e.g., for Gelato or Safe).
422
+ * @file This file provides a generic utility for creating a polling mechanism to track
423
+ * asynchronous tasks, such as API-based transaction status checks (e.g., for Gelato or Safe).
367
424
  */
368
425
 
426
+ /**
427
+ * Defines the parameters for the fetcher function used within the polling tracker.
428
+ * The fetcher is the core logic that performs the actual API call.
429
+ * @template R - The expected type of the successful API response.
430
+ * @template T - The type of the transaction object being tracked.
431
+ */
432
+ type PollingFetcherParams<R, T> = {
433
+ /** The transaction object being tracked. */
434
+ tx: T;
435
+ /** A callback to stop the polling mechanism, typically called on success or terminal failure. */
436
+ stopPolling: (options?: {
437
+ withoutRemoving?: boolean;
438
+ }) => void;
439
+ /** Callback to be invoked when the fetcher determines the transaction has succeeded. */
440
+ onSuccess: (response: R) => void;
441
+ /** Callback to be invoked when the fetcher determines the transaction has failed. */
442
+ onFailure: (response?: R) => void;
443
+ /** Optional callback for each successful poll, useful for updating UI with intermediate states. */
444
+ onIntervalTick?: (response: R) => void;
445
+ /** Optional callback for when a transaction is replaced (e.g., speed-up). */
446
+ onReplaced?: (response: R) => void;
447
+ };
369
448
  /**
370
449
  * Defines the configuration object for the `initializePollingTracker` function.
371
- * @template R The expected type of the successful API response from the fetcher.
372
- * @template T The type of the transaction object being tracked.
373
- * @template TR The type of the tracker identifier used in the `Transaction` type.
374
- */
375
- type InitializePollingTracker<R, T, TR> = {
376
- /** The transaction object to be tracked. It must include `txKey` and an optional `pending` status. */
377
- tx: T & Pick<Transaction<TR>, 'txKey'> & {
378
- pending?: boolean;
379
- };
380
- /** The function that performs the actual data fetching (e.g., an API call). */
381
- fetcher: (params: {
382
- /** The transaction object being tracked. */
383
- tx: T;
384
- /** A callback to stop the polling mechanism, typically called on success or terminal failure. */
385
- clearWatch: (withoutRemoving?: boolean) => void;
386
- /** Callback to be invoked when the fetcher determines the transaction has succeeded. */
387
- onSucceed: (response: R) => void;
388
- /** Callback to be invoked when the fetcher determines the transaction has failed. */
389
- onFailed: (response: R) => void;
390
- /** Optional callback for each successful poll, useful for updating UI with intermediate states. */
391
- onIntervalTick?: (response: R) => void;
392
- /** Optional callback for when a transaction is replaced by another. */
393
- onReplaced?: (response: R) => void;
394
- }) => Promise<Response>;
450
+ * @template R - The expected type of the successful API response.
451
+ * @template T - The type of the transaction object.
452
+ * @template TR - The type of the tracker identifier.
453
+ */
454
+ type PollingTrackerConfig<R, T, TR> = {
455
+ /** The transaction object to be tracked. It must include `txKey` and `pending` status. */
456
+ tx: T & Pick<Transaction<TR>, 'txKey' | 'pending'>;
457
+ /** The function that performs the data fetching (e.g., an API call) on each interval. */
458
+ fetcher: (params: PollingFetcherParams<R, T>) => Promise<void>;
459
+ /** Callback to be invoked when the transaction successfully completes. */
460
+ onSuccess: (response: R) => void;
461
+ /** Callback to be invoked when the transaction fails. */
462
+ onFailure: (response?: R) => void;
395
463
  /** Optional callback executed once when the tracker is initialized. */
396
464
  onInitialize?: () => void;
397
- /** Callback to be invoked when the transaction has succeeded. */
398
- onSucceed: (response: R) => void;
399
- /** Callback to be invoked when the transaction has failed. */
400
- onFailed: (response: R) => void;
401
465
  /** Optional callback for each successful poll. */
402
466
  onIntervalTick?: (response: R) => void;
403
467
  /** Optional callback for when a transaction is replaced. */
404
468
  onReplaced?: (response: R) => void;
405
469
  /** Optional function to remove the transaction from the main pool, typically after polling stops. */
406
- removeTxFromPool?: (taskId: string) => void;
470
+ removeTxFromPool?: (txKey: string) => void;
407
471
  /** The interval (in milliseconds) between polling attempts. Defaults to 5000ms. */
408
472
  pollingInterval?: number;
409
473
  /** The number of consecutive failed fetches before stopping the tracker. Defaults to 10. */
410
- retryCount?: number;
474
+ maxRetries?: number;
411
475
  };
412
476
  /**
413
477
  * Initializes a generic polling tracker that repeatedly calls a fetcher function
414
- * to monitor the status of a transaction or any asynchronous task.
478
+ * to monitor the status of an asynchronous task.
479
+ *
480
+ * This function handles the lifecycle of polling, including starting, stopping,
481
+ * and automatic termination after a certain number of failed attempts.
415
482
  *
416
- * @template R The expected type of the successful API response.
483
+ * @template R The expected type of the API response.
417
484
  * @template T The type of the transaction object.
418
485
  * @template TR The type of the tracker identifier.
419
- * @param {InitializePollingTracker<R, T, TR>} params - The configuration object for the tracker.
420
- * @returns {Promise<void>} A promise that resolves when the tracker is set up (note: polling happens asynchronously).
486
+ * @param {PollingTrackerConfig<R, T, TR>} config - The configuration for the tracker.
421
487
  */
422
- declare function initializePollingTracker<R, T, TR>({ onInitialize, tx, removeTxFromPool, fetcher, onFailed, onIntervalTick, onSucceed, pollingInterval, retryCount, onReplaced, }: InitializePollingTracker<R, T, TR>): Promise<void>;
488
+ declare function initializePollingTracker<R, T, TR>(config: PollingTrackerConfig<R, T, TR>): void;
489
+
490
+ /**
491
+ * @file This file contains a utility function for selecting a specific transaction adapter from a list.
492
+ */
493
+
494
+ /**
495
+ * Selects a transaction adapter from a list based on a provided key.
496
+ *
497
+ * This function searches through an array of `TxAdapter` instances and returns the one
498
+ * that matches the given `adapterKey`. If no specific adapter is found, it logs a warning
499
+ * and returns the first adapter in the array as a fallback. This fallback mechanism
500
+ * ensures that the system can still function, but it highlights a potential configuration issue.
501
+ *
502
+ * @template TR - The type of the tracker identifier.
503
+ * @template T - The transaction type, extending the base `Transaction`.
504
+ * @template A - The type for the adapter-specific context or API.
505
+ *
506
+ * @param {object} params - The parameters for the selection.
507
+ * @param {TransactionAdapter} params.adapterKey - The key of the desired adapter.
508
+ * @param {TxAdapter<TR, T, A>[]} params.adapters - An array of available transaction adapters.
509
+ *
510
+ * @returns {TxAdapter<TR, T, A> | undefined} The found transaction adapter, the fallback adapter, or undefined if the adapters array is empty.
511
+ */
512
+ declare const selectAdapterByKey: <TR, T extends Transaction<TR>, A>({ adapterKey, adapters, }: {
513
+ adapterKey: TransactionAdapter;
514
+ adapters: TxAdapter<TR, T, A>[];
515
+ }) => TxAdapter<TR, T, A> | undefined;
423
516
 
424
- export { type BaseTransaction, type EvmTransaction, type IInitializeTxTrackingStore, type ITxTrackingStore, type InitialTransaction, type InitialTransactionParams, type InitializePollingTracker, type SolanaTransaction, type StarknetTransaction, type StoreSlice, type Transaction, TransactionAdapter, type TransactionPool, TransactionStatus, type TxAdapter, createBoundedUseStore, createPulsarStore, initializePollingTracker, initializeTxTrackingStore, selectAllTransactions, selectAllTransactionsByActiveWallet, selectPendingTransactions, selectPendingTransactionsByActiveWallet, selectTXByKey };
517
+ export { type BaseTransaction, type EvmTransaction, type IInitializeTxTrackingStore, type ITxTrackingStore, type InitialTransaction, type InitialTransactionParams, type PollingTrackerConfig, type SolanaTransaction, type StarknetTransaction, type StoreSlice, type Transaction, TransactionAdapter, type TransactionPool, TransactionStatus, type TxActions, type TxAdapter, createBoundedUseStore, createPulsarStore, initializePollingTracker, initializeTxTrackingStore, selectAdapterByKey, selectAllTransactions, selectAllTransactionsByActiveWallet, selectPendingTransactions, selectPendingTransactionsByActiveWallet, selectTxByKey };