Skip to content

Commit

Permalink
Panic on failed transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
cmichi committed May 10, 2021
1 parent 342d79e commit 1bfaa8d
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 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

0 comments on commit 1bfaa8d

Please sign in to comment.