Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding support for 0.11.0 #52

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
316 changes: 197 additions & 119 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
[package]
name = "alloy-zksync"
version = "0.9.1"
version = "0.11.0"
edition = "2021"
authors = ["Igor Aleksanov <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "ZKsync network implementation for alloy"

[dependencies]
alloy = { version = "0.9.2", default-features = false, features = [
alloy = { version = "0.11.0", default-features = false, features = [
"rlp",
"serde", # TODO: Make optional along with other `serde` dependencies
"serde", # TODO: Make optional along with other `serde` dependencies
"rpc-types",
"signer-local",
"reqwest",
"contract",
] }
async-trait = "0.1.80"
async-trait = "0.1.86"
chrono = { version = "0.4.38", features = ["serde"] }
k256 = "0.13.3"
rand = "0.8.5"
rand = "0.9.0"
reqwest = "0.12.8"
serde = "1.0.203"
thiserror = "2.0.10"
thiserror = "2.0.11"
tracing = "0.1.40"
url = "2.5.2"

Expand All @@ -30,5 +30,5 @@ tokio = { version = "1.43.0", features = ["full"] }
anyhow = "1"
hex = "0.4.3"
assert_matches = "1.5.0"
serde_json = "1.0.1"
jsonrpsee = { version = "0.24.7", features = ["server"] }
serde_json = "1.0.138"
jsonrpsee = { version = "0.24.8", features = ["server"] }
10 changes: 2 additions & 8 deletions examples/deposit_erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ async fn main() -> Result<()> {
.expect("should parse private key");
let wallet = EthereumWallet::from(signer.clone());

let l1_provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet)
.on_http(l1_rpc_url);
let l1_provider = ProviderBuilder::new().wallet(wallet).on_http(l1_rpc_url);

let l1_gas_price = l1_provider.get_gas_price().await?;
// Deploy a test ERC20 token to be used for the deposit.
Expand All @@ -51,10 +48,7 @@ async fn main() -> Result<()> {
println!("L1 ERC20 token address: {}", erc20_token_address);

let zksync_wallet: ZksyncWallet = ZksyncWallet::from(signer.clone());
let zksync_provider = zksync_provider()
.with_recommended_fillers()
.wallet(zksync_wallet)
.on_http(l2_rpc_url);
let zksync_provider = zksync_provider().wallet(zksync_wallet).on_http(l2_rpc_url);

// use another test rich wallet as a receiver
// https://github.com/matter-labs/local-setup/blob/main/rich-wallets.json
Expand Down
10 changes: 2 additions & 8 deletions examples/deposit_eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,10 @@ async fn main() -> Result<()> {
.expect("should parse private key");
let wallet = EthereumWallet::from(signer.clone());

let l1_provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet)
.on_http(l1_rpc_url);
let l1_provider = ProviderBuilder::new().wallet(wallet).on_http(l1_rpc_url);

let zksync_wallet: ZksyncWallet = ZksyncWallet::from(signer.clone());
let zksync_provider = zksync_provider()
.with_recommended_fillers()
.wallet(zksync_wallet)
.on_http(l2_rpc_url);
let zksync_provider = zksync_provider().wallet(zksync_wallet).on_http(l2_rpc_url);

// use another test rich wallet as a receiver
// https://github.com/matter-labs/local-setup/blob/main/rich-wallets.json
Expand Down
11 changes: 4 additions & 7 deletions src/contracts/common/erc20.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! ZKsync-specific utilities related to ERC20 contracts.

use alloy::network::Ethereum;
use alloy::{
contract::Error,
dyn_abi::DynSolValue,
network::Network,
primitives::{Bytes, U256},
transports::Transport,
};
use ERC20::ERC20Instance;

Expand Down Expand Up @@ -35,13 +34,11 @@ alloy::sol! {
///
/// A `Result` containing the encoded token data as `Bytes` or an `Error`.
/// ```
pub(crate) async fn encode_token_data_for_bridge<T, P, N>(
erc20_contract: &ERC20Instance<T, P, N>,
pub(crate) async fn encode_token_data_for_bridge<P>(
erc20_contract: &ERC20Instance<(), P>,
) -> Result<Bytes, Error>
where
T: Transport + Clone,
P: alloy::providers::Provider<T, N>,
N: Network,
P: alloy::providers::Provider<Ethereum>,
{
let erc20_name = erc20_contract.name().call().await?._0;
let erc20_symbol = erc20_contract.symbol().call().await?._0;
Expand Down
15 changes: 12 additions & 3 deletions src/network/receipt_envelope.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use core::fmt;

use alloy::consensus::{TxReceipt, TxType};
use alloy::network::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
use alloy::network::eip2718::{Decodable2718, Eip2718Error, Encodable2718, Typed2718};
use alloy::network::AnyReceiptEnvelope;
use alloy::primitives::Log;
use core::convert::TryInto;
use core::fmt;
use serde::{Deserialize, Serialize};

/// Receipt envelope is a wrapper around the receipt data.
Expand Down Expand Up @@ -62,6 +62,15 @@ where
}
}

impl Typed2718 for ReceiptEnvelope {
fn ty(&self) -> u8 {
match self {
ReceiptEnvelope::Native(re) => re.ty(),
ReceiptEnvelope::Eip712(re) => re.ty(),
}
}
}

impl Encodable2718 for ReceiptEnvelope {
fn type_flag(&self) -> Option<u8> {
match self {
Expand Down
11 changes: 5 additions & 6 deletions src/network/receipt_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use alloy::rpc::types::{Log, TransactionReceipt};

use super::receipt_envelope::ReceiptEnvelope;
use crate::types::*;
use alloy::eips::eip7702::SignedAuthorization;

/// Transaction receipt type that includes L2 specific fields.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -44,6 +43,11 @@ impl ReceiptResponse {
pub fn l2_to_l1_logs(&self) -> &[L2ToL1Log] {
&self.l2_to_l1_logs
}

/// Returns the authorization list for the transaction.
pub fn authorization_list(&self) -> Option<&Vec<Address>> {
None
}
}

impl<T: TxReceipt<Log = Log>> alloy::network::ReceiptResponse for ReceiptResponse<T> {
Expand Down Expand Up @@ -107,11 +111,6 @@ impl<T: TxReceipt<Log = Log>> alloy::network::ReceiptResponse for ReceiptRespons
self.inner.to()
}

/// EIP-7702 Authorization list.
fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
self.inner.authorization_list()
}

/// Returns the cumulative gas used at this receipt.
fn cumulative_gas_used(&self) -> u64 {
self.inner.cumulative_gas_used()
Expand Down
14 changes: 4 additions & 10 deletions src/node_bindings/anvil_zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl AnvilZKsync {
pub fn new() -> Self {
let mut self_ = Self::default();
// Assign a random port so that we can run multiple instances.
let port = rand::thread_rng().gen_range(8000..16000);
let port = rand::rng().random_range(8000..16000);
self_.port = Some(port);
self_
}
Expand Down Expand Up @@ -458,9 +458,7 @@ mod tests {
let chain_id = 92;
let anvil_zksync = AnvilZKsync::new().chain_id(chain_id).spawn();
let rpc_url = anvil_zksync.endpoint_url();
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_http(rpc_url);
let provider = ProviderBuilder::new().on_http(rpc_url);

let returned_chain_id = provider.get_chain_id().await.unwrap();

Expand All @@ -474,9 +472,7 @@ mod tests {
async fn fork_initializes_correct_chain() {
let anvil_zksync = AnvilZKsync::new().fork("mainnet").spawn();
let rpc_url = anvil_zksync.endpoint_url();
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_http(rpc_url);
let provider = ProviderBuilder::new().on_http(rpc_url);

let chain_id = provider.get_chain_id().await.unwrap();

Expand All @@ -495,9 +491,7 @@ mod tests {
.spawn();

let rpc_url = anvil_zksync.endpoint_url();
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.on_http(rpc_url);
let provider = ProviderBuilder::new().on_http(rpc_url);

// Query the latest block number to verify the fork block number.
let block_number = provider.get_block_number().await.unwrap();
Expand Down
30 changes: 12 additions & 18 deletions src/provider/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ use alloy::{
primitives::{Address, Bytes, U256},
providers::{utils::Eip1559Estimation, WalletProvider},
rpc::types::eth::TransactionRequest as L1TransactionRequest,
transports::Transport,
};
use std::{marker::PhantomData, str::FromStr};
use std::str::FromStr;

pub const REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT: u64 = 800;

Expand Down Expand Up @@ -134,31 +133,27 @@ pub fn scale_l1_gas_limit(l1_gas_limit: u64) -> u64 {
}

/// Type that handles deposit logic for various scenarios: deposit ETH, ERC20 etc.
pub struct DepositExecutor<'a, T, P1, P2>
pub struct DepositExecutor<'a, P1, P2>
where
P1: alloy::providers::Provider<T, Ethereum>,
P2: ZksyncProvider<T> + WalletProvider<Zksync> + ?Sized,
T: Transport + Clone,
P1: alloy::providers::Provider<Ethereum>,
P2: ZksyncProvider + WalletProvider<Zksync> + ?Sized,
{
l1_provider: &'a P1,
l2_provider: &'a P2,
request: &'a DepositRequest,
_transport_phantom: PhantomData<T>,
}

impl<'a, T, P1, P2> DepositExecutor<'a, T, P1, P2>
impl<'a, P1, P2> DepositExecutor<'a, P1, P2>
where
P1: alloy::providers::Provider<T, Ethereum>,
P2: ZksyncProvider<T> + WalletProvider<Zksync> + ?Sized,
T: Transport + Clone,
P1: alloy::providers::Provider<Ethereum>,
P2: ZksyncProvider + WalletProvider<Zksync> + ?Sized,
{
/// Prepares an executor for a particular deposit request.
pub fn new(l1_provider: &'a P1, l2_provider: &'a P2, request: &'a DepositRequest) -> Self {
DepositExecutor {
l1_provider,
l2_provider,
request,
_transport_phantom: PhantomData,
}
}

Expand Down Expand Up @@ -250,16 +245,15 @@ where
Ok(l1_gas_limit)
}

async fn get_bridge_l2_tx_fee_params<Tr, P>(
async fn get_bridge_l2_tx_fee_params<P>(
&self,
bridge_hub_contract: &Bridgehub::BridgehubInstance<Tr, P, Ethereum>,
bridge_hub_contract: &Bridgehub::BridgehubInstance<(), &P>,
l1_to_l2_tx: TransactionRequest,
l2_chain_id: U256,
fee_params: &FeeParams,
) -> Result<BridgeL2TxFeeParams, L1CommunicationError>
where
Tr: Transport + Clone,
P: alloy::providers::Provider<Tr, Ethereum>,
P: alloy::providers::Provider<Ethereum>,
{
let gas_limit = self
.l2_provider
Expand Down Expand Up @@ -454,7 +448,7 @@ where
async fn submit(
&self,
tx_request: &L1TransactionRequest,
) -> Result<L1TransactionReceipt<T>, L1CommunicationError> {
) -> Result<L1TransactionReceipt, L1CommunicationError> {
let l1_gas_limit = self.get_l1_tx_gas_limit(tx_request).await?;
let l1_tx_request = tx_request.clone().with_gas_limit(l1_gas_limit);
let l1_tx_receipt = self
Expand Down Expand Up @@ -491,7 +485,7 @@ where
/// ## Returns
///
/// L1TransactionReceipt of the deposit transaction.
pub async fn execute(&self) -> Result<L1TransactionReceipt<T>, L1CommunicationError> {
pub async fn execute(&self) -> Result<L1TransactionReceipt, L1CommunicationError> {
let l2_chain_id = U256::from(self.l2_provider.get_chain_id().await.map_err(|_| {
L1CommunicationError::Custom("Error occurred while fetching L2 chain id.")
})?);
Expand Down
7 changes: 3 additions & 4 deletions src/provider/fillers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy::{
fillers::{FillerControlFlow, TxFiller},
Provider, SendableTx,
},
transports::{Transport, TransportResult},
transports::TransportResult,
};

use crate::network::{transaction_request::TransactionRequest, Zksync};
Expand Down Expand Up @@ -40,14 +40,13 @@ impl TxFiller<Zksync> for Eip712FeeFiller {

fn fill_sync(&self, _tx: &mut SendableTx<Zksync>) {}

async fn prepare<P, T>(
async fn prepare<P>(
&self,
provider: &P,
tx: &TransactionRequest,
) -> TransportResult<Self::Fillable>
where
P: Provider<T, Zksync>,
T: Transport + Clone,
P: Provider<Zksync>,
{
let fee = provider.estimate_fee(tx.clone()).await?;
Ok(fee)
Expand Down
14 changes: 5 additions & 9 deletions src/provider/l1_transaction_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ use crate::{contracts::l1::bridge_hub::Bridgehub::NewPriorityRequest, network::Z
use alloy::{
providers::{PendingTransactionBuilder, RootProvider},
rpc::types::eth::TransactionReceipt,
transports::Transport,
};

/// A wrapper struct to hold L1 transaction receipt and L2 provider
/// which is used by the associated functions.
pub struct L1TransactionReceipt<T> {
pub struct L1TransactionReceipt {
/// Ethereum transaction receipt.
inner: TransactionReceipt,
/// A reference to the L2 provider.
l2_provider: RootProvider<T, Zksync>,
l2_provider: RootProvider<Zksync>,
}

impl<T> L1TransactionReceipt<T>
where
T: Transport + Clone,
{
impl L1TransactionReceipt {
/// Creates a new `L1TransactionReceipt` object.
pub fn new(tx_receipt: TransactionReceipt, l2_provider: RootProvider<T, Zksync>) -> Self {
pub fn new(tx_receipt: TransactionReceipt, l2_provider: RootProvider<Zksync>) -> Self {
Self {
inner: tx_receipt,
l2_provider,
Expand All @@ -37,7 +33,7 @@ where
///
/// Will return an error if the transaction request used to create the object does not contain
/// priority operation information (e.g. it doesn't correspond to an L1->L2 transaction).
pub fn get_l2_tx(&self) -> Result<PendingTransactionBuilder<T, Zksync>, L1CommunicationError> {
pub fn get_l2_tx(&self) -> Result<PendingTransactionBuilder<Zksync>, L1CommunicationError> {
let l1_to_l2_tx_log = self
.inner
.inner
Expand Down
Loading