diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e5806c9561..d2082b820c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -200,6 +200,7 @@ See [the compatibility section](https://use.ink/faq/migrating-from-ink-4-to-5/#c - Split up `ink_linting` to mandatory and extra libraries - [#2032](https://github.com/use-ink/ink/pull/2032) - [E2E] resolve DispatchError error details for dry-runs - [#1994](https://github.com/use-ink/ink/pull/1994) - [E2E] update to new `drink` API - [#2005](https://github.com/use-ink/ink/pull/2005) +- [E2E] `set_account_balance` now can't set balance below existential deposit - [#1983](https://github.com/paritytech/ink/pull/1983) ## Version 5.0.0-alpha diff --git a/crates/env/src/engine/off_chain/test_api.rs b/crates/env/src/engine/off_chain/test_api.rs index f99a353e712..5c1e00bfeb2 100644 --- a/crates/env/src/engine/off_chain/test_api.rs +++ b/crates/env/src/engine/off_chain/test_api.rs @@ -27,7 +27,10 @@ use ink_engine::test_api::RecordedDebugMessages; use std::panic::UnwindSafe; pub use super::call_data::CallData; -pub use ink_engine::ChainExtension; +pub use ink_engine::{ + ext::ChainSpec, + ChainExtension, +}; /// Record for an emitted event. #[derive(Clone)] @@ -45,15 +48,27 @@ pub struct EmittedEvent { /// Note that account could refer to either a user account or /// a smart contract account. /// +/// If a 0 balance is set, this would not fail. This is useful for +/// reaping an account. +/// /// # Errors /// /// - If `account` does not exist. /// - If the underlying `account` type does not match. /// - If the underlying `new_balance` type does not match. +/// - If the `new_balance` is less than the existential minimum. pub fn set_account_balance(account_id: T::AccountId, new_balance: T::Balance) where T: Environment, // Just temporary for the MVP! { + let min = ChainSpec::default().minimum_balance; + if new_balance < min && new_balance != 0u128 { + panic!( + "Balance must be at least [{}]. Use 0 as balance to reap the account.", + min + ); + } + ::on_instance(|instance| { instance .engine diff --git a/crates/env/src/engine/off_chain/tests.rs b/crates/env/src/engine/off_chain/tests.rs index 6cd2e44f7dd..9b9848576cc 100644 --- a/crates/env/src/engine/off_chain/tests.rs +++ b/crates/env/src/engine/off_chain/tests.rs @@ -13,8 +13,13 @@ // limitations under the License. use crate::{ - engine::off_chain::impls::TopicsBuilder, + engine::off_chain::{ + impls::TopicsBuilder, + test_api::set_account_balance, + }, event::TopicsBuilderBackend, + types::Environment, + DefaultEnvironment, Result, }; @@ -41,3 +46,32 @@ fn topics_builder() -> Result<()> { Ok(()) }) } +#[test] +fn test_set_account_balance() -> Result<()> { + pub use ink_engine::ext::ChainSpec; + + crate::test::run_test::(|_| { + let minimum_balance = ChainSpec::default().minimum_balance; + + let result = std::panic::catch_unwind(|| { + set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(minimum_balance - 1), + ) + }); + + assert!(result.is_err()); + + set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(0u128), + ); + + set_account_balance::( + ::AccountId::from([0x1; 32]), + ::Balance::from(minimum_balance + 1), + ); + + Ok(()) + }) +}