Skip to content

Commit

Permalink
Error type for MultiCurrency trait and tokens module (#7)
Browse files Browse the repository at this point in the history
* Error type for MultiCurrency trait and tokens module.

* fmt

* Update error type.

* Update transfer unit test.
  • Loading branch information
shaunxw committed Nov 6, 2019
1 parent 47371ab commit 3d0a8a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
28 changes: 17 additions & 11 deletions tokens/src/lib.rs
Expand Up @@ -4,7 +4,7 @@ use rstd::result;
use sr_primitives::traits::{
CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member, SimpleArithmetic, StaticLookup,
};
use srml_support::{decl_event, decl_module, decl_storage, ensure, Parameter};
use srml_support::{decl_error, decl_event, decl_module, decl_storage, ensure, Parameter};
// FIXME: `srml-` prefix should be used for all srml modules, but currently `srml_system`
// would cause compiling error in `decl_module!` and `construct_runtime!`
// #3295 https://github.com/paritytech/substrate/issues/3295
Expand All @@ -23,7 +23,7 @@ pub trait Trait: srml_system::Trait {

decl_storage! {
trait Store for Module<T: Trait> as Tokens {
/// The total issuance of a token type;
/// The total issuance of a token type.
pub TotalIssuance get(fn total_issuance) build(|config: &GenesisConfig<T>| {
let issuance = config.initial_balance * (config.endowed_accounts.len() as u32).into();
config.tokens.iter().map(|id| (id.clone(), issuance)).collect::<Vec<_>>()
Expand Down Expand Up @@ -78,11 +78,20 @@ decl_module! {
}
}

decl_error! {
/// Error for token module.
pub enum Error {
BalanceTooLow,
TotalIssuanceOverflow,
}
}

impl<T: Trait> Module<T> {}

impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
type Balance = T::Balance;
type CurrencyId = T::CurrencyId;
type Error = Error;

fn total_inssuance(currency_id: Self::CurrencyId) -> Self::Balance {
<TotalIssuance<T>>::get(currency_id)
Expand All @@ -97,11 +106,8 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
from: &T::AccountId,
to: &T::AccountId,
amount: Self::Balance,
) -> result::Result<(), &'static str> {
ensure!(
Self::balance(currency_id, from) >= amount,
"balance too low to transfer",
);
) -> result::Result<(), Self::Error> {
ensure!(Self::balance(currency_id, from) >= amount, Error::BalanceTooLow);

if from != to {
<Balance<T>>::mutate(currency_id, from, |balance| *balance -= amount);
Expand All @@ -115,10 +121,10 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
currency_id: Self::CurrencyId,
who: &T::AccountId,
amount: Self::Balance,
) -> result::Result<(), &'static str> {
) -> result::Result<(), Self::Error> {
ensure!(
Self::total_inssuance(currency_id).checked_add(&amount).is_some(),
"total issuance overflow after deposit",
Error::TotalIssuanceOverflow,
);

<TotalIssuance<T>>::mutate(currency_id, |v| *v += amount);
Expand All @@ -131,10 +137,10 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
currency_id: Self::CurrencyId,
who: &T::AccountId,
amount: Self::Balance,
) -> result::Result<(), &'static str> {
) -> result::Result<(), Self::Error> {
ensure!(
Self::balance(currency_id, who).checked_sub(&amount).is_some(),
"balance too low to withdraw",
Error::BalanceTooLow,
);

<TotalIssuance<T>>::mutate(currency_id, |v| *v -= amount);
Expand Down
9 changes: 3 additions & 6 deletions tokens/src/tests.rs
Expand Up @@ -34,7 +34,7 @@ fn transfer_should_work() {

assert_noop!(
Tokens::transfer(Some(ALICE).into(), BOB, TEST_TOKEN_ID, 60),
"balance too low to transfer",
Error::BalanceTooLow.into(),
);
});
}
Expand All @@ -51,7 +51,7 @@ fn deposit_should_work() {

assert_noop!(
Tokens::deposit(TEST_TOKEN_ID, &ALICE, Balance::max_value()),
"total issuance overflow after deposit",
Error::TotalIssuanceOverflow,
);
});
}
Expand All @@ -66,9 +66,6 @@ fn withdraw_should_work() {
assert_eq!(Tokens::balance(TEST_TOKEN_ID, &ALICE), 50);
assert_eq!(Tokens::total_issuance(TEST_TOKEN_ID), 150);

assert_noop!(
Tokens::withdraw(TEST_TOKEN_ID, &ALICE, 60),
"balance too low to withdraw",
);
assert_noop!(Tokens::withdraw(TEST_TOKEN_ID, &ALICE, 60), Error::BalanceTooLow);
});
}
9 changes: 6 additions & 3 deletions traits/src/lib.rs
Expand Up @@ -12,6 +12,9 @@ pub trait MultiCurrency<AccountId> {
/// The balance of an account.
type Balance: SimpleArithmetic + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default;

/// The error type.
type Error: Into<&'static str>;

// Public immutables

/// The total amount of issuance of `currency_id`.
Expand All @@ -28,21 +31,21 @@ pub trait MultiCurrency<AccountId> {
from: &AccountId,
to: &AccountId,
amount: Self::Balance,
) -> result::Result<(), &'static str>;
) -> result::Result<(), Self::Error>;

/// Add `amount` to the balance of `who` under `currency_id` and increase total issuance.
fn deposit(
currency_id: Self::CurrencyId,
who: &AccountId,
amount: Self::Balance,
) -> result::Result<(), &'static str>;
) -> result::Result<(), Self::Error>;

/// Remove `amount` from the balance of `who` under `currency_id` and recude total issuance.
fn withdraw(
currency_id: Self::CurrencyId,
who: &AccountId,
amount: Self::Balance,
) -> result::Result<(), &'static str>;
) -> result::Result<(), Self::Error>;

/// Deduct the balance of `who` by up to `amount`.
///
Expand Down

0 comments on commit 3d0a8a2

Please sign in to comment.