Skip to content

Commit

Permalink
test(anvil): add more auto mine tests (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored May 16, 2022
1 parent e1c00aa commit baa5233
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ async fn can_impersonate() {

assert_eq!(tx.from, impersonated);
}

#[tokio::test(flavor = "multi_thread")]
async fn can_mine_manually() {
let (api, handle) = spawn(NodeConfig::test().with_port(next_port())).await;
let provider = handle.http_provider();

let start_num = provider.get_block_number().await.unwrap();

for (idx, _) in std::iter::repeat(()).take(10).enumerate() {
api.evm_mine(None).await.unwrap();
let num = provider.get_block_number().await.unwrap();
assert_eq!(num, start_num + idx + 1);
}
}
41 changes: 41 additions & 0 deletions anvil/tests/it/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,47 @@ async fn can_deploy_greeter_http() {
assert_eq!("Hello World!", greeting);
}

#[tokio::test(flavor = "multi_thread")]
async fn can_deploy_and_mine_manually() {
let (api, handle) = spawn(NodeConfig::test().with_port(next_port())).await;

// can mine in auto-mine mode
api.evm_mine(None).await.unwrap();
// disable auto mine
api.anvil_set_auto_mine(false).await.unwrap();
// can mine in manual mode
api.evm_mine(None).await.unwrap();

let provider = handle.http_provider();

let wallet = handle.dev_wallets().next().unwrap();
let client = Arc::new(SignerMiddleware::new(provider, wallet));

let tx = Greeter::deploy(Arc::clone(&client), "Hello World!".to_string()).unwrap().deployer.tx;

let tx = client.send_transaction(tx, None).await.unwrap();

// mine block with tx manually
api.evm_mine(None).await.unwrap();

let receipt = tx.await.unwrap().unwrap();

let address = receipt.contract_address.unwrap();
let greeter_contract = Greeter::new(address, Arc::clone(&client));
let greeting = greeter_contract.greet().call().await.unwrap();
assert_eq!("Hello World!", greeting);

let set_greeting = greeter_contract.set_greeting("Another Message".to_string());
let tx = set_greeting.send().await.unwrap();

// mine block manually
api.evm_mine(None).await.unwrap();

let _tx = tx.await.unwrap();
let greeting = greeter_contract.greet().call().await.unwrap();
assert_eq!("Another Message", greeting);
}

#[tokio::test(flavor = "multi_thread")]
async fn can_call_greeter_historic() {
let (_api, handle) = spawn(NodeConfig::test().with_port(next_port())).await;
Expand Down

0 comments on commit baa5233

Please sign in to comment.