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))
+10 -12
View File
@@ -65,7 +65,7 @@ fn issuing_with_next_asset_id_overflow_should_not_work() {
permissions: default_permission
}
),
"No new assets id available."
Error::<Test>::NoIdAvailable
);
assert_eq!(GenericAsset::next_asset_id(), u32::max_value());
});
@@ -173,7 +173,7 @@ fn transferring_amount_should_fail_when_transferring_more_than_free_balance() {
));
assert_noop!(
GenericAsset::transfer(Origin::signed(1), asset_id, 2, 2000),
"balance too low to send amount"
Error::<Test>::InsufficientBalance
);
});
}
@@ -198,7 +198,7 @@ fn transferring_less_than_one_unit_should_not_work() {
assert_eq!(GenericAsset::free_balance(&asset_id, &1), 100);
assert_noop!(
GenericAsset::transfer(Origin::signed(1), asset_id, 2, 0),
"cannot transfer zero amount"
Error::<Test>::ZeroAmount
);
});
}
@@ -256,7 +256,7 @@ fn transferring_more_units_than_total_supply_should_not_work() {
assert_eq!(GenericAsset::free_balance(&asset_id, &1), 100);
assert_noop!(
GenericAsset::transfer(Origin::signed(1), asset_id, 2, 101),
"balance too low to send amount"
Error::<Test>::InsufficientBalance
);
});
}
@@ -424,7 +424,7 @@ fn reserve_should_moves_amount_from_balance_to_reserved_balance() {
#[test]
fn reserve_should_not_moves_amount_from_balance_to_reserved_balance() {
ExtBuilder::default().free_balance((1, 0, 100)).build().execute_with(|| {
assert_noop!(GenericAsset::reserve(&1, &0, 120), "not enough free funds");
assert_noop!(GenericAsset::reserve(&1, &0, 120), Error::<Test>::InsufficientBalance);
assert_eq!(GenericAsset::free_balance(&1, &0), 100);
assert_eq!(GenericAsset::reserved_balance(&1, &0), 0);
});
@@ -626,7 +626,7 @@ fn mint_should_throw_permission_error() {
assert_noop!(
GenericAsset::mint(Origin::signed(origin), asset_id, to_account, amount),
"The origin does not have permission to mint an asset."
Error::<Test>::NoMintPermission,
);
});
}
@@ -687,7 +687,7 @@ fn burn_should_throw_permission_error() {
assert_noop!(
GenericAsset::burn(Origin::signed(origin), asset_id, to_account, amount),
"The origin does not have permission to burn an asset."
Error::<Test>::NoBurnPermission,
);
});
}
@@ -873,8 +873,6 @@ fn update_permission_should_throw_error_when_lack_of_permissions() {
burn: Owner::None,
};
let expected_error_message = "Origin does not have enough permission to update permissions.";
assert_ok!(GenericAsset::create(
Origin::signed(origin),
AssetOptions {
@@ -885,7 +883,7 @@ fn update_permission_should_throw_error_when_lack_of_permissions() {
assert_noop!(
GenericAsset::update_permission(Origin::signed(origin), asset_id, new_permission),
expected_error_message,
Error::<Test>::NoUpdatePermission,
);
});
}
@@ -963,7 +961,7 @@ fn create_asset_with_non_reserved_asset_id_should_not_work() {
permissions: default_permission.clone()
}
),
"Asset id not available."
Error::<Test>::IdUnavailable,
);
});
}
@@ -1005,7 +1003,7 @@ fn create_asset_with_a_taken_asset_id_should_not_work() {
permissions: default_permission.clone()
}
),
"Asset id already taken."
Error::<Test>::IdAlreadyTaken,
);
});
}