Skip to content

Instantly share code, notes, and snippets.

@gautamdhameja
Last active March 6, 2022 23:38
Show Gist options
  • Select an option

  • Save gautamdhameja/47ce08b54eb82338a8eb871676c9cd73 to your computer and use it in GitHub Desktop.

Select an option

Save gautamdhameja/47ce08b54eb82338a8eb871676c9cd73 to your computer and use it in GitHub Desktop.
A minimal Substrate module. Implements a simple token transfer function with token balances stored in module storage.
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::{
ensure,
pallet_prelude::{DispatchResult, ValueQuery, *},
};
use frame_system::pallet_prelude::*;
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::storage]
#[pallet::getter(fn total_supply)]
pub type TotalSupply<T> = StorageValue<_, u64>;
#[pallet::storage]
#[pallet::getter(fn balance_of)]
pub(super) type BalanceOf<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, u64, ValueQuery>;
// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/v3/runtime/events-and-errors
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
BalanceTransfer { value: u64, from: T::AccountId, to: T::AccountId },
}
// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
/// Error names should be descriptive.
NotEnoughBalance,
/// Errors should have helpful documentation associated with them.
StorageOverflow,
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
// Initialize the token with total supply and owner.
#[pallet::weight(10_000 + T::DbWeight::get().writes(2))]
pub fn init(origin: OriginFor<T>, total_supply: u64) -> DispatchResult {
let sender = ensure_signed(origin)?;
<TotalSupply<T>>::put(total_supply);
<BalanceOf<T>>::insert(sender, total_supply);
Ok(())
}
// Transfer tokens from one account to another.
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(2,2))]
pub fn transfer(origin: OriginFor<T>, to: T::AccountId, value: u64) -> DispatchResult {
let sender = ensure_signed(origin)?;
let sender_balance = Self::balance_of(sender.clone());
ensure!(sender_balance >= value, Error::<T>::NotEnoughBalance);
let updated_from_balance =
sender_balance.checked_sub(value).ok_or(Error::<T>::StorageOverflow)?;
let receiver_balance = Self::balance_of(to.clone());
let updated_to_balance =
receiver_balance.checked_add(value).ok_or(Error::<T>::StorageOverflow)?;
// reduce sender's balance
<BalanceOf<T>>::insert(sender.clone(), updated_from_balance);
// increase receiver's balance
<BalanceOf<T>>::insert(to.clone(), updated_to_balance);
Self::deposit_event(Event::BalanceTransfer { value, from: sender, to });
Ok(())
}
}
}
@edisinovcic

edisinovcic commented Sep 12, 2019 •

Copy link
Copy Markdown

There are some problems with this gist when running tutorial: https://www.parity.io/how-to-build-your-own-blockchain-using-parity-substrate/

I've updated so it works, here is the code. https://gist.github.com/edisinovcic/292862e47cfe9c3f11f92c93ad8fe0c8

Mainly import updates, probably rust stuff has changed in these 5 months :)

@gautamdhameja

Copy link
Copy Markdown
Author

Thanks, @edisinovcic. I've updated this gist with new imports. It should be fine now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment