Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Feat/add call deployer (#554)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
frjnn authored Nov 4, 2021
1 parent 23fb877 commit bd3a704
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions ethers-contract/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion ethers-contract/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 3 additions & 0 deletions ethers-contract/tests/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit bd3a704

Please sign in to comment.