Skip to content

Commit

Permalink
Remove BelowSubsistenceThreshold and NewContractNotFunded (#1062)
Browse files Browse the repository at this point in the history
* Remove `BelowSubsistenceThreshold` and `NewContractNotFunded`

* Fix `clippy:single-match`

* Fix doc-link

* Fix `clippy:redundant-pattern-matching`

* Fix error codes

* Fix error codes

* Remove superfluous whitespace

* Clarity what minimum balance refers to

* Fix typo: chain's ➜ chains

* Revert "Fix typo: chain's ➜ chains"

This reverts commit 40c0920.
  • Loading branch information
cmichi authored Dec 10, 2021
1 parent a96cf87 commit 9ed2f48
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 57 deletions.
14 changes: 6 additions & 8 deletions crates/engine/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,21 @@ define_error_codes! {
CalleeReverted = 2,
/// The passed key does not exist in storage.
KeyNotFound = 3,
/// Transfer failed because it would have brought the sender's total balance
/// below the subsistence threshold.
BelowSubsistenceThreshold = 4,
/// Deprecated and no longer returned: There is only the minimum balance.
_BelowSubsistenceThreshold = 4,
/// Transfer failed for other not further specified reason. Most probably
/// reserved or locked balance of the sender that was preventing the transfer.
TransferFailed = 5,
/// The newly created contract is below the subsistence threshold after executing
/// its constructor so no usable contract instance will be created.
NewContractNotFunded = 6,
/// Deprecated and no longer returned: Endowment is no longer required.
_EndowmentTooLow = 6,
/// No code could be found at the supplied code hash.
CodeNotFound = 7,
/// The account that was called is no contract, but a plain account.
/// The account that was called is no contract.
NotCallable = 8,
/// The call to `seal_debug_message` had no effect because debug message
/// recording was disabled.
LoggingDisabled = 9,
/// ECDSA pubkey recovery failed. Most probably wrong recovery id or signature.
/// ECDSA public key recovery failed. Most probably wrong recovery id or signature.
EcdsaRecoveryFailed = 11,
}

Expand Down
8 changes: 5 additions & 3 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ where
})
}

/// Returns the minimum balance that is required for creating an account.
/// Returns the minimum balance that is required for creating an account
/// (i.e. the chain's existential deposit).
///
/// # Errors
///
Expand Down Expand Up @@ -324,9 +325,10 @@ where
///
/// # Errors
///
/// - If the contract does not have sufficient funds.
/// - If the contract does not have sufficient free funds.
/// - If the transfer had brought the sender's total balance below the
/// subsistence threshold.
/// minimum balance. You need to use [`terminate_contract`] in case
/// this is your intention.
pub fn transfer<T>(destination: T::AccountId, value: T::Balance) -> Result<()>
where
T: Environment,
Expand Down
3 changes: 2 additions & 1 deletion crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ pub trait TypedEnvBackend: EnvBackend {
/// For more details visit: [`block_number`][`crate::block_number`]
fn block_number<T: Environment>(&mut self) -> T::BlockNumber;

/// Returns the minimum balance that is required for creating an account.
/// Returns the minimum balance that is required for creating an account
/// (i.e. the chain's existential deposit).
///
/// # Note
///
Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/engine/experimental_off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ impl From<ext::Error> for crate::Error {
ext::Error::CalleeTrapped => Self::CalleeTrapped,
ext::Error::CalleeReverted => Self::CalleeReverted,
ext::Error::KeyNotFound => Self::KeyNotFound,
ext::Error::BelowSubsistenceThreshold => Self::BelowSubsistenceThreshold,
ext::Error::_BelowSubsistenceThreshold => Self::_BelowSubsistenceThreshold,
ext::Error::TransferFailed => Self::TransferFailed,
ext::Error::NewContractNotFunded => Self::NewContractNotFunded,
ext::Error::_EndowmentTooLow => Self::_EndowmentTooLow,
ext::Error::CodeNotFound => Self::CodeNotFound,
ext::Error::NotCallable => Self::NotCallable,
ext::Error::LoggingDisabled => Self::LoggingDisabled,
Expand Down
3 changes: 2 additions & 1 deletion crates/env/src/engine/off_chain/db/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ impl ChainSpec {
self.gas_price = OffBalance::new(&gas_price)
}

/// Returns the minimum balance that is required for creating an account.
/// Returns the minimum balance that is required for creating an account
/// (i.e. the chain's existential deposit).
pub fn minimum_balance<T>(&self) -> Result<T::Balance>
where
T: Environment,
Expand Down
12 changes: 5 additions & 7 deletions crates/env/src/engine/on_chain/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,21 @@ define_error_codes! {
CalleeReverted = 2,
/// The passed key does not exist in storage.
KeyNotFound = 3,
/// Transfer failed because it would have brought the sender's total balance
/// below the subsistence threshold.
BelowSubsistenceThreshold = 4,
/// Deprecated and no longer returned: There is only the minimum balance.
_BelowSubsistenceThreshold = 4,
/// Transfer failed for other not further specified reason. Most probably
/// reserved or locked balance of the sender that was preventing the transfer.
TransferFailed = 5,
/// The newly created contract is below the subsistence threshold after executing
/// its constructor so no usable contract instance will be created.
NewContractNotFunded = 6,
/// Deprecated and no longer returned: Endowment is no longer required.
_EndowmentTooLow = 6,
/// No code could be found at the supplied code hash.
CodeNotFound = 7,
/// The account that was called is no contract.
NotCallable = 8,
/// The call to `seal_debug_message` had no effect because debug message
/// recording was disabled.
LoggingDisabled = 9,
/// ECDSA pubkey recovery failed. Most probably wrong recovery id or signature.
/// ECDSA public key recovery failed. Most probably wrong recovery id or signature.
EcdsaRecoveryFailed = 11,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ impl From<ext::Error> for Error {
ext::Error::CalleeTrapped => Self::CalleeTrapped,
ext::Error::CalleeReverted => Self::CalleeReverted,
ext::Error::KeyNotFound => Self::KeyNotFound,
ext::Error::BelowSubsistenceThreshold => Self::BelowSubsistenceThreshold,
ext::Error::_BelowSubsistenceThreshold => Self::_BelowSubsistenceThreshold,
ext::Error::TransferFailed => Self::TransferFailed,
ext::Error::NewContractNotFunded => Self::NewContractNotFunded,
ext::Error::_EndowmentTooLow => Self::_EndowmentTooLow,
ext::Error::CodeNotFound => Self::CodeNotFound,
ext::Error::NotCallable => Self::NotCallable,
ext::Error::LoggingDisabled => Self::LoggingDisabled,
Expand Down
10 changes: 4 additions & 6 deletions crates/env/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ pub enum Error {
CalleeReverted,
/// The queried contract storage entry is missing.
KeyNotFound,
/// Transfer failed because it would have brought the sender's total balance
/// below the subsistence threshold.
BelowSubsistenceThreshold,
/// Deprecated and no longer returned: There is only the minimum balance.
_BelowSubsistenceThreshold,
/// Transfer failed for other not further specified reason. Most probably
/// reserved or locked balance of the sender that was preventing the transfer.
TransferFailed,
/// The newly created contract is below the subsistence threshold after executing
/// its constructor so no usable contract instance will be created.
NewContractNotFunded,
/// Deprecated and no longer returned: Endowment is no longer required.
_EndowmentTooLow,
/// No code could be found at the supplied code hash.
CodeNotFound,
/// The account that was called is no contract, but a plain account.
Expand Down
3 changes: 2 additions & 1 deletion crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ where
ink_env::block_number::<T>()
}

/// Returns the minimum balance that is required for creating an account.
/// Returns the minimum balance that is required for creating an account
/// (i.e. the chain's existential deposit).
///
/// # Example
///
Expand Down
35 changes: 9 additions & 26 deletions examples/contract-transfer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ pub mod give_me {
#[ink(storage)]
pub struct GiveMe {}

/// The error types.
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Error {
/// Returned if the transfer failed.
TransferFailed,
/// Insufficient funds to execute transfer.
InsufficientFunds,
/// Transfer failed because it would have brought the contract's
/// balance below the subsistence threshold. No transfer is
/// allowed to do this. Use `self.env().terminate()` to recover
/// the deposit.
BelowSubsistenceThreshold,
}

impl GiveMe {
/// Creates a new instance of this contract.
#[ink(constructor)]
Expand All @@ -39,8 +24,9 @@ pub mod give_me {
/// # Errors
///
/// - 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 requested transfer would have brought this
/// contract's balance below the minimum balance (i.e. the chain's
/// existential deposit).
/// - Panics in case the transfer failed for another reason.
#[ink(message)]
pub fn give_me(&mut self, value: Balance) {
Expand All @@ -49,15 +35,12 @@ pub mod give_me {

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(_) => {}
if self.env().transfer(self.env().caller(), value).is_err() {
panic!(
"requested transfer failed. this can be the case if the contract does not\
have sufficient free funds or if the transfer would have brought the\
contract's balance below minimum balance."
)
}
}

Expand Down

0 comments on commit 9ed2f48

Please sign in to comment.