@tuwaio/pulsar-core 0.1.0 → 0.1.1
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.mts +102 -87
- package/dist/index.d.ts +102 -87
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,14 +4,14 @@ import { PersistOptions } from 'zustand/middleware';
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @file This file defines the core data structures and TypeScript types for the Pulsar transaction tracking engine.
|
|
7
|
-
* It
|
|
8
|
-
*
|
|
7
|
+
* It specifies the framework-agnostic models for transactions, their lifecycle statuses, and the interfaces for
|
|
8
|
+
* the Zustand-based store and chain-specific adapters.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* A utility type for creating modular Zustand store slices, enabling composable state management.
|
|
13
|
-
* @template T
|
|
14
|
-
* @template S
|
|
13
|
+
* @template T The state slice being defined.
|
|
14
|
+
* @template S The full store state that includes the slice `T`.
|
|
15
15
|
*/
|
|
16
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
|
/**
|
|
@@ -31,22 +31,27 @@ declare enum TransactionAdapter {
|
|
|
31
31
|
declare enum TransactionStatus {
|
|
32
32
|
/** The transaction failed to execute due to an on-chain error or rejection. */
|
|
33
33
|
Failed = "Failed",
|
|
34
|
-
/** The transaction was successfully mined and
|
|
34
|
+
/** The transaction was successfully mined and included in a block. */
|
|
35
35
|
Success = "Success",
|
|
36
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
40
|
* The fundamental structure for any transaction being tracked by Pulsar.
|
|
41
|
-
* This
|
|
42
|
-
* @template T
|
|
41
|
+
* This serves as the base upon which chain-specific transaction types are built.
|
|
42
|
+
* @template T The type of the tracker identifier (e.g., 'ethereum', 'gelato').
|
|
43
43
|
*/
|
|
44
44
|
type BaseTransaction<T> = {
|
|
45
|
-
/** A unique key identifying a re-executable action from the `TxActions` registry. */
|
|
46
|
-
actionKey?: string;
|
|
47
45
|
/** The chain identifier (e.g., 1 for Ethereum Mainnet, 'SN_MAIN' for Starknet). */
|
|
48
46
|
chainId: number | string;
|
|
49
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* User-facing description. Can be a single string for all states, or a tuple for specific states.
|
|
49
|
+
* @example
|
|
50
|
+
* // A single description for all states
|
|
51
|
+
* description: 'Swap 1 ETH for 1,500 USDC'
|
|
52
|
+
* // Specific descriptions for each state in order: [pending, success, error, replaced]
|
|
53
|
+
* description: ['Swapping...', 'Swapped Successfully', 'Swap Failed', 'Swap Replaced']
|
|
54
|
+
*/
|
|
50
55
|
description?: string | [string, string, string, string];
|
|
51
56
|
/** The error message if the transaction failed. */
|
|
52
57
|
errorMessage?: string;
|
|
@@ -66,7 +71,14 @@ type BaseTransaction<T> = {
|
|
|
66
71
|
pending: boolean;
|
|
67
72
|
/** The final on-chain status of the transaction. */
|
|
68
73
|
status?: TransactionStatus;
|
|
69
|
-
/**
|
|
74
|
+
/**
|
|
75
|
+
* User-facing title. Can be a single string for all states, or a tuple for specific states.
|
|
76
|
+
* @example
|
|
77
|
+
* // A single title for all states
|
|
78
|
+
* title: 'ETH/USDC Swap'
|
|
79
|
+
* // Specific titles for each state in order: [pending, success, error, replaced]
|
|
80
|
+
* title: ['Processing Swap', 'Swap Complete', 'Swap Error', 'Swap Replaced']
|
|
81
|
+
*/
|
|
70
82
|
title?: string | [string, string, string, string];
|
|
71
83
|
/** The specific tracker responsible for monitoring this transaction's status. */
|
|
72
84
|
tracker: T;
|
|
@@ -79,7 +91,7 @@ type BaseTransaction<T> = {
|
|
|
79
91
|
};
|
|
80
92
|
/**
|
|
81
93
|
* Represents an EVM-specific transaction, extending the base properties with EVM fields.
|
|
82
|
-
* @template T
|
|
94
|
+
* @template T The type of the tracker identifier.
|
|
83
95
|
*/
|
|
84
96
|
type EvmTransaction<T> = BaseTransaction<T> & {
|
|
85
97
|
adapter: TransactionAdapter.EVM;
|
|
@@ -102,7 +114,7 @@ type EvmTransaction<T> = BaseTransaction<T> & {
|
|
|
102
114
|
};
|
|
103
115
|
/**
|
|
104
116
|
* Represents a Solana-specific transaction, extending the base properties.
|
|
105
|
-
* @template T
|
|
117
|
+
* @template T The type of the tracker identifier.
|
|
106
118
|
*/
|
|
107
119
|
type SolanaTransaction<T> = BaseTransaction<T> & {
|
|
108
120
|
adapter: TransactionAdapter.SOLANA;
|
|
@@ -114,14 +126,14 @@ type SolanaTransaction<T> = BaseTransaction<T> & {
|
|
|
114
126
|
recentBlockhash?: string;
|
|
115
127
|
/** The slot in which the transaction was processed. */
|
|
116
128
|
slot?: number;
|
|
117
|
-
/** The number of confirmations
|
|
129
|
+
/** The number of confirmations received. `null` if the transaction is pending or unconfirmed. */
|
|
118
130
|
confirmations?: number | null;
|
|
119
|
-
/** The RPC URL used
|
|
131
|
+
/** The RPC URL used to submit and track this transaction. */
|
|
120
132
|
rpcUrl?: string;
|
|
121
133
|
};
|
|
122
134
|
/**
|
|
123
135
|
* Represents a Starknet-specific transaction, extending the base properties.
|
|
124
|
-
* @template T
|
|
136
|
+
* @template T The type of the tracker identifier.
|
|
125
137
|
*/
|
|
126
138
|
type StarknetTransaction<T> = BaseTransaction<T> & {
|
|
127
139
|
adapter: TransactionAdapter.Starknet;
|
|
@@ -137,38 +149,33 @@ type StarknetTransaction<T> = BaseTransaction<T> & {
|
|
|
137
149
|
};
|
|
138
150
|
/** A union type representing any possible transaction structure that Pulsar can handle. */
|
|
139
151
|
type Transaction<T> = EvmTransaction<T> | SolanaTransaction<T> | StarknetTransaction<T>;
|
|
140
|
-
/**
|
|
141
|
-
* A registry of functions that can be re-executed, keyed by `actionKey`.
|
|
142
|
-
* Used for implementing "Retry" functionality.
|
|
143
|
-
*/
|
|
144
|
-
type TxActions = Record<string, (...args: any[]) => Promise<unknown>>;
|
|
145
152
|
/**
|
|
146
153
|
* Represents the parameters required to initiate a new transaction tracking flow.
|
|
147
154
|
*/
|
|
148
|
-
type InitialTransactionParams = {
|
|
155
|
+
type InitialTransactionParams<A> = {
|
|
149
156
|
adapter: TransactionAdapter;
|
|
150
|
-
/**
|
|
151
|
-
|
|
152
|
-
/** A user-facing description for the transaction. */
|
|
157
|
+
/** The function that executes the on-chain action (e.g., sending a transaction) and returns a preliminary identifier like a hash. */
|
|
158
|
+
actionFunction: (...args: any[]) => Promise<A | undefined>;
|
|
159
|
+
/** A user-facing description for the transaction. Supports state-specific descriptions. */
|
|
153
160
|
description?: string | [string, string, string, string];
|
|
154
161
|
/** The target chain ID for the transaction. */
|
|
155
162
|
desiredChainID: number | string;
|
|
156
163
|
/** Any custom data to associate with the transaction. */
|
|
157
164
|
payload?: object;
|
|
158
|
-
/** A user-facing title for the transaction. */
|
|
165
|
+
/** A user-facing title for the transaction. Supports state-specific titles. */
|
|
159
166
|
title?: string | [string, string, string, string];
|
|
160
167
|
/** The application-specific type of the transaction. */
|
|
161
168
|
type: string;
|
|
162
169
|
/** If true, the detailed tracking modal will open automatically upon initiation. */
|
|
163
170
|
withTrackedModal?: boolean;
|
|
164
|
-
/**
|
|
171
|
+
/** The RPC URL to use for the transaction. Required for Solana transactions. */
|
|
165
172
|
rpcUrl?: string;
|
|
166
173
|
};
|
|
167
174
|
/**
|
|
168
175
|
* Represents a transaction in its temporary, pre-submission state.
|
|
169
176
|
* This is used for UI feedback while the transaction is being signed and sent.
|
|
170
177
|
*/
|
|
171
|
-
type InitialTransaction = InitialTransactionParams & {
|
|
178
|
+
type InitialTransaction<A> = InitialTransactionParams<A> & {
|
|
172
179
|
/** An error message if the initialization fails (e.g., user rejects signature). */
|
|
173
180
|
errorMessage?: string;
|
|
174
181
|
/** A flag indicating if the transaction is being processed (e.g., waiting for signature). */
|
|
@@ -180,9 +187,9 @@ type InitialTransaction = InitialTransactionParams & {
|
|
|
180
187
|
};
|
|
181
188
|
/**
|
|
182
189
|
* Defines the interface for a transaction adapter, which provides chain-specific logic and utilities.
|
|
183
|
-
* @template TR
|
|
184
|
-
* @template T
|
|
185
|
-
* @template A
|
|
190
|
+
* @template TR The type of the tracker identifier (e.g., a string enum).
|
|
191
|
+
* @template T The specific transaction type, extending `Transaction<TR>`.
|
|
192
|
+
* @template A The type of the key returned by the `actionFunction` (e.g., a transaction hash).
|
|
186
193
|
*/
|
|
187
194
|
type TxAdapter<TR, T extends Transaction<TR>, A> = {
|
|
188
195
|
/** The unique key identifying this adapter. */
|
|
@@ -192,18 +199,18 @@ type TxAdapter<TR, T extends Transaction<TR>, A> = {
|
|
|
192
199
|
walletAddress: string;
|
|
193
200
|
walletType: string;
|
|
194
201
|
};
|
|
195
|
-
/** Ensures the connected wallet is on the correct network for the transaction. */
|
|
202
|
+
/** Ensures the connected wallet is on the correct network for the transaction. Throws an error if the chain is mismatched. */
|
|
196
203
|
checkChainForTx: (chainId: string | number) => Promise<void>;
|
|
197
|
-
/** Determines the appropriate tracker and final `txKey`
|
|
204
|
+
/** Determines the appropriate tracker and final `txKey` from the result of an action. */
|
|
198
205
|
checkTransactionsTracker: (actionTxKey: A, walletType: string) => {
|
|
199
206
|
txKey: string;
|
|
200
207
|
tracker: TR;
|
|
201
208
|
};
|
|
202
|
-
/**
|
|
209
|
+
/** Selects and initializes the correct background tracker for a given transaction. */
|
|
203
210
|
checkAndInitializeTrackerInStore: (params: {
|
|
204
211
|
tx: T;
|
|
205
212
|
} & Pick<ITxTrackingStore<TR, T, A>, 'transactionsPool' | 'updateTxParams' | 'onSucceedCallbacks' | 'removeTxFromPool'>) => Promise<void>;
|
|
206
|
-
/** Returns the base URL for the blockchain explorer. */
|
|
213
|
+
/** Returns the base URL for the blockchain explorer for the current network. */
|
|
207
214
|
getExplorerUrl: () => string | undefined;
|
|
208
215
|
/** Optional: Fetches a name from a chain-specific name service (e.g., ENS). */
|
|
209
216
|
getName?: (address: string) => Promise<string | null>;
|
|
@@ -216,30 +223,32 @@ type TxAdapter<TR, T extends Transaction<TR>, A> = {
|
|
|
216
223
|
/** Optional: Logic to retry a failed transaction. */
|
|
217
224
|
retryTxAction?: (params: {
|
|
218
225
|
txKey: string;
|
|
219
|
-
tx: InitialTransactionParams
|
|
220
|
-
actions?: TxActions;
|
|
226
|
+
tx: InitialTransactionParams<A>;
|
|
221
227
|
onClose: (txKey?: string) => void;
|
|
222
228
|
} & Partial<Pick<ITxTrackingStore<TR, T, A>, 'handleTransaction'>>) => Promise<void>;
|
|
223
|
-
/**
|
|
229
|
+
/**
|
|
230
|
+
* Optional: Constructs a full explorer URL for a specific transaction.
|
|
231
|
+
* May require the full transaction pool to resolve details for replaced transactions.
|
|
232
|
+
*/
|
|
224
233
|
getExplorerTxUrl?: (transactionsPool: TransactionPool<TR, T>, txKey: string, replacedTxHash?: string) => string;
|
|
225
234
|
};
|
|
226
235
|
/**
|
|
227
236
|
* The complete interface for the Pulsar transaction tracking store.
|
|
228
|
-
* @template TR
|
|
229
|
-
* @template T
|
|
230
|
-
* @template A
|
|
237
|
+
* @template TR The type of the tracker identifier.
|
|
238
|
+
* @template T The transaction type.
|
|
239
|
+
* @template A The return type of the `actionFunction`.
|
|
231
240
|
*/
|
|
232
|
-
type ITxTrackingStore<TR, T extends Transaction<TR>, A> = IInitializeTxTrackingStore<TR, T> & {
|
|
241
|
+
type ITxTrackingStore<TR, T extends Transaction<TR>, A> = IInitializeTxTrackingStore<TR, T, A> & {
|
|
233
242
|
/**
|
|
234
|
-
* The
|
|
243
|
+
* The primary method for initiating and tracking a new transaction from start to finish.
|
|
235
244
|
* It manages UI state, executes the on-chain action, and initiates background tracking.
|
|
236
|
-
* @param params
|
|
245
|
+
* @param params The parameters for handling the transaction.
|
|
237
246
|
*/
|
|
238
247
|
handleTransaction: (params: {
|
|
239
248
|
/** The async function to execute (e.g., a smart contract write call). Must return a unique key or undefined. */
|
|
240
249
|
actionFunction: () => Promise<A | undefined>;
|
|
241
250
|
/** The metadata for the transaction. */
|
|
242
|
-
params: InitialTransactionParams
|
|
251
|
+
params: Omit<InitialTransactionParams<A>, 'actionFunction'>;
|
|
243
252
|
/** The default tracker to use if it cannot be determined automatically. */
|
|
244
253
|
defaultTracker?: TR;
|
|
245
254
|
}) => Promise<void>;
|
|
@@ -251,58 +260,64 @@ type ITxTrackingStore<TR, T extends Transaction<TR>, A> = IInitializeTxTrackingS
|
|
|
251
260
|
};
|
|
252
261
|
|
|
253
262
|
/**
|
|
254
|
-
* @file This file
|
|
255
|
-
*
|
|
263
|
+
* @file This file defines the core Zustand slice for managing the state of transactions. It includes the state,
|
|
264
|
+
* actions, and types necessary for initializing the store and performing CRUD operations on the transaction pool.
|
|
256
265
|
*/
|
|
257
266
|
|
|
258
267
|
/**
|
|
259
|
-
* Defines the structure of the transaction pool,
|
|
260
|
-
* @template TR
|
|
261
|
-
* @template T
|
|
268
|
+
* Defines the structure of the transaction pool, a key-value store of transactions indexed by their unique keys.
|
|
269
|
+
* @template TR The type of the tracker identifier.
|
|
270
|
+
* @template T The transaction type.
|
|
262
271
|
*/
|
|
263
272
|
type TransactionPool<TR, T extends Transaction<TR>> = Record<string, T>;
|
|
264
273
|
/**
|
|
265
|
-
* A utility type that
|
|
266
|
-
*
|
|
267
|
-
*
|
|
274
|
+
* A utility type that creates a union of all fields that can be safely updated
|
|
275
|
+
* on a transaction object via the `updateTxParams` action. This ensures type safety
|
|
276
|
+
* and prevents accidental modification of immutable properties.
|
|
277
|
+
* @template TR The type of the tracker identifier.
|
|
268
278
|
*/
|
|
269
279
|
type UpdatableTransactionFields<TR> = Partial<Pick<EvmTransaction<TR>, 'to' | 'nonce' | 'txKey' | 'pending' | 'hash' | 'status' | 'replacedTxHash' | 'errorMessage' | 'finishedTimestamp' | 'isTrackedModalOpen' | 'isError' | 'maxPriorityFeePerGas' | 'maxFeePerGas' | 'input' | 'value'>> & Partial<Pick<SolanaTransaction<TR>, 'slot' | 'confirmations' | 'fee' | 'instructions' | 'recentBlockhash' | 'rpcUrl'>>;
|
|
270
280
|
/**
|
|
271
|
-
*
|
|
272
|
-
* It includes the state and actions for managing
|
|
273
|
-
* @template TR
|
|
274
|
-
* @template T
|
|
281
|
+
* The interface for the base transaction tracking store slice.
|
|
282
|
+
* It includes the state and actions for managing the transaction lifecycle.
|
|
283
|
+
* @template TR The type of the tracker identifier.
|
|
284
|
+
* @template T The specific transaction type.
|
|
285
|
+
* @template A The return type of the initial action function.
|
|
275
286
|
*/
|
|
276
|
-
interface IInitializeTxTrackingStore<TR, T extends Transaction<TR
|
|
277
|
-
/**
|
|
287
|
+
interface IInitializeTxTrackingStore<TR, T extends Transaction<TR>, A> {
|
|
288
|
+
/** A callback function executed when any transaction successfully completes. */
|
|
278
289
|
onSucceedCallbacks?: (tx: T) => Promise<void> | void;
|
|
279
|
-
/** A pool of all transactions currently being tracked, indexed by
|
|
290
|
+
/** A pool of all transactions currently being tracked, indexed by `txKey`. */
|
|
280
291
|
transactionsPool: TransactionPool<TR, T>;
|
|
281
|
-
/** The
|
|
292
|
+
/** The `txKey` of the most recently added transaction. */
|
|
282
293
|
lastAddedTxKey?: string;
|
|
283
|
-
/** The state
|
|
284
|
-
initialTx?: InitialTransaction
|
|
285
|
-
/** Adds a new transaction to the tracking pool. */
|
|
286
|
-
addTxToPool: (tx:
|
|
287
|
-
/** Updates one or more
|
|
294
|
+
/** The state for a transaction being initiated, used for UI feedback before it's submitted to the chain. */
|
|
295
|
+
initialTx?: InitialTransaction<A>;
|
|
296
|
+
/** Adds a new transaction to the tracking pool and marks it as pending. */
|
|
297
|
+
addTxToPool: (tx: Transaction<TR>) => void;
|
|
298
|
+
/** Updates one or more properties of an existing transaction in the pool. */
|
|
288
299
|
updateTxParams: (txKey: string, fields: UpdatableTransactionFields<TR>) => void;
|
|
289
|
-
/** Removes a transaction from the tracking pool
|
|
300
|
+
/** Removes a transaction from the tracking pool by its key. */
|
|
290
301
|
removeTxFromPool: (txKey: string) => void;
|
|
291
|
-
/** Closes the tracking modal for a
|
|
302
|
+
/** Closes the tracking modal for a transaction and clears any initial transaction state. */
|
|
292
303
|
closeTxTrackedModal: (txKey?: string) => void;
|
|
293
|
-
/**
|
|
304
|
+
/** A selector function to retrieve the key of the last transaction added to the pool. */
|
|
294
305
|
getLastTxKey: () => string | undefined;
|
|
295
306
|
}
|
|
296
307
|
/**
|
|
297
|
-
* Creates a Zustand store slice
|
|
298
|
-
* This function is a slice creator
|
|
299
|
-
*
|
|
300
|
-
* @
|
|
301
|
-
* @
|
|
308
|
+
* Creates a Zustand store slice with the core logic for transaction state management.
|
|
309
|
+
* This function is a slice creator intended for use with Zustand's `create` function.
|
|
310
|
+
*
|
|
311
|
+
* @template TR The type of the tracker identifier.
|
|
312
|
+
* @template T The specific transaction type.
|
|
313
|
+
* @template A The return type of the initial action function.
|
|
314
|
+
* @param options Configuration for the store slice.
|
|
315
|
+
* @param options.onSucceedCallbacks An optional async callback to run when a transaction succeeds.
|
|
316
|
+
* @returns A Zustand store slice implementing `IInitializeTxTrackingStore`.
|
|
302
317
|
*/
|
|
303
|
-
declare function initializeTxTrackingStore<TR, T extends Transaction<TR
|
|
318
|
+
declare function initializeTxTrackingStore<TR, T extends Transaction<TR>, A>({ onSucceedCallbacks, }: {
|
|
304
319
|
onSucceedCallbacks?: (tx: T) => Promise<void> | void;
|
|
305
|
-
}): StoreSlice<IInitializeTxTrackingStore<TR, T>>;
|
|
320
|
+
}): StoreSlice<IInitializeTxTrackingStore<TR, T, A>>;
|
|
306
321
|
|
|
307
322
|
/**
|
|
308
323
|
* @file This file contains selector functions for deriving state from the transaction tracking store.
|
|
@@ -356,18 +371,18 @@ declare const selectPendingTransactionsByActiveWallet: <TR, T extends Transactio
|
|
|
356
371
|
/**
|
|
357
372
|
* Creates the main Pulsar store for transaction tracking.
|
|
358
373
|
*
|
|
359
|
-
* This function
|
|
360
|
-
*
|
|
361
|
-
* of a transaction.
|
|
374
|
+
* This function configures a Zustand store enhanced with persistence. It combines the core transaction management
|
|
375
|
+
* slice with a powerful orchestration logic that leverages chain-specific adapters to handle the entire
|
|
376
|
+
* lifecycle of a transaction—from initiation and chain validation to execution and background status tracking.
|
|
362
377
|
*
|
|
363
|
-
* @template TR
|
|
364
|
-
* @template T
|
|
365
|
-
* @template A
|
|
378
|
+
* @template TR The type of the tracker identifier (e.g., a string enum).
|
|
379
|
+
* @template T The specific transaction type, extending the base `Transaction`.
|
|
380
|
+
* @template A The type of the key returned by the `actionFunction` (e.g., a transaction hash).
|
|
366
381
|
*
|
|
367
|
-
* @param
|
|
368
|
-
* @param
|
|
369
|
-
* @param
|
|
370
|
-
* @param
|
|
382
|
+
* @param config Configuration object for creating the store.
|
|
383
|
+
* @param config.onSucceedCallbacks Optional async callback executed on transaction success.
|
|
384
|
+
* @param config.adapters An array of adapters for different chains or transaction types.
|
|
385
|
+
* @param options Configuration for the Zustand `persist` middleware.
|
|
371
386
|
* @returns A fully configured Zustand store instance.
|
|
372
387
|
*/
|
|
373
388
|
declare function createPulsarStore<TR, T extends Transaction<TR>, A>({ onSucceedCallbacks, adapters, ...options }: {
|
|
@@ -520,4 +535,4 @@ declare const selectAdapterByKey: <TR, T extends Transaction<TR>, A>({ adapterKe
|
|
|
520
535
|
adapters: TxAdapter<TR, T, A>[];
|
|
521
536
|
}) => TxAdapter<TR, T, A> | undefined;
|
|
522
537
|
|
|
523
|
-
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
|
|
538
|
+
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 TxAdapter, createBoundedUseStore, createPulsarStore, initializePollingTracker, initializeTxTrackingStore, selectAdapterByKey, selectAllTransactions, selectAllTransactionsByActiveWallet, selectPendingTransactions, selectPendingTransactionsByActiveWallet, selectTxByKey };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var immer=require('immer'),
|
|
1
|
+
'use strict';var immer=require('immer'),K=require('dayjs'),middleware=require('zustand/middleware'),vanilla=require('zustand/vanilla'),zustand=require('zustand');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var K__default=/*#__PURE__*/_interopDefault(K);function A({onSucceedCallbacks:n}){return (e,a)=>({onSucceedCallbacks:n,transactionsPool:{},lastAddedTxKey:void 0,initialTx:void 0,addTxToPool:t=>{e(i=>immer.produce(i,r=>{r.lastAddedTxKey=t.txKey,t.txKey&&(r.transactionsPool[t.txKey]={...t,pending:true});}));},updateTxParams:(t,i)=>{e(r=>immer.produce(r,s=>{let c=s.transactionsPool[t];c&&Object.assign(c,i);}));},removeTxFromPool:t=>{e(i=>immer.produce(i,r=>{delete r.transactionsPool[t];}));},closeTxTrackedModal:t=>{e(i=>immer.produce(i,r=>{t&&r.transactionsPool[t]&&(r.transactionsPool[t].isTrackedModalOpen=false),r.initialTx=void 0;}));},getLastTxKey:()=>a().lastAddedTxKey})}var k=n=>Object.values(n).sort((e,a)=>Number(e.localTimestamp)-Number(a.localTimestamp)),N=n=>k(n).filter(e=>e.pending),$=(n,e)=>n[e],b=(n,e)=>k(n).filter(a=>a.from.toLowerCase()===e.toLowerCase()),j=(n,e)=>b(n,e).filter(a=>a.pending);var y=({adapterKey:n,adapters:e})=>{if(!e||e.length===0){console.error("Adapter selection failed: The provided adapters array is empty.");return}let a=e.find(t=>t.key===n);return a||(console.warn(`No adapter found for key: "${n}". Falling back to the first available adapter: "${e[0].key}".`),e[0])};function Q({onSucceedCallbacks:n,adapters:e,...a}){return vanilla.createStore()(middleware.persist((t,i)=>({...A({onSucceedCallbacks:n})(t,i),initializeTransactionsPool:async()=>{let r=Object.values(i().transactionsPool).filter(s=>s.pending);await Promise.all(r.map(s=>y({adapterKey:s.adapter,adapters:e})?.checkAndInitializeTrackerInStore({tx:s,...i()})));},handleTransaction:async({defaultTracker:r,actionFunction:s,params:c})=>{let{desiredChainID:x,...m}=c,g=K__default.default().unix();t({initialTx:{...c,actionFunction:s,localTimestamp:g,isInitializing:true}});let l=y({adapterKey:m.adapter,adapters:e}),d=o=>{let u=o instanceof Error?o.message:String(o);t(T=>immer.produce(T,p=>{p.initialTx&&(p.initialTx.isInitializing=false,p.initialTx.errorMessage=u);}));};if(!l){let o=new Error("No adapter found for this transaction.");throw d(o),o}try{let{walletType:o,walletAddress:u}=l.getWalletInfo();await l.checkChainForTx(x);let T=await s();if(!T){t({initialTx:void 0});return}let{tracker:p,txKey:P}=l.checkTransactionsTracker(T,o),v={...m,walletType:o,from:u,tracker:p||r,chainId:x,localTimestamp:g,txKey:P,hash:p==="ethereum"?T:void 0,pending:!1,isTrackedModalOpen:c.withTrackedModal};i().addTxToPool(v),t(I=>immer.produce(I,R=>{R.initialTx&&(R.initialTx.isInitializing=!1,R.initialTx.lastTxKey=P);}));let h=i().transactionsPool[P];await l.checkAndInitializeTrackerInStore({tx:h,...i()});}catch(o){throw d(o),o}}}),{...a}))}var E=(t=>(t.EVM="evm",t.SOLANA="solana",t.Starknet="starknet",t))(E||{}),z=(t=>(t.Failed="Failed",t.Success="Success",t.Replaced="Replaced",t))(z||{});var ne=(n=>e=>zustand.useStore(n,e));var L=5e3,C=10;function ie(n){let{tx:e,fetcher:a,onInitialize:t,onSuccess:i,onFailure:r,onIntervalTick:s,onReplaced:c,removeTxFromPool:x,pollingInterval:m=L,maxRetries:g=C}=n;if(!e.pending)return;t?.();let l=g,d=true,o=T=>{d&&(d=false,x&&!T?.withoutRemoving&&x(e.txKey));};(async()=>{for(;d&&l>0;)try{if(await new Promise(T=>setTimeout(T,m)),!d)break;await a({tx:e,stopPolling:o,onSuccess:i,onFailure:r,onIntervalTick:s,onReplaced:c});}catch(T){console.error(`Polling fetcher for txKey ${e.txKey} threw an error:`,T),l--;}l<=0&&(console.warn(`Polling for txKey ${e.txKey} stopped after reaching the maximum number of retries.`),r(),o());})();}exports.TransactionAdapter=E;exports.TransactionStatus=z;exports.createBoundedUseStore=ne;exports.createPulsarStore=Q;exports.initializePollingTracker=ie;exports.initializeTxTrackingStore=A;exports.selectAdapterByKey=y;exports.selectAllTransactions=k;exports.selectAllTransactionsByActiveWallet=b;exports.selectPendingTransactions=N;exports.selectPendingTransactionsByActiveWallet=j;exports.selectTxByKey=$;//# sourceMappingURL=index.js.map
|
|
2
2
|
//# sourceMappingURL=index.js.map
|