@etherkit/viem-tx-tracker 0.0.2 → 0.0.4

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/src/index.ts CHANGED
@@ -1,15 +1,8 @@
1
1
  // Types
2
- export type {
3
- BlockTag,
4
- ExpectedEvent,
5
- NonceOption,
6
- TrackedRawTransactionParameters,
7
- TrackedSendTransactionParameters,
8
- TrackedTransaction,
9
- TrackedWalletClient,
10
- TrackedWriteContractParameters,
11
- TransactionMetadata,
12
- } from './types.js';
2
+ export type * from './types.js';
13
3
 
14
4
  // Factory
15
- export {createTrackedWalletClient} from './TrackedWalletClient.js';
5
+ export {
6
+ createTrackedWalletClient,
7
+ type TrackedWalletClientBuilder,
8
+ } from './TrackedWalletClient.js';
package/src/types.ts CHANGED
@@ -28,100 +28,57 @@ export type BlockTag = 'latest' | 'pending' | 'earliest' | 'safe' | 'finalized';
28
28
  */
29
29
  export type NonceOption = number | BlockTag;
30
30
 
31
- /**
32
- * Expected event for success detection.
33
- * Even if tx hash changes due to replacement, we can detect success by watching for this event.
34
- */
35
- export interface ExpectedEvent {
36
- address: Address;
37
- eventName: string;
38
- args?: Record<string, unknown>;
39
- }
31
+ export type ExpectedUpdate =
32
+ | {
33
+ address: `0x${string}`;
34
+ event: {topics: `0x${string}`[]};
35
+ }
36
+ | {
37
+ address: `0x${string}`;
38
+ call: {data: `0x${string}`; result: `0x${string}`};
39
+ };
40
40
 
41
41
  /**
42
42
  * Metadata that can be attached to a transaction for tracking purposes.
43
43
  * All fields are optional and extensible.
44
44
  */
45
45
  export interface TransactionMetadata {
46
- /**
47
- * Optional custom ID - if provided, used instead of tx hash for tracking.
48
- * Useful for correlating transactions with business logic (e.g., order IDs).
49
- */
50
46
  id?: string;
51
-
52
- /**
53
- * Human-readable title for the transaction.
54
- */
55
- title?: string;
56
-
57
- /**
58
- * Detailed description of what the transaction does.
59
- */
47
+ name?: string;
48
+ args?: any[];
60
49
  description?: string;
61
-
62
- /**
63
- * Expected event signature/filter for success detection.
64
- * Even if tx hash changes due to replacement, we can detect success by watching for this event.
65
- */
66
- expectedEvent?: ExpectedEvent;
67
-
68
- /**
69
- * Extensible: user can add any additional fields.
70
- */
50
+ expectedUpdate?: ExpectedUpdate;
71
51
  [key: string]: unknown;
72
52
  }
73
53
 
74
54
  /**
75
- * A tracked transaction record with all relevant information for tracking.
55
+ * Conditional type that makes metadata required or optional based on TMetadata.
56
+ * If TMetadata includes undefined (e.g., `MyMeta | undefined`), metadata is optional.
57
+ * Otherwise, metadata is required.
76
58
  */
77
- export interface TrackedTransaction<
78
- M extends TransactionMetadata = TransactionMetadata,
79
- > {
80
- /**
81
- * Tracking ID - either metadata.id or auto-generated UUID.
82
- */
83
- trackingId: string;
84
-
85
- /**
86
- * The transaction hash once known.
87
- */
88
- txHash: Hash;
59
+ export type MetadataField<TMetadata> = undefined extends TMetadata
60
+ ? {metadata?: TMetadata}
61
+ : {metadata: TMetadata};
89
62
 
90
- /**
91
- * Sender address.
92
- */
93
- from: Address;
94
-
95
- /**
96
- * Transaction nonce (actual nonce from fetched tx, or intended if fetch failed).
97
- */
98
- nonce: number;
99
-
100
- /**
101
- * Chain ID.
102
- */
103
- chainId: number;
104
-
105
- /**
106
- * The full metadata provided by the caller.
107
- */
108
- metadata: M;
109
-
110
- /**
111
- * Timestamp when the transaction was initiated (ms since epoch).
112
- */
113
- initiatedAt: number;
114
-
115
- /**
116
- * The original transaction request data.
117
- */
118
- request: unknown;
63
+ /**
64
+ * A tracked transaction record with all relevant information for tracking.
65
+ * The metadata field type matches what was provided to the TrackedWalletClient.
66
+ */
67
+ export interface TrackedTransaction<TMetadata> {
68
+ chainId?: number;
69
+ readonly hash: `0x${string}`;
70
+ readonly from: `0x${string}`;
71
+ nonce?: number;
72
+ readonly broadcastTimestampMs: number;
73
+ readonly metadata: TMetadata;
119
74
  }
120
75
 
121
76
  /**
122
- * Extended WriteContractParameters with optional metadata and flexible nonce.
77
+ * Extended WriteContractParameters with metadata and flexible nonce.
78
+ * Metadata is required unless TMetadata includes undefined.
123
79
  */
124
80
  export type TrackedWriteContractParameters<
81
+ TMetadata,
125
82
  TAbi extends Abi | readonly unknown[] = Abi,
126
83
  TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'> =
127
84
  ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
@@ -144,11 +101,6 @@ export type TrackedWriteContractParameters<
144
101
  >,
145
102
  'nonce'
146
103
  > & {
147
- /**
148
- * Optional metadata to attach to the transaction for tracking.
149
- */
150
- metadata?: TransactionMetadata;
151
-
152
104
  /**
153
105
  * Nonce option:
154
106
  * - number: exact nonce to use
@@ -156,12 +108,14 @@ export type TrackedWriteContractParameters<
156
108
  * - undefined: fetch nonce using 'pending' (default)
157
109
  */
158
110
  nonce?: NonceOption;
159
- };
111
+ } & MetadataField<TMetadata>;
160
112
 
161
113
  /**
162
- * Extended SendTransactionParameters with optional metadata and flexible nonce.
114
+ * Extended SendTransactionParameters with metadata and flexible nonce.
115
+ * Metadata is required unless TMetadata includes undefined.
163
116
  */
164
117
  export type TrackedSendTransactionParameters<
118
+ TMetadata,
165
119
  TChain extends Chain | undefined = Chain | undefined,
166
120
  TAccount extends Account | undefined = Account | undefined,
167
121
  TChainOverride extends Chain | undefined = Chain | undefined,
@@ -169,11 +123,6 @@ export type TrackedSendTransactionParameters<
169
123
  SendTransactionParameters<TChain, TAccount, TChainOverride>,
170
124
  'nonce'
171
125
  > & {
172
- /**
173
- * Optional metadata to attach to the transaction for tracking.
174
- */
175
- metadata?: TransactionMetadata;
176
-
177
126
  /**
178
127
  * Nonce option:
179
128
  * - number: exact nonce to use
@@ -181,28 +130,27 @@ export type TrackedSendTransactionParameters<
181
130
  * - undefined: fetch nonce using 'pending' (default)
182
131
  */
183
132
  nonce?: NonceOption;
184
- };
133
+ } & MetadataField<TMetadata>;
185
134
 
186
135
  /**
187
- * Parameters for sendRawTransaction with optional metadata.
136
+ * Parameters for sendRawTransaction with metadata.
188
137
  * The serialized transaction already contains from/nonce which will be decoded.
138
+ * Metadata is required unless TMetadata includes undefined.
189
139
  */
190
- export interface TrackedRawTransactionParameters {
140
+ export type TrackedRawTransactionParameters<TMetadata> = {
191
141
  /**
192
142
  * The RLP-encoded signed transaction.
193
143
  */
194
144
  serializedTransaction: TransactionSerialized;
195
-
196
- /**
197
- * Optional metadata to attach to the transaction for tracking.
198
- */
199
- metadata?: TransactionMetadata;
200
- }
145
+ } & MetadataField<TMetadata>;
201
146
 
202
147
  /**
203
148
  * A wallet client wrapper that tracks transactions with metadata.
149
+ * TMetadata is the first type parameter and is mandatory - it determines
150
+ * whether metadata is required or optional on transaction calls.
204
151
  */
205
152
  export interface TrackedWalletClient<
153
+ TMetadata,
206
154
  TTransport extends Transport = Transport,
207
155
  TChain extends Chain | undefined = Chain | undefined,
208
156
  TAccount extends Account | undefined = Account | undefined,
@@ -222,7 +170,7 @@ export interface TrackedWalletClient<
222
170
  // ============================================
223
171
 
224
172
  /**
225
- * Write to a contract with optional metadata tracking.
173
+ * Write to a contract with metadata tracking.
226
174
  * Returns immediately after broadcast with the transaction hash.
227
175
  */
228
176
  writeContract<
@@ -236,6 +184,7 @@ export interface TrackedWalletClient<
236
184
  TChainOverride extends Chain | undefined = undefined,
237
185
  >(
238
186
  args: TrackedWriteContractParameters<
187
+ TMetadata,
239
188
  TAbi,
240
189
  TFunctionName,
241
190
  TArgs,
@@ -246,19 +195,26 @@ export interface TrackedWalletClient<
246
195
  ): Promise<Hash>;
247
196
 
248
197
  /**
249
- * Send a transaction with optional metadata tracking.
198
+ * Send a transaction with metadata tracking.
250
199
  * Returns immediately after broadcast with the transaction hash.
251
200
  */
252
201
  sendTransaction<TChainOverride extends Chain | undefined = undefined>(
253
- args: TrackedSendTransactionParameters<TChain, TAccount, TChainOverride>,
202
+ args: TrackedSendTransactionParameters<
203
+ TMetadata,
204
+ TChain,
205
+ TAccount,
206
+ TChainOverride
207
+ >,
254
208
  ): Promise<Hash>;
255
209
 
256
210
  /**
257
- * Send a signed raw transaction with optional metadata tracking.
211
+ * Send a signed raw transaction with metadata tracking.
258
212
  * The nonce and from address are decoded from the serialized transaction.
259
213
  * Returns immediately after broadcast with the transaction hash.
260
214
  */
261
- sendRawTransaction(args: TrackedRawTransactionParameters): Promise<Hash>;
215
+ sendRawTransaction(
216
+ args: TrackedRawTransactionParameters<TMetadata>,
217
+ ): Promise<Hash>;
262
218
 
263
219
  // ============================================
264
220
  // Sync methods (wait for confirmation, return receipt)
@@ -279,6 +235,7 @@ export interface TrackedWalletClient<
279
235
  TChainOverride extends Chain | undefined = undefined,
280
236
  >(
281
237
  args: TrackedWriteContractParameters<
238
+ TMetadata,
282
239
  TAbi,
283
240
  TFunctionName,
284
241
  TArgs,
@@ -293,7 +250,12 @@ export interface TrackedWalletClient<
293
250
  * Returns the transaction receipt after the transaction is confirmed.
294
251
  */
295
252
  sendTransactionSync<TChainOverride extends Chain | undefined = undefined>(
296
- args: TrackedSendTransactionParameters<TChain, TAccount, TChainOverride>,
253
+ args: TrackedSendTransactionParameters<
254
+ TMetadata,
255
+ TChain,
256
+ TAccount,
257
+ TChainOverride
258
+ >,
297
259
  ): Promise<TransactionReceipt>;
298
260
 
299
261
  /**
@@ -301,7 +263,7 @@ export interface TrackedWalletClient<
301
263
  * Returns the transaction receipt after the transaction is confirmed.
302
264
  */
303
265
  sendRawTransactionSync(
304
- args: TrackedRawTransactionParameters,
266
+ args: TrackedRawTransactionParameters<TMetadata>,
305
267
  ): Promise<TransactionReceipt>;
306
268
 
307
269
  // ============================================
@@ -311,11 +273,11 @@ export interface TrackedWalletClient<
311
273
  /**
312
274
  * Subscribe to transaction broadcast events.
313
275
  * Called immediately after a transaction is successfully broadcast.
314
- * @param listener - Callback function receiving TrackedTransaction
276
+ * @param listener - Callback function receiving TrackedTransaction with TMetadata
315
277
  * @returns Unsubscribe function
316
278
  */
317
279
  onTransactionBroadcasted(
318
- listener: (event: TrackedTransaction) => void,
280
+ listener: (event: TrackedTransaction<TMetadata>) => void,
319
281
  ): () => void;
320
282
 
321
283
  /**
@@ -323,6 +285,6 @@ export interface TrackedWalletClient<
323
285
  * @param listener - The same listener function passed to onTransactionBroadcasted
324
286
  */
325
287
  offTransactionBroadcasted(
326
- listener: (event: TrackedTransaction) => void,
288
+ listener: (event: TrackedTransaction<TMetadata>) => void,
327
289
  ): void;
328
290
  }