1

How to access value X in pallet A from another pallet? For example, I saw a few pallets define type Currency = Balance, is it truly the value Balance in pallet_balances?

If not, how to define a variable X that is belong to 2 pallets? and when you query from any pallet, it always returns the same value at time t.

In this document, pallet coupling https://docs.substrate.io/build/pallet-coupling/ I think my pallet just uses the type defined in pallet_balances but not actually refer to the same value.

It said "Now, you can use the runtime configuration—runtime/src/lib.rs—to implement the pallet and specify the type to be Balances.

impl my_pallet::Config for Runtime {
    type MyCurrency = Balances;
}

The Balances type is specified in construct_runtime! macro as part of the pallet_balances that implements the Currency trait."

What I am trying to do is create a new pallet (called pallet_X). and this pallet is required to calculate a value X based on the user's balance (so it should be coupling pallet_balance), then staking X in pallet_staking.

1 Answer 1

0

How to access value X in pallet A from another pallet?

Pallets should not rely on each other directly - but only on a behaviour that could be implemented by another pallet. This makes it possible to have different implementations which allows extensibility and customization.

For example, I saw a few pallets define type Currency = Balance, is it truly the value Balance in pallet_balances?

Not necessarily, as this is decided upon by the runtime config. For example: there can be multiple pallet_balances instances. Or the Currency trait could be implemented by a 3rd party logic module (pallet or something entirely different).
Substrate pallets try to make as few assumptions as possible about how they are going to be used in order to not exclude unforseen use-cases.

If not, how to define a variable X that is belong to 2 pallets?

I dont know what you are trying to achieve and without that its not possible to suggest something specific. Please always include source code and the goal in your question.
Its possible to use type MyValue: Get<u32>; in both pallet configs and make the assumption that they both point to the same value. However, the pallets cannot enforce this - the runtime config can still configure different things for both pallets.

If its absolutely necessary, then you can proceed with the "Tight Coupling" where the config of Pallet A depends on the config of Pallet B (or vice versa). Then Pallet A can do <T as pallet_b::Config>::MyValue::get() and it is ensured that they use the same.

From a design perspective it may be desirable to split the data storage into its own pallet, to not have business logic pallets tightly couple each other, but only to the data storage pallet.

2
  • 1
    Thank you for your detail comment; the last 3 paragraphs are what I need. I also updated my question. @oliver-tale-yazdi
    – MinhK
    Jan 4 at 22:12
  • Then please mark it as answer, so other people find it. Jan 5 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.