From 681a60febc2ce1886c7e99395f88efdbfa0958fa Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 1 Sep 2022 10:32:38 +0300 Subject: [PATCH 01/10] first ver.: transfer_in api function implememted but we can't have it in on-chain env --- crates/engine/src/ext.rs | 31 +++++++++++++++++++++ crates/env/src/api.rs | 10 +++++++ crates/env/src/backend.rs | 5 ++++ crates/env/src/engine/off_chain/impls.rs | 8 ++++++ crates/env/src/engine/off_chain/test_api.rs | 22 +++++++++++++++ crates/env/src/lib.rs | 14 ++++++++++ crates/lang/src/env_access.rs | 5 ++++ 7 files changed, 95 insertions(+) diff --git a/crates/engine/src/ext.rs b/crates/engine/src/ext.rs index d784d12094b..a373232340f 100644 --- a/crates/engine/src/ext.rs +++ b/crates/engine/src/ext.rs @@ -200,6 +200,37 @@ impl Engine { Ok(()) } + /// Transfers value from the caller account to the contract. + pub fn transfer_in(&mut self, mut value: &[u8]) -> Result { + // Note that a transfer of `0` is allowed here + let increment = ::decode(&mut value) + .map_err(|_| Error::TransferFailed)?; + + let caller = self + .exec_context + .caller + .as_ref() + .expect("no caller has been set") + .as_bytes(); + + // Note that the destination account does not have to exist + let caller_old_balance = self.get_balance(caller.to_vec()).unwrap_or_default(); + + let contract = self.get_callee(); + let contract_old_balance = self + .get_balance(contract.clone()) + .map_err(|_| Error::TransferFailed)?; + + self.database + .set_balance(caller, caller_old_balance - increment); + self.database + .set_balance(&contract, contract_old_balance + increment); + + self.set_value_transferred(increment); + + Ok(()) + } + /// Deposits an event identified by the supplied topics and data. pub fn deposit_event(&mut self, topics: &[u8], data: &[u8]) { // The first byte contains the number of topics in the slice diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 581d7dabb4f..614cb0d906f 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -350,6 +350,16 @@ where }) } +/// TODO: add docs +pub fn transfer_in(value: E::Balance) -> Result<()> +where + E: Environment, +{ + ::on_instance(|instance| { + TypedEnvBackend::transfer_in::(instance, value) + }) +} + /// Returns the execution input to the executed contract and decodes it as `T`. /// /// # Note diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 43ee3252332..912ebf6b2de 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -441,6 +441,11 @@ pub trait TypedEnvBackend: EnvBackend { where E: Environment; + /// TODO: add docs + fn transfer_in(&mut self, value: E::Balance) -> Result<()> + where + E: Environment; + /// Returns a random hash seed. /// /// # Note diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 26e1e3b4f1a..1b462217fa5 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -478,6 +478,14 @@ impl TypedEnvBackend for EnvInstance { .map_err(Into::into) } + fn transfer_in(&mut self, value: E::Balance) -> Result<()> + where + E: Environment, + { + let enc_value = &scale::Encode::encode(&value)[..]; + self.engine.transfer_in(enc_value).map_err(Into::into) + } + fn weight_to_fee(&mut self, gas: u64) -> E::Balance { let mut output: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; self.engine.weight_to_fee(gas, &mut &mut output[..]); diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 4d73be03ac3..53607c1b556 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -209,6 +209,28 @@ where }) } +/// TODO: write docs +/// the trick here is that we can't write this fn in abstract way, +/// i.e. for all possible return values of the msg +/// it does not send amount back if extrinsic fails +/// which is (I suppose) the case in on-chain engine +/// If this ^^^ is correct, then no point for impl this helper at all +/// just leave it as transfer_in and require contact test writeer to do this manually +/// +/// IDEA: make this decl macro which expands to { transfer_in(); msg() } block +/// don't forget to write warning: it does not check if called msg was marked payable +/// defined as MBE in env.lib.rs +// pub fn call_payable(msg: F, amount: T::Balance) -> Result<()> +// where +// F: FnOnce() -> Result<()>, +// T: Environment, +// { +// ::on_instance(|instance| { +// instance.engine.transfer_in(amount); +// }); +// msg +// } + /// Returns the amount of storage cells used by the account `account_id`. /// /// Returns `None` if the `account_id` is non-existent. diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 306b6ea774e..3955339f63a 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -163,3 +163,17 @@ cfg_if::cfg_if! { } } } + +cfg_if::cfg_if! { +if #[cfg(feature = "std")] { + #[macro_export] + /// Some docs + macro_rules! pay_with_call { + ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => { { + $con.env().transfer_in($amt); + $con.$msg($($params:ty)?) + }} + } + +} +} diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index 84e4cb02963..f8bd3447d6c 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -684,6 +684,11 @@ where ink_env::transfer::(destination, value) } + /// TODO: add docs + pub fn transfer_in(self, value: E::Balance) -> Result<()> { + ink_env::transfer_in::(value) + } + /// Returns a random hash seed. /// /// # Example From 8800ba7f0e6c8663e37e13e3156499e8aea55f4e Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 1 Sep 2022 11:16:14 +0300 Subject: [PATCH 02/10] transfer_in moved to test_api --- crates/engine/src/ext.rs | 31 ------------ crates/env/src/api.rs | 10 ---- crates/env/src/backend.rs | 5 -- crates/env/src/engine/off_chain/impls.rs | 8 --- crates/env/src/engine/off_chain/test_api.rs | 56 +++++++++++++-------- crates/env/src/lib.rs | 10 ++-- crates/lang/src/env_access.rs | 5 -- 7 files changed, 40 insertions(+), 85 deletions(-) diff --git a/crates/engine/src/ext.rs b/crates/engine/src/ext.rs index a373232340f..d784d12094b 100644 --- a/crates/engine/src/ext.rs +++ b/crates/engine/src/ext.rs @@ -200,37 +200,6 @@ impl Engine { Ok(()) } - /// Transfers value from the caller account to the contract. - pub fn transfer_in(&mut self, mut value: &[u8]) -> Result { - // Note that a transfer of `0` is allowed here - let increment = ::decode(&mut value) - .map_err(|_| Error::TransferFailed)?; - - let caller = self - .exec_context - .caller - .as_ref() - .expect("no caller has been set") - .as_bytes(); - - // Note that the destination account does not have to exist - let caller_old_balance = self.get_balance(caller.to_vec()).unwrap_or_default(); - - let contract = self.get_callee(); - let contract_old_balance = self - .get_balance(contract.clone()) - .map_err(|_| Error::TransferFailed)?; - - self.database - .set_balance(caller, caller_old_balance - increment); - self.database - .set_balance(&contract, contract_old_balance + increment); - - self.set_value_transferred(increment); - - Ok(()) - } - /// Deposits an event identified by the supplied topics and data. pub fn deposit_event(&mut self, topics: &[u8], data: &[u8]) { // The first byte contains the number of topics in the slice diff --git a/crates/env/src/api.rs b/crates/env/src/api.rs index 614cb0d906f..581d7dabb4f 100644 --- a/crates/env/src/api.rs +++ b/crates/env/src/api.rs @@ -350,16 +350,6 @@ where }) } -/// TODO: add docs -pub fn transfer_in(value: E::Balance) -> Result<()> -where - E: Environment, -{ - ::on_instance(|instance| { - TypedEnvBackend::transfer_in::(instance, value) - }) -} - /// Returns the execution input to the executed contract and decodes it as `T`. /// /// # Note diff --git a/crates/env/src/backend.rs b/crates/env/src/backend.rs index 912ebf6b2de..43ee3252332 100644 --- a/crates/env/src/backend.rs +++ b/crates/env/src/backend.rs @@ -441,11 +441,6 @@ pub trait TypedEnvBackend: EnvBackend { where E: Environment; - /// TODO: add docs - fn transfer_in(&mut self, value: E::Balance) -> Result<()> - where - E: Environment; - /// Returns a random hash seed. /// /// # Note diff --git a/crates/env/src/engine/off_chain/impls.rs b/crates/env/src/engine/off_chain/impls.rs index 1b462217fa5..26e1e3b4f1a 100644 --- a/crates/env/src/engine/off_chain/impls.rs +++ b/crates/env/src/engine/off_chain/impls.rs @@ -478,14 +478,6 @@ impl TypedEnvBackend for EnvInstance { .map_err(Into::into) } - fn transfer_in(&mut self, value: E::Balance) -> Result<()> - where - E: Environment, - { - let enc_value = &scale::Encode::encode(&value)[..]; - self.engine.transfer_in(enc_value).map_err(Into::into) - } - fn weight_to_fee(&mut self, gas: u64) -> E::Balance { let mut output: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; self.engine.weight_to_fee(gas, &mut &mut output[..]); diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 53607c1b556..96286bd38e7 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -209,27 +209,41 @@ where }) } -/// TODO: write docs -/// the trick here is that we can't write this fn in abstract way, -/// i.e. for all possible return values of the msg -/// it does not send amount back if extrinsic fails -/// which is (I suppose) the case in on-chain engine -/// If this ^^^ is correct, then no point for impl this helper at all -/// just leave it as transfer_in and require contact test writeer to do this manually -/// -/// IDEA: make this decl macro which expands to { transfer_in(); msg() } block -/// don't forget to write warning: it does not check if called msg was marked payable -/// defined as MBE in env.lib.rs -// pub fn call_payable(msg: F, amount: T::Balance) -> Result<()> -// where -// F: FnOnce() -> Result<()>, -// T: Environment, -// { -// ::on_instance(|instance| { -// instance.engine.transfer_in(amount); -// }); -// msg -// } +/// Transfers value from the caller account to the contract. +pub fn transfer_in(value: T::Balance) +where + T: Environment, // Just temporary for the MVP! +{ + ::on_instance(|instance| { + let caller = instance + .engine + .exec_context + .caller + .as_ref() + .expect("no caller has been set") + .as_bytes() + .to_vec(); + + let caller_old_balance = instance + .engine + .get_balance(caller.clone()) + .unwrap_or_default(); + + let callee = instance.engine.get_callee(); + let contract_old_balance = instance + .engine + .get_balance(callee.clone()) + .unwrap_or_default(); + + instance + .engine + .set_balance(caller, caller_old_balance - value); + instance + .engine + .set_balance(callee, contract_old_balance + value); + instance.engine.set_value_transferred(value); + }); +} /// Returns the amount of storage cells used by the account `account_id`. /// diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 3955339f63a..82fc6224171 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -169,11 +169,11 @@ if #[cfg(feature = "std")] { #[macro_export] /// Some docs macro_rules! pay_with_call { - ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => { { - $con.env().transfer_in($amt); - $con.$msg($($params:ty)?) - }} - } + ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => { { + $con.env().transfer_in($amt); + $con.$msg($($params:ty)?) + }} + } } } diff --git a/crates/lang/src/env_access.rs b/crates/lang/src/env_access.rs index f8bd3447d6c..84e4cb02963 100644 --- a/crates/lang/src/env_access.rs +++ b/crates/lang/src/env_access.rs @@ -684,11 +684,6 @@ where ink_env::transfer::(destination, value) } - /// TODO: add docs - pub fn transfer_in(self, value: E::Balance) -> Result<()> { - ink_env::transfer_in::(value) - } - /// Returns a random hash seed. /// /// # Example From c49d0ffa99091e9350273f0c433fd75e6f0a74bf Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 1 Sep 2022 12:26:27 +0300 Subject: [PATCH 03/10] doc + example updated --- crates/env/src/lib.rs | 19 +++++++++---------- examples/contract-transfer/lib.rs | 24 ++++++++++++++++++------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 82fc6224171..98d2b1f5b07 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -165,15 +165,14 @@ cfg_if::cfg_if! { } cfg_if::cfg_if! { -if #[cfg(feature = "std")] { - #[macro_export] - /// Some docs - macro_rules! pay_with_call { - ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => { { - $con.env().transfer_in($amt); - $con.$msg($($params:ty)?) - }} + if #[cfg(feature = "std")] { + #[macro_export] + /// Prepend contract message call with value transfer. Used for tests in off-chain environment. + macro_rules! pay_with_call { + ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => {{ + transfer_in::($amt); + $con.$msg($($params:ty)?) + }} } - -} + } } diff --git a/examples/contract-transfer/lib.rs b/examples/contract-transfer/lib.rs index a46da79f193..3c8f19ced66 100644 --- a/examples/contract-transfer/lib.rs +++ b/examples/contract-transfer/lib.rs @@ -65,7 +65,9 @@ pub mod give_me { #[cfg(test)] mod tests { use super::*; + use ink_env::test::transfer_in; use ink_lang as ink; + use ink_lang::codegen::Env; #[ink::test] fn transfer_works() { @@ -104,17 +106,27 @@ pub mod give_me { // given let accounts = default_accounts(); let give_me = create_contract(100); + let contract_account = give_me.env().account_id(); // when - // Push the new execution context which sets Eve as caller and - // the `mock_transferred_value` as the value which the contract - // will see as transferred to it. + // Push the new execution context which sets initial balnaces and + // sets Eve as the caller + set_balance(accounts.eve, 100); + set_balance(contract_account, 0); set_sender(accounts.eve); - ink_env::test::set_value_transferred::(10); // then - // there must be no panic - give_me.was_it_ten(); + // we use helper macro to emulate method invocation coming with payment, + // and there must be no panic + ink_env::pay_with_call!(give_me.was_it_ten(), 10); + + // and + // balances should be changed properly + let contract_new_balance = get_balance(contract_account); + let caller_new_balance = get_balance(accounts.eve); + + assert_eq!(caller_new_balance, 100 - 10); + assert_eq!(contract_new_balance, 10); } #[ink::test] From 541d110fa1c891a15dad9e95840280fbe15f32a0 Mon Sep 17 00:00:00 2001 From: Sasha Gryaznov Date: Thu, 1 Sep 2022 21:31:47 +0300 Subject: [PATCH 04/10] Update examples/contract-transfer/lib.rs Co-authored-by: Hernando Castano --- examples/contract-transfer/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/contract-transfer/lib.rs b/examples/contract-transfer/lib.rs index 3c8f19ced66..5a846f0b30b 100644 --- a/examples/contract-transfer/lib.rs +++ b/examples/contract-transfer/lib.rs @@ -109,7 +109,7 @@ pub mod give_me { let contract_account = give_me.env().account_id(); // when - // Push the new execution context which sets initial balnaces and + // Push the new execution context which sets initial balances and // sets Eve as the caller set_balance(accounts.eve, 100); set_balance(contract_account, 0); From 71ea6bf9ff2b047afe2036d29bc51d545136c149 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 1 Sep 2022 21:45:19 +0300 Subject: [PATCH 05/10] use stmt moved to macro --- crates/env/src/lib.rs | 1 + examples/contract-transfer/lib.rs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 98d2b1f5b07..0564fca189f 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -170,6 +170,7 @@ cfg_if::cfg_if! { /// Prepend contract message call with value transfer. Used for tests in off-chain environment. macro_rules! pay_with_call { ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => {{ + use ink_env::test::transfer_in; transfer_in::($amt); $con.$msg($($params:ty)?) }} diff --git a/examples/contract-transfer/lib.rs b/examples/contract-transfer/lib.rs index 5a846f0b30b..dc03dcc82f6 100644 --- a/examples/contract-transfer/lib.rs +++ b/examples/contract-transfer/lib.rs @@ -65,7 +65,6 @@ pub mod give_me { #[cfg(test)] mod tests { use super::*; - use ink_env::test::transfer_in; use ink_lang as ink; use ink_lang::codegen::Env; From cd9ca7f34e243308ab5f2646e8cd8fea9433a41f Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 6 Sep 2022 21:35:09 +0300 Subject: [PATCH 06/10] docs and nitty gritties --- crates/env/src/engine/off_chain/test_api.rs | 2 ++ examples/contract-transfer/lib.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 96286bd38e7..232327375fb 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -200,6 +200,7 @@ where } /// Sets the value transferred from the caller to the callee as part of the call. +/// Please note that the actioning accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. pub fn set_value_transferred(value: T::Balance) where T: Environment, // Just temporary for the MVP! @@ -210,6 +211,7 @@ where } /// Transfers value from the caller account to the contract. +/// Please note that the actioning accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. pub fn transfer_in(value: T::Balance) where T: Environment, // Just temporary for the MVP! diff --git a/examples/contract-transfer/lib.rs b/examples/contract-transfer/lib.rs index dc03dcc82f6..27e419dadd9 100644 --- a/examples/contract-transfer/lib.rs +++ b/examples/contract-transfer/lib.rs @@ -66,7 +66,6 @@ pub mod give_me { mod tests { use super::*; use ink_lang as ink; - use ink_lang::codegen::Env; #[ink::test] fn transfer_works() { @@ -102,6 +101,7 @@ pub mod give_me { #[ink::test] fn test_transferred_value() { + use ink_lang::codegen::Env; // given let accounts = default_accounts(); let give_me = create_contract(100); From 40e827f12c119853922b1a8ae9cc46c821432d6c Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 6 Sep 2022 21:50:30 +0300 Subject: [PATCH 07/10] moved the macro to the test mod --- crates/env/src/engine/off_chain/test_api.rs | 9 +++++++++ crates/env/src/lib.rs | 14 -------------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 232327375fb..0fcc9fab14f 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -393,3 +393,12 @@ pub fn assert_contract_termination( assert_eq!(value_transferred, expected_value_transferred_to_beneficiary); assert_eq!(beneficiary, expected_beneficiary); } + +#[macro_export] +/// Prepend contract message call with value transfer. Used for tests in off-chain environment. +macro_rules! pay_with_call { + ($contract:ident . $message:ident ( $($params:ty)? ) , $amount:expr) => {{ + ::ink_env::test::transfer_in::($amount); + $contract.$message($($params:ty)?) + }} +} diff --git a/crates/env/src/lib.rs b/crates/env/src/lib.rs index 0564fca189f..306b6ea774e 100644 --- a/crates/env/src/lib.rs +++ b/crates/env/src/lib.rs @@ -163,17 +163,3 @@ cfg_if::cfg_if! { } } } - -cfg_if::cfg_if! { - if #[cfg(feature = "std")] { - #[macro_export] - /// Prepend contract message call with value transfer. Used for tests in off-chain environment. - macro_rules! pay_with_call { - ($con:ident . $msg:ident ( $($params:ty)? ) , $amt:expr) => {{ - use ink_env::test::transfer_in; - transfer_in::($amt); - $con.$msg($($params:ty)?) - }} - } - } -} From 65d7d218d94628a187bb62134e000875808eba9b Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 6 Sep 2022 21:53:08 +0300 Subject: [PATCH 08/10] spell fix --- crates/env/src/engine/off_chain/test_api.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 0fcc9fab14f..9eac1259ad6 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -200,7 +200,7 @@ where } /// Sets the value transferred from the caller to the callee as part of the call. -/// Please note that the actioning accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. +/// Please note that the acting accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. pub fn set_value_transferred(value: T::Balance) where T: Environment, // Just temporary for the MVP! @@ -211,7 +211,7 @@ where } /// Transfers value from the caller account to the contract. -/// Please note that the actioning accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. +/// Please note that the acting accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. pub fn transfer_in(value: T::Balance) where T: Environment, // Just temporary for the MVP! From 96908b7132d3f715b4801a8c32ebe8aea6435c33 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Sat, 10 Sep 2022 14:26:49 +0300 Subject: [PATCH 09/10] next review round suggestions applied --- crates/env/src/engine/off_chain/test_api.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index 9eac1259ad6..ff223f8ee88 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -200,6 +200,7 @@ where } /// Sets the value transferred from the caller to the callee as part of the call. +/// /// Please note that the acting accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. pub fn set_value_transferred(value: T::Balance) where @@ -211,6 +212,7 @@ where } /// Transfers value from the caller account to the contract. +/// /// Please note that the acting accounts should be set with [`set_caller()`] and [`set_callee()`] beforehand. pub fn transfer_in(value: T::Balance) where @@ -394,11 +396,11 @@ pub fn assert_contract_termination( assert_eq!(beneficiary, expected_beneficiary); } -#[macro_export] /// Prepend contract message call with value transfer. Used for tests in off-chain environment. +#[macro_export] macro_rules! pay_with_call { - ($contract:ident . $message:ident ( $($params:ty)? ) , $amount:expr) => {{ - ::ink_env::test::transfer_in::($amount); + ($contract:ident . $message:ident ( $($params:ty)? ) , $amount:expr) => {{ + $crate::test::transfer_in::($amount); $contract.$message($($params:ty)?) - }} + }} } From e60a5dcadba96041235581ab3239d261f9f00a0c Mon Sep 17 00:00:00 2001 From: Hernando Castano Date: Tue, 13 Sep 2022 16:57:49 +0200 Subject: [PATCH 10/10] Use four spaces for macro indentation --- crates/env/src/engine/off_chain/test_api.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index ff223f8ee88..09f9c82ad2d 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -399,8 +399,8 @@ pub fn assert_contract_termination( /// Prepend contract message call with value transfer. Used for tests in off-chain environment. #[macro_export] macro_rules! pay_with_call { - ($contract:ident . $message:ident ( $($params:ty)? ) , $amount:expr) => {{ - $crate::test::transfer_in::($amount); - $contract.$message($($params:ty)?) - }} + ($contract:ident . $message:ident ( $($params:ty)? ) , $amount:expr) => {{ + $crate::test::transfer_in::($amount); + $contract.$message($($params:ty)?) + }} }