Exploring Substrate
Hi there! welcome back, this will be the first post of a new serie exploring the Substrate framework. We will be using the tutorials and recipes from the Substrate Developer Hub as our road map to exploring and learning.
As the landing page of the hub says, Substrate is a modular framework that enables you to create purpose-built blockchains by composing custom or pre-built components.
, that means that you can create your first Substrate Chain by using the pre-built components that the framework offer. And that is what the first tutorial is about.
You can follow the first tutorial for detailed instructions, but make sure to review the installation page for setup your environment.
Let’s continue with the exploration, assuming that you already cloned the needed templates
, first we need to build the node
(in release mode).
|
|
And then you can run both front-end and node
in different terminals
First the node
using the --dev
and --tmp
flags
|
|
And the front-end
|
|
You will get a nice UI that allow you to interact with your first Substrate Chain :)
And even you can interact and make transfers between the test accounts :)
Great!! we complete the first tutorial, we can also explore other options un the UI and interact with our first chain :)
Let’s now continue, we will first add add the nicks
pallet
following the next tutorial. But first a little definition from the KB page:
Pallets are a special kind of Rust module made up of a set of types, trait implementations and functions from which Substrate runtimes can be composed. FRAME not only provides a library of commonly used Substrate pallets but also a framework to build custom domain-specific pallets, giving runtime engineers the flexibility to define their runtime’s behaviour according to their target use case. The result: each pallet has its own discrete logic which can modify the features and functionality of your blockchain’s state transition functions.
You can follow the detailed explanation in the tutorial, but in a nutshell to add the pallet
you need to modify two files. First you need to add the dep
to the runtime
crate.
|
|
Note: The version
and tag
may be outdate, you can check the latest ones.
And also we need to add the feature
to the std
features of the runtime
crate ( you can read in the tutorial for a nice explanation)
|
|
Great! Now is time to configure
the pallet
, since every pallet
needs to implement the Config
trait in order to be included in the runtime (by the construct_runtime
macro).
We can here use the help
of the rust-analyzer to get the skeleton of the needed members.
|
|
And, also detailed in the tutorial, we had another macro (parameter_types
) to help us defined constant values. Let use this macro to define some constants needed in the configuration.
|
|
And now let go to the trait implementation block and complete the needed config (using the code from the tutorial).
|
|
Last, we need to add the pallet
to the runtime
by adding to the construct_runtime
macro call.
|
|
Awesome! Time to compile again and see it in action :)
|
|
And we get an error, the trait From<usize>
is not implemented for u32
.
We can look at the Get trait in the substrate repo for more details in the implementation, but for now let go to our code and change the usize
to u32
. Update: there is already a PR in the tutorial repo to fix this and use u32.
|
|
Let try to compile again…
|
|
Nice! we can run again and interact with the nicks
pallet.
|
|
Set the nick of Alice address
Wait, looking at the Events
there is an error in the execution
1 2 3 | system:ExtrinsicFailed:: (phase={"applyExtrinsic":1})-2 DispatchError: {"module":{"index":9,"error":1}}, DispatchInfo: {"weight":50000000,"class":"Normal","paysFee":"Yes"} An extrinsic failed. \[error, info\] |
After looking how to read this errors I found a nice explanation in this so answer
For future occasions there it goes a tip when a dispatch error happens. index:9 and error:0 are the index of the pallet in construct_runtime! which is throwing the error, and the index of the error in the #[pallet::error] definition of the pallet concerned.
So, in our case index:9
is the nicks
pallet and we error:1
is TooLong
(defined in this line), since remember we set the MaxNickLength
to 16. Let’s try with a nick
with an accepted length(between 8 and 16).
Nice! works as expected! We can also check by query
ing the nameOf
using the address of Alice.
Great! we get the Alice’snick
(hex-encoded) and amount used for reserve the nick.
That’s all for today, we followed the first two tutorials of the Substrate Developer Hub and make our first interactions with the framework :)
As always, I write this as a learning journal and any feedback is welcome 🙌. In the next post I will try to write a simple pallet
and integrate with our current chain, stay tune 😃
Thanks!