Skip to content

Commit

Permalink
chore: fix up anvil API tests (#6517)
Browse files Browse the repository at this point in the history
* chore: fix up anvil API tests

* fmt
  • Loading branch information
Evalir authored Dec 4, 2023
1 parent 9f623ce commit 95aadf2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 50 deletions.
112 changes: 63 additions & 49 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,6 +15,7 @@ use ethers::{
},
utils::hex,
};
use foundry_common::types::{ToAlloy, ToEthers};
use foundry_evm::revm::primitives::SpecId;
use std::{
str::FromStr,
Expand All @@ -26,21 +28,22 @@ 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());
}

#[tokio::test(flavor = "multi_thread")]
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 <https://github.com/foundry-rs/foundry/issues/2341>
Expand All @@ -58,7 +61,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")]
Expand All @@ -71,18 +74,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);
Expand All @@ -93,7 +96,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();
}
Expand All @@ -108,10 +111,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);

Expand All @@ -134,8 +137,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")]
Expand All @@ -156,7 +159,9 @@ 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);

Expand All @@ -166,15 +171,15 @@ 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);

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();

Expand All @@ -193,19 +198,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
Expand All @@ -224,13 +229,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);
Expand Down Expand Up @@ -406,9 +411,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")
Expand All @@ -433,16 +438,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,
Expand All @@ -467,7 +472,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,
Expand All @@ -492,15 +497,15 @@ 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,
instance_id: api.instance_id(),
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(),
};
Expand Down Expand Up @@ -531,7 +536,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 =
Expand Down Expand Up @@ -576,17 +581,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
Expand All @@ -598,7 +603,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;
Expand All @@ -609,11 +614,20 @@ 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()
);
}
2 changes: 1 addition & 1 deletion crates/anvil/tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod abi;
mod anvil;
// mod anvil_api;
mod anvil_api;
mod api;
mod fork;
mod ganache;
Expand Down

0 comments on commit 95aadf2

Please sign in to comment.