1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Faucet module.
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::dispatch::DispatchResult;
use frame_support::traits::Currency;
use frame_support::unsigned::{TransactionSource, TransactionValidity, ValidateUnsigned};
use frame_support::{decl_event, decl_module};
use frame_system::{self as system, ensure_none, Trait as System};
use pallet_balances::{self as balances, Trait as Balances};
use sp_runtime::transaction_validity::ValidTransaction;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

/// The pallet's configuration trait.
pub trait Trait: Balances {
    const MINT_UNIT: Self::Balance;
    type Event: From<Event<Self>> + Into<<Self as System>::Event>;
}

decl_module! {
    /// The module declaration.
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        // Initialize events.
        fn deposit_event() = default;

        /// Mint balance into an account.
        #[weight = 0]
        pub fn mint(origin, key: <T as System>::AccountId) -> DispatchResult {
            let _ = ensure_none(origin)?;
            let imbalance = <balances::Module<T> as Currency<<T as System>::AccountId>>::deposit_creating(&key, T::MINT_UNIT);
            drop(imbalance);
            Self::deposit_event(RawEvent::Minted(key, T::MINT_UNIT));
            Ok(())
        }
    }
}

decl_event!(
    pub enum Event<T>
    where
        AccountId = <T as System>::AccountId,
        Balance = <T as Balances>::Balance,
    {
        Minted(AccountId, Balance),
    }
);

impl<T: Trait> ValidateUnsigned for Module<T> {
    type Call = Call<T>;

    fn validate_unsigned(_source: TransactionSource, _call: &Self::Call) -> TransactionValidity {
        let current_block = <frame_system::Module<T>>::block_number();
        ValidTransaction::with_tag_prefix("Faucet")
            .and_provides(current_block)
            .build()
    }
}