Skip to content

Commit

Permalink
chore: improve e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters committed Dec 11, 2024
1 parent f6a0ef3 commit c4d9aa7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 7 additions & 2 deletions e2e-tests-rust/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module containing extensions of existing alloy abstractions.
use alloy::network::ReceiptResponse;
use alloy::primitives::{Address, BlockHash};
use alloy::network::{ReceiptResponse,TxSigner};
use alloy::primitives::{Address,BlockHash,PrimitiveSignature};
use alloy::providers::WalletProvider;
use alloy::signers::local::PrivateKeySigner;
use alloy_zksync::network::Zksync;
Expand Down Expand Up @@ -78,6 +78,11 @@ pub trait ZksyncWalletProviderExt: WalletProvider<Zksync, Wallet = ZksyncWallet>
self.wallet_mut().register_signer(signer);
address
}

/// Registers provider signer.
fn register_signer<T: TxSigner<PrimitiveSignature> + Send + Sync + 'static>(&mut self, signer: T) {
self.wallet_mut().register_signer(signer);
}
}

impl<T: WalletProvider<Zksync, Wallet = ZksyncWallet>> ZksyncWalletProviderExt for T {}
18 changes: 18 additions & 0 deletions e2e-tests-rust/src/provider/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,24 @@ where
self
}

/// Builder-pattern method for setting the chain id.
pub fn with_chain_id(mut self, id: u64) -> Self {
self.inner = self.inner.with_chain_id(id);
self
}

/// Builder-pattern method for setting the recipient.
pub fn with_to(mut self, to: Address) -> Self {
self.inner = self.inner.with_to(to);
self
}

/// Builder-pattern method for setting the value.
pub fn with_value(mut self, value: U256) -> Self {
self.inner = self.inner.with_value(value);
self
}

/// Submits transaction to the node.
///
/// This does not wait for the transaction to be confirmed, but returns a [`PendingTransactionFinalizable`]
Expand Down
21 changes: 19 additions & 2 deletions e2e-tests-rust/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use alloy::providers::Provider;
use anvil_zksync_e2e_tests::{
init_testing_provider, AnvilZKsyncApi, ReceiptExt, ZksyncWalletProviderExt, DEFAULT_TX_VALUE,
};
use alloy::{
primitives::U256,
signers::local::PrivateKeySigner,
};
use std::convert::identity;
use std::time::Duration;

Expand Down Expand Up @@ -337,12 +341,25 @@ async fn cant_load_into_existing_state() -> anyhow::Result<()> {

#[tokio::test]
async fn set_chain_id() -> anyhow::Result<()> {
let provider = init_testing_provider(|node| node.block_time(3)).await?;
let mut provider = init_testing_provider(identity).await?;

let random_signer = PrivateKeySigner::random();
let random_signer_address = random_signer.address();

let new_chain_id = 261;
// Send transaction before changing chain id
provider.tx().with_to(random_signer_address).with_value(U256::from(1e18 as u64)).finalize().await?;

// Change chain id
let new_chain_id = 123;
provider.anvil_set_chain_id(new_chain_id).await?;

// Verify new chain id
assert_eq!(new_chain_id, provider.get_chain_id().await?);

// Verify transactions can be executed after chain id change
// Registering and using new signer to get new chain id applied
provider.register_signer(random_signer);
provider.tx().with_from(random_signer_address).with_chain_id(new_chain_id).finalize().await?;

Ok(())
}

0 comments on commit c4d9aa7

Please sign in to comment.