Migrate generic-asset, identity and im-online to decl_error (#4473)

* Migrate generic-asset, identity and im-online to decl_error

* Update democracy tests

* Update nicks test
This commit is contained in:
Stanislav Tkach
2019-12-21 16:10:29 +02:00
committed by Shawn Tabrizi
parent f6cbf4421f
commit 2c2e0d772d
8 changed files with 153 additions and 77 deletions
+53 -19
View File
@@ -151,7 +151,7 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode, HasCompact, Input, Output, Error};
use codec::{Decode, Encode, HasCompact, Input, Output, Error as CodecError};
use sp_runtime::{RuntimeDebug, DispatchResult, DispatchError};
use sp_runtime::traits::{
@@ -162,7 +162,7 @@ use sp_runtime::traits::{
use sp_std::prelude::*;
use sp_std::{cmp, result, fmt::Debug};
use frame_support::{
decl_event, decl_module, decl_storage, ensure, dispatch,
decl_event, decl_module, decl_storage, ensure, dispatch, decl_error,
traits::{
Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, ReservableCurrency,
SignedImbalance, UpdateBalanceOutcome, WithdrawReason, WithdrawReasons, TryDrop,
@@ -285,7 +285,7 @@ impl<AccountId: Encode> Encode for PermissionVersions<AccountId> {
impl<AccountId: Encode> codec::EncodeLike for PermissionVersions<AccountId> {}
impl<AccountId: Decode> Decode for PermissionVersions<AccountId> {
fn decode<I: Input>(input: &mut I) -> core::result::Result<Self, Error> {
fn decode<I: Input>(input: &mut I) -> core::result::Result<Self, CodecError> {
let version = PermissionVersionNumber::decode(input)?;
Ok(
match version {
@@ -320,8 +320,42 @@ impl<AccountId> Into<PermissionVersions<AccountId>> for PermissionLatest<Account
}
}
decl_error! {
/// Error for the identity module.
pub enum Error for Module<T: Trait> {
/// No new assets id available.
NoIdAvailable,
/// Cannot transfer zero amount.
ZeroAmount,
/// The origin does not have enough permission to update permissions.
NoUpdatePermission,
/// The origin does not have permission to mint an asset.
NoMintPermission,
/// The origin does not have permission to burn an asset.
NoBurnPermission,
/// Total issuance got overflowed after minting.
TotalMintingOverflow,
/// Free balance got overflowed after minting.
FreeMintingOverflow,
/// Total issuance got underflowed after burning.
TotalBurningUnderflow,
/// Free balance got underflowed after burning.
FreeBurningUnderflow,
/// Asset id is already taken.
IdAlreadyTaken,
/// Asset id not available.
IdUnavailable,
/// The balance is too low to send amount.
InsufficientBalance,
/// The account liquidity restrictions prevent withdrawal.
LiquidityRestrictions,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
/// Create a new kind of asset.
@@ -332,7 +366,7 @@ decl_module! {
let permissions: PermissionVersions<T::AccountId> = options.permissions.clone().into();
// The last available id serves as the overflow mark and won't be used.
let next_id = id.checked_add(&One::one()).ok_or_else(|| "No new assets id available.")?;
let next_id = id.checked_add(&One::one()).ok_or_else(|| Error::<T>::NoIdAvailable)?;
<NextAssetId<T>>::put(next_id);
<TotalIssuance<T>>::insert(id, &options.initial_issuance);
@@ -347,7 +381,7 @@ decl_module! {
/// Transfer some liquid free balance to another account.
pub fn transfer(origin, #[compact] asset_id: T::AssetId, to: T::AccountId, #[compact] amount: T::Balance) {
let origin = ensure_signed(origin)?;
ensure!(!amount.is_zero(), "cannot transfer zero amount");
ensure!(!amount.is_zero(), Error::<T>::ZeroAmount);
Self::make_transfer_with_event(&asset_id, &origin, &to, amount)?;
}
@@ -370,7 +404,7 @@ decl_module! {
Ok(())
} else {
Err("Origin does not have enough permission to update permissions.")?
Err(Error::<T>::NoUpdatePermission)?
}
}
@@ -384,9 +418,9 @@ decl_module! {
let original_free_balance = Self::free_balance(&asset_id, &to);
let current_total_issuance = <TotalIssuance<T>>::get(asset_id);
let new_total_issuance = current_total_issuance.checked_add(&amount)
.ok_or_else(|| "total_issuance got overflow after minting.")?;
.ok_or(Error::<T>::TotalMintingOverflow)?;
let value = original_free_balance.checked_add(&amount)
.ok_or_else(|| "free balance got overflow after minting.")?;
.ok_or(Error::<T>::FreeMintingOverflow)?;
<TotalIssuance<T>>::insert(asset_id, new_total_issuance);
Self::set_free_balance(&asset_id, &to, value);
@@ -395,7 +429,7 @@ decl_module! {
Ok(())
} else {
Err("The origin does not have permission to mint an asset.")?
Err(Error::<T>::NoMintPermission)?
}
}
@@ -412,9 +446,9 @@ decl_module! {
let current_total_issuance = <TotalIssuance<T>>::get(asset_id);
let new_total_issuance = current_total_issuance.checked_sub(&amount)
.ok_or_else(|| "total_issuance got underflow after burning")?;
.ok_or(Error::<T>::TotalBurningUnderflow)?;
let value = original_free_balance.checked_sub(&amount)
.ok_or_else(|| "free_balance got underflow after burning")?;
.ok_or(Error::<T>::FreeBurningUnderflow)?;
<TotalIssuance<T>>::insert(asset_id, new_total_issuance);
@@ -424,7 +458,7 @@ decl_module! {
Ok(())
} else {
Err("The origin does not have permission to burn an asset.")?
Err(Error::<T>::NoBurnPermission)?
}
}
@@ -545,14 +579,14 @@ impl<T: Trait> Module<T> {
options: AssetOptions<T::Balance, T::AccountId>,
) -> dispatch::DispatchResult {
let asset_id = if let Some(asset_id) = asset_id {
ensure!(!<TotalIssuance<T>>::exists(&asset_id), "Asset id already taken.");
ensure!(asset_id < Self::next_asset_id(), "Asset id not available.");
ensure!(!<TotalIssuance<T>>::exists(&asset_id), Error::<T>::IdAlreadyTaken);
ensure!(asset_id < Self::next_asset_id(), Error::<T>::IdUnavailable);
asset_id
} else {
let asset_id = Self::next_asset_id();
let next_id = asset_id
.checked_add(&One::one())
.ok_or_else(|| "No new user asset id available.")?;
.ok_or(Error::<T>::NoIdAvailable)?;
<NextAssetId<T>>::put(next_id);
asset_id
};
@@ -579,7 +613,7 @@ impl<T: Trait> Module<T> {
) -> dispatch::DispatchResult {
let new_balance = Self::free_balance(asset_id, from)
.checked_sub(&amount)
.ok_or_else(|| "balance too low to send amount")?;
.ok_or(Error::<T>::InsufficientBalance)?;
Self::ensure_can_withdraw(asset_id, from, amount, WithdrawReason::Transfer.into(), new_balance)?;
if from != to {
@@ -618,7 +652,7 @@ impl<T: Trait> Module<T> {
let original_reserve_balance = Self::reserved_balance(asset_id, who);
let original_free_balance = Self::free_balance(asset_id, who);
if original_free_balance < amount {
Err("not enough free funds")?
Err(Error::<T>::InsufficientBalance)?
}
let new_reserve_balance = original_reserve_balance + amount;
Self::set_reserved_balance(asset_id, who, new_reserve_balance);
@@ -767,7 +801,7 @@ impl<T: Trait> Module<T> {
{
Ok(())
} else {
Err("account liquidity restrictions prevent withdrawal")?
Err(Error::<T>::LiquidityRestrictions)?
}
}
@@ -1155,7 +1189,7 @@ where
) -> result::Result<Self::NegativeImbalance, DispatchError> {
let new_balance = Self::free_balance(who)
.checked_sub(&value)
.ok_or_else(|| "account has too few funds")?;
.ok_or(Error::<T>::InsufficientBalance)?;
Self::ensure_can_withdraw(who, value, reasons, new_balance)?;
<Module<T>>::set_free_balance(&U::asset_id(), who, new_balance);
Ok(NegativeImbalance::new(value))