@aztec/node-lib 3.0.0-canary.a9708bd → 3.0.0-devnet.3

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.
@@ -0,0 +1,122 @@
1
+ import {
2
+ createL1TxUtilsFromEthSigner as createL1TxUtilsFromEthSignerBase,
3
+ createL1TxUtilsFromViemWallet as createL1TxUtilsFromViemWalletBase,
4
+ } from '@aztec/ethereum';
5
+ import type { EthSigner, ExtendedViemWalletClient, L1TxUtilsConfig, ViemClient } from '@aztec/ethereum';
6
+ import {
7
+ createL1TxUtilsWithBlobsFromEthSigner as createL1TxUtilsWithBlobsFromEthSignerBase,
8
+ createL1TxUtilsWithBlobsFromViemWallet as createL1TxUtilsWithBlobsFromViemWalletBase,
9
+ } from '@aztec/ethereum/l1-tx-utils-with-blobs';
10
+ import { omit } from '@aztec/foundation/collection';
11
+ import { createLogger } from '@aztec/foundation/log';
12
+ import type { DateProvider } from '@aztec/foundation/timer';
13
+ import type { DataStoreConfig } from '@aztec/kv-store/config';
14
+ import { createStore } from '@aztec/kv-store/lmdb-v2';
15
+ import type { TelemetryClient } from '@aztec/telemetry-client';
16
+
17
+ import type { L1TxScope } from '../metrics/l1_tx_metrics.js';
18
+ import { L1TxMetrics } from '../metrics/l1_tx_metrics.js';
19
+ import { L1TxStore } from '../stores/l1_tx_store.js';
20
+
21
+ const L1_TX_STORE_NAME = 'l1-tx-utils';
22
+
23
+ /**
24
+ * Creates shared dependencies (logger, store, metrics) for L1TxUtils instances.
25
+ */
26
+ async function createSharedDeps(
27
+ config: DataStoreConfig & { scope?: L1TxScope },
28
+ deps: {
29
+ telemetry: TelemetryClient;
30
+ logger?: ReturnType<typeof createLogger>;
31
+ dateProvider?: DateProvider;
32
+ },
33
+ ) {
34
+ const logger = deps.logger ?? createLogger('l1-tx-utils');
35
+
36
+ // Note that we do NOT bind them to the rollup address, since we still need to
37
+ // monitor and cancel txs for previous rollups to free up our nonces.
38
+ const noRollupConfig = omit(config, 'l1Contracts');
39
+ const kvStore = await createStore(L1_TX_STORE_NAME, L1TxStore.SCHEMA_VERSION, noRollupConfig, logger);
40
+ const store = new L1TxStore(kvStore, logger);
41
+
42
+ const meter = deps.telemetry.getMeter('L1TxUtils');
43
+ const metrics = new L1TxMetrics(meter, config.scope ?? 'other', logger);
44
+
45
+ return { logger, store, metrics, dateProvider: deps.dateProvider };
46
+ }
47
+
48
+ /**
49
+ * Creates L1TxUtils with blobs from multiple Viem wallets, sharing store and metrics.
50
+ */
51
+ export async function createL1TxUtilsWithBlobsFromViemWallet(
52
+ clients: ExtendedViemWalletClient[],
53
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
54
+ deps: {
55
+ telemetry: TelemetryClient;
56
+ logger?: ReturnType<typeof createLogger>;
57
+ dateProvider?: DateProvider;
58
+ },
59
+ ) {
60
+ const sharedDeps = await createSharedDeps(config, deps);
61
+
62
+ return clients.map(client =>
63
+ createL1TxUtilsWithBlobsFromViemWalletBase(client, sharedDeps, config, config.debugMaxGasLimit),
64
+ );
65
+ }
66
+
67
+ /**
68
+ * Creates L1TxUtils with blobs from multiple EthSigners, sharing store and metrics.
69
+ */
70
+ export async function createL1TxUtilsWithBlobsFromEthSigner(
71
+ client: ViemClient,
72
+ signers: EthSigner[],
73
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
74
+ deps: {
75
+ telemetry: TelemetryClient;
76
+ logger?: ReturnType<typeof createLogger>;
77
+ dateProvider?: DateProvider;
78
+ },
79
+ ) {
80
+ const sharedDeps = await createSharedDeps(config, deps);
81
+
82
+ return signers.map(signer =>
83
+ createL1TxUtilsWithBlobsFromEthSignerBase(client, signer, sharedDeps, config, config.debugMaxGasLimit),
84
+ );
85
+ }
86
+
87
+ /**
88
+ * Creates L1TxUtils (without blobs) from multiple Viem wallets, sharing store and metrics.
89
+ */
90
+ export async function createL1TxUtilsFromViemWalletWithStore(
91
+ clients: ExtendedViemWalletClient[],
92
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
93
+ deps: {
94
+ telemetry: TelemetryClient;
95
+ logger?: ReturnType<typeof createLogger>;
96
+ dateProvider?: DateProvider;
97
+ scope?: L1TxScope;
98
+ },
99
+ ) {
100
+ const sharedDeps = await createSharedDeps(config, deps);
101
+
102
+ return clients.map(client => createL1TxUtilsFromViemWalletBase(client, sharedDeps, config));
103
+ }
104
+
105
+ /**
106
+ * Creates L1TxUtils (without blobs) from multiple EthSigners, sharing store and metrics.
107
+ */
108
+ export async function createL1TxUtilsFromEthSignerWithStore(
109
+ client: ViemClient,
110
+ signers: EthSigner[],
111
+ config: DataStoreConfig & Partial<L1TxUtilsConfig> & { debugMaxGasLimit?: boolean; scope?: L1TxScope },
112
+ deps: {
113
+ telemetry: TelemetryClient;
114
+ logger?: ReturnType<typeof createLogger>;
115
+ dateProvider?: DateProvider;
116
+ scope?: L1TxScope;
117
+ },
118
+ ) {
119
+ const sharedDeps = await createSharedDeps(config, deps);
120
+
121
+ return signers.map(signer => createL1TxUtilsFromEthSignerBase(client, signer, sharedDeps, config));
122
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_metrics.js';
@@ -0,0 +1,169 @@
1
+ import type { IL1TxMetrics, L1TxState } from '@aztec/ethereum';
2
+ import { TxUtilsState } from '@aztec/ethereum';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import {
5
+ Attributes,
6
+ type Histogram,
7
+ type Meter,
8
+ Metrics,
9
+ type UpDownCounter,
10
+ ValueType,
11
+ } from '@aztec/telemetry-client';
12
+
13
+ export type L1TxScope = 'sequencer' | 'prover' | 'other';
14
+
15
+ /**
16
+ * Metrics for L1 transaction utils tracking tx lifecycle and gas costs.
17
+ */
18
+ export class L1TxMetrics implements IL1TxMetrics {
19
+ // Time until tx is mined
20
+ private txMinedDuration: Histogram;
21
+
22
+ // Number of attempts until mined
23
+ private txAttemptsUntilMined: Histogram;
24
+
25
+ // Counters for end states
26
+ private txMinedCount: UpDownCounter;
27
+ private txRevertedCount: UpDownCounter;
28
+ private txCancelledCount: UpDownCounter;
29
+ private txNotMinedCount: UpDownCounter;
30
+
31
+ // Gas price histograms (at end state, in wei)
32
+ private maxPriorityFeeHistogram: Histogram;
33
+ private maxFeeHistogram: Histogram;
34
+ private blobFeeHistogram: Histogram;
35
+
36
+ constructor(
37
+ private meter: Meter,
38
+ private scope: L1TxScope = 'other',
39
+ private logger = createLogger('l1-tx-utils:metrics'),
40
+ ) {
41
+ this.txMinedDuration = this.meter.createHistogram(Metrics.L1_TX_MINED_DURATION, {
42
+ description: 'Time from initial tx send until mined',
43
+ unit: 's',
44
+ valueType: ValueType.INT,
45
+ });
46
+
47
+ this.txAttemptsUntilMined = this.meter.createHistogram(Metrics.L1_TX_ATTEMPTS_UNTIL_MINED, {
48
+ description: 'Number of tx attempts (including speed-ups) until mined',
49
+ unit: 'attempts',
50
+ valueType: ValueType.INT,
51
+ });
52
+
53
+ this.txMinedCount = this.meter.createUpDownCounter(Metrics.L1_TX_MINED_COUNT, {
54
+ description: 'Count of transactions successfully mined',
55
+ valueType: ValueType.INT,
56
+ });
57
+
58
+ this.txRevertedCount = this.meter.createUpDownCounter(Metrics.L1_TX_REVERTED_COUNT, {
59
+ description: 'Count of transactions that reverted',
60
+ valueType: ValueType.INT,
61
+ });
62
+
63
+ this.txCancelledCount = this.meter.createUpDownCounter(Metrics.L1_TX_CANCELLED_COUNT, {
64
+ description: 'Count of transactions cancelled',
65
+ valueType: ValueType.INT,
66
+ });
67
+
68
+ this.txNotMinedCount = this.meter.createUpDownCounter(Metrics.L1_TX_NOT_MINED_COUNT, {
69
+ description: 'Count of transactions not mined (timed out)',
70
+ valueType: ValueType.INT,
71
+ });
72
+
73
+ this.maxPriorityFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_PRIORITY_FEE, {
74
+ description: 'Max priority fee per gas at tx end state (in wei)',
75
+ unit: 'wei',
76
+ valueType: ValueType.INT,
77
+ });
78
+
79
+ this.maxFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_MAX_FEE, {
80
+ description: 'Max fee per gas at tx end state (in wei)',
81
+ unit: 'wei',
82
+ valueType: ValueType.INT,
83
+ });
84
+
85
+ this.blobFeeHistogram = this.meter.createHistogram(Metrics.L1_TX_BLOB_FEE, {
86
+ description: 'Max fee per blob gas at tx end state (in wei)',
87
+ unit: 'wei',
88
+ valueType: ValueType.INT,
89
+ });
90
+ }
91
+
92
+ /**
93
+ * Records metrics when a transaction is mined.
94
+ * @param state - The L1 transaction state
95
+ * @param l1Timestamp - The current L1 timestamp
96
+ */
97
+ public recordMinedTx(state: L1TxState, l1Timestamp: Date): void {
98
+ if (state.status !== TxUtilsState.MINED) {
99
+ this.logger.warn(
100
+ `Attempted to record mined tx metrics for a tx not in MINED state (state: ${TxUtilsState[state.status]})`,
101
+ { scope: this.scope, nonce: state.nonce },
102
+ );
103
+ return;
104
+ }
105
+
106
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
107
+ const isCancelTx = state.cancelTxHashes.length > 0;
108
+ const isReverted = state.receipt?.status === 'reverted';
109
+
110
+ if (isCancelTx) {
111
+ this.txCancelledCount.add(1, attributes);
112
+ } else if (isReverted) {
113
+ this.txRevertedCount.add(1, attributes);
114
+ } else {
115
+ this.txMinedCount.add(1, attributes);
116
+ }
117
+
118
+ // Record time to mine using provided L1 timestamp
119
+ const duration = Math.floor((l1Timestamp.getTime() - state.sentAtL1Ts.getTime()) / 1000);
120
+ this.txMinedDuration.record(duration, attributes);
121
+
122
+ // Record number of attempts until mined
123
+ const attempts = isCancelTx ? state.cancelTxHashes.length : state.txHashes.length;
124
+ this.txAttemptsUntilMined.record(attempts, attributes);
125
+
126
+ // Record gas prices at end state (in wei as integers)
127
+ const maxPriorityFeeWei = Number(state.gasPrice.maxPriorityFeePerGas);
128
+ const maxFeeWei = Number(state.gasPrice.maxFeePerGas);
129
+ const blobFeeWei = state.gasPrice.maxFeePerBlobGas ? Number(state.gasPrice.maxFeePerBlobGas) : undefined;
130
+
131
+ this.maxPriorityFeeHistogram.record(maxPriorityFeeWei, attributes);
132
+ this.maxFeeHistogram.record(maxFeeWei, attributes);
133
+
134
+ // Record blob fee if present (in wei as integer)
135
+ if (blobFeeWei !== undefined) {
136
+ this.blobFeeHistogram.record(blobFeeWei, attributes);
137
+ }
138
+
139
+ this.logger.debug(`Recorded tx end state metrics`, {
140
+ status: TxUtilsState[state.status],
141
+ nonce: state.nonce,
142
+ isCancelTx,
143
+ isReverted,
144
+ scope: this.scope,
145
+ maxPriorityFeeWei,
146
+ maxFeeWei,
147
+ blobFeeWei,
148
+ });
149
+ }
150
+
151
+ public recordDroppedTx(state: L1TxState): void {
152
+ if (state.status !== TxUtilsState.NOT_MINED) {
153
+ this.logger.warn(
154
+ `Attempted to record dropped tx metrics for a tx not in NOT_MINED state (state: ${TxUtilsState[state.status]})`,
155
+ { scope: this.scope, nonce: state.nonce },
156
+ );
157
+ return;
158
+ }
159
+
160
+ const attributes = { [Attributes.L1_TX_SCOPE]: this.scope };
161
+ this.txNotMinedCount.add(1, attributes);
162
+
163
+ this.logger.debug(`Recorded tx dropped metrics`, {
164
+ status: TxUtilsState[state.status],
165
+ nonce: state.nonce,
166
+ scope: this.scope,
167
+ });
168
+ }
169
+ }
@@ -0,0 +1 @@
1
+ export * from './l1_tx_store.js';
@@ -0,0 +1,387 @@
1
+ import type { IL1TxStore, L1BlobInputs, L1TxConfig, L1TxState } from '@aztec/ethereum';
2
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
3
+ import type { Logger } from '@aztec/foundation/log';
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
6
+
7
+ import type { TransactionReceipt } from 'viem';
8
+
9
+ /**
10
+ * Serializable version of L1TxRequest for storage.
11
+ */
12
+ interface SerializableL1TxRequest {
13
+ to: string | null;
14
+ data?: string;
15
+ value?: string;
16
+ }
17
+
18
+ /**
19
+ * Serializable version of GasPrice for storage.
20
+ */
21
+ interface SerializableGasPrice {
22
+ maxFeePerGas: string;
23
+ maxPriorityFeePerGas: string;
24
+ maxFeePerBlobGas?: string;
25
+ }
26
+
27
+ /**
28
+ * Serializable version of L1TxConfig for storage.
29
+ */
30
+ interface SerializableL1TxConfig {
31
+ gasLimit?: string;
32
+ txTimeoutAt?: number;
33
+ txTimeoutMs?: number;
34
+ checkIntervalMs?: number;
35
+ stallTimeMs?: number;
36
+ priorityFeeRetryBumpPercentage?: number;
37
+ maxSpeedUpAttempts?: number;
38
+ cancelTxOnTimeout?: boolean;
39
+ txCancellationFinalTimeoutMs?: number;
40
+ txUnseenConsideredDroppedMs?: number;
41
+ }
42
+
43
+ /**
44
+ * Serializable version of blob inputs for storage (without the actual blob data).
45
+ */
46
+ interface SerializableBlobMetadata {
47
+ maxFeePerBlobGas?: string;
48
+ }
49
+
50
+ /**
51
+ * Serializable version of L1TxState for storage.
52
+ * Dates and bigints are converted to strings/numbers for JSON serialization.
53
+ * Blob data is NOT included here - it's stored separately.
54
+ */
55
+ interface SerializableL1TxState {
56
+ id: number;
57
+ txHashes: string[];
58
+ cancelTxHashes: string[];
59
+ gasLimit: string;
60
+ gasPrice: SerializableGasPrice;
61
+ txConfigOverrides: SerializableL1TxConfig;
62
+ request: SerializableL1TxRequest;
63
+ status: number;
64
+ nonce: number;
65
+ sentAt: number;
66
+ lastSentAt: number;
67
+ receipt?: TransactionReceipt;
68
+ hasBlobInputs: boolean;
69
+ blobMetadata?: SerializableBlobMetadata;
70
+ }
71
+
72
+ /**
73
+ * Serializable blob inputs for separate storage.
74
+ */
75
+ interface SerializableBlobInputs {
76
+ blobs: string[]; // base64 encoded
77
+ kzg: string; // JSON stringified KZG instance
78
+ }
79
+
80
+ /**
81
+ * Store for persisting L1 transaction states across all L1TxUtils instances.
82
+ * Each state is stored individually with a unique ID, and blobs are stored separately.
83
+ * @remarks This class lives in this package instead of `ethereum` because it depends on `kv-store`.
84
+ */
85
+ export class L1TxStore implements IL1TxStore {
86
+ public static readonly SCHEMA_VERSION = 2;
87
+
88
+ private readonly states: AztecAsyncMap<string, string>; // key: "account-stateId", value: SerializableL1TxState
89
+ private readonly blobs: AztecAsyncMap<string, string>; // key: "account-stateId", value: SerializableBlobInputs
90
+ private readonly stateIdCounter: AztecAsyncMap<string, number>; // key: "account", value: next ID
91
+
92
+ constructor(
93
+ private readonly store: AztecAsyncKVStore,
94
+ private readonly log: Logger = createLogger('l1-tx-utils:store'),
95
+ ) {
96
+ this.states = store.openMap<string, string>('l1_tx_states');
97
+ this.blobs = store.openMap<string, string>('l1_tx_blobs');
98
+ this.stateIdCounter = store.openMap<string, number>('l1_tx_state_id_counter');
99
+ }
100
+
101
+ /**
102
+ * Gets the next available state ID for an account.
103
+ */
104
+ public consumeNextStateId(account: string): Promise<number> {
105
+ return this.store.transactionAsync(async () => {
106
+ const currentId = (await this.stateIdCounter.getAsync(account)) ?? 0;
107
+ const nextId = currentId + 1;
108
+ await this.stateIdCounter.set(account, nextId);
109
+ return nextId;
110
+ });
111
+ }
112
+
113
+ /**
114
+ * Creates a storage key for state/blob data.
115
+ */
116
+ private makeKey(account: string, stateId: number): string {
117
+ return `${account}-${stateId.toString().padStart(10, '0')}`;
118
+ }
119
+
120
+ /**
121
+ * Saves a single transaction state for a specific account.
122
+ * Blobs are not stored here, use saveBlobs instead.
123
+ * @param account - The sender account address
124
+ * @param state - Transaction state to save
125
+ */
126
+ public async saveState(account: string, state: L1TxState): Promise<L1TxState> {
127
+ const key = this.makeKey(account, state.id);
128
+
129
+ const serializable = this.serializeState(state);
130
+ await this.states.set(key, jsonStringify(serializable));
131
+ this.log.debug(`Saved tx state ${state.id} for account ${account} with nonce ${state.nonce}`);
132
+
133
+ return state as L1TxState;
134
+ }
135
+
136
+ /**
137
+ * Saves blobs for a given state.
138
+ * @param account - The sender account address
139
+ * @param stateId - The state ID
140
+ * @param blobInputs - Blob inputs to save
141
+ */
142
+ public async saveBlobs(account: string, stateId: number, blobInputs: L1BlobInputs | undefined): Promise<void> {
143
+ if (!blobInputs) {
144
+ return;
145
+ }
146
+ const key = this.makeKey(account, stateId);
147
+ const blobData = this.serializeBlobInputs(blobInputs);
148
+ await this.blobs.set(key, jsonStringify(blobData));
149
+ this.log.debug(`Saved blobs for state ${stateId} of account ${account}`);
150
+ }
151
+
152
+ /**
153
+ * Loads all transaction states for a specific account.
154
+ * @param account - The sender account address
155
+ * @returns Array of transaction states with their IDs
156
+ */
157
+ public async loadStates(account: string): Promise<L1TxState[]> {
158
+ const states: L1TxState[] = [];
159
+ const prefix = `${account}-`;
160
+
161
+ for await (const [key, stateJson] of this.states.entriesAsync({ start: prefix, end: `${prefix}Z` })) {
162
+ const [keyAccount, stateIdStr] = key.split('-');
163
+ if (keyAccount !== account) {
164
+ throw new Error(`Mismatched account in key: expected ${account} but got ${keyAccount}`);
165
+ }
166
+
167
+ const stateId = parseInt(stateIdStr, 10);
168
+
169
+ try {
170
+ const serialized: SerializableL1TxState = JSON.parse(stateJson);
171
+
172
+ // Load blobs if they exist
173
+ let blobInputs: L1BlobInputs | undefined;
174
+ if (serialized.hasBlobInputs) {
175
+ const blobJson = await this.blobs.getAsync(key);
176
+ if (blobJson) {
177
+ blobInputs = this.deserializeBlobInputs(JSON.parse(blobJson), serialized.blobMetadata);
178
+ }
179
+ }
180
+
181
+ const state = this.deserializeState(serialized, blobInputs);
182
+ states.push({ ...state, id: stateId });
183
+ } catch (err) {
184
+ this.log.error(`Failed to deserialize state ${key}`, err);
185
+ }
186
+ }
187
+
188
+ // Sort by ID
189
+ states.sort((a, b) => a.id - b.id);
190
+
191
+ this.log.debug(`Loaded ${states.length} tx states for account ${account}`);
192
+ return states;
193
+ }
194
+
195
+ /**
196
+ * Loads a single state by ID.
197
+ * @param account - The sender account address
198
+ * @param stateId - The state ID
199
+ * @returns The transaction state or undefined if not found
200
+ */
201
+ public async loadState(account: string, stateId: number): Promise<L1TxState | undefined> {
202
+ const key = this.makeKey(account, stateId);
203
+ const stateJson = await this.states.getAsync(key);
204
+
205
+ if (!stateJson) {
206
+ return undefined;
207
+ }
208
+
209
+ try {
210
+ const serialized: SerializableL1TxState = JSON.parse(stateJson);
211
+
212
+ // Load blobs if they exist
213
+ let blobInputs: L1BlobInputs | undefined;
214
+ if (serialized.hasBlobInputs) {
215
+ const blobJson = await this.blobs.getAsync(key);
216
+ if (blobJson) {
217
+ blobInputs = this.deserializeBlobInputs(JSON.parse(blobJson), serialized.blobMetadata);
218
+ }
219
+ }
220
+
221
+ const state = this.deserializeState(serialized, blobInputs);
222
+ return { ...state, id: stateId };
223
+ } catch (err) {
224
+ this.log.error(`Failed to deserialize state ${key}`, err);
225
+ return undefined;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Deletes a specific state and its associated blobs.
231
+ * @param account - The sender account address
232
+ * @param stateId - The state ID to delete
233
+ */
234
+ public async deleteState(account: string, stateId: number): Promise<void> {
235
+ const key = this.makeKey(account, stateId);
236
+ await this.states.delete(key);
237
+ await this.blobs.delete(key);
238
+ this.log.debug(`Deleted state ${stateId} for account ${account}`);
239
+ }
240
+
241
+ /**
242
+ * Clears all transaction states for a specific account.
243
+ * @param account - The sender account address
244
+ */
245
+ public async clearStates(account: string): Promise<void> {
246
+ const states = await this.loadStates(account);
247
+
248
+ for (const state of states) {
249
+ await this.deleteState(account, state.id);
250
+ }
251
+
252
+ await this.stateIdCounter.delete(account);
253
+ this.log.info(`Cleared all tx states for account ${account}`);
254
+ }
255
+
256
+ /**
257
+ * Gets all accounts that have stored states.
258
+ * @returns Array of account addresses
259
+ */
260
+ public async getAllAccounts(): Promise<string[]> {
261
+ const accounts = new Set<string>();
262
+
263
+ for await (const [key] of this.states.entriesAsync()) {
264
+ const account = key.substring(0, key.lastIndexOf('-'));
265
+ accounts.add(account);
266
+ }
267
+
268
+ return Array.from(accounts);
269
+ }
270
+
271
+ /**
272
+ * Closes the store.
273
+ */
274
+ public async close(): Promise<void> {
275
+ await this.store.close();
276
+ this.log.info('Closed L1 tx state store');
277
+ }
278
+
279
+ /**
280
+ * Serializes an L1TxState for storage.
281
+ */
282
+ private serializeState(state: L1TxState): SerializableL1TxState {
283
+ const txConfigOverrides: SerializableL1TxConfig = {
284
+ ...state.txConfigOverrides,
285
+ gasLimit: state.txConfigOverrides.gasLimit?.toString(),
286
+ txTimeoutAt: state.txConfigOverrides.txTimeoutAt?.getTime(),
287
+ };
288
+
289
+ return {
290
+ id: state.id,
291
+ txHashes: state.txHashes,
292
+ cancelTxHashes: state.cancelTxHashes,
293
+ gasLimit: state.gasLimit.toString(),
294
+ gasPrice: {
295
+ maxFeePerGas: state.gasPrice.maxFeePerGas.toString(),
296
+ maxPriorityFeePerGas: state.gasPrice.maxPriorityFeePerGas.toString(),
297
+ maxFeePerBlobGas: state.gasPrice.maxFeePerBlobGas?.toString(),
298
+ },
299
+ txConfigOverrides,
300
+ request: {
301
+ ...state.request,
302
+ value: state.request.value?.toString(),
303
+ },
304
+ status: state.status,
305
+ nonce: state.nonce,
306
+ sentAt: state.sentAtL1Ts.getTime(),
307
+ lastSentAt: state.lastSentAtL1Ts.getTime(),
308
+ receipt: state.receipt,
309
+ hasBlobInputs: state.blobInputs !== undefined,
310
+ blobMetadata: state.blobInputs?.maxFeePerBlobGas
311
+ ? { maxFeePerBlobGas: state.blobInputs.maxFeePerBlobGas.toString() }
312
+ : undefined,
313
+ };
314
+ }
315
+
316
+ /**
317
+ * Deserializes a stored state back to L1TxState.
318
+ */
319
+ private deserializeState(stored: SerializableL1TxState, blobInputs?: L1BlobInputs): L1TxState {
320
+ const txConfigOverrides: L1TxConfig = {
321
+ ...stored.txConfigOverrides,
322
+ gasLimit: stored.txConfigOverrides.gasLimit !== undefined ? BigInt(stored.txConfigOverrides.gasLimit) : undefined,
323
+ txTimeoutAt:
324
+ stored.txConfigOverrides.txTimeoutAt !== undefined ? new Date(stored.txConfigOverrides.txTimeoutAt) : undefined,
325
+ };
326
+
327
+ const receipt = stored.receipt
328
+ ? {
329
+ ...stored.receipt,
330
+ blockNumber: BigInt(stored.receipt.blockNumber),
331
+ cumulativeGasUsed: BigInt(stored.receipt.cumulativeGasUsed),
332
+ effectiveGasPrice: BigInt(stored.receipt.effectiveGasPrice),
333
+ gasUsed: BigInt(stored.receipt.gasUsed),
334
+ }
335
+ : undefined;
336
+
337
+ return {
338
+ id: stored.id,
339
+ txHashes: stored.txHashes as `0x${string}`[],
340
+ cancelTxHashes: stored.cancelTxHashes as `0x${string}`[],
341
+ gasLimit: BigInt(stored.gasLimit),
342
+ gasPrice: {
343
+ maxFeePerGas: BigInt(stored.gasPrice.maxFeePerGas),
344
+ maxPriorityFeePerGas: BigInt(stored.gasPrice.maxPriorityFeePerGas),
345
+ maxFeePerBlobGas: stored.gasPrice.maxFeePerBlobGas ? BigInt(stored.gasPrice.maxFeePerBlobGas) : undefined,
346
+ },
347
+ txConfigOverrides,
348
+ request: {
349
+ to: stored.request.to as `0x${string}` | null,
350
+ data: stored.request.data as `0x${string}` | undefined,
351
+ value: stored.request.value ? BigInt(stored.request.value) : undefined,
352
+ },
353
+ status: stored.status,
354
+ nonce: stored.nonce,
355
+ sentAtL1Ts: new Date(stored.sentAt),
356
+ lastSentAtL1Ts: new Date(stored.lastSentAt),
357
+ receipt,
358
+ blobInputs,
359
+ };
360
+ }
361
+
362
+ /**
363
+ * Serializes blob inputs for separate storage.
364
+ */
365
+ private serializeBlobInputs(blobInputs: L1BlobInputs): SerializableBlobInputs {
366
+ return {
367
+ blobs: blobInputs.blobs.map(b => Buffer.from(b).toString('base64')),
368
+ kzg: jsonStringify(blobInputs.kzg),
369
+ };
370
+ }
371
+
372
+ /**
373
+ * Deserializes blob inputs from storage, combining blob data with metadata.
374
+ */
375
+ private deserializeBlobInputs(stored: SerializableBlobInputs, metadata?: SerializableBlobMetadata): L1BlobInputs {
376
+ const blobInputs: L1BlobInputs = {
377
+ blobs: stored.blobs.map(b => new Uint8Array(Buffer.from(b, 'base64'))),
378
+ kzg: JSON.parse(stored.kzg),
379
+ };
380
+
381
+ if (metadata?.maxFeePerBlobGas) {
382
+ blobInputs.maxFeePerBlobGas = BigInt(metadata.maxFeePerBlobGas);
383
+ }
384
+
385
+ return blobInputs;
386
+ }
387
+ }