Add arithmetic dispatch errors. (#8726)

* Add arithmetic dispatch errors.

* Replace custom overflow errors.

* Replace custom underflow and division by zero errors.

* Replace overflow/underflow in token error.

* Add token and arithmetic errors in dispatch error equality test.

* Trigger CI.
This commit is contained in:
Shaun Wang
2021-05-10 20:14:02 +12:00
committed by GitHub
parent 655ebc95fb
commit 2a38b23062
12 changed files with 88 additions and 60 deletions
+2 -1
View File
@@ -71,6 +71,7 @@ Import the Assets module and types and derive your runtime's configuration trait
use pallet_assets as assets;
use frame_support::{decl_module, dispatch, ensure};
use frame_system::ensure_signed;
use sp_runtime::ArithmeticError;
pub trait Config: assets::Config { }
@@ -84,7 +85,7 @@ decl_module! {
const COUNT_AIRDROP_RECIPIENTS: u64 = 2;
const TOKENS_FIXED_SUPPLY: u64 = 100;
ensure!(!COUNT_AIRDROP_RECIPIENTS.is_zero(), "Divide by zero error.");
ensure!(!COUNT_AIRDROP_RECIPIENTS.is_zero(), ArithmeticError::DivisionByZero);
let asset_id = Self::next_asset_id();
+3 -3
View File
@@ -47,7 +47,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
who: &T::AccountId,
d: &mut AssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T, I>>,
) -> Result<bool, DispatchError> {
let accounts = d.accounts.checked_add(1).ok_or(Error::<T, I>::Overflow)?;
let accounts = d.accounts.checked_add(1).ok_or(ArithmeticError::Overflow)?;
let is_sufficient = if d.is_sufficient {
frame_system::Pallet::<T>::inc_sufficients(who);
d.sufficients += 1;
@@ -162,7 +162,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
id: T::AssetId,
who: &T::AccountId,
keep_alive: bool,
) -> Result<T::Balance, Error<T, I>> {
) -> Result<T::Balance, DispatchError> {
let details = Asset::<T, I>::get(id).ok_or_else(|| Error::<T, I>::Unknown)?;
ensure!(!details.is_frozen, Error::<T, I>::Frozen);
@@ -173,7 +173,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
// Frozen balance: account CANNOT be deleted
let required = frozen
.checked_add(&details.min_balance)
.ok_or(Error::<T, I>::Overflow)?;
.ok_or(ArithmeticError::Overflow)?;
account.balance.saturating_sub(required)
} else {
let is_provider = false;
+1 -3
View File
@@ -140,7 +140,7 @@ pub use types::*;
use sp_std::{prelude::*, borrow::Borrow};
use sp_runtime::{
RuntimeDebug, TokenError, traits::{
RuntimeDebug, TokenError, ArithmeticError, traits::{
AtLeast32BitUnsigned, Zero, StaticLookup, Saturating, CheckedSub, CheckedAdd, Bounded,
StoredMapError,
}
@@ -326,8 +326,6 @@ pub mod pallet {
BadWitness,
/// Minimum balance should be non-zero.
MinBalanceZero,
/// A mint operation lead to an overflow.
Overflow,
/// No provider reference exists to allow a non-zero balance of a non-self-sufficient asset.
NoProvider,
/// Invalid metadata given.