The Offchain Worker Example: A simple pallet demonstrating concepts, APIs and structures common to most offchain workers.
cargo doc --package pallet-example-offchain-worker --open to view this module’s
This pallet serves as an example showcasing Substrate off-chain worker and is not meant to be used in production.
In this example we are going to build a very simplistic, naive and definitely NOT
production-ready oracle for BTC/USD price.
Offchain Worker (OCW) will be triggered after every block, fetch the current price
and prepare either signed or unsigned transaction to feed the result back on chain.
The on-chain logic will simply aggregate the results and store last
64 values to compute
the average price.
Additional logic in OCW is put in place to prevent spamming the network with both signed
and unsigned transactions, and custom
UnsignedValidator makes sure that there is only
one unsigned transaction floating in the network.
pub use pallet::*;
- Based on the above
KeyTypeIdwe need to generate a pallet-specific crypto type wrappers. We can use from supported crypto kinds (
ecdsa) and augment the types with this pallet-specific identifier.
palletmodule in each FRAME pallet hosts the most important items needed to construct this pallet.
- Payload used by this example crate to hold price data required to submit a transaction.
- Defines application identifier for crypto keys of this module.