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

Set balance below existential #1983

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Messages return `TypeSpec` directly - #[1999](https://github.com/paritytech/ink/pull/1999)
- Fail when decoding from storage and not all bytes consumed - [#1897](https://github.com/paritytech/ink/pull/1897)
- [E2E] resolve DispatchError error details for dry-runs - [#1944](https://github.com/paritytech/ink/pull/1994)
- [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
Expand Down
10 changes: 10 additions & 0 deletions crates/env/src/engine/off_chain/test_api.rs
Expand Up @@ -27,6 +27,7 @@ use ink_engine::test_api::RecordedDebugMessages;
use std::panic::UnwindSafe;

pub use super::call_data::CallData;
pub use ink_engine::ext::ChainSpec;
pub use ink_engine::ChainExtension;

/// Record for an emitted event.
Expand All @@ -45,15 +46,24 @@ 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<T>(account_id: T::AccountId, new_balance: T::Balance)
where
T: Environment<Balance = u128>, // Just temporary for the MVP!
{
let min = ChainSpec::default().minimum_balance;
if new_balance < T::Balance::from(min) && new_balance != 0u128.into(){
panic!("Balance must be at least [{}]. Use 0 as balance to reap the account.", min);
}

<EnvInstance as OnInstance>::on_instance(|instance| {
instance
.engine
Expand Down
36 changes: 31 additions & 5 deletions crates/env/src/engine/off_chain/tests.rs
Expand Up @@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{
engine::off_chain::impls::TopicsBuilder,
event::TopicsBuilderBackend,
Result,
};
use crate::{engine::off_chain::impls::TopicsBuilder,
event::TopicsBuilderBackend,
Result,
engine::off_chain::test_api::set_account_balance,
types::Environment,
DefaultEnvironment};

#[test]
fn topics_builder() -> Result<()> {
Expand All @@ -41,3 +42,28 @@ fn topics_builder() -> Result<()> {
Ok(())
})
}
#[test]
fn test_set_account_balance() -> Result<()> {
pub use ink_engine::ext::ChainSpec;

crate::test::run_test::<DefaultEnvironment, _>(|_| {

let minimum_balance = ChainSpec::default().minimum_balance;

let result = std::panic::catch_unwind(|| {set_account_balance::<DefaultEnvironment>(
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
<DefaultEnvironment as Environment>::Balance::from(minimum_balance - 1))});

assert!(result.is_err());

set_account_balance::<DefaultEnvironment>(
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
<DefaultEnvironment as Environment>::Balance::from(0u128));

set_account_balance::<DefaultEnvironment>(
<DefaultEnvironment as Environment>::AccountId::from([0x1; 32]),
<DefaultEnvironment as Environment>::Balance::from(minimum_balance + 1));

Ok(())
})
}