Skip to content

Commit

Permalink
chore: use alloy zksync (#608)
Browse files Browse the repository at this point in the history
* remove zksync-web3-rs

* simplify estimate_fee calls
  • Loading branch information
nbaztec authored Nov 21, 2024
1 parent f83f08e commit f75d7bf
Show file tree
Hide file tree
Showing 25 changed files with 505 additions and 1,289 deletions.
729 changes: 124 additions & 605 deletions Cargo.lock

Large diffs are not rendered by default.

43 changes: 23 additions & 20 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ syn-solidity = "0.8.11"
alloy-chains = "0.1"
alloy-rlp = "0.3"
alloy-trie = "0.6.0"
alloy-zksync = "0.5"

## op-alloy
op-alloy-rpc-types = "0.5.0"
Expand All @@ -241,9 +242,7 @@ anstyle = "1.0"
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"}
era_test_node = { git = "https://github.com/matter-labs/era-test-node.git", rev = "94503847b402f0161c3372d5f357a4cb16a2d66d" }
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 Expand Up @@ -351,27 +350,31 @@ yansi = { version = "1.0", features = ["detect-tty", "detect-env"] }
# alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy", rev = "7fab7ee" }
# alloy-transport-ws = { git = "https://github.com/alloy-rs/alloy", rev = "7fab7ee" }

alloy-rpc-client = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-ledger = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types-txpool = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-trezor = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-json-rpc = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-consensus = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-contract = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-eips = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport-http = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types-anvil = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-serde = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport-ws = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-local = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-genesis = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-json-rpc = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-network = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-network-primitives = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-contract = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-provider = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-aws = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types-eth = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-network-primitives = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport-ipc = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-pubsub = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-client = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-eips = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-network = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types-engine = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types-trace = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-rpc-types-eth = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-serde = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-aws = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-gcp = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-ledger = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-local = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-signer-trezor = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport-http = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport-ipc = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
alloy-transport-ws = { git = "https://github.com/Karrq/alloy", branch = "zksync-v0.5.4" }
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
89 changes: 55 additions & 34 deletions crates/cast/bin/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ 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,
},
provider::ZksyncProvider,
wallet::ZksyncWallet,
};
use cast::{Cast, ZkCast};
use clap::{builder::ArgPredicate, Parser};
use eyre::Result;
use foundry_cli::{
Expand All @@ -15,10 +23,7 @@ use foundry_cli::{
};
use foundry_common::ens::NameOrAddress;
use foundry_config::Config;
use foundry_wallets::WalletSigner;
use foundry_zksync_core::{self, convert::ConvertAddress};
use std::{path::PathBuf, str::FromStr};
use zksync_web3_rs::eip712::PaymasterParams;

/// ZkSync-specific paymaster parameters for transactions
#[derive(Debug, Parser)]
Expand Down Expand Up @@ -144,6 +149,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 @@ -187,25 +193,43 @@ impl SendTxArgs {
// If we cannot successfully instantiate a local signer, then we will assume we don't have
// enough information to sign and we must bail.
} else {
// Retrieve the signer, and bail if it can't be constructed.
let signer = eth.wallet.signer().await?;
let from = signer.address();
// NOTE(zk): Avoid initializing `signer` twice as it will error out with Ledger, so we
// move the signers to their respective blocks.
if zksync_params.zksync {
// Retrieve the signer, and bail if it can't be constructed.
let zk_signer: foundry_wallets::WalletSigner = eth.wallet.signer().await?;
let from = zk_signer.address();

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

// Zksync transaction
let (tx, _) = builder.build(&zk_signer).await?;

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

if zksync_params.zksync {
let (tx, _) = builder.build(&signer).await?;
cast_send_zk(
&provider,
zksync_params,
provider,
zk_provider,
tx,
zksync_params,
cast_async,
confirmations,
timeout,
signer,
)
.await
} else {
// Retrieve the signer, and bail if it can't be constructed.
let signer = eth.wallet.signer().await?;
let from = signer.address();

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

// Standard transaction
let (tx, _) = builder.build(&signer).await?;

Expand Down Expand Up @@ -235,40 +259,37 @@ 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: ZksyncProvider<T>, 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);
}

foundry_zksync_core::estimate_gas(&mut zk_tx, &zk_provider).await?;

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
56 changes: 56 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,59 @@ 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 ZkCast instance from the provided client and Cast instance
///
/// # 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> {
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
2 changes: 1 addition & 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 All @@ -31,6 +30,7 @@ alloy-provider.workspace = true
alloy-rlp.workspace = true
alloy-transport.workspace = true
alloy-chains.workspace = true
alloy-zksync.workspace = true

clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
color-eyre.workspace = true
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
7 changes: 7 additions & 0 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use alloy_json_abi::JsonAbi;
use alloy_primitives::U256;
use alloy_provider::{network::AnyNetwork, Provider};
use alloy_transport::Transport;
use alloy_zksync::network::Zksync;
use eyre::{ContextCompat, Result};
use foundry_common::{
provider::{ProviderBuilder, RetryProvider},
Expand Down Expand Up @@ -90,6 +91,12 @@ pub fn get_provider(config: &Config) -> Result<RetryProvider> {
get_provider_builder(config)?.build()
}

/// Returns a [RetryProvider] instantiated using [Config]'s
/// RPC for ZKsync
pub fn get_provider_zksync(config: &Config) -> Result<RetryProvider<Zksync>> {
get_provider_builder(config)?.build_zksync()
}

/// Returns a [ProviderBuilder] instantiated using [Config] values.
///
/// Defaults to `http://localhost:8545` and `Mainnet`.
Expand Down
1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ alloy-transport-ipc.workspace = true
alloy-transport-ws.workspace = true
alloy-transport.workspace = true
alloy-consensus = { workspace = true, features = ["k256"] }
alloy-zksync.workspace = true

tower.workspace = true

Expand Down
Loading

0 comments on commit f75d7bf

Please sign in to comment.