2

I have one common pallet which is to be used by other pallets. For example, below is the common shared pallet:

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod tests;

pub trait Trait: system::Trait {
    type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
    type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
}

type AccountIdOf<T> = <T as system::Trait>::AccountId;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<AccountIdOf<T>>>::Balance;
type StructInfoOf<T> = StructInfo<AccountIdOf<T>, <T as system::Trait>::BlockNumber>;

#[derive(Encode, Decode, Default, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct StructInfo<AccountId, BlockNumber> {
    name: Vec<u8>,  
    owner: AccountId,
    created: BlockNumber,
}

decl_storage! {
    trait Store for Module<T: Trait> as TokenStore {

    pub Data get(fn data): map hasher(blake2_128_concat) u32 => Option<StructInfoOf<T>>;
    pub DataCount get(fn data_count): u32;


    }
}

decl_event!(
    pub enum Event<T>
    where
        AccountId = <T as system::Trait>::AccountId,
        Balance = BalanceOf<T>,
    {
        /// Test event
        TestEvent(u32, Balance, AccountId),
    }
);

decl_error! {
    pub enum Error for Module<T: Trait> {
        TestError
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        fn deposit_event() = default;


        #[weight = 10_000]
        pub fn test_dispatch_call(origin) -> DispatchResult {
            let caller = ensure_signed(origin)?;
            Ok(())
        }   
        
    }
}

impl<T: Trait> Module<T> {


    pub fn public_function(value: u32) -> BalanceOf<T>  {
        let value: u32 = 0;
        Self::data_count()
    }
}

but I am not sure how I can call Data, DataCount, test_dispatch_call, public_function from the common module.

Afeez Aziz
  • 1,337
  • 2
  • 12
  • 26
  • I tried based on https://stackoverflow.com/questions/56902167/in-substrate-is-there-a-way-to-use-storage-and-functions-from-one-custom-module?rq=1 and https://stackoverflow.com/questions/62718287/dispatching-a-function-defined-in-another-substrate-frame-pallet?rq=1 but unable to move forward as first link is "module" not sure it is the same as pallet and it does not work based on the code shared. – Afeez Aziz Feb 09 '21 at 04:30
  • 1
    module is same as pallet, and you should do the same. – kianenigma Feb 09 '21 at 05:22
  • @kianenigma based on your comment, `use crate::pallet_common;` I encountered `no 'pallet_token' in the root` – Afeez Aziz Feb 09 '21 at 05:39

1 Answers1

0

Add your pallet that need in Cargo.toml in [dependencies] like pallet-test = {default-features = false, path ='../test'} and in [features] [std] like 'pallet-test/std'

In current pallet: add pallet-test in pub trait Trait (v2) or pub trait Config(v3) like pub trait Config: frame_system::Config + pallet_test::Config{}

Access Function: <pallet_test::Module<T>>::function_test();

CocDap
  • 180
  • 4
  • 1
    that is an example of tight coupling, it is suggested to loosely couple instead, generally: https://substrate.dev/recipes/pallet-coupling.html – Nuke Aug 05 '21 at 21:37
  • @NukeManDan the link is broken, could you please fix it ? – gfan Nov 30 '21 at 01:33
  • @gfan https://docs.substrate.io/how-to-guides/v3/pallet-design/tight-coupling/ – CocDap Nov 30 '21 at 11:42