@etherkit/viem-tx-tracker 0.0.3 → 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
@@ -2,4 +2,7 @@
2
2
  export type * from './types.js';
3
3
 
4
4
  // Factory
5
- export {createTrackedWalletClient} from './TrackedWalletClient.js';
5
+ export {
6
+ createTrackedWalletClient,
7
+ type TrackedWalletClientBuilder,
8
+ } from './TrackedWalletClient.js';
package/src/types.ts CHANGED
@@ -51,24 +51,34 @@ export interface TransactionMetadata {
51
51
  [key: string]: unknown;
52
52
  }
53
53
 
54
+ /**
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.
58
+ */
59
+ export type MetadataField<TMetadata> = undefined extends TMetadata
60
+ ? {metadata?: TMetadata}
61
+ : {metadata: TMetadata};
62
+
54
63
  /**
55
64
  * A tracked transaction record with all relevant information for tracking.
65
+ * The metadata field type matches what was provided to the TrackedWalletClient.
56
66
  */
57
- export interface TrackedTransaction<
58
- M extends TransactionMetadata = TransactionMetadata,
59
- > {
67
+ export interface TrackedTransaction<TMetadata> {
60
68
  chainId?: number;
61
69
  readonly hash: `0x${string}`;
62
70
  readonly from: `0x${string}`;
63
71
  nonce?: number;
64
72
  readonly broadcastTimestampMs: number;
65
- readonly metadata: M;
73
+ readonly metadata: TMetadata;
66
74
  }
67
75
 
68
76
  /**
69
- * Extended WriteContractParameters with optional metadata and flexible nonce.
77
+ * Extended WriteContractParameters with metadata and flexible nonce.
78
+ * Metadata is required unless TMetadata includes undefined.
70
79
  */
71
80
  export type TrackedWriteContractParameters<
81
+ TMetadata,
72
82
  TAbi extends Abi | readonly unknown[] = Abi,
73
83
  TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'> =
74
84
  ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
@@ -91,11 +101,6 @@ export type TrackedWriteContractParameters<
91
101
  >,
92
102
  'nonce'
93
103
  > & {
94
- /**
95
- * Optional metadata to attach to the transaction for tracking.
96
- */
97
- metadata?: TransactionMetadata;
98
-
99
104
  /**
100
105
  * Nonce option:
101
106
  * - number: exact nonce to use
@@ -103,12 +108,14 @@ export type TrackedWriteContractParameters<
103
108
  * - undefined: fetch nonce using 'pending' (default)
104
109
  */
105
110
  nonce?: NonceOption;
106
- };
111
+ } & MetadataField<TMetadata>;
107
112
 
108
113
  /**
109
- * Extended SendTransactionParameters with optional metadata and flexible nonce.
114
+ * Extended SendTransactionParameters with metadata and flexible nonce.
115
+ * Metadata is required unless TMetadata includes undefined.
110
116
  */
111
117
  export type TrackedSendTransactionParameters<
118
+ TMetadata,
112
119
  TChain extends Chain | undefined = Chain | undefined,
113
120
  TAccount extends Account | undefined = Account | undefined,
114
121
  TChainOverride extends Chain | undefined = Chain | undefined,
@@ -116,11 +123,6 @@ export type TrackedSendTransactionParameters<
116
123
  SendTransactionParameters<TChain, TAccount, TChainOverride>,
117
124
  'nonce'
118
125
  > & {
119
- /**
120
- * Optional metadata to attach to the transaction for tracking.
121
- */
122
- metadata?: TransactionMetadata;
123
-
124
126
  /**
125
127
  * Nonce option:
126
128
  * - number: exact nonce to use
@@ -128,28 +130,27 @@ export type TrackedSendTransactionParameters<
128
130
  * - undefined: fetch nonce using 'pending' (default)
129
131
  */
130
132
  nonce?: NonceOption;
131
- };
133
+ } & MetadataField<TMetadata>;
132
134
 
133
135
  /**
134
- * Parameters for sendRawTransaction with optional metadata.
136
+ * Parameters for sendRawTransaction with metadata.
135
137
  * The serialized transaction already contains from/nonce which will be decoded.
138
+ * Metadata is required unless TMetadata includes undefined.
136
139
  */
137
- export interface TrackedRawTransactionParameters {
140
+ export type TrackedRawTransactionParameters<TMetadata> = {
138
141
  /**
139
142
  * The RLP-encoded signed transaction.
140
143
  */
141
144
  serializedTransaction: TransactionSerialized;
142
-
143
- /**
144
- * Optional metadata to attach to the transaction for tracking.
145
- */
146
- metadata?: TransactionMetadata;
147
- }
145
+ } & MetadataField<TMetadata>;
148
146
 
149
147
  /**
150
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.
151
151
  */
152
152
  export interface TrackedWalletClient<
153
+ TMetadata,
153
154
  TTransport extends Transport = Transport,
154
155
  TChain extends Chain | undefined = Chain | undefined,
155
156
  TAccount extends Account | undefined = Account | undefined,
@@ -169,7 +170,7 @@ export interface TrackedWalletClient<
169
170
  // ============================================
170
171
 
171
172
  /**
172
- * Write to a contract with optional metadata tracking.
173
+ * Write to a contract with metadata tracking.
173
174
  * Returns immediately after broadcast with the transaction hash.
174
175
  */
175
176
  writeContract<
@@ -183,6 +184,7 @@ export interface TrackedWalletClient<
183
184
  TChainOverride extends Chain | undefined = undefined,
184
185
  >(
185
186
  args: TrackedWriteContractParameters<
187
+ TMetadata,
186
188
  TAbi,
187
189
  TFunctionName,
188
190
  TArgs,
@@ -193,19 +195,26 @@ export interface TrackedWalletClient<
193
195
  ): Promise<Hash>;
194
196
 
195
197
  /**
196
- * Send a transaction with optional metadata tracking.
198
+ * Send a transaction with metadata tracking.
197
199
  * Returns immediately after broadcast with the transaction hash.
198
200
  */
199
201
  sendTransaction<TChainOverride extends Chain | undefined = undefined>(
200
- args: TrackedSendTransactionParameters<TChain, TAccount, TChainOverride>,
202
+ args: TrackedSendTransactionParameters<
203
+ TMetadata,
204
+ TChain,
205
+ TAccount,
206
+ TChainOverride
207
+ >,
201
208
  ): Promise<Hash>;
202
209
 
203
210
  /**
204
- * Send a signed raw transaction with optional metadata tracking.
211
+ * Send a signed raw transaction with metadata tracking.
205
212
  * The nonce and from address are decoded from the serialized transaction.
206
213
  * Returns immediately after broadcast with the transaction hash.
207
214
  */
208
- sendRawTransaction(args: TrackedRawTransactionParameters): Promise<Hash>;
215
+ sendRawTransaction(
216
+ args: TrackedRawTransactionParameters<TMetadata>,
217
+ ): Promise<Hash>;
209
218
 
210
219
  // ============================================
211
220
  // Sync methods (wait for confirmation, return receipt)
@@ -226,6 +235,7 @@ export interface TrackedWalletClient<
226
235
  TChainOverride extends Chain | undefined = undefined,
227
236
  >(
228
237
  args: TrackedWriteContractParameters<
238
+ TMetadata,
229
239
  TAbi,
230
240
  TFunctionName,
231
241
  TArgs,
@@ -240,7 +250,12 @@ export interface TrackedWalletClient<
240
250
  * Returns the transaction receipt after the transaction is confirmed.
241
251
  */
242
252
  sendTransactionSync<TChainOverride extends Chain | undefined = undefined>(
243
- args: TrackedSendTransactionParameters<TChain, TAccount, TChainOverride>,
253
+ args: TrackedSendTransactionParameters<
254
+ TMetadata,
255
+ TChain,
256
+ TAccount,
257
+ TChainOverride
258
+ >,
244
259
  ): Promise<TransactionReceipt>;
245
260
 
246
261
  /**
@@ -248,7 +263,7 @@ export interface TrackedWalletClient<
248
263
  * Returns the transaction receipt after the transaction is confirmed.
249
264
  */
250
265
  sendRawTransactionSync(
251
- args: TrackedRawTransactionParameters,
266
+ args: TrackedRawTransactionParameters<TMetadata>,
252
267
  ): Promise<TransactionReceipt>;
253
268
 
254
269
  // ============================================
@@ -258,11 +273,11 @@ export interface TrackedWalletClient<
258
273
  /**
259
274
  * Subscribe to transaction broadcast events.
260
275
  * Called immediately after a transaction is successfully broadcast.
261
- * @param listener - Callback function receiving TrackedTransaction
276
+ * @param listener - Callback function receiving TrackedTransaction with TMetadata
262
277
  * @returns Unsubscribe function
263
278
  */
264
279
  onTransactionBroadcasted(
265
- listener: (event: TrackedTransaction) => void,
280
+ listener: (event: TrackedTransaction<TMetadata>) => void,
266
281
  ): () => void;
267
282
 
268
283
  /**
@@ -270,6 +285,6 @@ export interface TrackedWalletClient<
270
285
  * @param listener - The same listener function passed to onTransactionBroadcasted
271
286
  */
272
287
  offTransactionBroadcasted(
273
- listener: (event: TrackedTransaction) => void,
288
+ listener: (event: TrackedTransaction<TMetadata>) => void,
274
289
  ): void;
275
290
  }