ethers-rs/ethers-middleware/tests/it/transformer.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
4.1 KiB
Rust
Raw Normal View History

ci/test: improve CI jobs and tests (#2189) * ci: move to scripts directory * nits * ci: improve main CI jobs * fix: install script * fix * fix: use curl for windows installation * fix: wasm typo * tests: move to single binary * chore: clippy * chore: clippy * chore: clippy * fix: test command * fix: quote tests * update script * fix: action exclude * fix: dev deps * fix: only run wasm in own job * ci: add aarch64 targets * test: rm useless test * ci: update security audit * ci: add deny CI * chore: rm unused audit.toml * chore: update geth.rs * ci: remove unusable targets * fix: install script path * fix: wasm * improve script * fix: failing ci * fix: contract tests * ci: improve install script * update middleware tests * move integration etherscan tests to tests/ dir * fix: eip2930 access_list field name * add pendingtransaction must_use * add random anvil comment * ci: add miri job * ci: simplify * fixci * Revert "add pendingtransaction must_use" This reverts commit 770b21b4a3c6ef8900a6aa1cd46aa9638317a60d. * fix: macos script * fix: use curl in script * unused ci * update script * fix wasm * rm_miri * fix: signer test * fix: wasm ci * fix: ipc test * fix: live celo tests * fix: abi online source test * fix: windows paths in test * chore: update serial_test * ci: run live tests separately * fix: provider tests * fix: unused var * fix: feature * fix merge * fix: etherscan key tests * ci: rm duplicate audit * fix: split etherscan test ci * fix: etherscan test * fix: generate multiple unused ports * fix: source test * fix: udeps * rm unused
2023-03-01 00:26:27 +00:00
use crate::{get_wallet, spawn_anvil};
use ethers_contract::abigen;
use ethers_core::{abi::AbiEncode, types::*};
use ethers_middleware::{
transformer::{ds_proxy::factory::DsProxyFactory, DsProxy, TransformerMiddleware},
MiddlewareBuilder, SignerMiddleware,
};
use ethers_providers::{Http, Middleware, Provider};
use ethers_signers::{LocalWallet, Signer};
use rand::Rng;
use std::sync::Arc;
type HttpWallet = SignerMiddleware<Provider<Http>, LocalWallet>;
abigen!(SimpleStorage, "../tests/testdata/SimpleStorage.json");
#[tokio::test]
#[ignore]
async fn ds_proxy_transformer() {
// randomness
let mut rng = rand::thread_rng();
// spawn anvil and instantiate a signer middleware.
let (provider, anvil) = spawn_anvil();
let wallet = get_wallet(&anvil, 0);
let address = wallet.address();
let provider = Arc::new(provider.with_signer(wallet));
// deploy DsProxyFactory which we'll use to deploy a new DsProxy contract.
let deploy_tx = DsProxyFactory::deploy(provider.clone(), ()).unwrap();
let ds_proxy_factory = deploy_tx.send().await.unwrap();
// deploy a new DsProxy contract.
let ds_proxy = DsProxy::build::<HttpWallet, Arc<HttpWallet>>(
provider.clone(),
Some(ds_proxy_factory.address()),
provider.address(),
)
.await
.unwrap();
let ds_proxy_addr = ds_proxy.address();
// deploy SimpleStorage and try to update its value via transformer middleware.
let deploy_tx = SimpleStorage::deploy(provider.clone(), ()).unwrap();
let simple_storage = deploy_tx.send().await.unwrap();
// instantiate a new transformer middleware.
let provider = TransformerMiddleware::new(provider, ds_proxy);
// broadcast the setValue tx via transformer middleware (first wallet).
let expected_value: u64 = rng.gen();
let _receipt = simple_storage
.set_value(expected_value.into())
.send()
.await
.unwrap()
.await
.unwrap()
.unwrap();
// verify that DsProxy's state was updated.
let last_sender = provider.get_storage_at(ds_proxy_addr, H256::zero(), None).await.unwrap();
let last_value =
provider.get_storage_at(ds_proxy_addr, H256::from_low_u64_be(1u64), None).await.unwrap();
assert_eq!(last_sender, address.into());
assert_eq!(last_value, H256::from_low_u64_be(expected_value));
}
#[tokio::test]
async fn ds_proxy_code() {
// randomness
let mut rng = rand::thread_rng();
// spawn anvil and instantiate a signer middleware.
let (provider, anvil) = spawn_anvil();
let wallet = get_wallet(&anvil, 0);
let address = wallet.address();
let provider = Arc::new(provider.with_signer(wallet));
// deploy DsProxyFactory which we'll use to deploy a new DsProxy contract.
let deploy_tx = DsProxyFactory::deploy(provider.clone(), ()).unwrap();
let ds_proxy_factory = deploy_tx.send().await.unwrap();
// deploy a new DsProxy contract.
let ds_proxy = DsProxy::build::<HttpWallet, Arc<HttpWallet>>(
provider.clone(),
Some(ds_proxy_factory.address()),
provider.address(),
)
.await
.unwrap();
let ds_proxy_addr = ds_proxy.address();
// encode the calldata
let expected_value: u64 = rng.gen();
let calldata = SetValueCall { value: expected_value.into() }.encode();
// execute code via the deployed DsProxy contract.
ds_proxy
.execute::<HttpWallet, Arc<HttpWallet>, Bytes>(
Arc::clone(&provider),
SIMPLESTORAGE_BYTECODE.clone(),
calldata.into(),
)
.expect("could not construct DSProxy contract call")
.legacy()
.send()
.await
.unwrap()
.await
.unwrap()
.unwrap();
// verify that DsProxy's state was updated.
let last_sender = provider.get_storage_at(ds_proxy_addr, H256::zero(), None).await.unwrap();
let last_value =
provider.get_storage_at(ds_proxy_addr, H256::from_low_u64_be(1u64), None).await.unwrap();
assert_eq!(last_sender, address.into());
assert_eq!(last_value, H256::from_low_u64_be(expected_value));
}