I have seen the Substrate Tutorial on creating crates of individual Substrate Runtime modules here in order to re-use the functionality, but I wondered if there is a way for one custom module to access the storage or functions from another custom module?
Something along these lines:
/// In ModuleA
pub type IndexType = u64;
decl_storage! {
trait Store for Module<T: Trait> as ModuleA {
pub MyIndexCount get(my_index_count): Option<IndexType>;
}
}
And then inside ModuleB - what do I need to do to use/include the functionality of ModuleA, and how do I call it?
/// In ModuleB
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
fn deposit_event<T>() = default;
pub fn edit_index(origin) -> Result {
let sender = ensure_signed(origin)?;
// --->>>> I want to read some storage from ModuleA whilst inside ModuleB
let c: IndexType = ReadStorageFromModuleA >>> my_index_count().ok_or("Storage Read Error: cannot get index")?;
// change storage in ModuleA from ModuleB
WriteToStorageInModuleA <MyIndexCount<T>>::put(&c + 1);
Ok(())
}
}
}