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

Panic on failed transfer #790

Merged
merged 1 commit into from
May 11, 2021
Merged
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
54 changes: 29 additions & 25 deletions examples/contract-transfer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,30 @@ pub mod give_me {
///
/// # Errors
///
/// - Returns `Error::InsufficientFunds` in case the requested transfer of
/// `value` exceeds the contracts balance.
/// - Returns `Error::BelowSubsistenceThreshold` in case the requested transfer
/// of `value` would have brought the contract's balance below the subsistence
/// threshold.
/// - Returns `Error::TransferFailed` in case the transfer failed for another
/// reason.
/// - Panics in case the requested transfer exceeds the contract balance.
/// - Panics in case the requested transfer would have brought the
/// contract balance below the subsistence threshold.
/// - Panics in case the transfer failed for another reason.
#[ink(message)]
pub fn give_me(&mut self, value: Balance) -> Result<(), Error> {
if value > self.env().balance() {
return Err(Error::InsufficientFunds)
pub fn give_me(&mut self, value: Balance) {
ink_env::debug_println(&ink_prelude::format!("requested value: {}", value));
ink_env::debug_println(&ink_prelude::format!(
"contract balance: {}",
self.env().balance()
));

assert!(value <= self.env().balance(), "insufficient funds!");

match self.env().transfer(self.env().caller(), value) {
Err(ink_env::Error::BelowSubsistenceThreshold) => {
panic!(
"requested transfer would have brought contract\
below subsistence threshold!"
)
}
Err(_) => panic!("transfer failed!"),
Ok(_) => {}
}
self.env()
.transfer(self.env().caller(), value)
.map_err(|err| {
match err {
ink_env::Error::BelowSubsistenceThreshold => {
Error::BelowSubsistenceThreshold
}
_ => Error::TransferFailed,
}
})
}

/// Asserts that the token amount sent as payment with this call
Expand Down Expand Up @@ -119,13 +121,14 @@ pub mod give_me {
// when
set_sender(accounts.eve);
set_balance(accounts.eve, 0);
assert_eq!(give_me.give_me(80), Ok(()));
give_me.give_me(80);

// then
assert_eq!(get_balance(accounts.eve), 80);
}

#[ink::test]
#[should_panic(expected = "insufficient funds!")]
fn transfer_fails_insufficient_funds() {
// given
let contract_balance = 100;
Expand All @@ -134,10 +137,10 @@ pub mod give_me {

// when
set_sender(accounts.eve);
let ret = give_me.give_me(120);
give_me.give_me(120);

// then
assert_eq!(ret, Err(Error::InsufficientFunds));
// `give_me` must already have panicked here
}

#[ink::test]
Expand Down Expand Up @@ -270,6 +273,7 @@ pub mod give_me {
}

#[ink::test]
#[should_panic(expected = "insufficient funds!")]
fn transfer_fails_insufficient_funds() {
// given
let contract_balance = 100;
Expand All @@ -278,10 +282,10 @@ pub mod give_me {

// when
set_sender(accounts.eve);
let ret = give_me.give_me(120);
give_me.give_me(120);

// then
assert_eq!(ret, Err(Error::InsufficientFunds));
// `give_me` must already have panicked here
}

#[ink::test]
Expand Down