Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve contract-transfer example #789

Merged
merged 2 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/contract-transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ink_metadata = { version = "3.0.0-rc3", path = "../../crates/metadata", default-
ink_env = { version = "3.0.0-rc3", path = "../../crates/env", default-features = false }
ink_storage = { version = "3.0.0-rc3", path = "../../crates/storage", default-features = false }
ink_lang = { version = "3.0.0-rc3", path = "../../crates/lang", default-features = false }
ink_prelude = { version = "3.0.0-rc3", path = "../../crates/prelude", default-features = false }

scale = { package = "parity-scale-codec", version = "2.1", default-features = false, features = ["derive"] }
scale-info = { version = "0.6", default-features = false, features = ["derive"], optional = true }
Expand Down
71 changes: 65 additions & 6 deletions examples/contract-transfer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,25 @@ pub mod give_me {
})
}

/// Returns `true` if the token amount which the contract received
/// with this call is exactly `10`.
/// Asserts that the token amount sent as payment with this call
/// is exactly `10`. This method will fail otherwise, and the
/// transaction would then be reverted.
///
/// # 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(&self) -> bool {
self.env().transferred_balance() == 10
pub fn was_it_ten(&self) {
let msg = ink_prelude::format!(
"received payment: {}",
self.env().transferred_balance()
);
ink_env::debug_println(&msg);
assert!(
self.env().transferred_balance() == 10,
"payment was not ten"
);
}
}

Expand Down Expand Up @@ -157,7 +166,38 @@ pub mod give_me {
);

// then
assert_eq!(give_me.was_it_ten(), true);
// there must be no panic
give_me.was_it_ten();
}

#[ink::test]
#[should_panic(expected = "payment was not ten")]
fn test_transferred_value_must_fail() {
// given
let accounts = default_accounts();
let 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 = 13;

// 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
give_me.was_it_ten();
}

/// Creates a new instance of `GiveMe` with `initial_balance`.
Expand Down Expand Up @@ -258,7 +298,26 @@ pub mod give_me {
ink_env::test::set_value_transferred::<ink_env::DefaultEnvironment>(10);

// then
assert_eq!(give_me.was_it_ten(), true);
// there must be no panic
give_me.was_it_ten();
}

#[ink::test]
#[should_panic(expected = "payment was not ten")]
fn test_transferred_value_must_fail() {
// given
let accounts = default_accounts();
let give_me = create_contract(100);

// when
// 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.
set_sender(accounts.eve);
ink_env::test::set_value_transferred::<ink_env::DefaultEnvironment>(13);

// then
give_me.was_it_ten();
}

/// Creates a new instance of `GiveMe` with `initial_balance`.
Expand Down