2023-02-25 22:24:20 +00:00
|
|
|
use crate::{get_wallet, spawn_anvil};
|
|
|
|
use ethers_contract::abigen;
|
|
|
|
use ethers_core::{abi::AbiEncode, types::*};
|
2021-01-22 09:25:22 +00:00
|
|
|
use ethers_middleware::{
|
2023-02-25 22:24:20 +00:00
|
|
|
transformer::{ds_proxy::factory::DsProxyFactory, DsProxy, TransformerMiddleware},
|
|
|
|
MiddlewareBuilder, SignerMiddleware,
|
2021-01-22 09:25:22 +00:00
|
|
|
};
|
2021-01-22 10:49:52 +00:00
|
|
|
use ethers_providers::{Http, Middleware, Provider};
|
2021-07-29 20:22:25 +00:00
|
|
|
use ethers_signers::{LocalWallet, Signer};
|
2021-01-22 09:25:22 +00:00
|
|
|
use rand::Rng;
|
2023-02-25 22:24:20 +00:00
|
|
|
use std::sync::Arc;
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
type HttpWallet = SignerMiddleware<Provider<Http>, LocalWallet>;
|
|
|
|
|
2023-02-25 22:24:20 +00:00
|
|
|
abigen!(SimpleStorage, "../tests/testdata/SimpleStorage.json");
|
2021-10-31 11:34:51 +00:00
|
|
|
|
2021-01-22 09:25:22 +00:00
|
|
|
#[tokio::test]
|
2023-02-25 22:24:20 +00:00
|
|
|
#[ignore]
|
2021-01-22 09:25:22 +00:00
|
|
|
async fn ds_proxy_transformer() {
|
|
|
|
// randomness
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
2022-06-01 15:22:39 +00:00
|
|
|
// spawn anvil and instantiate a signer middleware.
|
2023-02-25 22:24:20 +00:00
|
|
|
let (provider, anvil) = spawn_anvil();
|
|
|
|
let wallet = get_wallet(&anvil, 0);
|
|
|
|
let address = wallet.address();
|
|
|
|
let provider = Arc::new(provider.with_signer(wallet));
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// deploy DsProxyFactory which we'll use to deploy a new DsProxy contract.
|
2023-02-25 22:24:20 +00:00
|
|
|
let deploy_tx = DsProxyFactory::deploy(provider.clone(), ()).unwrap();
|
|
|
|
let ds_proxy_factory = deploy_tx.send().await.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// deploy a new DsProxy contract.
|
|
|
|
let ds_proxy = DsProxy::build::<HttpWallet, Arc<HttpWallet>>(
|
2023-02-25 22:24:20 +00:00
|
|
|
provider.clone(),
|
2021-01-22 09:25:22 +00:00
|
|
|
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.
|
2023-02-25 22:24:20 +00:00
|
|
|
let deploy_tx = SimpleStorage::deploy(provider.clone(), ()).unwrap();
|
|
|
|
let simple_storage = deploy_tx.send().await.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// instantiate a new transformer middleware.
|
2023-02-25 22:24:20 +00:00
|
|
|
let provider = TransformerMiddleware::new(provider, ds_proxy);
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// broadcast the setValue tx via transformer middleware (first wallet).
|
|
|
|
let expected_value: u64 = rng.gen();
|
2023-02-25 22:24:20 +00:00
|
|
|
let _receipt = simple_storage
|
|
|
|
.set_value(expected_value.into())
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// verify that DsProxy's state was updated.
|
2021-10-29 12:29:35 +00:00
|
|
|
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();
|
2023-02-25 22:24:20 +00:00
|
|
|
assert_eq!(last_sender, address.into());
|
2021-01-22 09:25:22 +00:00
|
|
|
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();
|
|
|
|
|
2022-06-01 15:22:39 +00:00
|
|
|
// spawn anvil and instantiate a signer middleware.
|
2023-02-25 22:24:20 +00:00
|
|
|
let (provider, anvil) = spawn_anvil();
|
|
|
|
let wallet = get_wallet(&anvil, 0);
|
|
|
|
let address = wallet.address();
|
|
|
|
let provider = Arc::new(provider.with_signer(wallet));
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// deploy DsProxyFactory which we'll use to deploy a new DsProxy contract.
|
2023-02-25 22:24:20 +00:00
|
|
|
let deploy_tx = DsProxyFactory::deploy(provider.clone(), ()).unwrap();
|
|
|
|
let ds_proxy_factory = deploy_tx.send().await.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// deploy a new DsProxy contract.
|
|
|
|
let ds_proxy = DsProxy::build::<HttpWallet, Arc<HttpWallet>>(
|
2023-02-25 22:24:20 +00:00
|
|
|
provider.clone(),
|
2021-01-22 09:25:22 +00:00
|
|
|
Some(ds_proxy_factory.address()),
|
|
|
|
provider.address(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let ds_proxy_addr = ds_proxy.address();
|
|
|
|
|
2023-02-25 22:24:20 +00:00
|
|
|
// encode the calldata
|
2021-01-22 09:25:22 +00:00
|
|
|
let expected_value: u64 = rng.gen();
|
2023-02-25 22:24:20 +00:00
|
|
|
let calldata = SetValueCall { value: expected_value.into() }.encode();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// execute code via the deployed DsProxy contract.
|
2021-01-22 10:49:52 +00:00
|
|
|
ds_proxy
|
|
|
|
.execute::<HttpWallet, Arc<HttpWallet>, Bytes>(
|
2021-01-22 09:25:22 +00:00
|
|
|
Arc::clone(&provider),
|
2023-02-25 22:24:20 +00:00
|
|
|
SIMPLESTORAGE_BYTECODE.clone(),
|
|
|
|
calldata.into(),
|
2021-01-22 09:25:22 +00:00
|
|
|
)
|
2021-01-22 10:49:52 +00:00
|
|
|
.expect("could not construct DSProxy contract call")
|
2021-08-09 00:50:38 +00:00
|
|
|
.legacy()
|
2021-01-22 10:49:52 +00:00
|
|
|
.send()
|
2021-01-22 09:25:22 +00:00
|
|
|
.await
|
2023-02-25 22:24:20 +00:00
|
|
|
.unwrap()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2021-01-22 10:49:52 +00:00
|
|
|
.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// verify that DsProxy's state was updated.
|
2021-10-29 12:29:35 +00:00
|
|
|
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();
|
2023-02-25 22:24:20 +00:00
|
|
|
assert_eq!(last_sender, address.into());
|
2021-01-22 09:25:22 +00:00
|
|
|
assert_eq!(last_value, H256::from_low_u64_be(expected_value));
|
|
|
|
}
|