diff --git a/pallets/assets/src/lib.rs b/pallets/assets/src/lib.rs index 7ecd5db031bd0..f95679738e160 100644 --- a/pallets/assets/src/lib.rs +++ b/pallets/assets/src/lib.rs @@ -247,6 +247,8 @@ decl_error! { BalanceLow, /// Balance should be non-zero BalanceZero, + /// Overflow + Overflow, } } @@ -303,17 +305,21 @@ impl Module { id } - pub fn assets_mint(id: &T::AssetId, to: &T::AccountId, amount: &T::Balance) -> T::Balance { + pub fn assets_mint(id: &T::AssetId, to: &T::AccountId, amount: &T::Balance) -> DispatchResult { + let origin_balance = >::get((id, to)); >::mutate((id, to), |balance| *balance += *amount); >::mutate((id), |total| *total += *amount); - >::get((id, to)) + let final_balance = >::get((id, to)); + ensure!(final_balance >= origin_balance, Error::::Overflow); + Ok(()) } - pub fn assets_burn(id: &T::AssetId, to: &T::AccountId, amount: &T::Balance) -> T::Balance { - //TODO ensure amount - >::mutate((id, to), |balance| *balance -= *amount); + pub fn assets_burn(id: &T::AssetId, from: &T::AccountId, amount: &T::Balance) -> DispatchResult { + let origin_balance = >::get((id, from)); + ensure!(origin_balance >= *amount, Error::::BalanceLow); + >::mutate((id, from), |balance| *balance -= *amount); >::mutate((id), |total| *total -= *amount); - >::get((id, to)) + Ok(()) } } diff --git a/pallets/erc20-app/src/lib.rs b/pallets/erc20-app/src/lib.rs index b2171752053df..3cb8081c0f1cb 100644 --- a/pallets/erc20-app/src/lib.rs +++ b/pallets/erc20-app/src/lib.rs @@ -24,7 +24,7 @@ use sp_core::{H160, U256}; use frame_system::{self as system, ensure_signed}; use frame_support::{ decl_error, decl_event, decl_module, decl_storage, - dispatch::DispatchResult, + dispatch::DispatchResult, ensure, }; use artemis_core::{Application, BridgedAssetId}; @@ -66,6 +66,8 @@ decl_error! { InvalidAssetId, /// The submitted payload could not be decoded. InvalidPayload, + /// Asset could not be burned + BurnFailure, } } @@ -89,8 +91,9 @@ decl_module! { // >::do_burn(asset_id, &who, amount)?; let native_asset_id = >::get_native_asset_id(asset_id); - >::assets_burn(&native_asset_id, &who, &amount.low_u128().saturated_into::()); + let result = >::assets_burn(&native_asset_id, &who, &amount.low_u128().saturated_into::()); + ensure!(result.is_ok(), Error::::BurnFailure); Self::deposit_event(RawEvent::Transfer(asset_id, who.clone(), recipient, amount)); Ok(()) } diff --git a/pallets/eth-app/src/lib.rs b/pallets/eth-app/src/lib.rs index 9c871619ddbd7..bd45e7ca70652 100644 --- a/pallets/eth-app/src/lib.rs +++ b/pallets/eth-app/src/lib.rs @@ -21,7 +21,7 @@ use frame_system::{self as system, ensure_signed}; use frame_support::{ decl_error, decl_event, decl_module, decl_storage, - dispatch::DispatchResult, + dispatch::DispatchResult, ensure, }; use sp_std::prelude::*; use sp_core::{H160, U256}; @@ -66,6 +66,8 @@ decl_error! { pub enum Error for Module { /// The submitted payload could not be decoded. InvalidPayload, + /// Asset could not be burned + BurnFailure, } } @@ -86,8 +88,9 @@ decl_module! { // >::do_burn(asset_id, &who, amount)?; let asset_id = >::get_native_asset_id(asset_id); - >::assets_burn(&asset_id, &who, &amount.low_u128().saturated_into::()); + let result = >::assets_burn(&asset_id, &who, &amount.low_u128().saturated_into::()); + ensure!(result.is_ok(), Error::::BurnFailure); Self::deposit_event(RawEvent::Transfer(who.clone(), recipient, amount)); Ok(()) }