From 1e04fa027a49dcc2ddcb0e4d69d2de800b66db56 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 17 Mar 2022 10:01:54 +0100 Subject: [PATCH] refactor: deploy function --- .../src/contract/methods.rs | 37 ++++++------------- ethers-contract/tests/abigen.rs | 21 +++++++++++ 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/ethers-contract/ethers-contract-abigen/src/contract/methods.rs b/ethers-contract/ethers-contract-abigen/src/contract/methods.rs index 7cc2a9c3f..c4ed893ca 100644 --- a/ethers-contract/ethers-contract-abigen/src/contract/methods.rs +++ b/ethers-contract/ethers-contract-abigen/src/contract/methods.rs @@ -58,39 +58,26 @@ impl Context { #bytecode_name.clone().into() }; - let get_client = quote! { - self.deref().client().clone() - }; - let deploy_tokens = quote! { - /// Constructs the general purpose `Deployer` instance based on the provided constructor arguments - /// You must call `send()` in order to actually deploy the contract. + let deploy = quote! { + /// Constructs the general purpose `Deployer` instance based on the provided constructor arguments and sends it. + /// Returns a new instance of this contract afterwards /// /// Notes: /// 1. If there are no constructor arguments, you should pass `()` as the argument. /// 1. The default poll duration is 7 seconds. /// 1. The default number of confirmations is 1 block. - pub fn deploy_tokens(self, constructor_args: T) -> Result<#ethers_contract::factory::Deployer, #ethers_contract::call::ContractError> { - let factory = #ethers_contract::factory::ContractFactory::new(#get_abi, #get_bytecode, #get_client); - factory.deploy_tokens(constructor_args.into_tokens()) + // TODO make this return an additional helper type + pub async fn deploy(client client: ::std::sync::Arc, constructor_args: T) -> Result<#ethers_contract::factory::Deployer, #ethers_contract::call::ContractError> { + let factory = #ethers_contract::factory::ContractFactory::new(#get_abi, #get_bytecode, :client); + let contract = factory.deploy(constructor_args)?.send().await?; + Self(contract) } }; - // /// Constructs the deployment transaction and returns a `Deployer` instance. - // /// You must call `send()` in order to actually deploy the contract. - // /// - // /// Notes: - // /// 1. If there are no constructor arguments, you should pass `()` as the argument. - // /// 1. The default poll duration is 7 seconds. - // /// 1. The default number of confirmations is 1 block. - // pub fn deploy(self, constructor_args: T) -> Result, - // ContractError> { self.deploy_tokens(constructor_args. - // into_tokens()) // let factory = ContractFactory::new(abi.unwrap(), - // bytecode.unwrap(), client.clone()); } - - return quote! { - - #deploy_tokens - } + + // TODO generate all constructors, + + return deploy } quote! {} diff --git a/ethers-contract/tests/abigen.rs b/ethers-contract/tests/abigen.rs index 9017633da..e1adb8889 100644 --- a/ethers-contract/tests/abigen.rs +++ b/ethers-contract/tests/abigen.rs @@ -478,6 +478,27 @@ fn can_handle_case_sensitive_calls() { let _ = contract.INDEX(); } +#[tokio::test] +async fn can_deploy_greeter() { + abigen!(Greeter, "ethers-contract/tests/solidity-contracts/greeter_with_struct.json",); + let ganache = ethers_core::utils::Ganache::new().spawn(); + let from = ganache.addresses()[0]; + let provider = Provider::try_from(ganache.endpoint()) + .unwrap() + .with_sender(from) + .interval(std::time::Duration::from_millis(10)); + let client = Arc::new(provider); + + Greeter::deploy(client, ()); + // let addr = factory.deploy(()).unwrap().legacy().send().await.unwrap().address(); + // + // let contract = AbiEncoderv2Test::new(addr, client.clone()); + // let person = Person { name: "Alice".to_string(), age: 20u64.into() }; + // + // let res = contract.default_person().call().await.unwrap(); + // assert_eq!(res, person); +} + #[tokio::test] async fn can_abiencoderv2_output() { abigen!(AbiEncoderv2Test, "ethers-contract/tests/solidity-contracts/abiencoderv2test_abi.json",);