mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-09 19:57:59 +00:00
More decl_error! migrations (#4427)
* Update assets to `decl_error` * Update aura to `decl_error` * Update authority discovery to `decl_error` * Update collective to `decl_error` * Update evm to `decl_error!` * Fix error with replace * Revert "Update authority discovery to `decl_error`" This reverts commit 26e8f3c56656963d847e984c6f2c8e1f88014899. * Revert "Update aura to `decl_error`" This reverts commit 8f17c44ca8375a4a755710aaab7ad4d9522c4376. * Update democracy to `decl_error` * Update finality-tracker to `decl_error` * Update grandpa to `decl_error` * `assert` to `ensure` in dispatchable function
This commit is contained in:
committed by
Gavin Wood
parent
9550c845f3
commit
57c19bcf4e
@@ -84,7 +84,8 @@
|
||||
//! ### Simple Code Snippet
|
||||
//!
|
||||
//! ```rust,ignore
|
||||
//! use frame_support::{decl_module, dispatch};
|
||||
//! use pallet_assets as assets;
|
||||
//! use frame_support::{decl_module, dispatch, ensure};
|
||||
//! use frame_system::{self as system, ensure_signed};
|
||||
//!
|
||||
//! pub trait Trait: assets::Trait { }
|
||||
@@ -92,14 +93,15 @@
|
||||
//! decl_module! {
|
||||
//! pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
//! pub fn issue_token_airdrop(origin) -> dispatch::Result {
|
||||
//! let sender = ensure_signed(origin).map_err(|e| e.as_str())?;
|
||||
//!
|
||||
//! const ACCOUNT_ALICE: u64 = 1;
|
||||
//! const ACCOUNT_BOB: u64 = 2;
|
||||
//! const COUNT_AIRDROP_RECIPIENTS = 2;
|
||||
//! const COUNT_AIRDROP_RECIPIENTS: u64 = 2;
|
||||
//! const TOKENS_FIXED_SUPPLY: u64 = 100;
|
||||
//!
|
||||
//! ensure!(!COUNT_AIRDROP_RECIPIENTS.is_zero(), "Divide by zero error.");
|
||||
//!
|
||||
//! let sender = ensure_signed(origin)?;
|
||||
//! let asset_id = Self::next_asset_id();
|
||||
//!
|
||||
//! <NextAssetId<T>>::mutate(|asset_id| *asset_id += 1);
|
||||
@@ -130,8 +132,8 @@
|
||||
// Ensure we're `no_std` when compiling for Wasm.
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
use frame_support::{Parameter, decl_module, decl_event, decl_storage, ensure};
|
||||
use sp_runtime::traits::{Member, SimpleArithmetic, Zero, StaticLookup};
|
||||
use frame_support::{Parameter, decl_module, decl_event, decl_storage, decl_error, ensure};
|
||||
use sp_runtime::traits::{Member, SimpleArithmetic, Zero, StaticLookup, ModuleDispatchError};
|
||||
use frame_system::{self as system, ensure_signed};
|
||||
use sp_runtime::traits::One;
|
||||
|
||||
@@ -149,12 +151,14 @@ pub trait Trait: frame_system::Trait {
|
||||
|
||||
decl_module! {
|
||||
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
|
||||
type Error = Error;
|
||||
|
||||
fn deposit_event() = default;
|
||||
/// Issue a new class of fungible assets. There are, and will only ever be, `total`
|
||||
/// such assets and they'll all belong to the `origin` initially. It will have an
|
||||
/// identifier `AssetId` instance: this will be specified in the `Issued` event.
|
||||
fn issue(origin, #[compact] total: T::Balance) {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let origin = ensure_signed(origin).map_err(|e| e.as_str())?;
|
||||
|
||||
let id = Self::next_asset_id();
|
||||
<NextAssetId<T>>::mutate(|id| *id += One::one());
|
||||
@@ -171,12 +175,12 @@ decl_module! {
|
||||
target: <T::Lookup as StaticLookup>::Source,
|
||||
#[compact] amount: T::Balance
|
||||
) {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let origin = ensure_signed(origin).map_err(|e| e.as_str())?;
|
||||
let origin_account = (id, origin.clone());
|
||||
let origin_balance = <Balances<T>>::get(&origin_account);
|
||||
let target = T::Lookup::lookup(target)?;
|
||||
ensure!(!amount.is_zero(), "transfer amount should be non-zero");
|
||||
ensure!(origin_balance >= amount, "origin account balance must be greater than or equal to the transfer amount");
|
||||
ensure!(!amount.is_zero(), Error::AmountZero);
|
||||
ensure!(origin_balance >= amount, Error::BalanceLow);
|
||||
|
||||
Self::deposit_event(RawEvent::Transferred(id, origin, target.clone(), amount));
|
||||
<Balances<T>>::insert(origin_account, origin_balance - amount);
|
||||
@@ -185,9 +189,9 @@ decl_module! {
|
||||
|
||||
/// Destroy any assets of `id` owned by `origin`.
|
||||
fn destroy(origin, #[compact] id: T::AssetId) {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let origin = ensure_signed(origin).map_err(|e| e.as_str())?;
|
||||
let balance = <Balances<T>>::take((id, &origin));
|
||||
ensure!(!balance.is_zero(), "origin balance should be non-zero");
|
||||
ensure!(!balance.is_zero(), Error::BalanceZero);
|
||||
|
||||
<TotalSupply<T>>::mutate(id, |total_supply| *total_supply -= balance);
|
||||
Self::deposit_event(RawEvent::Destroyed(id, origin, balance));
|
||||
@@ -195,7 +199,7 @@ decl_module! {
|
||||
}
|
||||
}
|
||||
|
||||
decl_event!(
|
||||
decl_event! {
|
||||
pub enum Event<T> where
|
||||
<T as frame_system::Trait>::AccountId,
|
||||
<T as Trait>::Balance,
|
||||
@@ -208,7 +212,19 @@ decl_event!(
|
||||
/// Some assets were destroyed.
|
||||
Destroyed(AssetId, AccountId, Balance),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
decl_error! {
|
||||
pub enum Error {
|
||||
/// Transfer amount should be non-zero
|
||||
AmountZero,
|
||||
/// Account balance must be greater than or equal to the transfer amount
|
||||
BalanceLow,
|
||||
/// Balance should be non-zero
|
||||
BalanceZero,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
decl_storage! {
|
||||
trait Store for Module<T: Trait> as Assets {
|
||||
@@ -337,7 +353,7 @@ mod tests {
|
||||
assert_eq!(Assets::balance(0, 2), 50);
|
||||
assert_ok!(Assets::destroy(Origin::signed(1), 0));
|
||||
assert_eq!(Assets::balance(0, 1), 0);
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 1, 50), "origin account balance must be greater than or equal to the transfer amount");
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 1, 50), Error::BalanceLow);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -346,7 +362,7 @@ mod tests {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Assets::issue(Origin::signed(1), 100));
|
||||
assert_eq!(Assets::balance(0, 1), 100);
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 0), "transfer amount should be non-zero");
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 0), Error::AmountZero);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -355,7 +371,7 @@ mod tests {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Assets::issue(Origin::signed(1), 100));
|
||||
assert_eq!(Assets::balance(0, 1), 100);
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 101), "origin account balance must be greater than or equal to the transfer amount");
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 101), Error::BalanceLow);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -373,7 +389,7 @@ mod tests {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Assets::issue(Origin::signed(1), 100));
|
||||
assert_eq!(Assets::balance(0, 2), 0);
|
||||
assert_noop!(Assets::destroy(Origin::signed(2), 0), "origin balance should be non-zero");
|
||||
assert_noop!(Assets::destroy(Origin::signed(2), 0), Error::BalanceZero);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user