Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: dedup abi values formatting, cleanups #6196

Merged
merged 8 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use ethers::{
utils::{hex, keccak256, rlp},
};
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use foundry_common::abi::format_token;
use foundry_common::fmt::format_token;
use foundry_evm::{
backend::{DatabaseError, DatabaseResult},
constants::DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE,
Expand Down
20 changes: 9 additions & 11 deletions crates/anvil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,22 @@ impl NodeHandle {
self.config.get_ipc_path()
}

/// Returns a Provider for the http endpoint
/// Constructs a [`RetryProvider`] for this handle's HTTP endpoint.
pub fn http_provider(&self) -> RetryProvider {
ProviderBuilder::new(self.http_endpoint())
ProviderBuilder::new(&self.http_endpoint())
.build()
.expect("Failed to connect using http provider")
.expect("failed to build HTTP provider")
.interval(Duration::from_millis(500))
}

/// Connects to the websocket Provider of the node
pub async fn ws_provider(&self) -> RetryProvider {
ProviderBuilder::new(self.ws_endpoint())
.build()
.expect("Failed to connect to node's websocket")
/// Constructs a [`RetryProvider`] for this handle's WS endpoint.
pub fn ws_provider(&self) -> RetryProvider {
ProviderBuilder::new(&self.ws_endpoint()).build().expect("failed to build WS provider")
}

/// Connects to the ipc endpoint of the node, if spawned
pub async fn ipc_provider(&self) -> Option<RetryProvider> {
ProviderBuilder::new(self.config.get_ipc_path()?).build().ok()
/// Constructs a [`RetryProvider`] for this handle's IPC endpoint, if any.
pub fn ipc_provider(&self) -> Option<RetryProvider> {
ProviderBuilder::new(&self.config.get_ipc_path()?).build().ok()
}

/// Signer accounts that can sign messages/transactions from the EVM node
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/tests/it/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async fn can_get_block_number_ipc() {
let block_num = api.block_number().unwrap();
assert_eq!(block_num, U256::zero());

let provider = handle.ipc_provider().await.unwrap();
let provider = handle.ipc_provider().unwrap();

let num = provider.get_block_number().await.unwrap();
assert_eq!(num, block_num.as_u64().into());
Expand All @@ -34,7 +34,7 @@ async fn can_get_block_number_ipc() {
async fn test_sub_new_heads_ipc() {
let (api, handle) = spawn(ipc_config()).await;

let provider = handle.ipc_provider().await.unwrap();
let provider = handle.ipc_provider().unwrap();

let blocks = provider.subscribe_blocks().await.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion crates/anvil/tests/it/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async fn watch_events() {
let mut stream = event.stream().await.unwrap();

// Also set up a subscription for the same thing
let ws = Arc::new(handle.ws_provider().await);
let ws = Arc::new(handle.ws_provider());
let contract2 = SimpleStorage::new(contract.address(), ws);
let event2 = contract2.event::<ValueChanged>();
let mut subscription = event2.subscribe().await.unwrap();
Expand Down
8 changes: 4 additions & 4 deletions crates/anvil/tests/it/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -180,7 +180,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -284,7 +284,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -373,7 +373,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
Expand Down
10 changes: 5 additions & 5 deletions crates/anvil/tests/it/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::sync::Arc;
async fn test_sub_new_heads() {
let (api, handle) = spawn(NodeConfig::test()).await;

let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let blocks = provider.subscribe_blocks().await.unwrap();

Expand All @@ -34,7 +34,7 @@ async fn test_sub_logs_legacy() {
abigen!(EmitLogs, "test-data/emit_logs.json");

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
Expand Down Expand Up @@ -73,7 +73,7 @@ async fn test_sub_logs() {
abigen!(EmitLogs, "test-data/emit_logs.json");

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
Expand Down Expand Up @@ -111,7 +111,7 @@ async fn test_sub_logs_impersonated() {
abigen!(EmitLogs, "test-data/emit_logs.json");

let (api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

// impersonate account
let impersonate = Address::random();
Expand Down Expand Up @@ -253,7 +253,7 @@ async fn test_subscriptions() {
async fn test_sub_new_heads_fast() {
let (api, handle) = spawn(NodeConfig::test()).await;

let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let blocks = provider.subscribe_blocks().await.unwrap();

Expand Down
10 changes: 5 additions & 5 deletions crates/anvil/tests/it/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
Expand Down Expand Up @@ -75,7 +75,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -107,7 +107,7 @@ async fn test_solc_revert_example() {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -161,7 +161,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -208,7 +208,7 @@ contract Contract {

let (_api, handle) = spawn(NodeConfig::test()).await;

let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/tests/it/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down Expand Up @@ -119,7 +119,7 @@ contract Contract {
let (abi, bytecode, _) = contract.into_contract_bytecode().into_parts();

let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();
let wallets = handle.dev_wallets().collect::<Vec<_>>();
let client = Arc::new(SignerMiddleware::new(provider, wallets[0].clone()));

Expand Down
12 changes: 6 additions & 6 deletions crates/anvil/tests/it/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ async fn can_call_greeter_historic() {
#[tokio::test(flavor = "multi_thread")]
async fn can_deploy_greeter_ws() {
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
Expand All @@ -382,7 +382,7 @@ async fn can_deploy_greeter_ws() {
#[tokio::test(flavor = "multi_thread")]
async fn can_deploy_get_code() {
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));
Expand Down Expand Up @@ -496,7 +496,7 @@ async fn call_past_state() {
async fn can_handle_multiple_concurrent_transfers_with_same_nonce() {
let (_api, handle) = spawn(NodeConfig::test()).await;

let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let accounts: Vec<_> = handle.dev_wallets().collect();
let from = accounts[0].address();
Expand Down Expand Up @@ -526,7 +526,7 @@ async fn can_handle_multiple_concurrent_transfers_with_same_nonce() {
#[tokio::test(flavor = "multi_thread")]
async fn can_handle_multiple_concurrent_deploys_with_same_nonce() {
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let from = wallet.address();
Expand Down Expand Up @@ -560,7 +560,7 @@ async fn can_handle_multiple_concurrent_deploys_with_same_nonce() {
#[tokio::test(flavor = "multi_thread")]
async fn can_handle_multiple_concurrent_transactions_with_same_nonce() {
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let wallet = handle.dev_wallets().next().unwrap();
let from = wallet.address();
Expand Down Expand Up @@ -778,7 +778,7 @@ async fn can_stream_pending_transactions() {
spawn(NodeConfig::test().with_blocktime(Some(Duration::from_secs(2)))).await;
let num_txs = 5;
let provider = handle.http_provider();
let ws_provider = handle.ws_provider().await;
let ws_provider = handle.ws_provider();

let accounts = provider.get_accounts().await.unwrap();
let tx = TransactionRequest::new().from(accounts[0]).to(accounts[0]).value(1e18 as u64);
Expand Down
4 changes: 2 additions & 2 deletions crates/anvil/tests/it/wsapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async fn can_get_block_number_ws() {
let block_num = api.block_number().unwrap();
assert_eq!(block_num, U256::zero());

let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let num = provider.get_block_number().await.unwrap();
assert_eq!(num, block_num.as_u64().into());
Expand All @@ -18,7 +18,7 @@ async fn can_get_block_number_ws() {
#[tokio::test(flavor = "multi_thread")]
async fn can_dev_get_balance_ws() {
let (_api, handle) = spawn(NodeConfig::test()).await;
let provider = handle.ws_provider().await;
let provider = handle.ws_provider();

let genesis_balance = handle.genesis_balance();
for acc in handle.genesis_accounts() {
Expand Down
3 changes: 2 additions & 1 deletion crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use ethers::{
use eyre::{Result, WrapErr};
use foundry_cli::{handler, prompt, stdin, utils};
use foundry_common::{
abi::{format_tokens, get_event},
abi::get_event,
fmt::format_tokens,
fs,
selectors::{
decode_calldata, decode_event_topic, decode_function_selector, import_selectors,
Expand Down
17 changes: 7 additions & 10 deletions crates/cast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,26 @@ where
if res.is_empty() {
// check that the recipient is a contract that can be called
if let Some(NameOrAddress::Address(addr)) = tx.to() {
let code = self.provider.get_code(*addr, block).await?;
if code.is_empty() {
eyre::bail!("Contract {:?} does not exist", addr)
if let Ok(code) = self.provider.get_code(*addr, block).await {
if code.is_empty() {
eyre::bail!("contract {addr:?} does not exist")
}
}
}
}
return Err(err).wrap_err(
"could not decode output. did you specify the wrong function return data type perhaps?"
"could not decode output; did you specify the wrong function return data type?"
);
}
};
}

// handle case when return type is not specified
Ok(if decoded.is_empty() {
format!("{res}\n")
} else {
// seth compatible user-friendly return type conversions
decoded
.iter()
.map(TokenDisplay)
.map(|token| token.to_string())
.collect::<Vec<_>>()
.join("\n")
decoded.iter().map(format_token).collect::<Vec<_>>().join("\n")
})
}

Expand Down
4 changes: 2 additions & 2 deletions crates/cheatcodes/src/impls/evm/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl Cheatcode for rpcCall {
let Self { method, params } = self;
let url =
ccx.data.db.active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
let provider = ProviderBuilder::new(url).build()?;
let provider = ProviderBuilder::new(&url).build()?;

let params_json: serde_json::Value = serde_json::from_str(params)?;
let result = RuntimeOrHandle::new()
Expand Down Expand Up @@ -250,7 +250,7 @@ impl Cheatcode for eth_getLogsCall {

let url =
ccx.data.db.active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
let provider = ProviderBuilder::new(url).build()?;
let provider = ProviderBuilder::new(&url).build()?;
let mut filter =
Filter::new().address(addr.to_ethers()).from_block(from_block).to_block(to_block);
for (i, topic) in topics.iter().enumerate() {
Expand Down
Loading
Loading