diff --git a/crates/env/src/call/call_builder.rs b/crates/env/src/call/call_builder.rs index 95c851c6f76..3ea6c065c97 100644 --- a/crates/env/src/call/call_builder.rs +++ b/crates/env/src/call/call_builder.rs @@ -28,7 +28,6 @@ use crate::{ Error, }; use core::marker::PhantomData; -use ink_primitives::Clear; use num_traits::Zero; /// The final parameters to the cross-contract call. @@ -71,10 +70,8 @@ where E: Environment, { /// Returns the account ID of the called contract instance. - /// - /// Returns `None` if no account ID has been set for the call. #[inline] - pub fn callee(&self) -> &Option { + pub fn callee(&self) -> &E::AccountId { &self.call_type.callee } @@ -205,11 +202,9 @@ where /// # type AccountId = ::AccountId; /// # type Balance = ::Balance; /// build_call::() -/// .call_type( -/// Call::new() -/// .callee(AccountId::from([0x42; 32])) -/// .gas_limit(5000) -/// .transferred_value(10)) +/// .call(AccountId::from([0x42; 32])) +/// .gas_limit(5000) +/// .transferred_value(10) /// .exec_input( /// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF])) /// .push_arg(42u8) @@ -241,9 +236,8 @@ where /// # }; /// # type AccountId = ::AccountId; /// let my_return_value: i32 = build_call::() -/// .call_type(Call::new() -/// .callee(AccountId::from([0x42; 32])) -/// .gas_limit(5000)) +/// .call_type(Call::new(AccountId::from([0x42; 32]))) +/// .gas_limit(5000) /// .transferred_value(10) /// .exec_input( /// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF])) @@ -270,8 +264,7 @@ where /// # use ink_primitives::Clear; /// # type AccountId = ::AccountId; /// let my_return_value: i32 = build_call::() -/// .call_type(DelegateCall::new() -/// .code_hash(::Hash::CLEAR_HASH)) +/// .delegate(::Hash::CLEAR_HASH) /// .exec_input( /// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF])) /// .push_arg(42u8) @@ -306,12 +299,9 @@ where /// # type AccountId = ::AccountId; /// # type Balance = ::Balance; /// let call_result = build_call::() -/// .call_type( -/// Call::new() -/// .callee(AccountId::from([0x42; 32])) -/// .gas_limit(5000) -/// .transferred_value(10), -/// ) +/// .call(AccountId::from([0x42; 32])) +/// .gas_limit(5000) +/// .transferred_value(10) /// .try_invoke() /// .expect("Got an error from the Contract's pallet."); /// @@ -343,41 +333,26 @@ where /// The default call type for cross-contract calls. Performs a cross-contract call to `callee` /// with gas limit `gas_limit`, transferring `transferred_value` of currency. pub struct Call { - callee: Option, + callee: E::AccountId, gas_limit: Gas, transferred_value: E::Balance, } -impl Default for Call { - fn default() -> Self { - Call { - callee: Default::default(), +impl Call { + /// Returns a clean builder for [`Call`]. + pub fn new(callee: E::AccountId) -> Self { + Self { + callee, gas_limit: Default::default(), transferred_value: E::Balance::zero(), } } } -impl Call { - /// Returns a clean builder for [`Call`]. - pub fn new() -> Self { - Default::default() - } -} - impl Call where E: Environment, { - /// Sets the `callee` for the current cross-contract call. - pub fn callee(self, callee: E::AccountId) -> Self { - Call { - callee: Some(callee), - gas_limit: self.gas_limit, - transferred_value: self.transferred_value, - } - } - /// Sets the `gas_limit` for the current cross-contract call. pub fn gas_limit(self, gas_limit: Gas) -> Self { Call { @@ -404,16 +379,8 @@ pub struct DelegateCall { impl DelegateCall { /// Returns a clean builder for [`DelegateCall`] - pub const fn new() -> Self { - DelegateCall { - code_hash: E::Hash::CLEAR_HASH, - } - } -} - -impl Default for DelegateCall { - fn default() -> Self { - Self::new() + pub const fn new(code_hash: E::Hash) -> Self { + DelegateCall { code_hash } } } @@ -521,26 +488,43 @@ where } } -impl CallBuilder>, Args, RetType> +impl CallBuilder, Args, RetType> where E: Environment, { - /// Sets the `callee` for the current cross-contract call. - pub fn callee(self, callee: E::AccountId) -> Self { - let call_type = self.call_type.value(); + /// Prepares the `CallBuilder` for a cross-contract [`Call`]. + pub fn call( + self, + callee: E::AccountId, + ) -> CallBuilder>, Args, RetType> { CallBuilder { - call_type: Set(Call { - callee: Some(callee), - gas_limit: call_type.gas_limit, - transferred_value: call_type.transferred_value, - }), + call_type: Set(Call::new(callee)), + call_flags: self.call_flags, + exec_input: self.exec_input, + return_type: self.return_type, + _phantom: Default::default(), + } + } + + /// Prepares the `CallBuilder` for a cross-contract [`DelegateCall`]. + pub fn delegate( + self, + code_hash: E::Hash, + ) -> CallBuilder>, Args, RetType> { + CallBuilder { + call_type: Set(DelegateCall::new(code_hash)), call_flags: self.call_flags, exec_input: self.exec_input, return_type: self.return_type, _phantom: Default::default(), } } +} +impl CallBuilder>, Args, RetType> +where + E: Environment, +{ /// Sets the `gas_limit` for the current cross-contract call. pub fn gas_limit(self, gas_limit: Gas) -> Self { let call_type = self.call_type.value(); diff --git a/crates/env/src/engine/on_chain/impls.rs b/crates/env/src/engine/on_chain/impls.rs index e3a7d4a4e6c..ae9fa3fdb1b 100644 --- a/crates/env/src/engine/on_chain/impls.rs +++ b/crates/env/src/engine/on_chain/impls.rs @@ -412,11 +412,7 @@ impl TypedEnvBackend for EnvInstance { { let mut scope = self.scoped_buffer(); let gas_limit = params.gas_limit(); - let callee = params - .callee() - .as_ref() - .expect("An account ID must be set in order to call a contract."); - let enc_callee = scope.take_encoded(callee); + let enc_callee = scope.take_encoded(params.callee()); let enc_transferred_value = scope.take_encoded(params.transferred_value()); let call_flags = params.call_flags(); let enc_input = if !call_flags.forward_input() && !call_flags.clone_input() { diff --git a/crates/ink/codegen/src/generator/as_dependency/call_builder.rs b/crates/ink/codegen/src/generator/as_dependency/call_builder.rs index 06ce61e4425..fe5f09feafe 100644 --- a/crates/ink/codegen/src/generator/as_dependency/call_builder.rs +++ b/crates/ink/codegen/src/generator/as_dependency/call_builder.rs @@ -391,7 +391,7 @@ impl CallBuilder<'_> { #( , #input_bindings : #input_types )* ) -> #output_type { ::ink::env::call::build_call::() - .call_type(::ink::env::call::Call::new().callee(::ink::ToAccountId::to_account_id(self))) + .call(::ink::ToAccountId::to_account_id(self)) .exec_input( ::ink::env::call::ExecutionInput::new( ::ink::env::call::Selector::new([ #( #selector_bytes ),* ]) diff --git a/crates/ink/codegen/src/generator/trait_def/call_builder.rs b/crates/ink/codegen/src/generator/trait_def/call_builder.rs index 34221367b36..69db77bb431 100644 --- a/crates/ink/codegen/src/generator/trait_def/call_builder.rs +++ b/crates/ink/codegen/src/generator/trait_def/call_builder.rs @@ -319,7 +319,7 @@ impl CallBuilder<'_> { #( , #input_bindings : #input_types )* ) -> Self::#output_ident { ::ink::env::call::build_call::() - .call_type(::ink::env::call::Call::new().callee(::ink::ToAccountId::to_account_id(self))) + .call(::ink::ToAccountId::to_account_id(self)) .exec_input( ::ink::env::call::ExecutionInput::new( ::ink::env::call::Selector::new([ #( #selector_bytes ),* ]) diff --git a/crates/ink/src/env_access.rs b/crates/ink/src/env_access.rs index 89cbcf82556..f54660cd42a 100644 --- a/crates/ink/src/env_access.rs +++ b/crates/ink/src/env_access.rs @@ -524,8 +524,7 @@ where /// pub fn invoke_contract(&self) -> i32 { /// let call_params = build_call::() /// .call_type( - /// Call::new() - /// .callee(AccountId::from([0x42; 32])) + /// Call::new(AccountId::from([0x42; 32])) /// .gas_limit(5000) /// .transferred_value(10)) /// .exec_input( @@ -588,8 +587,7 @@ where /// pub fn invoke_contract_delegate(&self) -> i32 { /// let call_params = build_call::() /// .call_type( - /// DelegateCall::new() - /// .code_hash(::Hash::CLEAR_HASH)) + /// DelegateCall::new(::Hash::CLEAR_HASH)) /// .exec_input( /// ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE])) /// .push_arg(42u8) diff --git a/examples/erc1155/lib.rs b/examples/erc1155/lib.rs index 4f703b2a922..ed49e73bcc6 100644 --- a/examples/erc1155/lib.rs +++ b/examples/erc1155/lib.rs @@ -358,7 +358,6 @@ mod erc1155 { { use ink::env::call::{ build_call, - Call, ExecutionInput, Selector, }; @@ -366,7 +365,8 @@ mod erc1155 { // If our recipient is a smart contract we need to see if they accept or // reject this transfer. If they reject it we need to revert the call. let result = build_call::() - .call_type(Call::new().callee(to).gas_limit(5000)) + .call(to) + .gas_limit(5000) .exec_input( ExecutionInput::new(Selector::new(ON_ERC_1155_RECEIVED_SELECTOR)) .push_arg(caller) diff --git a/examples/lang-err-integration-tests/call-builder/lib.rs b/examples/lang-err-integration-tests/call-builder/lib.rs index 1d143fadece..092ad1d0358 100755 --- a/examples/lang-err-integration-tests/call-builder/lib.rs +++ b/examples/lang-err-integration-tests/call-builder/lib.rs @@ -21,7 +21,6 @@ mod call_builder { use ink::env::{ call::{ build_call, - Call, ExecutionInput, Selector, }, @@ -52,7 +51,7 @@ mod call_builder { selector: [u8; 4], ) -> Option { let result = build_call::() - .call_type(Call::new().callee(address)) + .call(address) .exec_input(ExecutionInput::new(Selector::new(selector))) .returns::<()>() .try_invoke() @@ -79,7 +78,7 @@ mod call_builder { use ink::env::call::build_call; build_call::() - .call_type(Call::new().callee(address)) + .call(address) .exec_input(ExecutionInput::new(Selector::new(selector))) .returns::<()>() .invoke() diff --git a/examples/multisig/lib.rs b/examples/multisig/lib.rs index 250db845d3c..b5452c7b1c6 100755 --- a/examples/multisig/lib.rs +++ b/examples/multisig/lib.rs @@ -67,7 +67,6 @@ mod multisig { env::{ call::{ build_call, - Call, ExecutionInput, }, CallFlags, @@ -310,7 +309,6 @@ mod multisig { /// use ink::env::{ /// call::{ /// utils::ArgumentList, - /// Call, /// CallParams, /// ExecutionInput, /// Selector, @@ -536,12 +534,9 @@ mod multisig { let t = self.take_transaction(trans_id).expect(WRONG_TRANSACTION_ID); assert!(self.env().transferred_value() == t.transferred_value); let result = build_call::<::Env>() - .call_type( - Call::new() - .callee(t.callee) - .gas_limit(t.gas_limit) - .transferred_value(t.transferred_value), - ) + .call(t.callee) + .gas_limit(t.gas_limit) + .transferred_value(t.transferred_value) .call_flags(CallFlags::default().set_allow_reentry(t.allow_reentry)) .exec_input( ExecutionInput::new(t.selector.into()).push_arg(CallInput(&t.input)), @@ -574,12 +569,9 @@ mod multisig { self.ensure_confirmed(trans_id); let t = self.take_transaction(trans_id).expect(WRONG_TRANSACTION_ID); let result = build_call::<::Env>() - .call_type( - Call::new() - .callee(t.callee) - .gas_limit(t.gas_limit) - .transferred_value(t.transferred_value), - ) + .call(t.callee) + .gas_limit(t.gas_limit) + .transferred_value(t.transferred_value) .call_flags(CallFlags::default().set_allow_reentry(t.allow_reentry)) .exec_input( ExecutionInput::new(t.selector.into()).push_arg(CallInput(&t.input)), diff --git a/examples/upgradeable-contracts/forward-calls/lib.rs b/examples/upgradeable-contracts/forward-calls/lib.rs index 51c4144cc06..d65c5111680 100644 --- a/examples/upgradeable-contracts/forward-calls/lib.rs +++ b/examples/upgradeable-contracts/forward-calls/lib.rs @@ -17,7 +17,6 @@ #[ink::contract] pub mod proxy { - use ink::env::call::Call; /// A simple proxy contract. #[ink(storage)] @@ -70,12 +69,9 @@ pub mod proxy { #[ink(message, payable, selector = _)] pub fn forward(&self) -> u32 { ink::env::call::build_call::() - .call_type( - Call::new() - .callee(self.forward_to) - .transferred_value(self.env().transferred_value()) - .gas_limit(0), - ) + .call(self.forward_to) + .transferred_value(self.env().transferred_value()) + .gas_limit(0) .call_flags( ink::env::CallFlags::default() .set_forward_input(true)