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.
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: