@etherkit/viem-tx-tracker 0.0.5 → 0.0.6

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/types.ts CHANGED
@@ -60,6 +60,11 @@ export interface CreateTrackedWalletClientOptions<
60
60
  * (e.g., PopulatedMetadata, FunctionCallMetadata, or a union including FunctionCallMetadata).
61
61
  */
62
62
  populateMetadata?: TPopulate;
63
+ /**
64
+ * A clock function that returns the current time in milliseconds.
65
+ * Defaults to Date.now.
66
+ */
67
+ clock?: () => number;
63
68
  }
64
69
 
65
70
  /**
@@ -85,17 +90,97 @@ export type MetadataField<TMetadata> = undefined extends TMetadata
85
90
  : {metadata: TMetadata};
86
91
 
87
92
  /**
88
- * A tracked transaction record with all relevant information for tracking.
89
- * The metadata field type matches what was provided to the TrackedWalletClient.
93
+ * Access list type used in EIP-2930 and EIP-1559 transactions.
90
94
  */
91
- export interface TrackedTransaction<TMetadata> {
92
- chainId?: number;
95
+ export type AccessList = readonly {
96
+ address: Address;
97
+ storageKeys: readonly `0x${string}`[];
98
+ }[];
99
+
100
+ /**
101
+ * Base transaction fields present in all tracked transactions.
102
+ */
103
+ export type BaseTrackedTransaction<TMetadata> = {
104
+ readonly chainId?: number;
93
105
  readonly hash: `0x${string}`;
94
106
  readonly from: `0x${string}`;
95
- nonce?: number;
96
107
  readonly broadcastTimestampMs: number;
97
108
  readonly metadata: TMetadata;
98
- }
109
+ };
110
+
111
+ /**
112
+ * A fully known tracked transaction with all fields confirmed from chain.
113
+ * Emitted via transaction:fetched when tx data is fetched from chain,
114
+ * or immediately for sendRawTransaction where we can parse the tx.
115
+ *
116
+ * When known=true, all values are the actual confirmed values used by the chain.
117
+ */
118
+ export type KnownTrackedTransaction<TMetadata> =
119
+ BaseTrackedTransaction<TMetadata> & {
120
+ readonly known: true;
121
+ readonly to: `0x${string}` | null;
122
+ readonly nonce: number;
123
+ readonly value: bigint;
124
+ readonly data: `0x${string}`;
125
+ readonly gas: bigint;
126
+ } & (
127
+ | {
128
+ readonly txType: 'eip1559';
129
+ readonly chainId: number; // Required for EIP-1559
130
+ readonly maxFeePerGas: bigint;
131
+ readonly maxPriorityFeePerGas: bigint;
132
+ readonly accessList?: AccessList; // EIP-1559 can also have access lists
133
+ }
134
+ | {
135
+ readonly txType: 'legacy';
136
+ readonly chainId?: number; // Optional for legacy (pre-EIP-155 txs don't have it)
137
+ readonly gasPrice: bigint;
138
+ }
139
+ | {
140
+ readonly txType: 'eip2930';
141
+ readonly chainId: number; // Required for EIP-2930
142
+ readonly gasPrice: bigint;
143
+ readonly accessList: AccessList; // Required for EIP-2930
144
+ }
145
+ );
146
+
147
+ /**
148
+ * A partially known tracked transaction with intended/provided values.
149
+ * Emitted immediately via transaction:broadcasted.
150
+ *
151
+ * When known=false, values are what we intended/provided, but the wallet
152
+ * may have modified them (e.g., gas estimation, nonce override).
153
+ * All optional fields are populated if we have the data.
154
+ *
155
+ * txType is inferred from provided params:
156
+ * - maxFeePerGas provided → 'eip1559'
157
+ * - gasPrice + accessList provided → 'eip2930'
158
+ * - gasPrice only → 'legacy'
159
+ * - undefined → wallet will determine type
160
+ */
161
+ export type UnknownTrackedTransaction<TMetadata> =
162
+ BaseTrackedTransaction<TMetadata> & {
163
+ readonly known: false;
164
+ readonly txType?: 'eip1559' | 'legacy' | 'eip2930'; // Inferred from params if possible
165
+ readonly to?: `0x${string}` | null;
166
+ readonly nonce?: number;
167
+ readonly value?: bigint;
168
+ readonly data?: `0x${string}`;
169
+ readonly gas?: bigint;
170
+ readonly gasPrice?: bigint;
171
+ readonly maxFeePerGas?: bigint;
172
+ readonly maxPriorityFeePerGas?: bigint;
173
+ readonly accessList?: AccessList;
174
+ };
175
+
176
+ /**
177
+ * A tracked transaction - discriminated by 'known' field.
178
+ * - known=true: Values are confirmed from chain fetch
179
+ * - known=false: Values are intended/provided, may differ from actual
180
+ */
181
+ export type TrackedTransaction<TMetadata> =
182
+ | KnownTrackedTransaction<TMetadata>
183
+ | UnknownTrackedTransaction<TMetadata>;
99
184
 
100
185
  /**
101
186
  * Extended WriteContractParameters with metadata and flexible nonce.
@@ -387,24 +472,42 @@ export interface TrackedWalletClient<
387
472
  // ============================================
388
473
 
389
474
  /**
390
- * Subscribe to transaction broadcast events.
391
- * Called immediately after a transaction is successfully broadcast.
392
- * @param listener - Callback function receiving TrackedTransaction with TMetadata
475
+ * Subscribe to transaction events.
476
+ * @param event - The event type to subscribe to
477
+ * @param listener - Callback function receiving the event data
393
478
  * @returns Unsubscribe function
394
479
  */
395
- onTransactionBroadcasted(
396
- listener: (event: TrackedTransaction<TMetadata>) => void,
480
+ on<TEvent extends keyof TrackedWalletClientEvents<TMetadata>>(
481
+ event: TEvent,
482
+ listener: (data: TrackedWalletClientEvents<TMetadata>[TEvent]) => void,
397
483
  ): () => void;
398
484
 
399
485
  /**
400
- * Unsubscribe from transaction broadcast events.
401
- * @param listener - The same listener function passed to onTransactionBroadcasted
486
+ * Unsubscribe from transaction events.
487
+ * @param event - The event type to unsubscribe from
488
+ * @param listener - The same listener function passed to on
402
489
  */
403
- offTransactionBroadcasted(
404
- listener: (event: TrackedTransaction<TMetadata>) => void,
490
+ off<TEvent extends keyof TrackedWalletClientEvents<TMetadata>>(
491
+ event: TEvent,
492
+ listener: (data: TrackedWalletClientEvents<TMetadata>[TEvent]) => void,
405
493
  ): void;
406
494
  }
407
495
 
496
+ /**
497
+ * Event map for TrackedWalletClient events.
498
+ */
499
+ export type TrackedWalletClientEvents<TMetadata> = {
500
+ /**
501
+ * Emitted immediately after a transaction is successfully broadcast.
502
+ */
503
+ 'transaction:broadcasted': TrackedTransaction<TMetadata>;
504
+ /**
505
+ * Emitted when full transaction data is successfully fetched from chain.
506
+ * Not guaranteed to fire if fetch fails (tx not in mempool yet, network issues, etc.)
507
+ */
508
+ 'transaction:fetched': KnownTrackedTransaction<TMetadata>;
509
+ };
510
+
408
511
  /**
409
512
  * A wallet client wrapper that tracks transactions with auto-populated metadata.
410
513
  * TMetadata must be a type where FunctionCallMetadata is assignable to it.
@@ -534,20 +637,23 @@ export interface TrackedWalletClientAutoPopulate<
534
637
  // ============================================
535
638
 
536
639
  /**
537
- * Subscribe to transaction broadcast events.
538
- * Called immediately after a transaction is successfully broadcast.
539
- * @param listener - Callback function receiving TrackedTransaction with TMetadata
640
+ * Subscribe to transaction events.
641
+ * @param event - The event type to subscribe to
642
+ * @param listener - Callback function receiving the event data
540
643
  * @returns Unsubscribe function
541
644
  */
542
- onTransactionBroadcasted(
543
- listener: (event: TrackedTransaction<TMetadata>) => void,
645
+ on<TEvent extends keyof TrackedWalletClientEvents<TMetadata>>(
646
+ event: TEvent,
647
+ listener: (data: TrackedWalletClientEvents<TMetadata>[TEvent]) => void,
544
648
  ): () => void;
545
649
 
546
650
  /**
547
- * Unsubscribe from transaction broadcast events.
548
- * @param listener - The same listener function passed to onTransactionBroadcasted
651
+ * Unsubscribe from transaction events.
652
+ * @param event - The event type to unsubscribe from
653
+ * @param listener - The same listener function passed to on
549
654
  */
550
- offTransactionBroadcasted(
551
- listener: (event: TrackedTransaction<TMetadata>) => void,
655
+ off<TEvent extends keyof TrackedWalletClientEvents<TMetadata>>(
656
+ event: TEvent,
657
+ listener: (data: TrackedWalletClientEvents<TMetadata>[TEvent]) => void,
552
658
  ): void;
553
659
  }