substrate基础(7)-将pallet添加到runtime

本文依旧基于substrate-node-template 进行操作.
该项目主要是为了方便开发人员调试和测试,以及初次接触substrate的入门者首选,掌握该项目后,再去接触正式项目,会容易很多
本文假定你已经编译好了这个项目,具体编译可以参考本系列第一篇文章:substrate基础(1)-substrate-node-template编译及部署

本文通过案例主要介绍如何将pallet添加到runtime:

  1. 质押费用来为自己的账户设置一个昵称
  2. 删除该账户自定义昵称并退还质押费用
  3. 在不退还质押的情况下强行删除该账户的昵称

1. 添加依赖

在项目根目录,编辑文件runtime/Cargo.toml
[dependencies]下添加:

1
2
# 不要直接复制我这一行。其中的version default-features branch请参考该[dependencies]中相同git属性别的依赖所使用的,保持一致即可,否则编译很可能会失败。
pallet-nicks = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" }

[features]std[]中,添加如下一行:

1
'pallet-nicks/std', 

然后在当前目录runtime/下检测是否能够编译成功:

1
cargo check -p pallet-nicks

2. 查看nick-pallet的配置并在runtime中添加相关参数

  1. 查看昵称plate配置源码
    其中已经声明的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#[pallet::config]
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;

/// Reservation fee.
/// 预留费用,也就是质押费用
#[pallet::constant]
type ReservationFee: Get<BalanceOf<Self>>;

/// What to do with slashed funds.
type Slashed: OnUnbalanced<NegativeImbalanceOf<Self>>;

/// The origin which may forcibly set or remove a name. Root can always do this.
/// 谁可以强制设置或修改昵称,root账户可以随时修改
type ForceOrigin: EnsureOrigin<Self::Origin>;

/// The minimum length a name may be.
/// 昵称最小长度
#[pallet::constant]
type MinLength: Get<u32>;

/// The maximum length a name may be.
/// 昵称最大长度
#[pallet::constant]
type MaxLength: Get<u32>;
}
  1. 编辑runtime/src/lib.rs,在parameter_types!宏代码块中添加下方代码:
    这个parameter_types!主要是用来设置管理全局变量的
1
2
3
4
5
6
7
parameter_types! {
// Choose a fee that incentivizes desireable behavior.
pub const NickReservationFee: u128 = 100;
pub const MinNickLength: u32 = 8;
// Maximum bounds on storage are important to secure your chain.
pub const MaxNickLength: u32 = 32;
}
  1. runtime/src/lib.rs添加如下代码块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
impl pallet_nicks::Config for Runtime {
// The Balances pallet implements the ReservableCurrency trait.
// `Balances` is defined in `construct_runtime!` macro. See below.
// https://paritytech.github.io/substrate/master/pallet_balances/index.html#implementations-2
type Currency = Balances;

// Use the NickReservationFee from the parameter_types block.
type ReservationFee = NickReservationFee;

// No action is taken when deposits are forfeited.
type Slashed = ();

// Configure the FRAME System Root origin as the Nick pallet admin.
// https://paritytech.github.io/substrate/master/frame_system/enum.RawOrigin.html#variant.Root
type ForceOrigin = frame_system::EnsureRoot<AccountId>;

// Use the MinNickLength from the parameter_types block.
type MinLength = MinNickLength;

// Use the MaxNickLength from the parameter_types block.
type MaxLength = MaxNickLength;

// The ubiquitous event type.
type Event = Event;
}
  1. runtime/src/lib.rs,在construct_runtime!宏代码块中的Runtime枚举添加下方代码:
1
Nicks: pallet_nicks,
  1. 然后在runtime/目录下检测是否能够编译成功:
1
cargo check -p node-template-runtime

3. 启动节点并连接页面进行交互

3.1 编译并启动节点:

回到项目根目录,编译节点:

1
cargo build --release

启动节点

1
./target/release/node-template --dev --base-path ./tmp/

3.2 启动web

substrate-front-end-template 可视化,具体编译和部署,请自行查阅资料或参考:substrate基础(1)-substrate-node-template编译及部署

3.3 设置昵称

页面中,账户为:ALICE,下方Pallet Interactor选中Extrinsic,第一行下拉框Pallets\RPC中,选择nicks;第二行下拉框Callables中,选中setName。然后下方文本框输入昵称:HelloPlate,最后执行Signed

3.4 查询昵称

页面中,账户为:ALICE,下方Pallet Interactor选中Query,第一行下拉框Pallets\RPC中,选择nicks;第二行下拉框Callables中,选中nameOf。然后下方文本框输入账户地址(alice的):5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY,最后执行Query

其中返回的是昵称的十六进制编码,以及设置昵称所质押的费用

3.5 其余操作

其余操作类似,可以自行尝试下sudo

总结

按照本文流程,每个结果都可以复现的,亲自实现的。

本文编辑完毕

参考

[1] Substrate官方文档

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2017-2023 Jason
  • Visitors: | Views:

谢谢打赏~

微信