Skip to content

Commit

Permalink
remove zksync-web3-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec committed Nov 19, 2024
1 parent 1a5e5a8 commit 9047673
Show file tree
Hide file tree
Showing 20 changed files with 505 additions and 1,769 deletions.
1,498 changes: 298 additions & 1,200 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ alloy-chains = "0.1"
alloy-rlp = "0.3"
alloy-trie = "0.6.0"
# alloy-zksync = "0.1.0"
alloy-zksync = { git = "https://github.com/popzxc/alloy-zksync", branch = "main"}
# alloy-zksync = { git = "https://github.com/popzxc/alloy-zksync", branch = "main"}
# alloy-zksync = { git = "https://github.com/nbaztec/alloy-zksync", branch = "v0.5"}
alloy-zksync = { path = "../nbaztec-alloy-zksync" }

## op-alloy
op-alloy-rpc-types = "0.5.0"
Expand All @@ -244,8 +246,6 @@ terminal_size = "0.4"

## zksync
era_test_node = { git="https://github.com/matter-labs/era-test-node.git" , rev = "94503847b402f0161c3372d5f357a4cb16a2d66d" }
zksync-web3-rs = {git = "https://github.com/jrigada/zksync-web3-rs.git", rev = "bc3e6d3e75b7ff3747ff2dccefa9fb74d770931b"}
# zksync-web3-rs = {git = "https://github.com/lambdaclass/zksync-web3-rs.git", rev = "56653345d14331e0865a6785c77cdda63c94eeba"}
zksync_basic_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "6c034f6e180cc92e99766f14c8840c90efa56cec" }
zksync_types = { git = "https://github.com/matter-labs/zksync-era.git", rev = "6c034f6e180cc92e99766f14c8840c90efa56cec" }
zksync_state = { git = "https://github.com/matter-labs/zksync-era.git", rev = "6c034f6e180cc92e99766f14c8840c90efa56cec" }
Expand Down
4 changes: 3 additions & 1 deletion crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ foundry-config.workspace = true
foundry-evm.workspace = true
foundry-wallets.workspace = true
foundry-zksync-core.workspace = true
zksync-web3-rs.workspace = true

alloy-chains.workspace = true
alloy-consensus = { workspace = true, features = ["serde", "kzg"] }
Expand All @@ -59,6 +58,9 @@ alloy-signer-local = { workspace = true, features = ["mnemonic", "keystore"] }
alloy-signer.workspace = true
alloy-sol-types.workspace = true
alloy-transport.workspace = true
alloy-zksync.workspace = true

zksync_types.workspace = true

chrono.workspace = true
eyre.workspace = true
Expand Down
79 changes: 50 additions & 29 deletions crates/cast/bin/cmd/send.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
use crate::tx::{self, CastTxBuilder};
use alloy_network::{AnyNetwork, EthereumWallet};
use alloy_network::{AnyNetwork, EthereumWallet, TransactionBuilder};
use alloy_primitives::{Address, Bytes, TxHash};
use alloy_provider::{Provider, ProviderBuilder};
use alloy_rpc_types::TransactionRequest;
use alloy_serde::WithOtherFields;
use alloy_signer::Signer;
use alloy_transport::Transport;
use cast::Cast;
use alloy_zksync::{
network::{
transaction_request::TransactionRequest as ZkTransactionRequest,
unsigned_tx::eip712::PaymasterParams, Zksync,
},
wallet::ZksyncWallet,
};
use cast::{Cast, ZkCast};
use clap::{builder::ArgPredicate, Parser};
use eyre::Result;
use foundry_cli::{
Expand All @@ -15,10 +22,9 @@ use foundry_cli::{
};
use foundry_common::ens::NameOrAddress;
use foundry_config::Config;
use foundry_wallets::WalletSigner;
use foundry_zksync_core::{self, convert::ConvertAddress};
use foundry_zksync_core::convert::ConvertU256;
use std::{path::PathBuf, str::FromStr};
use zksync_web3_rs::eip712::PaymasterParams;
use zksync_types::fee::Fee;

/// ZkSync-specific paymaster parameters for transactions
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -144,6 +150,7 @@ impl SendTxArgs {
config.zksync.compile = zksync_params.zksync;

let provider = utils::get_provider(&config)?;
let zk_provider = utils::get_provider_zksync(&config)?;

let builder = CastTxBuilder::new(&provider, tx, &config)
.await?
Expand Down Expand Up @@ -189,20 +196,33 @@ impl SendTxArgs {
} else {
// Retrieve the signer, and bail if it can't be constructed.
let signer = eth.wallet.signer().await?;
let zk_signer = eth.wallet.signer().await?;
let from = signer.address();

tx::validate_from_address(eth.wallet.from, from)?;

if zksync_params.zksync {
// Zksync transaction
let (tx, _) = builder.build(&signer).await?;

let wallet = EthereumWallet::from(signer);
let provider = ProviderBuilder::<_, _, AnyNetwork>::default()
.wallet(wallet)
.on_provider(&provider);

let zk_wallet = ZksyncWallet::from(zk_signer);
let zk_provider = ProviderBuilder::<_, _, Zksync>::default()
.wallet(zk_wallet)
.on_provider(&zk_provider);

cast_send_zk(
&provider,
zksync_params,
provider,
zk_provider,
tx,
zksync_params,
cast_async,
confirmations,
timeout,
signer,
)
.await
} else {
Expand Down Expand Up @@ -235,40 +255,41 @@ async fn cast_send<P: Provider<T, AnyNetwork>, T: Transport + Clone>(
handle_transaction_result(&cast, tx_hash, cast_async, confs, timeout).await
}

#[allow(clippy::too_many_arguments)]
async fn cast_send_zk<P: Provider<T, AnyNetwork>, T: Transport + Clone>(
async fn cast_send_zk<P: Provider<T, AnyNetwork>, Z: Provider<T, Zksync>, T: Transport + Clone>(
provider: P,
zksync_params: ZksyncParams,
zk_provider: Z,
tx: WithOtherFields<TransactionRequest>,
zksync_params: ZksyncParams,
cast_async: bool,
confs: u64,
timeout: u64,
signer: WalletSigner,
) -> Result<()> {
// ZkSync transaction
let paymaster_params = zksync_params
.paymaster_address
.and_then(|addr| zksync_params.paymaster_input.map(|input| (addr, input)))
.map(|(addr, input)| PaymasterParams {
paymaster: Address::from_str(&addr).expect("Invalid paymaster address").to_h160(),
paymaster_input: Bytes::from_str(&input).expect("Invalid paymaster input").to_vec(),
paymaster: Address::from_str(&addr).expect("Invalid paymaster address"),
paymaster_input: Bytes::from_str(&input).expect("Invalid paymaster input"),
});

// Build EIP712 transaction for ZKSync
let tx = foundry_zksync_core::new_eip712_transaction(
tx,
Vec::new(), // Empty factory_deps
paymaster_params,
&provider,
signer,
)
.await
.map_err(|e| eyre::eyre!("Failed to create EIP712 transaction: {}", e))?;

// Use send_raw_transaction for ZKSync
let tx_hash = provider.send_raw_transaction(&tx).await?.tx_hash().to_owned();
let cast = Cast::new(provider);
handle_transaction_result(&cast, &tx_hash, cast_async, confs, timeout).await
let mut zk_tx: ZkTransactionRequest = tx.inner.clone().into();
if let Some(paymaster_params) = paymaster_params {
zk_tx.set_paymaster(paymaster_params);
}

let fee = provider.raw_request::<_, Fee>("zks_estimateFee".into(), [&zk_tx]).await.unwrap();
zk_tx.set_gas_limit(fee.gas_limit.as_u64());
zk_tx.set_max_fee_per_gas(fee.max_fee_per_gas.as_u128());
zk_tx.set_max_priority_fee_per_gas(fee.max_priority_fee_per_gas.as_u128());
zk_tx.set_gas_per_pubdata(fee.gas_per_pubdata_limit.to_ru256());

let cast = ZkCast::new(zk_provider, Cast::new(provider));
let pending_tx = cast.send_zk(zk_tx).await?;

let tx_hash = pending_tx.inner().tx_hash();

handle_transaction_result(cast.as_ref(), tx_hash, cast_async, confs, timeout).await
}

async fn handle_transaction_result<P: Provider<T, AnyNetwork>, T: Transport + Clone>(
Expand Down
69 changes: 69 additions & 0 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use alloy_rpc_types::{BlockId, BlockNumberOrTag, Filter, TransactionRequest};
use alloy_serde::WithOtherFields;
use alloy_sol_types::sol;
use alloy_transport::Transport;
use alloy_zksync::network::{
transaction_request::TransactionRequest as ZkTransactionRequest, Zksync,
};
use base::{Base, NumberWithBase, ToBase};
use chrono::DateTime;
use eyre::{Context, ContextCompat, Result};
Expand Down Expand Up @@ -69,6 +72,72 @@ sol! {
}
}

pub struct ZkCast<P, T, Z> {
provider: Z,
inner: Cast<P, T>,
}

impl<P, T, Z> AsRef<Cast<P, T>> for ZkCast<P, T, Z>
where
P: Provider<T, AnyNetwork>,
T: Transport + Clone,
Z: Provider<T, Zksync>,
{
fn as_ref(&self) -> &Cast<P, T> {
&self.inner
}
}

impl<P, T, Z> ZkCast<P, T, Z>
where
P: Provider<T, AnyNetwork>,
T: Transport + Clone,
Z: Provider<T, Zksync>,
{
/// Creates a new Cast instance from the provided client
///
/// # Example
///
/// ```
/// use alloy_provider::{network::AnyNetwork, ProviderBuilder, RootProvider};
/// use cast::Cast;
///
/// # async fn foo() -> eyre::Result<()> {
/// let provider =
/// ProviderBuilder::<_, _, AnyNetwork>::default().on_builtin("http://localhost:8545").await?;
/// let cast = Cast::new(provider);
/// let zk_provider =
/// ProviderBuilder::<_, _, Zksync>::default().on_builtin("http://localhost:8011").await?;
/// let zk_cast = ZkCast::new(provider, cast);
/// # Ok(())
/// # }
/// ```
pub fn new(provider: Z, cast: Cast<P, T>) -> Self {
Self { provider, inner: cast }
}

pub async fn send_zk(
&self,
tx: ZkTransactionRequest,
) -> Result<PendingTransactionBuilder<T, Zksync>> {
let res = self.provider.send_transaction(tx).await?;

Ok(res)
}
}

// pub struct Cast<P, T, Z> {
// provider: P,
// zk_provider: Option<Z>,
// transport: PhantomData<T>,
// }

// impl<T, P, Z> Cast<P, T, Z>
// where
// T: Transport + Clone,
// P: Provider<T, AnyNetwork>,
// Z: Provider<T, Zksync>,
// {
pub struct Cast<P, T> {
provider: P,
transport: PhantomData<T>,
Expand Down
1 change: 0 additions & 1 deletion crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ forge-script-sequence.workspace = true
foundry-zksync-core.workspace = true
foundry-zksync-compiler.workspace = true
foundry-zksync-inspectors.workspace = true
zksync-web3-rs.workspace = true

zksync_types.workspace = true

Expand Down
2 changes: 1 addition & 1 deletion crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ use std::{
};
use zksync_types::{
block::{pack_block_info, unpack_block_info},
transaction_request::PaymasterParams,
utils::{decompose_full_nonce, nonces_to_full_nonce},
ACCOUNT_CODE_STORAGE_ADDRESS, CONTRACT_DEPLOYER_ADDRESS, CURRENT_VIRTUAL_BLOCK_INFO_POSITION,
H256, KNOWN_CODES_STORAGE_ADDRESS, L2_BASE_TOKEN_ADDRESS, NONCE_HOLDER_ADDRESS,
SYSTEM_CONTEXT_ADDRESS,
};
use zksync_web3_rs::eip712::PaymasterParams;

mod utils;

Expand Down
1 change: 0 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ foundry-config.workspace = true
foundry-debugger.workspace = true
foundry-evm.workspace = true
foundry-wallets.workspace = true
zksync-web3-rs.workspace = true

foundry-compilers = { workspace = true, features = ["full"] }

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/opts/build/zksync.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{collections::HashSet, path::PathBuf};

use alloy_primitives::{Address, Bytes};
use clap::Parser;
use foundry_compilers::zksolc::settings::{ZkSolcError, ZkSolcWarning};
use foundry_config::ZkSyncConfig;
use serde::Serialize;
use zksync_web3_rs::types::{Address, Bytes};

#[derive(Clone, Debug, Default, Serialize, Parser)]
#[clap(next_help_heading = "ZKSync configuration")]
Expand Down
6 changes: 6 additions & 0 deletions crates/common/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ pub fn try_get_http_provider(builder: impl AsRef<str>) -> Result<RetryProvider>
ProviderBuilder::new(builder.as_ref()).build()
}

/// Constructs a ZKsync provider with a 100 millisecond interval poll if it's a localhost URL (most
/// likely an anvil or other dev node) and with the default, or 7 second otherwise.
#[inline]
pub fn try_get_zksync_http_provider(builder: impl AsRef<str>) -> Result<RetryProvider<Zksync>> {
ProviderBuilder::new(builder.as_ref()).build_zksync()
}
/// Helper type to construct a `RetryProvider`
#[derive(Debug)]
pub struct ProviderBuilder {
Expand Down
1 change: 0 additions & 1 deletion crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ soldeer-commands.workspace = true
quick-junit = "0.5.0"

# zk
zksync-web3-rs.workspace = true
zksync_types.workspace = true

[target.'cfg(unix)'.dependencies]
Expand Down
Loading

0 comments on commit 9047673

Please sign in to comment.