Unverified Commit a35f76ac authored by Michael Müller's avatar Michael Müller Committed by GitHub
Browse files

Add demonstration how to mock transferred_balance (#555)

* [chores] implicitely ➜ implicitly

* [examples] Add demonstration of how to mock transferred_balance

* [lang] Fix clippy 'needless_lifetimes'

Error was:

explicit lifetimes given in parameter types where they could
be elided (or replaced with `'_` if needed by type declaration)

* [lang] Apply cargo fmt

* [examples] Fix param
parent 89620e7d
Pipeline #113089 failed with stages
in 23 minutes and 46 seconds
...@@ -357,7 +357,7 @@ We won't be going into the details for any of those but will briefly present the ...@@ -357,7 +357,7 @@ We won't be going into the details for any of those but will briefly present the
| `#[ink(topic)]` | Applicate on ink! event field. | Tells the ink! codegen to provide a topic hash for the given field. Every ink! event can only have a limited number of such topic field. Similar semantics as to indexed event arguments in Solidity. | | `#[ink(topic)]` | Applicate on ink! event field. | Tells the ink! codegen to provide a topic hash for the given field. Every ink! event can only have a limited number of such topic field. Similar semantics as to indexed event arguments in Solidity. |
| `#[ink(message)]` | Applicable to methods. | Flags a method for the ink! storage struct as message making it available to the API for calling the contract. | | `#[ink(message)]` | Applicable to methods. | Flags a method for the ink! storage struct as message making it available to the API for calling the contract. |
| `#[ink(constructor)]` | Applicable to method. | Flags a method for the ink! storage struct as constructor making it available to the API for instantiating the contract. | | `#[ink(constructor)]` | Applicable to method. | Flags a method for the ink! storage struct as constructor making it available to the API for instantiating the contract. |
| `#[ink(payable)]` **new** | Applicable to ink! messages. | Allows receiving value as part of the call of the ink! message. ink! constructors are implicitely payable. | | `#[ink(payable)]` **new** | Applicable to ink! messages. | Allows receiving value as part of the call of the ink! message. ink! constructors are implicitly payable. |
| `#[ink(selector = "..")]` **new** | Applicable to ink! messages and ink! constructors. | Specifies a concrete dispatch selector for the flagged entity. This allows a contract author to precisely control the selectors of their APIs making it possible to rename their API without breakage. | | `#[ink(selector = "..")]` **new** | Applicable to ink! messages and ink! constructors. | Specifies a concrete dispatch selector for the flagged entity. This allows a contract author to precisely control the selectors of their APIs making it possible to rename their API without breakage. |
| `#[ink(namespace = "..")]` **new** | Applicable to ink! trait implementation blocks. | Changes the resulting selectors of all the ink! messages and ink! constructors within the trait implementation. Allows to disambiguate between trait implementations with overlapping message or constructor names. Use only with great care and consideration! | | `#[ink(namespace = "..")]` **new** | Applicable to ink! trait implementation blocks. | Changes the resulting selectors of all the ink! messages and ink! constructors within the trait implementation. Allows to disambiguate between trait implementations with overlapping message or constructor names. Use only with great care and consideration! |
| `#[ink(implementation)]` **new** | Applicable to ink! implementation blocks. | Tells the ink! codegen that some implementation block shall be granted access to ink! internals even without it containing any ink! messages or ink! constructors. | | `#[ink(implementation)]` **new** | Applicable to ink! implementation blocks. | Tells the ink! codegen that some implementation block shall be granted access to ink! internals even without it containing any ink! messages or ink! constructors. |
......
...@@ -88,9 +88,9 @@ impl Metadata<'_> { ...@@ -88,9 +88,9 @@ impl Metadata<'_> {
} }
/// Extracts the doc strings from the given slice of attributes. /// Extracts the doc strings from the given slice of attributes.
fn extract_doc_comments<'a>( fn extract_doc_comments(
attributes: &'a [syn::Attribute], attributes: &[syn::Attribute],
) -> impl Iterator<Item = String> + 'a { ) -> impl Iterator<Item = String> + '_ {
attributes attributes
.iter() .iter()
.filter_map(|attribute| { .filter_map(|attribute| {
...@@ -109,7 +109,7 @@ impl Metadata<'_> { ...@@ -109,7 +109,7 @@ impl Metadata<'_> {
} }
/// Generates ink! metadata for all contract constructors. /// Generates ink! metadata for all contract constructors.
fn generate_constructors<'a>(&'a self) -> impl Iterator<Item = TokenStream2> + 'a { fn generate_constructors(&self) -> impl Iterator<Item = TokenStream2> + '_ {
self.contract self.contract
.module() .module()
.impls() .impls()
...@@ -200,7 +200,7 @@ impl Metadata<'_> { ...@@ -200,7 +200,7 @@ impl Metadata<'_> {
} }
} }
fn generate_messages<'a>(&'a self) -> impl Iterator<Item = TokenStream2> + 'a { fn generate_messages(&self) -> impl Iterator<Item = TokenStream2> + '_ {
self.contract self.contract
.module() .module()
.impls() .impls()
...@@ -272,7 +272,7 @@ impl Metadata<'_> { ...@@ -272,7 +272,7 @@ impl Metadata<'_> {
} }
/// Generates ink! metadata for all user provided ink! event definitions. /// Generates ink! metadata for all user provided ink! event definitions.
fn generate_events<'a>(&'a self) -> impl Iterator<Item = TokenStream2> + 'a { fn generate_events(&self) -> impl Iterator<Item = TokenStream2> + '_ {
self.contract.module().events().map(|event| { self.contract.module().events().map(|event| {
let span = event.span(); let span = event.span();
let ident = event.ident(); let ident = event.ident();
...@@ -293,9 +293,7 @@ impl Metadata<'_> { ...@@ -293,9 +293,7 @@ impl Metadata<'_> {
} }
/// Generate ink! metadata for a single argument of an ink! event definition. /// Generate ink! metadata for a single argument of an ink! event definition.
fn generate_event_args<'a>( fn generate_event_args(event: &ir::Event) -> impl Iterator<Item = TokenStream2> + '_ {
event: &'a ir::Event,
) -> impl Iterator<Item = TokenStream2> + 'a {
event.fields().map(|event_field| { event.fields().map(|event_field| {
let span = event_field.span(); let span = event_field.span();
let ident = event_field.ident(); let ident = event_field.ident();
...@@ -317,7 +315,7 @@ impl Metadata<'_> { ...@@ -317,7 +315,7 @@ impl Metadata<'_> {
} }
/// Generates the documentation for the contract module. /// Generates the documentation for the contract module.
fn generate_docs<'a>(&'a self) -> impl Iterator<Item = String> + 'a { fn generate_docs(&self) -> impl Iterator<Item = String> + '_ {
Self::extract_doc_comments(self.contract.module().attrs()) Self::extract_doc_comments(self.contract.module().attrs())
} }
} }
......
...@@ -75,6 +75,18 @@ pub mod give_me { ...@@ -75,6 +75,18 @@ pub mod give_me {
} }
}) })
} }
/// Returns `true` if the token amount which the contract received
/// with this call is exactly `10`.
///
/// # Note
///
/// The method needs to be annotated with `payable`; only then it is
/// allowed to receive value as part of the call.
#[ink(message, payable, selector = "0xCAFEBABE")]
pub fn was_it_ten(&mut self) -> bool {
self.env().transferred_balance() == 10
}
} }
#[cfg(test)] #[cfg(test)]
...@@ -118,20 +130,51 @@ pub mod give_me { ...@@ -118,20 +130,51 @@ pub mod give_me {
assert_eq!(ret, Err(Error::InsufficientFunds)); assert_eq!(ret, Err(Error::InsufficientFunds));
} }
#[ink::test]
fn test_transferred_value() {
// given
let accounts = default_accounts();
let mut give_me = create_contract(100);
// when
set_sender(accounts.eve);
let mut data = ink_env::test::CallData::new(ink_env::call::Selector::new([
0xCA, 0xFE, 0xBA, 0xBE,
]));
data.push_arg(&accounts.eve);
let mock_transferred_balance = 10;
// Push the new execution context which sets Eve as caller and
// the `mock_transferred_balance` as the value which the contract
// will see as transferred to it.
ink_env::test::push_execution_context::<ink_env::DefaultEnvironment>(
accounts.eve,
contract_id(),
1000000,
mock_transferred_balance,
data,
);
// then
assert_eq!(give_me.was_it_ten(), true);
}
/// Creates a new instance of `GiveMe` with `initial_balance`. /// Creates a new instance of `GiveMe` with `initial_balance`.
/// ///
/// Returns the `contract_instance`. /// Returns the `contract_instance`.
fn create_contract(initial_balance: Balance) -> GiveMe { fn create_contract(initial_balance: Balance) -> GiveMe {
let accounts = default_accounts(); let accounts = default_accounts();
let contract_id = ink_env::test::get_current_contract_account_id::<
ink_env::DefaultEnvironment,
>()
.expect("Cannot get contract id");
set_sender(accounts.alice); set_sender(accounts.alice);
set_balance(contract_id, initial_balance); set_balance(contract_id(), initial_balance);
GiveMe::new() GiveMe::new()
} }
fn contract_id() -> AccountId {
ink_env::test::get_current_contract_account_id::<ink_env::DefaultEnvironment>(
)
.expect("Cannot get contract id")
}
fn set_sender(sender: AccountId) { fn set_sender(sender: AccountId) {
let callee = ink_env::account_id::<ink_env::DefaultEnvironment>() let callee = ink_env::account_id::<ink_env::DefaultEnvironment>()
.unwrap_or([0x0; 32].into()); .unwrap_or([0x0; 32].into());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment