If you are building a pallet (pallet2) which has a direct dependency on another pallet (pallet1), you can you "tight coupling" inherit pallet1's Config
in pallet2's Config
definition:
pub trait Config: pallet1::Config {
...
}
To access public storage items from pallet1 in pallet2, you then just need to call the appropriate function on that storage item:
let value: u64 = pallet1::MyValue::<T>::get();
if value < new_value {
pallet1::MyValue::<T>set(new_value);
}
To access other public functions from pallet1 in pallet2, you need to use the Pallet
type where these functions are implmented:
pallet1::Pallet::<T>::public_function();
Here is a simple example of two pallets interacting in this way:
pallet1.rs
Note that all the things in this pallet are marked public (pub
)
use frame_support::pallet_prelude::*;
#[frame_support::pallet]
mod pallet {
pub trait Config: system::Config {}
#[pallet::storage]
pub(super) type MyValue<T: Config> = StorageValue<_, u64, ValueQuery>;
// -- snip --
}
impl<T: Config> Pallet<T> {
pub fn get_value() -> u32 {
<Something<T>>::get()
}
}
pallet2.rs
use frame_support::pallet_prelude::*;
use crate::pallet1;
mod pallet {
pub trait Config: pallet1::Config {
// -- snip --
}
#[pallet::call]
impl<T: Config> Pallet<T> {
pub fn get_value_directly(_origin: OriginFor<T>) -> Result {
let value = pallet1::MyValue::<T>::get();
Ok(())
}
pub fn set_value_directly(_origin: OriginFor<T>, value: u32) -> Result {
pallet1::MyValue::<T>::put(value);
Ok(())
}
pub fn get_value_public_function(_origin: OriginFor<T>) -> Result {
let value = pallet1::Pallet::<T>::get_value();
Ok(())
}
}
}