7

From a parachain, how to get the current relay chain block number?

I would like to write a hook using the relay chain block as a trigger.

1 Answer 1

8

Great question!

You can include the following in your parachain's runtime:

use cumulus_pallet_parachain_system::RelaychainBlockNumberProvider;

Then, in the parachain's runtime, define the following in your pallet's config:

type RelayChainBlockNumber = RelaychainBlockNumberProvider<Runtime>;

In your pallet you will have to import BlockNumberProvider:

use sp_runtime::traits::BlockNumberProvider;

Define it in your pallet's config:

type RelayChainBlockNumber: BlockNumberProvider<BlockNumber = Self::BlockNumber>;

And, finally, you can get the relaychain's block number in your pallet:

let current_relaychain_block_number = T::RelayChainBlockNumber::current_block_number();
log::info!("The current relaychain block number is {:?}", current_relaychain_block_number);

Here is an example:

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.