@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.
@@ -17,13 +17,13 @@ import {
17
17
  import {Emitter} from 'radiate';
18
18
  import type {
19
19
  BlockTag,
20
+ MetadataField,
20
21
  NonceOption,
21
22
  TrackedRawTransactionParameters,
22
23
  TrackedSendTransactionParameters,
23
24
  TrackedTransaction,
24
25
  TrackedWalletClient,
25
26
  TrackedWriteContractParameters,
26
- TransactionMetadata,
27
27
  } from './types.js';
28
28
 
29
29
  /**
@@ -71,400 +71,484 @@ interface TransactionContext {
71
71
  intendedNonce: number;
72
72
  }
73
73
 
74
+ /**
75
+ * Infer transport type from WalletClient
76
+ */
77
+ type InferTransport<T> =
78
+ T extends WalletClient<infer TTransport, any, any> ? TTransport : Transport;
79
+
80
+ /**
81
+ * Infer chain type from WalletClient
82
+ */
83
+ type InferChain<T> =
84
+ T extends WalletClient<any, infer TChain, any> ? TChain : Chain | undefined;
85
+
86
+ /**
87
+ * Infer account type from WalletClient
88
+ */
89
+ type InferAccount<T> =
90
+ T extends WalletClient<any, any, infer TAccount>
91
+ ? TAccount
92
+ : Account | undefined;
93
+
94
+ /**
95
+ * Builder interface returned by createTrackedWalletClient for the curried API.
96
+ */
97
+ export interface TrackedWalletClientBuilder<TMetadata> {
98
+ /**
99
+ * Create the tracked wallet client using the provided wallet and public clients.
100
+ *
101
+ * @param walletClient - The underlying viem WalletClient
102
+ * @param publicClient - A PublicClient for nonce fetching and tx verification
103
+ * @returns A TrackedWalletClient instance
104
+ */
105
+ using<TClient extends WalletClient>(
106
+ walletClient: TClient,
107
+ publicClient: PublicClient,
108
+ ): TrackedWalletClient<
109
+ TMetadata,
110
+ InferTransport<TClient>,
111
+ InferChain<TClient>,
112
+ InferAccount<TClient>
113
+ >;
114
+ }
115
+
74
116
  /**
75
117
  * Create a tracked wallet client that wraps a viem WalletClient.
76
118
  *
77
119
  * The tracked client provides the same API as WalletClient but with:
78
- * - Optional metadata field for transaction tracking
120
+ * - Metadata field for transaction tracking (required unless TMetadata includes undefined)
79
121
  * - Automatic nonce fetching (with 'pending' by default)
80
122
  * - Post-broadcast transaction verification
81
- * - TODO: Event emission for tracking
123
+ * - Event emission for tracking
124
+ *
125
+ * @typeParam TMetadata - The metadata type. Use `MyMeta | undefined` to make metadata optional.
126
+ * @returns A builder with a `.using()` method to provide the wallet and public clients
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * // With required metadata
131
+ * const tracked = createTrackedWalletClient<{purpose: string}>()
132
+ * .using(walletClient, publicClient);
82
133
  *
83
- * @param walletClient - The underlying viem WalletClient
84
- * @param publicClient - A PublicClient for nonce fetching and tx verification
85
- * @returns A TrackedWalletClient instance
134
+ * // With optional metadata
135
+ * const tracked = createTrackedWalletClient<{purpose: string} | undefined>()
136
+ * .using(walletClient, publicClient);
137
+ * ```
86
138
  */
87
139
  export function createTrackedWalletClient<
88
- TTransport extends Transport = Transport,
89
- TChain extends Chain | undefined = Chain | undefined,
90
- TAccount extends Account | undefined = Account | undefined,
91
- >(
92
- walletClient: WalletClient<TTransport, TChain, TAccount>,
93
- publicClient: PublicClient,
94
- ): TrackedWalletClient<TTransport, TChain, TAccount> {
95
- // Create emitter for transaction broadcast events
96
- const emitter = new Emitter<{
97
- 'transaction:broadcasted': TrackedTransaction;
98
- }>();
140
+ TMetadata,
141
+ >(): TrackedWalletClientBuilder<TMetadata> {
142
+ return {
143
+ using<TClient extends WalletClient>(
144
+ walletClient: TClient,
145
+ publicClient: PublicClient,
146
+ ): TrackedWalletClient<
147
+ TMetadata,
148
+ InferTransport<TClient>,
149
+ InferChain<TClient>,
150
+ InferAccount<TClient>
151
+ > {
152
+ // Type aliases for internal use
153
+ type TTransport = InferTransport<TClient>;
154
+ type TChain = InferChain<TClient>;
155
+ type TAccount = InferAccount<TClient>;
156
+
157
+ // Create emitter for transaction broadcast events
158
+ const emitter = new Emitter<{
159
+ 'transaction:broadcasted': TrackedTransaction<TMetadata>;
160
+ }>();
161
+
162
+ /**
163
+ * Resolve the nonce to use for a transaction.
164
+ *
165
+ * @param nonceOption - The nonce option provided by the caller
166
+ * @param from - The sender address
167
+ * @returns The resolved nonce number
168
+ */
169
+ async function resolveNonce(
170
+ nonceOption: NonceOption | undefined,
171
+ from: Address,
172
+ ): Promise<number> {
173
+ if (typeof nonceOption === 'number') {
174
+ // Explicit number - use as-is
175
+ return nonceOption;
176
+ }
177
+
178
+ // Block tag (string) or undefined - fetch from chain
179
+ const blockTag = isBlockTag(nonceOption) ? nonceOption : 'pending';
180
+ return await publicClient.getTransactionCount({
181
+ address: from,
182
+ blockTag,
183
+ });
184
+ }
99
185
 
100
- /**
101
- * Resolve the nonce to use for a transaction.
102
- *
103
- * @param nonceOption - The nonce option provided by the caller
104
- * @param from - The sender address
105
- * @returns The resolved nonce number
106
- */
107
- async function resolveNonce(
108
- nonceOption: NonceOption | undefined,
109
- from: Address,
110
- ): Promise<number> {
111
- if (typeof nonceOption === 'number') {
112
- // Explicit number - use as-is
113
- return nonceOption;
114
- }
115
-
116
- // Block tag (string) or undefined - fetch from chain
117
- const blockTag = isBlockTag(nonceOption) ? nonceOption : 'pending';
118
- return await publicClient.getTransactionCount({
119
- address: from,
120
- blockTag,
121
- });
122
- }
186
+ /**
187
+ * Extract common transaction context (account, nonce) from request args.
188
+ * This is the shared logic between all transaction methods.
189
+ *
190
+ * @param args - The transaction args containing account and nonce options
191
+ * @returns TransactionContext with resolved from address and nonce
192
+ */
193
+ async function extractTransactionContext(args: {
194
+ account?: Account | Address;
195
+ nonce?: NonceOption;
196
+ }): Promise<TransactionContext> {
197
+ // Get account/from address
198
+ const account = args.account ?? walletClient.account;
199
+ const from = resolveAccountAddress(account);
200
+
201
+ if (!from) {
202
+ throw new Error(
203
+ '[TrackedWalletClient] No account available. ' +
204
+ 'Provide an account in the request or configure the wallet client with an account.',
205
+ );
206
+ }
207
+
208
+ // Resolve nonce
209
+ const intendedNonce = await resolveNonce(args.nonce, from);
210
+
211
+ return {from, intendedNonce};
212
+ }
123
213
 
124
- /**
125
- * Extract common transaction context (account, nonce) from request args.
126
- * This is the shared logic between all transaction methods.
127
- *
128
- * @param args - The transaction args containing account and nonce options
129
- * @returns TransactionContext with resolved from address and nonce
130
- */
131
- async function extractTransactionContext(args: {
132
- account?: Account | Address;
133
- nonce?: NonceOption;
134
- }): Promise<TransactionContext> {
135
- // Get account/from address
136
- const account = args.account ?? walletClient.account;
137
- const from = resolveAccountAddress(account);
138
-
139
- if (!from) {
140
- throw new Error(
141
- '[TrackedWalletClient] No account available. ' +
142
- 'Provide an account in the request or configure the wallet client with an account.',
143
- );
144
- }
145
-
146
- // Resolve nonce
147
- const intendedNonce = await resolveNonce(args.nonce, from);
148
-
149
- return {from, intendedNonce};
150
- }
214
+ /**
215
+ * Extract transaction context from a serialized (signed) transaction.
216
+ * Parses the transaction and recovers the sender address.
217
+ *
218
+ * @param serializedTransaction - The RLP-encoded signed transaction
219
+ * @returns TransactionContext with from address and nonce
220
+ */
221
+ async function extractRawTransactionContext(
222
+ serializedTransaction: TransactionSerialized,
223
+ ): Promise<TransactionContext> {
224
+ // Parse the serialized transaction to get the nonce
225
+ const parsedTx = parseTransaction(serializedTransaction);
226
+
227
+ if (parsedTx.nonce === undefined) {
228
+ throw new Error(
229
+ '[TrackedWalletClient] Could not extract nonce from serialized transaction.',
230
+ );
231
+ }
232
+
233
+ // Recover the sender address from the signature
234
+ const from = await recoverTransactionAddress({
235
+ serializedTransaction,
236
+ });
237
+
238
+ return {
239
+ from,
240
+ intendedNonce: parsedTx.nonce,
241
+ };
242
+ }
151
243
 
152
- /**
153
- * Extract transaction context from a serialized (signed) transaction.
154
- * Parses the transaction and recovers the sender address.
155
- *
156
- * @param serializedTransaction - The RLP-encoded signed transaction
157
- * @returns TransactionContext with from address and nonce
158
- */
159
- async function extractRawTransactionContext(
160
- serializedTransaction: TransactionSerialized,
161
- ): Promise<TransactionContext> {
162
- // Parse the serialized transaction to get the nonce
163
- const parsedTx = parseTransaction(serializedTransaction);
164
-
165
- if (parsedTx.nonce === undefined) {
166
- throw new Error(
167
- '[TrackedWalletClient] Could not extract nonce from serialized transaction.',
168
- );
169
- }
170
-
171
- // Recover the sender address from the signature
172
- const from = await recoverTransactionAddress({
173
- serializedTransaction,
174
- });
175
-
176
- return {
177
- from,
178
- intendedNonce: parsedTx.nonce,
179
- };
180
- }
244
+ /**
245
+ * Fetch the transaction after broadcast to verify nonce.
246
+ * Logs a warning if the nonce was overridden or if tx cannot be found.
247
+ *
248
+ * @param hash - The transaction hash
249
+ * @param intendedNonce - The nonce we intended to use
250
+ * @returns The actual nonce, or the intended nonce if fetch failed
251
+ */
252
+ async function verifyTransactionNonce(
253
+ hash: Hash,
254
+ intendedNonce: number,
255
+ ): Promise<number> {
256
+ try {
257
+ const tx = await publicClient.getTransaction({hash});
258
+ const actualNonce = tx.nonce;
259
+
260
+ if (actualNonce !== intendedNonce) {
261
+ console.warn(
262
+ `[TrackedWalletClient] Nonce mismatch: intended ${intendedNonce}, actual ${actualNonce}. ` +
263
+ `Wallet may have overridden the nonce.`,
264
+ );
265
+ }
266
+
267
+ return actualNonce;
268
+ } catch (fetchError) {
269
+ // Transaction not found in mempool/chain yet
270
+ console.warn(
271
+ `[TrackedWalletClient] Could not fetch tx ${hash} after broadcast. ` +
272
+ `It may not be in the mempool yet.`,
273
+ );
274
+ return intendedNonce;
275
+ }
276
+ }
181
277
 
182
- /**
183
- * Fetch the transaction after broadcast to verify nonce.
184
- * Logs a warning if the nonce was overridden or if tx cannot be found.
185
- *
186
- * @param hash - The transaction hash
187
- * @param intendedNonce - The nonce we intended to use
188
- * @returns The actual nonce, or the intended nonce if fetch failed
189
- */
190
- async function verifyTransactionNonce(
191
- hash: Hash,
192
- intendedNonce: number,
193
- ): Promise<number> {
194
- try {
195
- const tx = await publicClient.getTransaction({hash});
196
- const actualNonce = tx.nonce;
197
-
198
- if (actualNonce !== intendedNonce) {
199
- console.warn(
200
- `[TrackedWalletClient] Nonce mismatch: intended ${intendedNonce}, actual ${actualNonce}. ` +
201
- `Wallet may have overridden the nonce.`,
202
- );
278
+ /**
279
+ * Create a tracked transaction record.
280
+ */
281
+ function createTrackedTransactionRecord(
282
+ txHash: Hash,
283
+ from: Address,
284
+ nonce: number,
285
+ metadata: TMetadata,
286
+ ): TrackedTransaction<TMetadata> {
287
+ return {
288
+ hash: txHash,
289
+ from,
290
+ nonce,
291
+ chainId: walletClient.chain?.id,
292
+ metadata,
293
+ broadcastTimestampMs: Date.now(),
294
+ };
203
295
  }
204
296
 
205
- return actualNonce;
206
- } catch (fetchError) {
207
- // Transaction not found in mempool/chain yet
208
- console.warn(
209
- `[TrackedWalletClient] Could not fetch tx ${hash} after broadcast. ` +
210
- `It may not be in the mempool yet.`,
211
- );
212
- return intendedNonce;
213
- }
214
- }
297
+ /**
298
+ * Common wrapper for transaction methods that broadcast (sendTransaction, writeContract).
299
+ * Handles nonce resolution, underlying call, post-broadcast verification, and tracking record creation.
300
+ */
301
+ async function executeTrackedTransaction<T, R>(args: {
302
+ account?: Account | Address;
303
+ nonce?: NonceOption;
304
+ metadata: TMetadata;
305
+ restArgs: T;
306
+ execute: (argsWithNonce: T & {nonce: number}) => Promise<R>;
307
+ extractHash: (result: R) => Hash;
308
+ }): Promise<R> {
309
+ const {metadata, restArgs, execute, extractHash} = args;
310
+
311
+ // Extract common context
312
+ const {from, intendedNonce} = await extractTransactionContext(args);
313
+
314
+ // Execute the underlying transaction with nonce injected
315
+ const result = await execute({
316
+ ...restArgs,
317
+ nonce: intendedNonce,
318
+ } as T & {
319
+ nonce: number;
320
+ });
321
+ const hash = extractHash(result);
322
+
323
+ // Verify transaction and get actual nonce
324
+ const actualNonce = await verifyTransactionNonce(hash, intendedNonce);
325
+
326
+ // Create tracked transaction record
327
+ const trackedTx = createTrackedTransactionRecord(
328
+ hash,
329
+ from,
330
+ actualNonce,
331
+ metadata,
332
+ );
215
333
 
216
- /**
217
- * Create a tracked transaction record.
218
- */
219
- function createTrackedTransaction<M extends TransactionMetadata>(
220
- txHash: Hash,
221
- from: Address,
222
- nonce: number,
223
- metadata: M | undefined,
224
- request: unknown,
225
- ): TrackedTransaction<M> {
226
- return {
227
- hash: txHash,
228
- from,
229
- nonce,
230
- chainId: walletClient.chain?.id,
231
- metadata: (metadata ?? {}) as M,
232
- broadcastTimestampMs: Date.now(),
233
- };
234
- }
334
+ // Emit transaction broadcasted event
335
+ emitter.emit('transaction:broadcasted', trackedTx);
235
336
 
236
- /**
237
- * Common wrapper for transaction methods that broadcast (sendTransaction, writeContract).
238
- * Handles nonce resolution, underlying call, post-broadcast verification, and tracking record creation.
239
- */
240
- async function executeTrackedTransaction<T, R>(args: {
241
- account?: Account | Address;
242
- nonce?: NonceOption;
243
- metadata?: TransactionMetadata;
244
- restArgs: T;
245
- execute: (argsWithNonce: T & {nonce: number}) => Promise<R>;
246
- extractHash: (result: R) => Hash;
247
- }): Promise<R> {
248
- const {metadata, restArgs, execute, extractHash} = args;
249
-
250
- // Extract common context
251
- const {from, intendedNonce} = await extractTransactionContext(args);
252
-
253
- // Execute the underlying transaction with nonce injected
254
- const result = await execute({...restArgs, nonce: intendedNonce} as T & {
255
- nonce: number;
256
- });
257
- const hash = extractHash(result);
258
-
259
- // Verify transaction and get actual nonce
260
- const actualNonce = await verifyTransactionNonce(hash, intendedNonce);
261
-
262
- // Create tracked transaction record
263
- const trackedTx = createTrackedTransaction(
264
- hash,
265
- from,
266
- actualNonce,
267
- metadata,
268
- restArgs,
269
- );
270
-
271
- // Emit transaction broadcasted event
272
- emitter.emit('transaction:broadcasted', trackedTx);
273
-
274
- return result;
275
- }
337
+ return result;
338
+ }
276
339
 
277
- /**
278
- * Common wrapper for raw transaction broadcasts (sendRawTransaction).
279
- * Decodes the transaction to extract from/nonce, broadcasts, and creates tracking record.
280
- */
281
- async function executeTrackedRawTransaction<R>(args: {
282
- serializedTransaction: TransactionSerialized;
283
- metadata?: TransactionMetadata;
284
- execute: () => Promise<R>;
285
- extractHash: (result: R) => Hash;
286
- }): Promise<R> {
287
- const {serializedTransaction, metadata, execute, extractHash} = args;
288
-
289
- // Extract context from the serialized transaction
290
- const {from, intendedNonce} = await extractRawTransactionContext(
291
- serializedTransaction,
292
- );
293
-
294
- // Execute the broadcast
295
- const result = await execute();
296
- const hash = extractHash(result);
297
-
298
- // For raw transactions, the nonce is already embedded, so no verification needed
299
- // (wallet cannot override nonce in an already-signed transaction)
300
-
301
- // Create tracked transaction record
302
- const trackedTx = createTrackedTransaction(
303
- hash,
304
- from,
305
- intendedNonce,
306
- metadata,
307
- {serializedTransaction},
308
- );
309
-
310
- // Emit transaction broadcasted event
311
- emitter.emit('transaction:broadcasted', trackedTx);
312
-
313
- return result;
314
- }
340
+ /**
341
+ * Common wrapper for raw transaction broadcasts (sendRawTransaction).
342
+ * Decodes the transaction to extract from/nonce, broadcasts, and creates tracking record.
343
+ */
344
+ async function executeTrackedRawTransaction<R>(args: {
345
+ serializedTransaction: TransactionSerialized;
346
+ metadata: TMetadata;
347
+ execute: () => Promise<R>;
348
+ extractHash: (result: R) => Hash;
349
+ }): Promise<R> {
350
+ const {serializedTransaction, metadata, execute, extractHash} = args;
351
+
352
+ // Extract context from the serialized transaction
353
+ const {from, intendedNonce} = await extractRawTransactionContext(
354
+ serializedTransaction,
355
+ );
315
356
 
316
- return {
317
- walletClient,
318
- publicClient,
319
-
320
- // ============================================
321
- // Async methods (return hash)
322
- // ============================================
323
-
324
- async writeContract<
325
- const TAbi extends Abi | readonly unknown[],
326
- TFunctionName extends ContractFunctionName<
327
- TAbi,
328
- 'nonpayable' | 'payable'
329
- >,
330
- TArgs extends ContractFunctionArgs<
331
- TAbi,
332
- 'nonpayable' | 'payable',
333
- TFunctionName
334
- >,
335
- TChainOverride extends Chain | undefined = undefined,
336
- >(
337
- args: TrackedWriteContractParameters<
338
- TAbi,
339
- TFunctionName,
340
- TArgs,
341
- TChain,
342
- TAccount,
343
- TChainOverride
344
- >,
345
- ): Promise<Hash> {
346
- const {metadata, nonce, ...writeArgs} = args;
347
-
348
- return executeTrackedTransaction({
349
- account: normalizeAccount(args.account),
350
- nonce,
351
- metadata,
352
- restArgs: writeArgs,
353
- execute: (argsWithNonce) =>
354
- walletClient.writeContract(argsWithNonce as any),
355
- extractHash: (hash) => hash,
356
- });
357
- },
357
+ // Execute the broadcast
358
+ const result = await execute();
359
+ const hash = extractHash(result);
358
360
 
359
- async sendTransaction<TChainOverride extends Chain | undefined = undefined>(
360
- args: TrackedSendTransactionParameters<TChain, TAccount, TChainOverride>,
361
- ): Promise<Hash> {
362
- const {metadata, nonce, ...sendArgs} = args;
363
-
364
- return executeTrackedTransaction({
365
- account: normalizeAccount(args.account),
366
- nonce,
367
- metadata,
368
- restArgs: sendArgs,
369
- execute: (argsWithNonce) =>
370
- walletClient.sendTransaction(argsWithNonce as any),
371
- extractHash: (hash) => hash,
372
- });
373
- },
361
+ // For raw transactions, the nonce is already embedded, so no verification needed
362
+ // (wallet cannot override nonce in an already-signed transaction)
374
363
 
375
- async sendRawTransaction(
376
- args: TrackedRawTransactionParameters,
377
- ): Promise<Hash> {
378
- const {metadata, serializedTransaction} = args;
379
-
380
- return executeTrackedRawTransaction({
381
- serializedTransaction,
382
- metadata,
383
- execute: () => walletClient.sendRawTransaction({serializedTransaction}),
384
- extractHash: (hash) => hash,
385
- });
386
- },
364
+ // Create tracked transaction record
365
+ const trackedTx = createTrackedTransactionRecord(
366
+ hash,
367
+ from,
368
+ intendedNonce,
369
+ metadata,
370
+ );
387
371
 
388
- // ============================================
389
- // Sync methods (return receipt, wait for confirmation)
390
- // ============================================
391
-
392
- async writeContractSync<
393
- const TAbi extends Abi | readonly unknown[],
394
- TFunctionName extends ContractFunctionName<
395
- TAbi,
396
- 'nonpayable' | 'payable'
397
- >,
398
- TArgs extends ContractFunctionArgs<
399
- TAbi,
400
- 'nonpayable' | 'payable',
401
- TFunctionName
402
- >,
403
- TChainOverride extends Chain | undefined = undefined,
404
- >(
405
- args: TrackedWriteContractParameters<
406
- TAbi,
407
- TFunctionName,
408
- TArgs,
409
- TChain,
410
- TAccount,
411
- TChainOverride
412
- >,
413
- ): Promise<TransactionReceipt> {
414
- const {metadata, nonce, ...writeArgs} = args;
415
-
416
- return executeTrackedTransaction({
417
- account: normalizeAccount(args.account),
418
- nonce,
419
- metadata,
420
- restArgs: writeArgs,
421
- execute: (argsWithNonce) =>
422
- walletClient.writeContractSync(argsWithNonce as any),
423
- extractHash: (receipt) => receipt.transactionHash,
424
- });
425
- },
372
+ // Emit transaction broadcasted event
373
+ emitter.emit('transaction:broadcasted', trackedTx);
426
374
 
427
- async sendTransactionSync<
428
- TChainOverride extends Chain | undefined = undefined,
429
- >(
430
- args: TrackedSendTransactionParameters<TChain, TAccount, TChainOverride>,
431
- ): Promise<TransactionReceipt> {
432
- const {metadata, nonce, ...sendArgs} = args;
433
-
434
- return executeTrackedTransaction({
435
- account: normalizeAccount(args.account),
436
- nonce,
437
- metadata,
438
- restArgs: sendArgs,
439
- execute: (argsWithNonce) =>
440
- walletClient.sendTransactionSync(argsWithNonce as any),
441
- extractHash: (receipt) => receipt.transactionHash,
442
- });
443
- },
375
+ return result;
376
+ }
444
377
 
445
- async sendRawTransactionSync(
446
- args: TrackedRawTransactionParameters,
447
- ): Promise<TransactionReceipt> {
448
- const {metadata, serializedTransaction} = args;
449
-
450
- return executeTrackedRawTransaction({
451
- serializedTransaction,
452
- metadata,
453
- execute: () =>
454
- walletClient.sendRawTransactionSync({serializedTransaction}),
455
- extractHash: (receipt) => receipt.transactionHash,
456
- });
378
+ return {
379
+ walletClient: walletClient as unknown as WalletClient<
380
+ TTransport,
381
+ TChain,
382
+ TAccount
383
+ >,
384
+ publicClient,
385
+
386
+ // ============================================
387
+ // Async methods (return hash)
388
+ // ============================================
389
+
390
+ async writeContract<
391
+ const TAbi extends Abi | readonly unknown[],
392
+ TFunctionName extends ContractFunctionName<
393
+ TAbi,
394
+ 'nonpayable' | 'payable'
395
+ >,
396
+ TArgs extends ContractFunctionArgs<
397
+ TAbi,
398
+ 'nonpayable' | 'payable',
399
+ TFunctionName
400
+ >,
401
+ TChainOverride extends Chain | undefined = undefined,
402
+ >(
403
+ args: TrackedWriteContractParameters<
404
+ TMetadata,
405
+ TAbi,
406
+ TFunctionName,
407
+ TArgs,
408
+ TChain,
409
+ TAccount,
410
+ TChainOverride
411
+ >,
412
+ ): Promise<Hash> {
413
+ const {metadata, nonce, ...writeArgs} = args;
414
+
415
+ return executeTrackedTransaction({
416
+ account: normalizeAccount(args.account),
417
+ nonce,
418
+ metadata: metadata as TMetadata,
419
+ restArgs: writeArgs,
420
+ execute: (argsWithNonce) =>
421
+ walletClient.writeContract(argsWithNonce as any),
422
+ extractHash: (hash) => hash,
423
+ });
424
+ },
425
+
426
+ async sendTransaction<
427
+ TChainOverride extends Chain | undefined = undefined,
428
+ >(
429
+ args: TrackedSendTransactionParameters<
430
+ TMetadata,
431
+ TChain,
432
+ TAccount,
433
+ TChainOverride
434
+ >,
435
+ ): Promise<Hash> {
436
+ const {metadata, nonce, ...sendArgs} = args;
437
+
438
+ return executeTrackedTransaction({
439
+ account: normalizeAccount(args.account),
440
+ nonce,
441
+ metadata: metadata as TMetadata,
442
+ restArgs: sendArgs,
443
+ execute: (argsWithNonce) =>
444
+ walletClient.sendTransaction(argsWithNonce as any),
445
+ extractHash: (hash) => hash,
446
+ });
447
+ },
448
+
449
+ async sendRawTransaction(
450
+ args: TrackedRawTransactionParameters<TMetadata>,
451
+ ): Promise<Hash> {
452
+ const {metadata, serializedTransaction} = args;
453
+
454
+ return executeTrackedRawTransaction({
455
+ serializedTransaction,
456
+ metadata: metadata as TMetadata,
457
+ execute: () =>
458
+ walletClient.sendRawTransaction({serializedTransaction}),
459
+ extractHash: (hash) => hash,
460
+ });
461
+ },
462
+
463
+ // ============================================
464
+ // Sync methods (return receipt, wait for confirmation)
465
+ // ============================================
466
+
467
+ async writeContractSync<
468
+ const TAbi extends Abi | readonly unknown[],
469
+ TFunctionName extends ContractFunctionName<
470
+ TAbi,
471
+ 'nonpayable' | 'payable'
472
+ >,
473
+ TArgs extends ContractFunctionArgs<
474
+ TAbi,
475
+ 'nonpayable' | 'payable',
476
+ TFunctionName
477
+ >,
478
+ TChainOverride extends Chain | undefined = undefined,
479
+ >(
480
+ args: TrackedWriteContractParameters<
481
+ TMetadata,
482
+ TAbi,
483
+ TFunctionName,
484
+ TArgs,
485
+ TChain,
486
+ TAccount,
487
+ TChainOverride
488
+ >,
489
+ ): Promise<TransactionReceipt> {
490
+ const {metadata, nonce, ...writeArgs} = args;
491
+
492
+ return executeTrackedTransaction({
493
+ account: normalizeAccount(args.account),
494
+ nonce,
495
+ metadata: metadata as TMetadata,
496
+ restArgs: writeArgs,
497
+ execute: (argsWithNonce) =>
498
+ walletClient.writeContractSync(argsWithNonce as any),
499
+ extractHash: (receipt) => receipt.transactionHash,
500
+ });
501
+ },
502
+
503
+ async sendTransactionSync<
504
+ TChainOverride extends Chain | undefined = undefined,
505
+ >(
506
+ args: TrackedSendTransactionParameters<
507
+ TMetadata,
508
+ TChain,
509
+ TAccount,
510
+ TChainOverride
511
+ >,
512
+ ): Promise<TransactionReceipt> {
513
+ const {metadata, nonce, ...sendArgs} = args;
514
+
515
+ return executeTrackedTransaction({
516
+ account: normalizeAccount(args.account),
517
+ nonce,
518
+ metadata: metadata as TMetadata,
519
+ restArgs: sendArgs,
520
+ execute: (argsWithNonce) =>
521
+ walletClient.sendTransactionSync(argsWithNonce as any),
522
+ extractHash: (receipt) => receipt.transactionHash,
523
+ });
524
+ },
525
+
526
+ async sendRawTransactionSync(
527
+ args: TrackedRawTransactionParameters<TMetadata>,
528
+ ): Promise<TransactionReceipt> {
529
+ const {metadata, serializedTransaction} = args;
530
+
531
+ return executeTrackedRawTransaction({
532
+ serializedTransaction,
533
+ metadata: metadata as TMetadata,
534
+ execute: () =>
535
+ walletClient.sendRawTransactionSync({serializedTransaction}),
536
+ extractHash: (receipt) => receipt.transactionHash,
537
+ });
538
+ },
539
+
540
+ // ============================================
541
+ // Event subscription methods
542
+ // ============================================
543
+
544
+ onTransactionBroadcasted: (
545
+ listener: (event: TrackedTransaction<TMetadata>) => void,
546
+ ) => emitter.on('transaction:broadcasted', listener),
547
+
548
+ offTransactionBroadcasted: (
549
+ listener: (event: TrackedTransaction<TMetadata>) => void,
550
+ ) => emitter.off('transaction:broadcasted', listener),
551
+ };
457
552
  },
458
-
459
- // ============================================
460
- // Event subscription methods
461
- // ============================================
462
-
463
- onTransactionBroadcasted: (listener: (event: TrackedTransaction) => void) =>
464
- emitter.on('transaction:broadcasted', listener),
465
-
466
- offTransactionBroadcasted: (
467
- listener: (event: TrackedTransaction) => void,
468
- ) => emitter.off('transaction:broadcasted', listener),
469
553
  };
470
554
  }