簡(jiǎn)介
在GitHub 上創(chuàng)建測(cè)試用的Pallet 項(xiàng)目
-
訪問(wèn):
https://github.com/substrate-developer-hub/substrate-pallet-template需要注意這是一個(gè)項(xiàng)目獎(jiǎng)里面的pallet模板,點(diǎn)擊綠色的大按鈕(use this template)
image.png -
然后我們給自己的倉(cāng)庫(kù)起名
test-pallet,(實(shí)際上任何名字都可以)
image.png
Clone 新建立的項(xiàng)目到 template /pallets 目錄下面
cd /Users/apple/lin-files/work-files/coding/git-fiels/learning-rust/start-a-private-network/substrate-node-template/pallets- 如果你的Substrate-node-template也在git下面,可以添加submodule ,
git submodule add git@github.com:kami1983/test-pallet.git否則直接 git clone 就可以了。 - (題外話(huà))通過(guò)submodule 會(huì)在根目錄的 .git/config 下面生成:
[submodule "start-a-private-network/substrate-node-template/pallets/test-pallet"]
url = git@github.com:kami1983/test-pallet.git
active = true
- 此時(shí):
git add --all;git commit -m "ass"pallets/test-pallet 目錄也會(huì)被提交。
image.png - 好了 test-pallet 項(xiàng)目代碼準(zhǔn)備好后就可以進(jìn)行下一步操作了。
重新命名你的Crate
- 修改
pallets/test-pallet/Cargo.toml
[package]
authors = ['Kmai1983']
description = '建立一個(gè)測(cè)試用的Pallet create'
edition = '2018'
homepage = 'https://github.com/kami1983'
license = 'Unlicensed'
name = 'test-pallet'
repository = 'https://github.com/kami1983/test-pallet'
version = '3.0.0'
編譯 Pallet
-
cd test-pallet輸入check 命令:SKIP_WASM_BUILD=1 cargo check - 使用如下命令,確認(rèn) Substrate 模塊模板測(cè)試通過(guò):
SKIP_WASM_BUILD=1 cargo test
將模塊添加到節(jié)點(diǎn)中
- 修改
runtime/Cargo.toml
# --snip--
test-pallet = { path = '../pallets/test-pallet', default-features = false, version = '3.0.0' }
# toward the bottom
[features]
default = ['std']
std = [
'test-pallet/std',
# --snip--
]
- 接下來(lái)更新
runtime/src/lib.rs啟用我們的 runtime pallet:
// add this line (or modify all of these to your named pallet
// corresponding to `test-pallet` in its cargo.toml file)
pub use test_pallet;
// --snip--
// add this block
impl test_pallet::Config for Runtime {
type Event = Event;
}
// --snip--
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
// --snip--
// add the following line
TestPallet: test_pallet::{Module, Call, Storage, Event<T>},
}
);
運(yùn)行節(jié)點(diǎn)
-
cargo run -- --dev --tmp成功啟動(dòng)后,訪問(wèn)https://polkadot.js.org/鏈接到我們啟動(dòng)的測(cè)試鏈上,然后選擇 開(kāi)發(fā)者-》交易-》可以看到 testPallet 也就是表示我們成功了。

image.png
發(fā)布模塊
將模塊發(fā)布到 GitHub
- 將代碼上傳到Github 后,我們就可以將依賴(lài)指向成GIt地址,修改
runtime/Cargo.toml
[dependencies.your-pallet-name]
default_features = false
git = 'https://github.com/your-username/your-pallet'
branch = 'master'
# You may choose a specific commit or tag instead of branch
# rev = '<git-commit>'
# tag = '<some tag>
將模塊發(fā)布到 Crates.io
- 將代碼包發(fā)布到Crate.io后,就可以修改
runtime/Cargo.toml
[dependencies.your-pallet-name]
default_features = false
version = 'some-compatible-version'
結(jié)束
感謝閱讀,See you at work.


