Skip to content

Commit

Permalink
Merge pull request #4 from mangata-finance/fix/underflow
Browse files Browse the repository at this point in the history
asset underflow fixed
  • Loading branch information
gleb-urvanov authored Dec 8, 2020
2 parents c5e89e0 + 6761e74 commit e5911f2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
18 changes: 12 additions & 6 deletions pallets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ decl_error! {
BalanceLow,
/// Balance should be non-zero
BalanceZero,
/// Overflow
Overflow,
}
}

Expand Down Expand Up @@ -303,17 +305,21 @@ impl<T: Trait> Module<T> {
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 = <Balances<T>>::get((id, to));
<Balances<T>>::mutate((id, to), |balance| *balance += *amount);
<TotalSupply<T>>::mutate((id), |total| *total += *amount);
<Balances<T>>::get((id, to))
let final_balance = <Balances<T>>::get((id, to));
ensure!(final_balance >= origin_balance, Error::<T>::Overflow);
Ok(())
}

pub fn assets_burn(id: &T::AssetId, to: &T::AccountId, amount: &T::Balance) -> T::Balance {
//TODO ensure amount
<Balances<T>>::mutate((id, to), |balance| *balance -= *amount);
pub fn assets_burn(id: &T::AssetId, from: &T::AccountId, amount: &T::Balance) -> DispatchResult {
let origin_balance = <Balances<T>>::get((id, from));
ensure!(origin_balance >= *amount, Error::<T>::BalanceLow);
<Balances<T>>::mutate((id, from), |balance| *balance -= *amount);
<TotalSupply<T>>::mutate((id), |total| *total -= *amount);
<Balances<T>>::get((id, to))
Ok(())
}
}

Expand Down
7 changes: 5 additions & 2 deletions pallets/erc20-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -66,6 +66,8 @@ decl_error! {
InvalidAssetId,
/// The submitted payload could not be decoded.
InvalidPayload,
/// Asset could not be burned
BurnFailure,
}
}

Expand All @@ -89,8 +91,9 @@ decl_module! {

// <asset::Module<T>>::do_burn(asset_id, &who, amount)?;
let native_asset_id = <asset::Module<T>>::get_native_asset_id(asset_id);
<assets::Module<T>>::assets_burn(&native_asset_id, &who, &amount.low_u128().saturated_into::<T::Balance>());
let result = <assets::Module<T>>::assets_burn(&native_asset_id, &who, &amount.low_u128().saturated_into::<T::Balance>());

ensure!(result.is_ok(), Error::<T>::BurnFailure);
Self::deposit_event(RawEvent::Transfer(asset_id, who.clone(), recipient, amount));
Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions pallets/eth-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -66,6 +66,8 @@ decl_error! {
pub enum Error for Module<T: Trait> {
/// The submitted payload could not be decoded.
InvalidPayload,
/// Asset could not be burned
BurnFailure,
}
}

Expand All @@ -86,8 +88,9 @@ decl_module! {

// <asset::Module<T>>::do_burn(asset_id, &who, amount)?;
let asset_id = <asset::Module<T>>::get_native_asset_id(asset_id);
<assets::Module<T>>::assets_burn(&asset_id, &who, &amount.low_u128().saturated_into::<T::Balance>());
let result = <assets::Module<T>>::assets_burn(&asset_id, &who, &amount.low_u128().saturated_into::<T::Balance>());

ensure!(result.is_ok(), Error::<T>::BurnFailure);
Self::deposit_event(RawEvent::Transfer(who.clone(), recipient, amount));
Ok(())
}
Expand Down

0 comments on commit e5911f2

Please sign in to comment.