alloy_zksync/types/mod.rs
1//! ZKsync-specific type definitions.
2
3use crate::network::unsigned_tx::eip712::PaymasterParams;
4use alloy::primitives::{Address, B256, Bytes, U64, U256};
5use chrono::{DateTime, Utc};
6use serde::{Deserialize, Serialize};
7
8/// Response type for `zks_estimateFee`.
9#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
10pub struct Eip712Fee {
11 /// Amount of gas to be spent on the transaction.
12 #[serde(with = "alloy::serde::quantity")]
13 pub gas_limit: u64,
14 /// Maximum gas user agrees to spend on a single pubdata byte published to L1.
15 pub gas_per_pubdata_limit: U256,
16 /// EIP-1559 gas price.
17 #[serde(with = "alloy::serde::quantity")]
18 pub max_fee_per_gas: u128,
19 /// EIP-1559 tip.
20 #[serde(with = "alloy::serde::quantity")]
21 pub max_priority_fee_per_gas: u128,
22}
23
24/// Response type for `zks_getBridgeContracts`.
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
26#[serde(rename_all = "camelCase")]
27pub struct BridgeAddresses {
28 /// The address of the default shared bridge on Layer 1.
29 pub l1_shared_default_bridge: Option<Address>,
30 /// The address of the default shared bridge on Layer 2.
31 pub l2_shared_default_bridge: Option<Address>,
32 /// The address of the default ERC-20 bridge on Layer 1.
33 pub l1_erc20_default_bridge: Option<Address>,
34 /// The address of the default ERC-20 bridge on Layer 2.
35 pub l2_erc20_default_bridge: Option<Address>,
36 /// The address of the Wrapped Ethereum (WETH) bridge on Layer 1.
37 pub l1_weth_bridge: Option<Address>,
38 /// The address of the Wrapped Ethereum (WETH) bridge on Layer 2.
39 pub l2_weth_bridge: Option<Address>,
40 /// The address of the legacy shared bridge on Layer 2.
41 pub l2_legacy_shared_bridge: Option<Address>,
42}
43
44/// Hashes of base system contracts.
45#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
46pub struct BaseSystemContractsHashes {
47 /// Hash of the bootloader system contract.
48 pub bootloader: B256,
49 /// Hash of the default account abstraction system contract.
50 pub default_aa: B256,
51 /// Hash of the evm emulator system contract.
52 pub evm_emulator: Option<B256>,
53}
54
55/// Current status of the batch.
56#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
57#[serde(rename_all = "camelCase")]
58pub enum BlockStatus {
59 /// The block has been fully executed on L2.
60 Sealed,
61 /// The block has been verified on L1.
62 Verified,
63}
64
65/// Response type for `zks_getBlockDetails`.
66#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
67#[serde(rename_all = "camelCase")]
68pub struct BlockDetails {
69 /// Number of the block.
70 pub number: u64,
71 /// Corresponding L1 batch number.
72 pub l1_batch_number: u64,
73 /// Address of the operator who committed the block.
74 pub operator_address: Address,
75 /// Version of the ZKsync protocol the block was committed under.
76 pub protocol_version: Option<String>,
77 /// Unix timestamp of when the first transaction in the block was processed (i.e., when the block was opened).
78 pub timestamp: u64,
79 /// Number of L1 transactions included in the block.
80 pub l1_tx_count: u64,
81 /// Number of L2 transactions included in the block.
82 pub l2_tx_count: u64,
83 /// Hash of the L2 block.
84 pub root_hash: Option<B256>,
85 /// Current status of the block: verified or sealed.
86 pub status: BlockStatus,
87 /// Transaction hash of the commit operation on L1 for the batch containing this L2 block.
88 pub commit_tx_hash: Option<B256>,
89 /// Timestamp when the batch containing this L2 block was committed on L1.
90 pub committed_at: Option<DateTime<Utc>>,
91 /// Transaction hash of the proof submission on L1 for the batch containing this L2 block.
92 pub prove_tx_hash: Option<B256>,
93 /// Timestamp when the proof was submitted on L1 for the batch containing this L2 block.
94 pub proven_at: Option<DateTime<Utc>>,
95 /// Transaction hash of the execution on L1 for the batch containing this L2 block.
96 pub execute_tx_hash: Option<B256>,
97 /// Timestamp when the execution was completed on L1 for the batch containing this L2 block.
98 pub executed_at: Option<DateTime<Utc>>,
99 /// L1 gas price at the time of the block's execution.
100 pub l1_gas_price: U256,
101 /// Fair gas price on L2 at the time of the block's execution.
102 pub l2_fair_gas_price: U256,
103 /// Cost of publishing one byte (in wei).
104 pub fair_pubdata_price: Option<U256>,
105 /// Hashes for the base system contracts used for block execution.
106 pub base_system_contracts_hashes: BaseSystemContractsHashes,
107}
108
109/// Current status of the transaction.
110#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
111#[serde(rename_all = "camelCase")]
112pub enum TransactionStatus {
113 /// The transaction is not executed yet.
114 Pending,
115 /// The transaction is included in the block.
116 Included,
117 /// The transaction is verified on L1.
118 Verified,
119 /// The transaction execution failed.
120 Failed,
121}
122
123/// Response type for `zks_getTransactionDetails`.
124#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
125#[serde(rename_all = "camelCase")]
126pub struct TransactionDetails {
127 /// Indicates whether the transaction originated on Layer 1.
128 pub is_l1_originated: bool,
129 /// Current status of the transaction: pending, included, verified or failed.
130 pub status: TransactionStatus,
131 /// Transaction fee.
132 pub fee: U256,
133 /// Gas amount per unit of public data for this transaction.
134 pub gas_per_pubdata: U256,
135 /// Address of the transaction initiator.
136 pub initiator_address: Address,
137 /// Timestamp when the transaction was received.
138 pub received_at: DateTime<Utc>,
139 /// Transaction hash of the commit operation.
140 pub eth_commit_tx_hash: Option<B256>,
141 /// Transaction hash of the proof submission.
142 pub eth_prove_tx_hash: Option<B256>,
143 /// Transaction hash of the execution.
144 pub eth_execute_tx_hash: Option<B256>,
145}
146
147/// Response type for `zks_getL1BatchDetails`.
148#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
149#[serde(rename_all = "camelCase")]
150pub struct L1BatchDetails {
151 /// L1 batch number.
152 pub number: u64,
153 /// Unix timestamp when the batch was processed.
154 pub timestamp: u64,
155 /// Number of L1 transactions included in the batch.
156 pub l1_tx_count: u64,
157 /// Number of L2 transactions associated with this batch.
158 pub l2_tx_count: u64,
159 /// Root hash of the state after processing the batch.
160 pub root_hash: Option<B256>,
161 /// Current status of the batch: sealed or verified.
162 pub status: BlockStatus,
163 /// L1 transaction hash for the commit operation.
164 pub commit_tx_hash: Option<B256>,
165 /// Timestamp when the batch was committed on L1.
166 pub committed_at: Option<DateTime<Utc>>,
167 /// L1 transaction hash for the proof submission.
168 pub prove_tx_hash: Option<B256>,
169 /// Timestamp when the proof was submitted.
170 pub proven_at: Option<DateTime<Utc>>,
171 /// L1 transaction hash for the execution.
172 pub execute_tx_hash: Option<B256>,
173 /// Timestamp when the execution was completed.
174 pub executed_at: Option<DateTime<Utc>>,
175 /// Gas price on L1 at the time of batch processing.
176 pub l1_gas_price: U256,
177 /// Fair gas price on L2 at the time of batch processing.
178 pub l2_fair_gas_price: U256,
179 /// Cost of publishing one byte (in wei).
180 pub fair_pubdata_price: Option<U256>,
181 /// Hashes of the base system contracts involved in the batch.
182 pub base_system_contracts_hashes: BaseSystemContractsHashes,
183}
184
185/// Static fee model parameters for the V2 fee model.
186#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
187pub struct FeeModelConfigV2 {
188 /// Minimal gas price on L2.
189 pub minimal_l2_gas_price: U256,
190 /// Compute overhead part in fee calculation.
191 pub compute_overhead_part: f64,
192 /// Public data overhead part in fee calculation.
193 pub pubdata_overhead_part: f64,
194 /// Overhead in L1 gas for a batch of transactions.
195 pub batch_overhead_l1_gas: U256,
196 /// Maximum gas allowed per batch.
197 pub max_gas_per_batch: U256,
198 /// Maximum amount of public data allowed per batch.
199 pub max_pubdata_per_batch: U256,
200}
201
202/// Conversion ratio between BaseToken and ETH.
203#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
204pub struct BaseTokenConversionRatio {
205 /// Numerator of the conversion ratio.
206 pub numerator: u64,
207 /// Denominator of the conversion ratio.
208 pub denominator: u64,
209}
210
211/// Dynamic fee model parameters for the V2 fee model.
212#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
213pub struct FeeParamsV2 {
214 /// Static fee model parameters.
215 pub config: FeeModelConfigV2,
216 /// L1 gas price.
217 pub l1_gas_price: U256,
218 /// Price of storing public data on L1.
219 pub l1_pubdata_price: U256,
220 /// BaseToken<->ETH conversion ratio.
221 pub conversion_ratio: BaseTokenConversionRatio,
222}
223
224/// Static fee model parameters for the V1 fee model (no longer in active use).
225#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
226pub struct FeeModelConfigV1 {
227 /// The minimal acceptable L2 gas price, i.e. the price that should include the cost of computation/proving as well
228 /// as potentially premium for congestion.
229 /// Unlike the `V2`, this price will be directly used as the `fair_l2_gas_price` in the bootloader.
230 pub minimal_l2_gas_price: u64,
231}
232
233/// Dynamic fee model parameters for the V1 fee model (no longer in active use).
234#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
235pub struct FeeParamsV1 {
236 /// Static parameters.
237 pub config: FeeModelConfigV1,
238 /// L1 gas price.
239 pub l1_gas_price: u64,
240}
241
242/// Response type for `zks_getFeeParams`.
243#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
244#[allow(clippy::large_enum_variant)]
245pub enum FeeParams {
246 /// V1 fee model parameters (no longer in active use).
247 V1(FeeParamsV1),
248 /// V2 fee model parameters.
249 V2(FeeParamsV2),
250}
251
252/// Configuration of the L1 verifier contract.
253#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
254pub struct L1VerifierConfig {
255 /// Verification key hash for the topmost recursion level.
256 pub recursion_scheduler_level_vk_hash: B256,
257}
258
259/// Response type for `zks_getProtocolVersion`.
260#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
261pub struct ProtocolVersion {
262 /// Minor version of the protocol (corresponds to the used VKs).
263 #[serde(rename = "minorVersion")]
264 pub minor_version: Option<u16>,
265 /// Unix timestamp of the version's activation.
266 pub timestamp: u64,
267 /// Hashes of various verification keys used in the protocol.
268 pub verification_keys_hashes: Option<L1VerifierConfig>,
269 /// Hashes of the base system contracts.
270 pub base_system_contracts: Option<BaseSystemContractsHashes>,
271 /// Bootloader code hash.
272 #[serde(rename = "bootloaderCodeHash")]
273 pub bootloader_code_hash: Option<B256>,
274 /// Default account code hash.
275 #[serde(rename = "defaultAccountCodeHash")]
276 pub default_account_code_hash: Option<B256>,
277 /// EVM emulator code hash.
278 #[serde(rename = "evmSimulatorCodeHash")]
279 pub evm_emulator_code_hash: Option<B256>,
280 /// Hash of the transaction used for the system upgrade
281 #[serde(rename = "l2SystemUpgradeTxHash")]
282 pub l2_system_upgrade_tx_hash: Option<B256>,
283}
284
285/// Merkle proof for a storage value.
286#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
287pub struct StorageProof {
288 /// Storage key for which the proof is provided.
289 pub key: B256,
290 /// Hashes that constitute the Merkle path from the leaf node (representing the storage key-value pair) to the root of the Merkle tree.
291 /// The path is ordered from the root to the leaf.
292 /// The root hash itself is not included in this array because it is published on L1 as part of the L1 batch commit data.
293 pub proof: Vec<B256>,
294 /// Value stored in the specified storage key at the time of the specified l1BatchNumber.
295 pub value: B256,
296 /// A 1-based index representing the position of the tree entry within the Merkle tree.
297 /// This index is used to help reconstruct the Merkle path during verification.
298 pub index: u64,
299}
300
301/// Response type for `zks_getProof`.
302#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
303#[serde(rename_all = "camelCase")]
304pub struct Proof {
305 /// Account address associated with the storage proofs.
306 pub address: Address,
307 /// Storage proof for the requested keys.
308 pub storage_proof: Vec<StorageProof>,
309}
310
311/// Log of a storage access.
312#[derive(Debug, Clone, Serialize, Deserialize)]
313#[serde(rename_all = "camelCase")]
314pub struct StorageLog {
315 pub address: Address,
316 pub key: U256,
317 pub written_value: U256,
318}
319
320/// A log produced by a transaction.
321#[derive(Debug, Clone, Serialize, Deserialize)]
322#[serde(rename_all = "camelCase")]
323pub struct Log {
324 /// Address from which this log originated.
325 pub address: Address,
326 /// An array of 0 to 4 indexed log arguments.
327 pub topics: Vec<B256>,
328 /// Contains non-indexed arguments of the log.
329 pub data: Bytes,
330 /// Hash of the block where this log was in.
331 pub block_hash: Option<B256>,
332 /// Block number where this log was in.
333 pub block_number: Option<U64>,
334 /// L1 batch number where this log was in.
335 pub l1_batch_number: Option<U64>,
336 /// Hash of the transactions from which this log was created.
337 pub transaction_hash: Option<B256>,
338 /// Transaction index position from which the log created.
339 pub transaction_index: Option<U64>,
340 /// Log index position in the block.
341 pub log_index: Option<U64>,
342 /// Log index position in the transaction.
343 pub transaction_log_index: Option<U64>,
344 /// Log type.
345 pub log_type: Option<String>,
346 /// True when the log was removed, false if it's a valid log.
347 pub removed: Option<bool>,
348 /// Unix timestamp of when the first transaction in the block was processed (i.e., when the block was opened).
349 pub block_timestamp: Option<U64>,
350}
351
352/// Type for L2 to L1 logs that are returned as a part of `eth_getTransactionReceipt` response.
353#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
354#[serde(rename_all = "camelCase")]
355pub struct L2ToL1Log {
356 /// Hash of the block where this log was in.
357 pub block_hash: Option<B256>,
358 /// Block number where this log was in.
359 pub block_number: U64,
360 /// L1 batch number where this log was in.
361 pub l1_batch_number: Option<U64>,
362 /// Log index position in the block.
363 pub log_index: U256,
364 /// Transaction index position from which the log created.
365 pub transaction_index: U64,
366 /// Hash of the transactions from which this log was created.
367 pub transaction_hash: B256,
368 /// Log index position in the transaction.
369 pub transaction_log_index: U256,
370 /// The number of the transaction in the batch where the log occurred.
371 pub tx_index_in_l1_batch: Option<U64>,
372 /// The id of the shard the opcode was called (it is currently always 0).
373 pub shard_id: U64,
374 /// A boolean flag that indicates whether the log is a service log. It is not used right now.
375 pub is_service: bool,
376 /// The value of this in the frame where the L2 to L1 log was emitted.
377 pub sender: Address,
378 /// Key and value are two 32-byte values that can be used to carry some data with the log.
379 pub key: B256,
380 /// Key and value are two 32-byte values that can be used to carry some data with the log.
381 pub value: B256,
382}
383
384/// Response type for `zks_getL2ToL1LogProof` and `zks_getL2ToL1MsgProof`.
385#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
386#[serde(rename_all = "camelCase")]
387pub struct L2ToL1LogProof {
388 /// The Merkle proof for the message.
389 pub proof: Vec<B256>,
390 /// The position of the leaf in the Merkle tree of L2 to L1 messages for the block.
391 pub id: u32,
392 /// The root hash representing the Merkle tree root at the time the log was generated.
393 pub root: B256,
394}
395
396#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
397#[serde(rename_all = "camelCase")]
398pub struct Execute {
399 pub contract_address: Option<Address>,
400 pub calldata: Bytes,
401 pub value: U256,
402 /// Factory dependencies: list of contract bytecodes associated with the deploy transaction.
403 pub factory_deps: Vec<Bytes>,
404}
405
406#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
407pub struct InputData {
408 pub hash: B256,
409 pub data: Bytes,
410}
411
412#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
413#[repr(u8)]
414pub enum OpProcessingType {
415 Common = 0,
416 OnlyRollup = 1,
417}
418
419#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq)]
420#[repr(u8)]
421pub enum PriorityQueueType {
422 #[default]
423 Deque = 0,
424 HeapBuffer = 1,
425 Heap = 2,
426}
427
428#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
429pub struct L1TxCommonData {
430 /// Sender of the transaction.
431 pub sender: Address,
432 /// Unique ID of the priority operation.
433 pub serial_id: u64,
434 /// Additional payment to the operator as an incentive to perform the operation. The contract uses a value of 192 bits.
435 pub layer_2_tip_fee: U256,
436 /// The total cost the sender paid for the transaction.
437 pub full_fee: U256,
438 /// The maximal fee per gas to be used for L1->L2 transaction
439 pub max_fee_per_gas: U256,
440 /// The maximum number of gas that a transaction can spend at a price of gas equals 1.
441 pub gas_limit: U256,
442 /// The maximum number of gas per 1 byte of pubdata.
443 pub gas_per_pubdata_limit: U256,
444 /// Indicator that the operation can interact with Rollup and Porter trees, or only with Rollup.
445 pub op_processing_type: OpProcessingType,
446 /// Priority operations queue type.
447 pub priority_queue_type: PriorityQueueType,
448 /// Tx hash of the transaction in the ZKsync network. Calculated as the encoded transaction data hash.
449 pub canonical_tx_hash: B256,
450 /// The amount of ETH that should be minted with this transaction
451 pub to_mint: U256,
452 /// The recipient of the refund of the transaction
453 pub refund_recipient: Address,
454}
455
456#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
457#[serde(rename_all = "camelCase")]
458pub struct L2TxCommonData {
459 pub nonce: u32,
460 pub fee: Eip712Fee,
461 pub initiator_address: Address,
462 pub signature: Bytes,
463 pub transaction_type: String,
464 pub input: Option<InputData>,
465 pub paymaster_params: PaymasterParams,
466}
467
468#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
469pub struct ProtocolUpgradeTxCommonData {
470 /// Sender of the transaction.
471 pub sender: Address,
472 /// ID of the upgrade.
473 pub upgrade_id: String,
474 /// The maximal fee per gas to be used for L1->L2 transaction
475 pub max_fee_per_gas: U256,
476 /// The maximum number of gas that a transaction can spend at a price of gas equals 1.
477 pub gas_limit: U256,
478 /// The maximum number of gas per 1 byte of pubdata.
479 pub gas_per_pubdata_limit: U256,
480 /// Block in which Ethereum transaction was included.
481 pub eth_block: u64,
482 /// Tx hash of the transaction in the ZKsync network. Calculated as the encoded transaction data hash.
483 pub canonical_tx_hash: B256,
484 /// The amount of ETH that should be minted with this transaction
485 pub to_mint: U256,
486 /// The recipient of the refund of the transaction
487 pub refund_recipient: Address,
488}
489
490#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
491pub enum ExecuteTransactionCommon {
492 L1(L1TxCommonData),
493 L2(L2TxCommonData),
494 ProtocolUpgrade(ProtocolUpgradeTxCommonData),
495}
496
497#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
498pub struct Transaction {
499 /// Common information about the transaction.
500 pub common_data: ExecuteTransactionCommon,
501 /// Details regarding the execution of the transaction.
502 pub execute: Execute,
503 /// Timestamp when the transaction was received, in milliseconds.
504 pub received_timestamp_ms: u64,
505 /// Raw bytes of the transaction.
506 pub raw_bytes: Option<Bytes>,
507}