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

[wip] feat(anvil): migrate tests to alloy #6960

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
feat: introduce sol-types with #[sol(rpc)]]
  • Loading branch information
Evalir committed Feb 8, 2024
commit e0fb79b69d89fe19f84f29322f76fdba8e7c7a66
33 changes: 13 additions & 20 deletions Cargo.lock

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

13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ alloy-transport = { git = "https://github.com/alloy-rs/alloy" }
alloy-transport-http = { git = "https://github.com/alloy-rs/alloy" }
alloy-transport-ipc = { git = "https://github.com/alloy-rs/alloy" }
alloy-transport-ws = { git = "https://github.com/alloy-rs/alloy" }
alloy-primitives = "0.6.2"
alloy-dyn-abi = "0.6.2"
alloy-json-abi = "0.6.2"
alloy-sol-types = "0.6.2"
alloy-primitives = "0.6"
alloy-dyn-abi = "0.6"
alloy-json-abi = "0.6"
alloy-sol-types = "0.6"
syn-solidity = "0.6.0"
alloy-chains = "0.1"

Expand Down Expand Up @@ -223,6 +223,11 @@ ethers-signers = { git = "https://github.com/gakonst/ethers-rs", rev = "f0e5b194
ethers-middleware = { git = "https://github.com/gakonst/ethers-rs", rev = "f0e5b194f09c533feb10d1a686ddb9e5946ec107" }
ethers-solc = { git = "https://github.com/gakonst/ethers-rs", rev = "f0e5b194f09c533feb10d1a686ddb9e5946ec107" }

alloy-sol-types = { git = "https://github.com/alloy-rs/core", rev = "18b0509950c90d9ec38f25913b692ae4cdd6f227" }
alloy-dyn-abi = { git = "https://github.com/alloy-rs/core", rev = "18b0509950c90d9ec38f25913b692ae4cdd6f227" }
alloy-json-abi = { git = "https://github.com/alloy-rs/core", rev = "18b0509950c90d9ec38f25913b692ae4cdd6f227" }
alloy-primitives = { git = "https://github.com/alloy-rs/core", rev = "18b0509950c90d9ec38f25913b692ae4cdd6f227" }

revm = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
revm-primitives = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
revm-interpreter = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
Expand Down
80 changes: 74 additions & 6 deletions crates/anvil/tests/it/abi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! commonly used abigen generated types

use alloy_sol_types::sol;
use ethers::{
contract::{abigen, EthEvent},
types::Address,
Expand Down Expand Up @@ -39,12 +40,79 @@ abigen!(
_exists(uint256)(bool)
]"#
);
abigen!(
BUSD,
r#"[
balanceOf(address)(uint256)
]"#
);

sol! {
#[sol(rpc)]
contract Multicall {
struct Call {
address target;
bytes callData;
}
function aggregate(Call[] memory calls) public returns (uint256 blockNumber, bytes[] memory returnData);
function getEthBalance(address addr) public view returns (uint256 balance);
function getBlockHash(uint256 blockNumber) public view returns (bytes32 blockHash);
function getLastBlockHash() public view returns (bytes32 blockHash);
function getCurrentBlockTimestamp() public view returns (uint256 timestamp);
function getCurrentBlockDifficulty() public view returns (uint256 difficulty);
function getCurrentBlockGasLimit() public view returns (uint256 gaslimit);
function getCurrentBlockCoinbase() public view returns (address coinbase);
}
}

sol! {
#[sol(rpc)]
contract SolSimpleStorage {
event ValueChanged(address indexed author, address indexed oldAuthor, string oldValue, string newValue);

constructor(string memory value) public;

function getValue() view public returns (string memory);

function setValue(string memory value) public;

function setValues(string memory value, string memory value2);

function _hashPuzzle() public view returns (uint256);
}

}

sol! {
#[sol(rpc)]
contract SolGreeter {
function greet() external view returns (string memory);
function setGreeting(string memory _greeting) external;
}
}

sol! {
#[sol(rpc)]
contract ERC721 {
function balanceOf(address account) external view returns (uint256);
function ownerOf(uint256 tokenId) external view returns (address);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function tokenURI(uint256 tokenId) external view returns (string memory);
function getApproved(uint256 tokenId) external view returns (address);
function setApprovalForAll(address operator, bool _approved) external;
function isApprovedForAll(address owner, address operator) external view returns (bool);
function transferFrom(address from, address to, uint256 tokenId) external;
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
function _transfer(address from, address to, uint256 tokenId) external;
function _approve(address to, uint256 tokenId) external;
function _burn(uint256 tokenId) external;
function _safeMint(address to, uint256 tokenId, bytes calldata data) external;
function _mint(address to, uint256 tokenId) external;
function _exists(uint256 tokenId) external view returns (bool);
}
}

sol! {
#[sol(rpc)]
contract BinanceUSD {
function balanceOf(address account) external view returns (uint256);
}
}

// <https://docs.soliditylang.org/en/latest/control-structures.html#revert>
pub(crate) const VENDING_MACHINE_CONTRACT: &str = r#"// SPDX-License-Identifier: GPL-3.0
Expand Down
48 changes: 27 additions & 21 deletions crates/anvil/tests/it/anvil_api.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//! tests for custom anvil endpoints
use crate::{abi::*, fork::fork_config, utils::ethers_http_provider};
use crate::{
abi::{BinanceUSD, Greeter, Multicall, SolGreeter},
fork::fork_config,
utils::ethers_http_provider,
};
use alloy_primitives::{Address as rAddress, B256, U256 as rU256};
use alloy_providers::provider::TempProvider;
use alloy_rpc_types::BlockNumberOrTag;
use alloy_sol_types::SolCall;
use anvil::{eth::api::CLIENT_VERSION, spawn, Hardfork, NodeConfig};
use anvil_core::{
eth::EthRequest,
types::{AnvilMetadata, ForkedNetwork, Forking, NodeEnvironment, NodeForkConfig, NodeInfo},
};
use ethers::{
abi::AbiDecode,
prelude::{Middleware, SignerMiddleware},
types::{
transaction::eip2718::TypedTransaction, Address, BlockNumber, Eip1559TransactionRequest,
Expand Down Expand Up @@ -153,6 +157,7 @@ async fn can_impersonate_contract() {

let greeter_contract =
Greeter::deploy(provider, "Hello World!".to_string()).unwrap().send().await.unwrap();
let greeter = SolGreeter::new(greeter_contract.address().to_alloy(), handle.http_provider());
let impersonate = greeter_contract.address();

let to = Address::random();
Expand All @@ -170,7 +175,7 @@ async fn can_impersonate_contract() {
let res = provider.send_transaction(tx.clone(), None).await;
res.unwrap_err();

let greeting = greeter_contract.greet().call().await.unwrap();
let greeting = greeter.greet().call().await.unwrap()._0;
assert_eq!("Hello World!", greeting);

api.anvil_impersonate_account(impersonate.to_alloy()).await.unwrap();
Expand All @@ -185,7 +190,7 @@ async fn can_impersonate_contract() {
let res = provider.send_transaction(tx, None).await;
res.unwrap_err();

let greeting = greeter_contract.greet().call().await.unwrap();
let greeting = greeter.greet().call().await.unwrap()._0;
assert_eq!("Hello World!", greeting);
}

Expand Down Expand Up @@ -409,7 +414,6 @@ async fn test_timestamp_interval() {
async fn test_can_set_storage_bsc_fork() {
let (api, handle) =
spawn(NodeConfig::test().with_eth_rpc_url(Some("https://bsc-dataseed.binance.org/"))).await;
let provider = Arc::new(ethers_http_provider(&handle.http_endpoint()));

let busd_addr: Address = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56".parse().unwrap();
let idx: U256 =
Expand All @@ -425,11 +429,14 @@ async fn test_can_set_storage_bsc_fork() {
hex::decode("70a082310000000000000000000000000000000000000000000000000000000000000000")
.unwrap();

let busd = BUSD::new(busd_addr, provider);
let call = busd::BalanceOfCall::decode(&input).unwrap();
let provider = handle.http_provider();

let contract = BinanceUSD::new(busd_addr.to_alloy(), provider);
let busd_call_input = BinanceUSD::balanceOfCall::abi_decode(&input, false).unwrap();

let balance = busd.balance_of(call.0).call().await.unwrap();
assert_eq!(balance, U256::from(12345u64));
let balance = contract.balanceOf(busd_call_input.account).call().await.unwrap()._0;

assert_eq!(balance, rU256::from(12345u64));
}

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -605,7 +612,6 @@ async fn test_fork_revert_next_block_timestamp() {
#[tokio::test(flavor = "multi_thread")]
async fn test_fork_revert_call_latest_block_timestamp() {
let (api, handle) = spawn(fork_config()).await;
let provider = ethers_http_provider(&handle.http_endpoint());

// Mine a new block, and check the new block gas limit
api.mine_one().await;
Expand All @@ -615,25 +621,25 @@ async fn test_fork_revert_call_latest_block_timestamp() {
api.mine_one().await;
api.evm_revert(snapshot_id).await.unwrap();

let multicall = MulticallContract::new(
Address::from_str("0xeefba1e63905ef1d7acba5a8513c70307c1ce441").unwrap(),
provider.into(),
let multicall = Multicall::new(
rAddress::from_str("0xeefba1e63905ef1d7acba5a8513c70307c1ce441").unwrap(),
handle.http_provider(),
);

assert_eq!(
multicall.get_current_block_timestamp().await.unwrap(),
latest_block.header.timestamp.to_ethers()
multicall.getCurrentBlockTimestamp().call().await.unwrap().timestamp,
latest_block.header.timestamp
);
assert_eq!(
multicall.get_current_block_difficulty().await.unwrap(),
latest_block.header.difficulty.to_ethers()
multicall.getCurrentBlockDifficulty().call().await.unwrap().difficulty,
latest_block.header.difficulty
);
assert_eq!(
multicall.get_current_block_gas_limit().await.unwrap(),
latest_block.header.gas_limit.to_ethers()
multicall.getCurrentBlockGasLimit().call().await.unwrap().gaslimit,
latest_block.header.gas_limit
);
assert_eq!(
multicall.get_current_block_coinbase().await.unwrap(),
latest_block.header.miner.to_ethers()
multicall.getCurrentBlockCoinbase().call().await.unwrap().coinbase,
latest_block.header.miner
);
}
2 changes: 1 addition & 1 deletion crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ serde_regex = "1"
serde.workspace = true
thiserror = "1"
toml = { version = "0.8", features = ["preserve_order"] }
toml_edit = "0.21"
toml_edit = "0.22.4"
tracing.workspace = true
walkdir = "2"

Expand Down
Loading