From 1875bb0e0681392f57da665b78e3c892e42c4f34 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Mon, 4 Dec 2023 11:17:34 -0400 Subject: [PATCH 1/2] chore: fix up anvil API tests --- crates/anvil/tests/it/anvil_api.rs | 100 +++++++++++++++-------------- crates/anvil/tests/it/main.rs | 2 +- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/crates/anvil/tests/it/anvil_api.rs b/crates/anvil/tests/it/anvil_api.rs index f18c05a442de..88a945e41c0a 100644 --- a/crates/anvil/tests/it/anvil_api.rs +++ b/crates/anvil/tests/it/anvil_api.rs @@ -1,5 +1,6 @@ //! tests for custom anvil endpoints use crate::{abi::*, fork::fork_config}; +use alloy_rpc_types::BlockNumberOrTag; use anvil::{eth::api::CLIENT_VERSION, spawn, Hardfork, NodeConfig}; use anvil_core::{ eth::EthRequest, @@ -14,6 +15,7 @@ use ethers::{ }, utils::hex, }; +use foundry_common::types::{ToEthers, ToAlloy}; use foundry_evm::revm::primitives::SpecId; use std::{ str::FromStr, @@ -26,8 +28,8 @@ async fn can_set_gas_price() { let (api, handle) = spawn(NodeConfig::test().with_hardfork(Some(Hardfork::Berlin))).await; let provider = handle.ethers_http_provider(); - let gas_price = 1337u64.into(); - api.anvil_set_min_gas_price(gas_price).await.unwrap(); + let gas_price: U256 = 1337u64.into(); + api.anvil_set_min_gas_price(gas_price.to_alloy()).await.unwrap(); assert_eq!(gas_price, provider.get_gas_price().await.unwrap()); } @@ -35,12 +37,12 @@ async fn can_set_gas_price() { async fn can_set_block_gas_limit() { let (api, _) = spawn(NodeConfig::test().with_hardfork(Some(Hardfork::Berlin))).await; - let block_gas_limit = 1337u64.into(); - assert!(api.evm_set_block_gas_limit(block_gas_limit).unwrap()); + let block_gas_limit: U256 = 1337u64.into(); + assert!(api.evm_set_block_gas_limit(block_gas_limit.to_alloy()).unwrap()); // Mine a new block, and check the new block gas limit api.mine_one().await; - let latest_block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap(); - assert_eq!(block_gas_limit, latest_block.gas_limit); + let latest_block = api.block_by_number(alloy_rpc_types::BlockNumberOrTag::Latest).await.unwrap().unwrap(); + assert_eq!(block_gas_limit.to_alloy(), latest_block.header.gas_limit); } // Ref @@ -58,7 +60,7 @@ async fn can_set_storage() { let storage_value = api.storage_at(addr, slot, None).await.unwrap(); assert_eq!(val, storage_value); - assert_eq!(val, H256::from_uint(&U256::from(12345))); + assert_eq!(val.to_ethers(), H256::from_uint(&U256::from(12345))); } #[tokio::test(flavor = "multi_thread")] @@ -71,18 +73,18 @@ async fn can_impersonate_account() { let val = 1337u64; let funding = U256::from(1e18 as u64); // fund the impersonated account - api.anvil_set_balance(impersonate, funding).await.unwrap(); + api.anvil_set_balance(impersonate.to_alloy(), funding.to_alloy()).await.unwrap(); - let balance = api.balance(impersonate, None).await.unwrap(); - assert_eq!(balance, funding); + let balance = api.balance(impersonate.to_alloy(), None).await.unwrap(); + assert_eq!(balance, funding.to_alloy()); let tx = TransactionRequest::new().from(impersonate).to(to).value(val); let res = provider.send_transaction(tx.clone(), None).await; res.unwrap_err(); - api.anvil_impersonate_account(impersonate).await.unwrap(); - assert!(api.accounts().unwrap().contains(&impersonate)); + api.anvil_impersonate_account(impersonate.to_alloy()).await.unwrap(); + assert!(api.accounts().unwrap().contains(&impersonate.to_alloy())); let res = provider.send_transaction(tx.clone(), None).await.unwrap().await.unwrap().unwrap(); assert_eq!(res.from, impersonate); @@ -93,7 +95,7 @@ async fn can_impersonate_account() { let balance = provider.get_balance(to, None).await.unwrap(); assert_eq!(balance, val.into()); - api.anvil_stop_impersonating_account(impersonate).await.unwrap(); + api.anvil_stop_impersonating_account(impersonate.to_alloy()).await.unwrap(); let res = provider.send_transaction(tx, None).await; res.unwrap_err(); } @@ -108,10 +110,10 @@ async fn can_auto_impersonate_account() { let val = 1337u64; let funding = U256::from(1e18 as u64); // fund the impersonated account - api.anvil_set_balance(impersonate, funding).await.unwrap(); + api.anvil_set_balance(impersonate.to_alloy(), funding.to_alloy()).await.unwrap(); - let balance = api.balance(impersonate, None).await.unwrap(); - assert_eq!(balance, funding); + let balance = api.balance(impersonate.to_alloy(), None).await.unwrap(); + assert_eq!(balance, funding.to_alloy()); let tx = TransactionRequest::new().from(impersonate).to(to).value(val); @@ -134,8 +136,8 @@ async fn can_auto_impersonate_account() { res.unwrap_err(); // explicitly impersonated accounts get returned by `eth_accounts` - api.anvil_impersonate_account(impersonate).await.unwrap(); - assert!(api.accounts().unwrap().contains(&impersonate)); + api.anvil_impersonate_account(impersonate.to_alloy()).await.unwrap(); + assert!(api.accounts().unwrap().contains(&impersonate.to_alloy())); } #[tokio::test(flavor = "multi_thread")] @@ -156,7 +158,7 @@ async fn can_impersonate_contract() { let provider = handle.ethers_http_provider(); // fund the impersonated account - api.anvil_set_balance(impersonate, U256::from(1e18 as u64)).await.unwrap(); + api.anvil_set_balance(impersonate.to_alloy(), U256::from(1e18 as u64).to_alloy()).await.unwrap(); let tx = TransactionRequest::new().from(impersonate).to(to).value(val); @@ -166,7 +168,7 @@ async fn can_impersonate_contract() { let greeting = greeter_contract.greet().call().await.unwrap(); assert_eq!("Hello World!", greeting); - api.anvil_impersonate_account(impersonate).await.unwrap(); + api.anvil_impersonate_account(impersonate.to_alloy()).await.unwrap(); let res = provider.send_transaction(tx.clone(), None).await.unwrap().await.unwrap().unwrap(); assert_eq!(res.from, impersonate); @@ -174,7 +176,7 @@ async fn can_impersonate_contract() { let balance = provider.get_balance(to, None).await.unwrap(); assert_eq!(balance, val.into()); - api.anvil_stop_impersonating_account(impersonate).await.unwrap(); + api.anvil_stop_impersonating_account(impersonate.to_alloy()).await.unwrap(); let res = provider.send_transaction(tx, None).await; res.unwrap_err(); @@ -193,19 +195,19 @@ async fn can_impersonate_gnosis_safe() { let code = provider.get_code(safe, None).await.unwrap(); assert!(!code.is_empty()); - api.anvil_impersonate_account(safe).await.unwrap(); + api.anvil_impersonate_account(safe.to_alloy()).await.unwrap(); let code = provider.get_code(safe, None).await.unwrap(); assert!(!code.is_empty()); let balance = U256::from(1e18 as u64); // fund the impersonated account - api.anvil_set_balance(safe, balance).await.unwrap(); + api.anvil_set_balance(safe.to_alloy(), balance.to_alloy()).await.unwrap(); let on_chain_balance = provider.get_balance(safe, None).await.unwrap(); assert_eq!(on_chain_balance, balance); - api.anvil_stop_impersonating_account(safe).await.unwrap(); + api.anvil_stop_impersonating_account(safe.to_alloy()).await.unwrap(); let code = provider.get_code(safe, None).await.unwrap(); // code is added back after stop impersonating @@ -224,13 +226,13 @@ async fn can_impersonate_multiple_account() { let val = 1337u64; let funding = U256::from(1e18 as u64); // fund the impersonated accounts - api.anvil_set_balance(impersonate0, funding).await.unwrap(); - api.anvil_set_balance(impersonate1, funding).await.unwrap(); + api.anvil_set_balance(impersonate0.to_alloy(), funding.to_alloy()).await.unwrap(); + api.anvil_set_balance(impersonate1.to_alloy(), funding.to_alloy()).await.unwrap(); let tx = TransactionRequest::new().from(impersonate0).to(to).value(val); - api.anvil_impersonate_account(impersonate0).await.unwrap(); - api.anvil_impersonate_account(impersonate1).await.unwrap(); + api.anvil_impersonate_account(impersonate0.to_alloy()).await.unwrap(); + api.anvil_impersonate_account(impersonate1.to_alloy()).await.unwrap(); let res0 = provider.send_transaction(tx.clone(), None).await.unwrap().await.unwrap().unwrap(); assert_eq!(res0.from, impersonate0); @@ -406,9 +408,9 @@ async fn test_can_set_storage_bsc_fork() { let value: H256 = "0x0000000000000000000000000000000000000000000000000000000000003039".parse().unwrap(); - api.anvil_set_storage_at(busd_addr, idx, value).await.unwrap(); - let storage = api.storage_at(busd_addr, idx, None).await.unwrap(); - assert_eq!(storage, value); + api.anvil_set_storage_at(busd_addr.to_alloy(), idx.to_alloy(), value.to_alloy()).await.unwrap(); + let storage = api.storage_at(busd_addr.to_alloy(), idx.to_alloy(), None).await.unwrap(); + assert_eq!(storage.to_ethers(), value); let input = hex::decode("70a082310000000000000000000000000000000000000000000000000000000000000000") @@ -433,16 +435,16 @@ async fn can_get_node_info() { let block = provider.get_block(block_number).await.unwrap().unwrap(); let expected_node_info = NodeInfo { - current_block_number: U64([0]), + current_block_number: U64([0]).to_alloy(), current_block_timestamp: 1, - current_block_hash: block.hash.unwrap(), + current_block_hash: block.hash.unwrap().to_alloy(), hard_fork: SpecId::SHANGHAI, transaction_order: "fees".to_owned(), environment: NodeEnvironment { - base_fee: U256::from_str("0x3b9aca00").unwrap(), + base_fee: U256::from_str("0x3b9aca00").unwrap().to_alloy(), chain_id: 0x7a69, - gas_limit: U256::from_str("0x1c9c380").unwrap(), - gas_price: U256::from_str("0x77359400").unwrap(), + gas_limit: U256::from_str("0x1c9c380").unwrap().to_alloy(), + gas_price: U256::from_str("0x77359400").unwrap().to_alloy(), }, fork_config: NodeForkConfig { fork_url: None, @@ -467,7 +469,7 @@ async fn can_get_metadata() { let block = provider.get_block(block_number).await.unwrap().unwrap(); let expected_metadata = AnvilMetadata { - latest_block_hash: block.hash.unwrap(), + latest_block_hash: block.hash.unwrap().to_alloy(), latest_block_number: block_number, chain_id, client_version: CLIENT_VERSION, @@ -492,7 +494,7 @@ async fn can_get_metadata_on_fork() { let block = provider.get_block(block_number).await.unwrap().unwrap(); let expected_metadata = AnvilMetadata { - latest_block_hash: block.hash.unwrap(), + latest_block_hash: block.hash.unwrap().to_alloy(), latest_block_number: block_number, chain_id, client_version: CLIENT_VERSION, @@ -500,7 +502,7 @@ async fn can_get_metadata_on_fork() { forked_network: Some(ForkedNetwork { chain_id, fork_block_number: block_number, - fork_block_hash: block.hash.unwrap(), + fork_block_hash: block.hash.unwrap().to_alloy(), }), snapshots: Default::default(), }; @@ -531,7 +533,7 @@ async fn test_get_transaction_receipt() { // set the base fee let new_base_fee = U256::from(1_000); - api.anvil_set_next_block_base_fee_per_gas(new_base_fee).await.unwrap(); + api.anvil_set_next_block_base_fee_per_gas(new_base_fee.to_alloy()).await.unwrap(); // send a EIP-1559 transaction let tx = @@ -576,17 +578,17 @@ async fn test_fork_revert_next_block_timestamp() { // Mine a new block, and check the new block gas limit api.mine_one().await; - let latest_block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap(); + let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap(); let snapshot_id = api.evm_snapshot().await.unwrap(); api.mine_one().await; api.evm_revert(snapshot_id).await.unwrap(); - let block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap(); + let block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap(); assert_eq!(block, latest_block); api.mine_one().await; - let block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap(); - assert!(block.timestamp > latest_block.timestamp); + let block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap(); + assert!(block.header.timestamp > latest_block.header.timestamp); } // test that after a snapshot revert, the env block is reset @@ -598,7 +600,7 @@ async fn test_fork_revert_call_latest_block_timestamp() { // Mine a new block, and check the new block gas limit api.mine_one().await; - let latest_block = api.block_by_number(BlockNumber::Latest).await.unwrap().unwrap(); + let latest_block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap(); let snapshot_id = api.evm_snapshot().await.unwrap(); api.mine_one().await; @@ -609,11 +611,11 @@ async fn test_fork_revert_call_latest_block_timestamp() { provider.into(), ); - assert_eq!(multicall.get_current_block_timestamp().await.unwrap(), latest_block.timestamp); - assert_eq!(multicall.get_current_block_difficulty().await.unwrap(), latest_block.difficulty); - assert_eq!(multicall.get_current_block_gas_limit().await.unwrap(), latest_block.gas_limit); + assert_eq!(multicall.get_current_block_timestamp().await.unwrap(), latest_block.header.timestamp.to_ethers()); + assert_eq!(multicall.get_current_block_difficulty().await.unwrap(), latest_block.header.difficulty.to_ethers()); + assert_eq!(multicall.get_current_block_gas_limit().await.unwrap(), latest_block.header.gas_limit.to_ethers()); assert_eq!( multicall.get_current_block_coinbase().await.unwrap(), - latest_block.author.unwrap_or_default() + latest_block.header.miner.to_ethers() ); } diff --git a/crates/anvil/tests/it/main.rs b/crates/anvil/tests/it/main.rs index 506b6eb09b86..153d664245c1 100644 --- a/crates/anvil/tests/it/main.rs +++ b/crates/anvil/tests/it/main.rs @@ -1,6 +1,6 @@ mod abi; mod anvil; -// mod anvil_api; +mod anvil_api; mod api; mod fork; mod ganache; From ef339795c6fe83d05fa74d300f122c809392e631 Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Mon, 4 Dec 2023 11:18:17 -0400 Subject: [PATCH 2/2] fmt --- crates/anvil/tests/it/anvil_api.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/anvil/tests/it/anvil_api.rs b/crates/anvil/tests/it/anvil_api.rs index 88a945e41c0a..0d3e2057eb59 100644 --- a/crates/anvil/tests/it/anvil_api.rs +++ b/crates/anvil/tests/it/anvil_api.rs @@ -15,7 +15,7 @@ use ethers::{ }, utils::hex, }; -use foundry_common::types::{ToEthers, ToAlloy}; +use foundry_common::types::{ToAlloy, ToEthers}; use foundry_evm::revm::primitives::SpecId; use std::{ str::FromStr, @@ -41,7 +41,8 @@ async fn can_set_block_gas_limit() { assert!(api.evm_set_block_gas_limit(block_gas_limit.to_alloy()).unwrap()); // Mine a new block, and check the new block gas limit api.mine_one().await; - let latest_block = api.block_by_number(alloy_rpc_types::BlockNumberOrTag::Latest).await.unwrap().unwrap(); + let latest_block = + api.block_by_number(alloy_rpc_types::BlockNumberOrTag::Latest).await.unwrap().unwrap(); assert_eq!(block_gas_limit.to_alloy(), latest_block.header.gas_limit); } @@ -158,7 +159,9 @@ async fn can_impersonate_contract() { let provider = handle.ethers_http_provider(); // fund the impersonated account - api.anvil_set_balance(impersonate.to_alloy(), U256::from(1e18 as u64).to_alloy()).await.unwrap(); + api.anvil_set_balance(impersonate.to_alloy(), U256::from(1e18 as u64).to_alloy()) + .await + .unwrap(); let tx = TransactionRequest::new().from(impersonate).to(to).value(val); @@ -611,9 +614,18 @@ async fn test_fork_revert_call_latest_block_timestamp() { provider.into(), ); - assert_eq!(multicall.get_current_block_timestamp().await.unwrap(), latest_block.header.timestamp.to_ethers()); - assert_eq!(multicall.get_current_block_difficulty().await.unwrap(), latest_block.header.difficulty.to_ethers()); - assert_eq!(multicall.get_current_block_gas_limit().await.unwrap(), latest_block.header.gas_limit.to_ethers()); + assert_eq!( + multicall.get_current_block_timestamp().await.unwrap(), + latest_block.header.timestamp.to_ethers() + ); + assert_eq!( + multicall.get_current_block_difficulty().await.unwrap(), + latest_block.header.difficulty.to_ethers() + ); + assert_eq!( + multicall.get_current_block_gas_limit().await.unwrap(), + latest_block.header.gas_limit.to_ethers() + ); assert_eq!( multicall.get_current_block_coinbase().await.unwrap(), latest_block.header.miner.to_ethers()