2021-08-23 09:56:44 +00:00
|
|
|
#![cfg(not(target_arch = "wasm32"))]
|
2021-10-10 08:31:34 +00:00
|
|
|
#![allow(unused)]
|
2021-01-22 09:25:22 +00:00
|
|
|
use ethers_contract::{BaseContract, ContractFactory};
|
2022-06-01 15:22:39 +00:00
|
|
|
use ethers_core::{abi::Abi, types::*, utils::Anvil};
|
2021-01-22 09:25:22 +00:00
|
|
|
use ethers_middleware::{
|
|
|
|
transformer::{DsProxy, TransformerMiddleware},
|
|
|
|
SignerMiddleware,
|
|
|
|
};
|
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-10-31 11:34:51 +00:00
|
|
|
use ethers_solc::Solc;
|
2021-01-22 09:25:22 +00:00
|
|
|
use rand::Rng;
|
|
|
|
use std::{convert::TryFrom, sync::Arc, time::Duration};
|
|
|
|
|
|
|
|
type HttpWallet = SignerMiddleware<Provider<Http>, LocalWallet>;
|
|
|
|
|
2021-10-31 11:34:51 +00:00
|
|
|
// compiles the given contract and returns the ABI and Bytecode
|
|
|
|
fn compile_contract(path: &str, name: &str) -> (Abi, Bytes) {
|
|
|
|
let path = format!("./tests/solidity-contracts/{}", path);
|
|
|
|
let compiled = Solc::default().compile_source(&path).unwrap();
|
|
|
|
let contract = compiled.get(&path, name).expect("could not find contract");
|
2021-12-08 00:38:29 +00:00
|
|
|
let (abi, bin, _) = contract.into_parts_or_default();
|
|
|
|
(abi, bin)
|
2021-10-31 11:34:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 09:25:22 +00:00
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(not(feature = "celo"))]
|
|
|
|
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.
|
|
|
|
let anvil = Anvil::new().spawn();
|
|
|
|
let wallet: LocalWallet = anvil.keys()[0].clone().into();
|
|
|
|
let provider = Provider::<Http>::try_from(anvil.endpoint())
|
2021-01-22 09:25:22 +00:00
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(10u64));
|
2021-07-29 20:22:25 +00:00
|
|
|
let chain_id = provider.get_chainid().await.unwrap().as_u64();
|
|
|
|
let wallet = wallet.with_chain_id(chain_id);
|
2021-01-22 09:25:22 +00:00
|
|
|
let signer_middleware = SignerMiddleware::new(provider.clone(), wallet);
|
|
|
|
let wallet_addr = signer_middleware.address();
|
|
|
|
let provider = Arc::new(signer_middleware.clone());
|
|
|
|
|
|
|
|
// deploy DsProxyFactory which we'll use to deploy a new DsProxy contract.
|
2021-10-31 11:34:51 +00:00
|
|
|
let (abi, bytecode) = compile_contract("DSProxy.sol", "DSProxyFactory");
|
|
|
|
let factory = ContractFactory::new(abi, bytecode, Arc::clone(&provider));
|
2021-08-09 00:50:38 +00:00
|
|
|
let ds_proxy_factory = factory.deploy(()).unwrap().legacy();
|
|
|
|
let ds_proxy_factory = ds_proxy_factory.send().await.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// deploy a new DsProxy contract.
|
|
|
|
let ds_proxy = DsProxy::build::<HttpWallet, Arc<HttpWallet>>(
|
|
|
|
Arc::clone(&provider),
|
|
|
|
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.
|
2021-10-31 11:34:51 +00:00
|
|
|
let (abi, bytecode) = compile_contract("SimpleStorage.sol", "SimpleStorage");
|
|
|
|
let factory = ContractFactory::new(abi, bytecode, Arc::clone(&provider));
|
2021-08-09 00:50:38 +00:00
|
|
|
let deployer = factory.deploy(()).unwrap().legacy();
|
|
|
|
let simple_storage = deployer.send().await.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// instantiate a new transformer middleware.
|
|
|
|
let provider = TransformerMiddleware::new(signer_middleware, ds_proxy.clone());
|
|
|
|
|
|
|
|
// broadcast the setValue tx via transformer middleware (first wallet).
|
|
|
|
let expected_value: u64 = rng.gen();
|
|
|
|
let calldata = simple_storage
|
|
|
|
.encode("setValue", U256::from(expected_value))
|
|
|
|
.expect("could not get ABI encoded data");
|
2021-10-29 12:29:35 +00:00
|
|
|
let tx = TransactionRequest::new().to(simple_storage.address()).data(calldata);
|
|
|
|
provider.send_transaction(tx, None).await.unwrap().await.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();
|
2021-01-22 09:25:22 +00:00
|
|
|
assert_eq!(last_sender, wallet_addr.into());
|
|
|
|
assert_eq!(last_value, H256::from_low_u64_be(expected_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[cfg(not(feature = "celo"))]
|
|
|
|
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.
|
|
|
|
let anvil = Anvil::new().spawn();
|
|
|
|
let wallet: LocalWallet = anvil.keys()[1].clone().into();
|
|
|
|
let provider = Provider::<Http>::try_from(anvil.endpoint())
|
2021-01-22 09:25:22 +00:00
|
|
|
.unwrap()
|
|
|
|
.interval(Duration::from_millis(10u64));
|
2021-07-29 20:22:25 +00:00
|
|
|
let chain_id = provider.get_chainid().await.unwrap().as_u64();
|
|
|
|
let wallet = wallet.with_chain_id(chain_id);
|
2021-01-22 09:25:22 +00:00
|
|
|
let signer_middleware = SignerMiddleware::new(provider.clone(), wallet);
|
|
|
|
let wallet_addr = signer_middleware.address();
|
|
|
|
let provider = Arc::new(signer_middleware.clone());
|
|
|
|
|
|
|
|
// deploy DsProxyFactory which we'll use to deploy a new DsProxy contract.
|
2021-10-31 11:34:51 +00:00
|
|
|
let (abi, bytecode) = compile_contract("DSProxy.sol", "DSProxyFactory");
|
|
|
|
let factory = ContractFactory::new(abi, bytecode, Arc::clone(&provider));
|
2021-08-09 00:50:38 +00:00
|
|
|
let ds_proxy_factory = factory.deploy(()).unwrap().legacy();
|
|
|
|
let ds_proxy_factory = ds_proxy_factory.send().await.unwrap();
|
2021-01-22 09:25:22 +00:00
|
|
|
|
|
|
|
// deploy a new DsProxy contract.
|
|
|
|
let ds_proxy = DsProxy::build::<HttpWallet, Arc<HttpWallet>>(
|
|
|
|
Arc::clone(&provider),
|
|
|
|
Some(ds_proxy_factory.address()),
|
|
|
|
provider.address(),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let ds_proxy_addr = ds_proxy.address();
|
|
|
|
|
|
|
|
// compile the SimpleStorage contract which we will use to interact via DsProxy.
|
2021-10-31 11:34:51 +00:00
|
|
|
let (abi, bytecode) = compile_contract("SimpleStorage.sol", "SimpleStorage");
|
|
|
|
let ss_base_contract: BaseContract = abi.into();
|
2021-01-22 09:25:22 +00:00
|
|
|
let expected_value: u64 = rng.gen();
|
|
|
|
let calldata = ss_base_contract
|
|
|
|
.encode("setValue", U256::from(expected_value))
|
|
|
|
.expect("could not get ABI encoded data");
|
|
|
|
|
|
|
|
// 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),
|
2021-10-31 11:34:51 +00:00
|
|
|
bytecode.clone(),
|
2021-01-22 09:25:22 +00:00
|
|
|
calldata,
|
|
|
|
)
|
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
|
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();
|
2021-01-22 09:25:22 +00:00
|
|
|
assert_eq!(last_sender, wallet_addr.into());
|
|
|
|
assert_eq!(last_value, H256::from_low_u64_be(expected_value));
|
|
|
|
}
|