Created
April 25, 2019 08:33
-
-
Save yuyasugano/ba0e17aa31b1f89b61ce6ccea2706119 to your computer and use it in GitHub Desktop.
Substrate token runtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A runtime module template with necessary imports | |
/// Feel free to remove or edit this file as needed. | |
/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs | |
/// If you remove this file, you can remove those references | |
/// For more guidance on Substrate modules, see the example module | |
/// https://github.com/paritytech/substrate/blob/master/srml/example/src/lib.rs | |
use rstd::prelude::*; | |
use support::{decl_module, decl_storage, decl_event, dispatch::Result, StorageValue, StorageMap, ensure}; | |
use system::{self, ensure_signed}; | |
/// The module's configuration trait. | |
pub trait Trait: system::Trait { | |
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>; | |
} | |
decl_storage! { | |
trait Store for Module<T: Trait> as Template { | |
pub TotalSupply get(total_supply): u64 = 21000000; | |
pub BalanceOf get(balance_of): map T::AccountId => u64; | |
Init get(is_init): bool; | |
} | |
} | |
decl_module! { | |
/// The module declaration. | |
pub struct Module<T: Trait> for enum Call where origin: T::Origin { | |
// Initialize default event handling | |
fn deposit_event<T>() = default; | |
// Initialize the token | |
// transfers the total_supply amount to the caller | |
fn init(origin) -> Result { | |
let sender = ensure_signed(origin)?; | |
ensure!(Self::is_init() == false, "Already initialized."); | |
<BalanceOf<T>>::insert(sender, Self::total_supply()); | |
<Init<T>>::put(true); | |
Ok(()) | |
} | |
// Transfer tokens from an account to another | |
fn transfer(origin, to: T::AccountId, value: u64) -> Result { | |
let sender = ensure_signed(origin)?; | |
let sender_balance = Self::balance_of(sender.clone()); | |
// This ensures that sender has more than sending units | |
ensure!(sender_balance >= value, "No enough balance."); | |
let updated_from_balance = sender_balance.checked_sub(value).ok_or("Overflow happened in balance")?; | |
let recipient_balance = Self::balance_of(to.clone()); | |
let updated_to_balance = recipient_balance.checked_add(value).ok_or("overflow happened in balance")?; | |
// Sub sender's balance | |
<BalanceOf<T>>::insert(sender.clone(), updated_from_balance); | |
// Add recipient's balance | |
<BalanceOf<T>>::insert(to.clone(), updated_to_balance); | |
Self::deposit_event(RawEvent::Transfer(sender, to, value)); | |
Ok(()) | |
} | |
} | |
} | |
decl_event!( | |
pub enum Event<T> where AccountId = <T as system::Trait>::AccountId { | |
// Event for transfer of tokens | |
// from, to, value | |
Transfer(AccountId, AccountId, u64), | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment