I'm trying to avoid using tight coupling for my pallets as it seems to be a bad idea for later enhancements. I'm also facing issues when trying to create a tests.rs file for, but that's another issue and probably related to my inexperience with rust.
The thing is, I have two pallets (pallet1 and pallet2). Pallet1 models an item and pallet2 is another 'object' but it is expected to be attached to the item on pallet1. So for example, when I create an item on pallet1, it creates a call on pallet2 that creates an Object with the Item id, using its trait like:
/// Pallet1 lib.rs
#[pallet::config]
pub trait Config: frame_system::Config + pallet_pallet1::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type Pallet2: Pallet2Provider<Self>;
}
#[pallet::weight(0)]
pub fn create_item(origin: OriginFor<T>, id: Vec<u8>) -> DispatchResult {
// Do some stuff
T::Pallet2::create_object(id);
Ok(())
}
/// Pallet2 lib.rs
#[pallet::storage]
pub(super) type Objects<T: Config> =
StorageMap<_, Twox64Concat, id, Object<T>>;
impl Pallet2Provider<T> for Pallet<T> {
fn create_object(id: Vec<u8>) {
<Objects<T>>::insert(id, Object::new(id));
}
}
The code above is just an example, and it doesn't have to be correct, but it's just to illustrate the issue. There is another function within the Pallet2 that modifies the object, but at the same time modifies some value of the Pallet1 item, so I'm tight coupling Pallet1 to Pallet2 so thats why I'm adding the pallet_pallet1:Config
dependency to the Pallet2 Config.
If both pallets have to interact with each other, they can be merged on a single pallet right? but it will be a large lib.rs and make it hard to read and maintain. Splitting this logic is what im trying to achieve but I don't know if I'm supposed to do tight or loose coupling.
Besides this issue, is there any real example where two pallets interact with each other and the first one modifies the second storage and viceversa in a loose way? I think using tight coupling is the easiest way to do so.