Feat/add call deployer (#554)

* contract: add .call() method to Deployer

It is now possible to dry run a contract deployment.

* add .call() method of Deployer to unreleased

* add PR to changelog
This commit is contained in:
Francesco Iannelli 2021-11-04 14:02:05 +01:00 committed by GitHub
parent 23fb877c16
commit bd3a704200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 1 deletions

View File

@ -4,6 +4,7 @@
### Unreleased
- Add `.call()` method to `Deployer` for performing dry runs of contract deployments. [#554](https://github.com/gakonst/ethers-rs/pull/554)
- Improve error message from failure in `ethers_contract_abigen::Source::parse` [#552](https://github.com/gakonst/ethers-rs/pull/552)
- use enumerated aliases for overloaded functions [#545](https://github.com/gakonst/ethers-rs/pull/545)
- move `AbiEncode` `AbiDecode` trait to ethers-core and implement for core types [#531](https://github.com/gakonst/ethers-rs/pull/531)

View File

@ -46,6 +46,19 @@ impl<M: Middleware> Deployer<M> {
self
}
/// Dry runs the deployment of the contract
///
/// Note: this function _does not_ send a transaction from your account
pub async fn call(&self) -> Result<(), ContractError<M>> {
self.client
.call(&self.tx, Some(self.block.into()))
.await
.map_err(ContractError::MiddlewareError)?;
// TODO: It would be nice to handle reverts in a structured way.
Ok(())
}
/// Broadcasts the contract deployment transaction and after waiting for it to
/// be sufficiently confirmed (default: 1), it returns a [`Contract`](crate::Contract)
/// struct at the deployed contract's address.

View File

@ -49,5 +49,7 @@ pub fn connect(ganache: &GanacheInstance, idx: usize) -> Arc<Provider<Http>> {
/// Launches a ganache instance and deploys the SimpleStorage contract
pub async fn deploy<M: Middleware>(client: Arc<M>, abi: Abi, bytecode: Bytes) -> Contract<M> {
let factory = ContractFactory::new(abi, bytecode, client);
factory.deploy("initial value".to_string()).unwrap().legacy().send().await.unwrap()
let deployer = factory.deploy("initial value".to_string()).unwrap();
assert!(deployer.call().await.is_ok());
deployer.legacy().send().await.unwrap()
}

View File

@ -39,6 +39,9 @@ mod eth_tests {
// (practically it's not expected that you'll need to deploy multiple instances of
// the _same_ deployer, so it's fine to clone here from a dev UX vs perf tradeoff)
let deployer = factory.deploy("initial value".to_string()).unwrap().legacy();
// dry runs the deployment of the contract. takes the deployer by reference, no need to
// clone.
assert!(deployer.call().await.is_ok());
let contract = deployer.clone().send().await.unwrap();
let get_value = contract.method::<_, String>("getValue", ()).unwrap();