Skip to content

Commit

Permalink
fix: doc tests + event_filter tests + return impl Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Jan 8, 2025
1 parent d3694c8 commit 2d9c488
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 64 deletions.
1 change: 1 addition & 0 deletions crates/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ alloy-rpc-client = { workspace = true, features = ["pubsub", "ws"] }
alloy-transport-http.workspace = true
alloy-node-bindings.workspace = true
alloy-provider = { workspace = true, features = ["anvil-node"] }
alloy-signer-local.workspace = true

reqwest.workspace = true
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/contract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sol! {
}
// Build a provider.
let provider = ProviderBuilder::new().with_recommended_fillers().on_builtin("http://localhost:8545").await?;
let provider = ProviderBuilder::new().on_builtin("http://localhost:8545").await?;
// If `#[sol(bytecode = "0x...")]` is provided, the contract can be deployed with `MyContract::deploy`,
// and a new instance will be created.
Expand Down
11 changes: 3 additions & 8 deletions crates/contract/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,7 @@ mod tests {
use super::*;
use alloy_consensus::Transaction;
use alloy_primitives::{address, b256, bytes, hex, utils::parse_units, B256};
use alloy_provider::{
fillers::FillProvider, layers::AnvilProvider, utils::JoinedRecommendedFillers, Provider,
ProviderBuilder, RootProvider, WalletProvider,
};
use alloy_provider::{Provider, ProviderBuilder, WalletProvider};
use alloy_rpc_types_eth::AccessListItem;
use alloy_sol_types::sol;

Expand Down Expand Up @@ -620,12 +617,10 @@ mod tests {
}
}

type AnvilFillProvider =
FillProvider<JoinedRecommendedFillers, AnvilProvider<RootProvider>, Ethereum>;
/// Creates a new call_builder to test field modifications, taken from [call_encoding]
#[allow(clippy::type_complexity)]
fn build_call_builder(
) -> CallBuilder<(), AnvilFillProvider, PhantomData<MyContract::doStuffCall>> {
fn build_call_builder() -> CallBuilder<(), impl Provider, PhantomData<MyContract::doStuffCall>>
{
let provider = ProviderBuilder::new().on_anvil();
let contract = MyContract::new(Address::ZERO, provider);
let call_builder = contract.doStuff(U256::ZERO, true).with_cloned_provider();
Expand Down
78 changes: 26 additions & 52 deletions crates/contract/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,10 @@ pub(crate) mod subscription {
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{address, U256};
use alloy_network::EthereumWallet;
use alloy_primitives::U256;
use alloy_signer_local::PrivateKeySigner;
use alloy_sol_types::sol;
use MyContract::MyContractInstance;

sol! {
// solc v0.8.24; solc a.sol --via-ir --optimize --bin
Expand Down Expand Up @@ -311,12 +312,15 @@ mod tests {

let anvil = alloy_node_bindings::Anvil::new().spawn();

let provider = alloy_provider::ProviderBuilder::new().on_http(anvil.endpoint_url());
let pk: PrivateKeySigner =
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse().unwrap();
let wallet = EthereumWallet::from(pk);
let provider = alloy_provider::ProviderBuilder::new()
.wallet(wallet.clone())
.on_http(anvil.endpoint_url());

let from = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
let contract_addr =
MyContract::deploy_builder(&provider).from(from).deploy().await.unwrap();
let contract = MyContractInstance::new(contract_addr, &provider);
// let from = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
let contract = MyContract::deploy(&provider).await.unwrap();

let event: Event<(), _, MyContract::MyEvent, _> = Event::new(&provider, Filter::new());
let all = event.query().await.unwrap();
Expand All @@ -327,15 +331,8 @@ mod tests {

let poller = event.watch().await.unwrap();

let _receipt = contract
.doEmit()
.from(from)
.send()
.await
.unwrap()
.get_receipt()
.await
.expect("no receipt");
let _receipt =
contract.doEmit().send().await.unwrap().get_receipt().await.expect("no receipt");

let expected_event = MyContract::MyEvent {
_0: 42,
Expand All @@ -362,7 +359,6 @@ mod tests {
// send the wrong event and make sure it is NOT picked up by the event filter
let _wrong_receipt = contract
.doEmitWrongEvent()
.from(from)
.send()
.await
.unwrap()
Expand All @@ -378,6 +374,7 @@ mod tests {
#[cfg(feature = "pubsub")]
{
let provider = alloy_provider::ProviderBuilder::new()
.wallet(wallet)
.on_builtin(&anvil.ws_endpoint())
.await
.unwrap();
Expand All @@ -387,15 +384,7 @@ mod tests {

let sub = event.subscribe().await.unwrap();

contract
.doEmit()
.from(from)
.send()
.await
.unwrap()
.get_receipt()
.await
.expect("no receipt");
contract.doEmit().send().await.unwrap().get_receipt().await.expect("no receipt");

let mut stream = sub.into_stream();

Expand All @@ -411,7 +400,6 @@ mod tests {
// send the request to emit the wrong event
contract
.doEmitWrongEvent()
.from(from)
.send()
.await
.unwrap()
Expand All @@ -432,12 +420,14 @@ mod tests {
let _ = tracing_subscriber::fmt::try_init();

let anvil = alloy_node_bindings::Anvil::new().spawn();
let provider = alloy_provider::ProviderBuilder::new().on_http(anvil.endpoint_url());
let pk: PrivateKeySigner =
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".parse().unwrap();
let wallet = EthereumWallet::from(pk);
let provider = alloy_provider::ProviderBuilder::new()
.wallet(wallet.clone())
.on_http(anvil.endpoint_url());

let from = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
let contract_addr =
MyContract::deploy_builder(&provider).from(from).deploy().await.unwrap();
let contract = MyContractInstance::new(contract_addr, &provider);
let contract = MyContract::deploy(&provider).await.unwrap();

let event: Event<(), _, MyContract::MyEvent, _> = Event::new(&provider, Filter::new())
.address(*contract.address())
Expand All @@ -447,15 +437,8 @@ mod tests {

let poller = event.watch().await.unwrap();

let _receipt = contract
.doEmit()
.from(from)
.send()
.await
.unwrap()
.get_receipt()
.await
.expect("no receipt");
let _receipt =
contract.doEmit().send().await.unwrap().get_receipt().await.expect("no receipt");

let expected_event = MyContract::MyEvent {
_0: 42,
Expand All @@ -482,7 +465,6 @@ mod tests {
// send the wrong event and make sure it is NOT picked up by the event filter
let _wrong_receipt = contract
.doEmitWrongEvent()
.from(from)
.send()
.await
.unwrap()
Expand All @@ -498,6 +480,7 @@ mod tests {
#[cfg(feature = "pubsub")]
{
let provider = alloy_provider::ProviderBuilder::new()
.wallet(wallet)
.on_builtin(&anvil.ws_endpoint())
.await
.unwrap();
Expand All @@ -509,15 +492,7 @@ mod tests {

let sub = event.subscribe().await.unwrap();

contract
.doEmit()
.from(from)
.send()
.await
.unwrap()
.get_receipt()
.await
.expect("no receipt");
contract.doEmit().send().await.unwrap().get_receipt().await.expect("no receipt");

let mut stream = sub.into_stream();

Expand All @@ -533,7 +508,6 @@ mod tests {
// send the request to emit the wrong event
contract
.doEmitWrongEvent()
.from(from)
.send()
.await
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/chain_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
/// # use alloy_rpc_types_eth::TransactionRequest;
/// # use alloy_provider::{ProviderBuilder, RootProvider, Provider};
/// # async fn test<W: NetworkWallet<Ethereum> + Clone>(url: url::Url, wallet: W) -> Result<(), Box<dyn std::error::Error>> {
/// let provider = ProviderBuilder::new()
/// let provider = ProviderBuilder::default()
/// .with_chain_id(1)
/// .wallet(wallet)
/// .on_http(url);
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum GasFillable {
/// # use alloy_rpc_types_eth::TransactionRequest;
/// # use alloy_provider::{ProviderBuilder, RootProvider, Provider};
/// # async fn test<W: NetworkWallet<Ethereum> + Clone>(url: url::Url, wallet: W) -> Result<(), Box<dyn std::error::Error>> {
/// let provider = ProviderBuilder::new()
/// let provider = ProviderBuilder::default()
/// .with_gas_estimation()
/// .wallet(wallet)
/// .on_http(url);
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/src/fillers/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl NonceManager for CachedNonceManager {
/// # use alloy_rpc_types_eth::TransactionRequest;
/// # use alloy_provider::{ProviderBuilder, RootProvider, Provider};
/// # async fn test<W: NetworkWallet<Ethereum> + Clone>(url: url::Url, wallet: W) -> Result<(), Box<dyn std::error::Error>> {
/// let provider = ProviderBuilder::new()
/// let provider = ProviderBuilder::default()
/// .with_simple_nonce_management()
/// .wallet(wallet)
/// .on_http(url);
Expand Down

0 comments on commit 2d9c488

Please sign in to comment.