git.delta.rocks / unique-network / refs/commits / 8b6fe6666d58

difftreelog

source

runtime/src/template.rs4.3 KiBsourcehistory
1/// A runtime module template with necessary imports23/// Feel free to remove or edit this file as needed.4/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs5/// If you remove this file, you can remove those references678/// For more guidance on Substrate modules, see the example module9/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs1011use frame_support::{decl_module, decl_storage, decl_event, dispatch::DispatchResult};12use system::ensure_signed;1314/// The module's configuration trait.15pub trait Trait: system::Trait {16	// TODO: Add other types and constants required configure this module.1718	/// The overarching event type.19	type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;20}2122// This module's storage items.23decl_storage! {24	trait Store for Module<T: Trait> as TemplateModule {25		// Just a dummy storage item.26		// Here we are declaring a StorageValue, `Something` as a Option<u32>27		// `get(fn something)` is the default getter which returns either the stored `u32` or `None` if nothing stored28		Something get(fn something): Option<u32>;29	}30}3132// The module's dispatchable functions.33decl_module! {34	/// The module declaration.35	pub struct Module<T: Trait> for enum Call where origin: T::Origin {36		// Initializing events37		// this is needed only if you are using events in your module38		fn deposit_event() = default;3940		// Just a dummy entry point.41		// function that can be called by the external world as an extrinsics call42		// takes a parameter of the type `AccountId`, stores it and emits an event43		pub fn do_something(origin, something: u32) -> DispatchResult {44			// TODO: You only need this if you want to check it was signed.45			let who = ensure_signed(origin)?;4647			// TODO: Code to execute when something calls this.48			// For example: the following line stores the passed in u32 in the storage49			Something::put(something);5051			// here we are raising the Something event52			Self::deposit_event(RawEvent::SomethingStored(something, who));53			Ok(())54		}55	}56}5758decl_event!(59	pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {60		// Just a dummy event.61		// Event `Something` is declared with a parameter of the type `u32` and `AccountId`62		// To emit this event, we call the deposit funtion, from our runtime funtions63		SomethingStored(u32, AccountId),64	}65);6667/// tests for this module68#[cfg(test)]69mod tests {70	use super::*;7172	use sp_core::H256;73	use frame_support::{impl_outer_origin, assert_ok, parameter_types, weights::Weight};74	use sp_runtime::{75		traits::{BlakeTwo256, IdentityLookup}, testing::Header, Perbill,76	};7778	impl_outer_origin! {79		pub enum Origin for Test {}80	}8182	// For testing the module, we construct most of a mock runtime. This means83	// first constructing a configuration type (`Test`) which `impl`s each of the84	// configuration traits of modules we want to use.85	#[derive(Clone, Eq, PartialEq)]86	pub struct Test;87	parameter_types! {88		pub const BlockHashCount: u64 = 250;89		pub const MaximumBlockWeight: Weight = 1024;90		pub const MaximumBlockLength: u32 = 2 * 1024;91		pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);92	}93	impl system::Trait for Test {94		type Origin = Origin;95		type Call = ();96		type Index = u64;97		type BlockNumber = u64;98		type Hash = H256;99		type Hashing = BlakeTwo256;100		type AccountId = u64;101		type Lookup = IdentityLookup<Self::AccountId>;102		type Header = Header;103		type Event = ();104		type BlockHashCount = BlockHashCount;105		type MaximumBlockWeight = MaximumBlockWeight;106		type MaximumBlockLength = MaximumBlockLength;107		type AvailableBlockRatio = AvailableBlockRatio;108		type Version = ();109		type ModuleToIndex = ();110	}111	impl Trait for Test {112		type Event = ();113	}114	type TemplateModule = Module<Test>;115116	// This function basically just builds a genesis storage key/value store according to117	// our desired mockup.118	fn new_test_ext() -> sp_io::TestExternalities {119		system::GenesisConfig::default().build_storage::<Test>().unwrap().into()120	}121122	#[test]123	fn it_works_for_default_value() {124		new_test_ext().execute_with(|| {125			// Just a dummy test for the dummy funtion `do_something`126			// calling the `do_something` function with a value 42127			assert_ok!(TemplateModule::do_something(Origin::signed(1), 42));128			// asserting that the stored value is equal to what we stored129			assert_eq!(TemplateModule::something(), Some(42));130		});131	}132}