-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create utilities for common test patterns
- Loading branch information
Showing
8 changed files
with
574 additions
and
288 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! Module containing extensions of existing alloy abstractions. | ||
use alloy::network::ReceiptResponse; | ||
use alloy::primitives::{Address, BlockHash}; | ||
use alloy::providers::WalletProvider; | ||
use alloy::signers::local::PrivateKeySigner; | ||
use alloy_zksync::network::Zksync; | ||
use alloy_zksync::wallet::ZksyncWallet; | ||
|
||
pub trait ReceiptExt: ReceiptResponse { | ||
fn block_number_ext(&self) -> anyhow::Result<u64> { | ||
self.block_number().ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"receipt (hash={}) does not have block number", | ||
self.transaction_hash() | ||
) | ||
}) | ||
} | ||
|
||
fn block_hash_ext(&self) -> anyhow::Result<BlockHash> { | ||
self.block_hash().ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"receipt (hash={}) does not have block hash", | ||
self.transaction_hash() | ||
) | ||
}) | ||
} | ||
|
||
/// Asserts that receipts belong to a block and that block is the same for both of them. | ||
fn assert_same_block(&self, other: &Self) -> anyhow::Result<()> { | ||
let lhs_number = self.block_number_ext()?; | ||
let rhs_number = other.block_number_ext()?; | ||
let lhs_hash = self.block_hash_ext()?; | ||
let rhs_hash = other.block_hash_ext()?; | ||
|
||
if lhs_number == rhs_number && lhs_hash == rhs_hash { | ||
Ok(()) | ||
} else { | ||
anyhow::bail!( | ||
"receipt (hash={}, block={}) is not from the same block as receipt (hash={}, block={})", | ||
self.transaction_hash(), | ||
lhs_number, | ||
other.transaction_hash(), | ||
rhs_number | ||
) | ||
} | ||
} | ||
/// Asserts that receipt is successful. | ||
fn assert_successful(&self) -> anyhow::Result<()> { | ||
if !self.status() { | ||
anyhow::bail!( | ||
"receipt (hash={}, block={:?}) is not successful", | ||
self.transaction_hash(), | ||
self.block_number(), | ||
); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: ReceiptResponse> ReceiptExt for T {} | ||
|
||
pub trait ZksyncWalletProviderExt: WalletProvider<Zksync, Wallet = ZksyncWallet> { | ||
/// Creates and registers a random signer. Returns new signer's address. | ||
fn register_random_signer(&mut self) -> Address { | ||
let signer = PrivateKeySigner::random(); | ||
let address = signer.address(); | ||
self.wallet_mut().register_signer(signer); | ||
address | ||
} | ||
} | ||
|
||
impl<T: WalletProvider<Zksync, Wallet = ZksyncWallet>> ZksyncWalletProviderExt for T {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,8 @@ | ||
use alloy::network::Network; | ||
use alloy::providers::{Provider, ProviderCall}; | ||
use alloy::rpc::client::NoParams; | ||
use alloy::serde::WithOtherFields; | ||
use alloy::transports::Transport; | ||
use alloy_zksync::network::Zksync; | ||
#![allow(async_fn_in_trait)] | ||
|
||
pub mod utils; | ||
mod ext; | ||
mod provider; | ||
mod utils; | ||
|
||
pub trait AnvilZKsyncApiProvider<T>: Provider<T, Zksync> | ||
where | ||
T: Transport + Clone, | ||
{ | ||
/// Custom version of [`alloy::providers::ext::AnvilApi::anvil_mine_detailed`] that returns | ||
/// block representation with transactions that contain extra custom fields. | ||
fn mine_detailed( | ||
&self, | ||
) -> ProviderCall< | ||
T, | ||
NoParams, | ||
alloy::rpc::types::Block< | ||
WithOtherFields<<Zksync as Network>::TransactionResponse>, | ||
<Zksync as Network>::HeaderResponse, | ||
>, | ||
> { | ||
self.client().request_noparams("anvil_mine_detailed").into() | ||
} | ||
} | ||
|
||
impl<P, T> AnvilZKsyncApiProvider<T> for P | ||
where | ||
T: Transport + Clone, | ||
P: Provider<T, Zksync>, | ||
{ | ||
} | ||
pub use ext::{ReceiptExt, ZksyncWalletProviderExt}; | ||
pub use provider::{init_testing_provider, AnvilZKsyncApi, TestingProvider}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use alloy::network::Network; | ||
use alloy::providers::{Provider, ProviderCall}; | ||
use alloy::rpc::client::NoParams; | ||
use alloy::serde::WithOtherFields; | ||
use alloy::transports::Transport; | ||
use alloy_zksync::network::Zksync; | ||
|
||
/// RPC interface that gives access to methods specific to anvil-zksync. | ||
pub trait AnvilZKsyncApi<T>: Provider<T, Zksync> | ||
where | ||
T: Transport + Clone, | ||
{ | ||
/// Custom version of [`alloy::providers::ext::AnvilApi::anvil_mine_detailed`] that returns | ||
/// block representation with transactions that contain extra custom fields. | ||
fn anvil_zksync_mine_detailed( | ||
&self, | ||
) -> ProviderCall< | ||
T, | ||
NoParams, | ||
alloy::rpc::types::Block< | ||
WithOtherFields<<Zksync as Network>::TransactionResponse>, | ||
<Zksync as Network>::HeaderResponse, | ||
>, | ||
> { | ||
self.client().request_noparams("anvil_mine_detailed").into() | ||
} | ||
} | ||
|
||
impl<P, T> AnvilZKsyncApi<T> for P | ||
where | ||
T: Transport + Clone, | ||
P: Provider<T, Zksync>, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod anvil_zksync; | ||
mod testing; | ||
|
||
pub use anvil_zksync::AnvilZKsyncApi; | ||
pub use testing::{init_testing_provider, TestingProvider}; |
Oops, something went wrong.