Skip to content

Commit 1d4429e

Browse files
Zihan ZhaoZihan Zhao
Zihan Zhao
authored and
Zihan Zhao
committed
replace nicks with lottery, and build successfully
1 parent 5416c44 commit 1d4429e

File tree

6 files changed

+14762
-71
lines changed

6 files changed

+14762
-71
lines changed

content/md/en/docs/tutorials/build-application-logic/add-a-pallet.md

+75-63
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ As you saw in [Build a local blockchain](/tutorials/build-a-blockchain/build-loc
1515
This tutorial introduces the basic steps for adding a new pallet to the runtime for the node template.
1616
The steps are similar any time you want to add a new FRAME pallet to the runtime.
1717
However, each pallet requires specific configuration settings—for example, the specific parameters and types required to perform the functions that the pallet implements.
18-
For this tutorial, you'll add the [Nicks pallet](https://paritytech.github.io/substrate/master/pallet_nicks/index.html) to the runtime for the node template, so you'll see how to configure the settings that are specific to the Nicks pallet.
19-
The Nicks pallet allows blockchain users to pay a deposit to reserve a nickname for an account they control. It implements the following functions:
20-
21-
- The `set_name` function to collect a deposit and set the name of an account if the name is not already taken.
22-
- The `clear_name` function to remove the name associated with an account and return the deposit.
23-
- The `kill_name` function to forcibly remove an account name without returning the deposit.
18+
For this tutorial, you'll add the [Lottery pallet](https://paritytech.github.io/polkadot-sdk/master/pallet_lottery/) to the runtime for the node template, so you'll see how to configure the settings that are specific to the Lottery pallet.
19+
The Lottery pallet allows blockchain users, just like a normal lottery system, to "buy a ticket", which is used to fund the pot. Then, is reallocated to a single user.
2420

2521
Note that this tutorial is a stepping stone to more advanced tutorials that illustrate how to add pallets with more complex configuration settings, how to create custom pallets, and how to publish pallets.
2622

@@ -46,7 +42,7 @@ By completing this tutorial, you will use the Nicks pallet to accomplish the fol
4642

4743
- See changes to the runtime by interacting with the new pallet using the front-end template.
4844

49-
## Add the Nicks pallet dependencies
45+
## Add the Lottery pallet dependencies
5046

5147
Before you can use a new pallet, you must add some information about it to the configuration file that the compiler uses to build the runtime binary.
5248

@@ -59,32 +55,32 @@ Because the Substrate runtime compiles to both a native platform binary that inc
5955
For information about adding dependencies in `Cargo.toml` files, see [Dependencies](https://doc.rust-lang.org/cargo/guide/dependencies.html) in the Cargo documentation.
6056
For information about enabling and managing features from dependent packages, see [Features](https://doc.rust-lang.org/cargo/reference/features.html) in the Cargo documentation.
6157

62-
To add the dependencies for the Nicks pallet to the runtime:
58+
To add the dependencies for the Lottery pallet to the runtime:
6359

6460
1. Open a terminal shell and change to the root directory for the node template.
6561

6662
1. Open the `runtime/Cargo.toml` configuration file in a text editor.
6763

6864
1. Locate the [dependencies] section and note how other pallets are imported.
6965

70-
1. Copy an existing pallet dependency description and replace the pallet name with `pallet-nicks` to make the pallet available to the node template runtime.
66+
1. Copy an existing pallet dependency description and replace the pallet name with `pallet-lottery` to make the pallet available to the node template runtime.
7167

7268
For example, add a line similar to the following:
7369

7470
```toml
75-
pallet-nicks = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", branch = "polkadot-v1.0.0" }
71+
pallet-lottery = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false }
7672
```
7773

78-
This line imports the `pallet-nicks` crate as a dependency and specifies the following:
74+
This line imports the `pallet-lottery` crate as a dependency and specifies the following:
7975

8076
- Version to identify which version of the crate you want to import.
8177
- The default behavior for including pallet features when compiling the runtime with the standard Rust libraries.
82-
- Repository location for retrieving the `pallet-nicks` crate.
83-
- Branch to use for retrieving the crate. Be sure to use the same **version** and **branch** information for the Nicks pallet as you see used for the other pallets included in the runtime.
78+
- Repository location for retrieving the `pallet-lottery` crate.
79+
- Tag to use for retrieving the crate. Be sure to use the same **version** and **tag** information for the Nicks pallet as you see used for the other pallets included in the runtime.
8480

8581
These details should be the same for every pallet in any given version of the node template.
8682

87-
1. Add the `pallet-nicks/std` features to the list of `features` to enable when compiling the runtime.
83+
1. Add the `pallet-lottery/std` features to the list of `features` to enable when compiling the runtime.
8884

8985
```toml
9086
[features]
@@ -93,7 +89,7 @@ To add the dependencies for the Nicks pallet to the runtime:
9389
...
9490
"pallet-aura/std",
9591
"pallet-balances/std",
96-
"pallet-nicks/std",
92+
"pallet-lottery/std",
9793
...
9894
]
9995
```
@@ -117,19 +113,25 @@ The `Config` trait is used to identify the parameters and types that the pallet
117113

118114
Most of the pallet-specific code required to add a pallet is implemented using the `Config` trait.
119115
You can review what you to need to implement for any pallet by referring to its Rust documentation or the source code for the pallet.
120-
For example, to see what you need to implement for the `nicks` pallet, you can refer to the Rust documentation for [`pallet_nicks::Config`](https://paritytech.github.io/substrate/master/pallet_nicks/pallet/trait.Config.html) or the trait definition in the [Nicks pallet source code](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/nicks/src/lib.rs).
116+
For example, to see what you need to implement for the `Lottery` pallet, you can refer to the trait definition in the [Lottery pallet source code](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/lottery/src/lib.rs).
121117

122-
For this tutorial, you can see that the `Config` trait in the `nicks` pallet declares the following types:
118+
For this tutorial, you can see that the `Config` trait in the `lottery` pallet declares the following types:
123119

124120
```rust
125-
pub trait Config: Config {
126-
type RuntimeEvent: From<Event<Self>> + IsType<<Self as Config>::RuntimeEvent>;
127-
type Currency: ReservableCurrency<Self::AccountId>;
128-
type ReservationFee: Get<<<Self as Config>::Currency as Currency<<Self as Config>::AccountId>>::Balance>;
129-
type Slashed: OnUnbalanced<<<Self as Config>::Currency as Currency<<Self as Config>::AccountId>>::NegativeImbalance>;
130-
type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
131-
type MinLength: Get<u32>;
132-
type MaxLength: Get<u32>;
121+
pub trait Config: frame_system::Config {
122+
type PalletId: Get<PalletId>;
123+
type RuntimeCall: Parameter
124+
+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
125+
+ GetDispatchInfo
126+
+ From<frame_system::Call<Self>>;
127+
type Currency: ReservableCurrency<Self::AccountId>;
128+
type Randomness: Randomness<Self::Hash, BlockNumberFor<Self>>;
129+
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
130+
type ManagerOrigin: EnsureOrigin<Self::RuntimeOrigin>;
131+
type MaxCalls: Get<u32>;
132+
type ValidateCall: ValidateCall<Self>;
133+
type MaxGenerateRandom: Get<u32>;
134+
type WeightInfo: WeightInfo;
133135
}
134136
```
135137

@@ -172,62 +174,72 @@ To review the `Config` trait for the Balances pallet:
172174
As you can see in this example, the `impl pallet_balances::Config` block allows you to configure the types and parameters that are specified by the Balances pallet `Config` trait.
173175
For example, this `impl` block configures the Balances pallet to use the `u128` type to track balances.
174176

175-
## Implement the configuration for Nicks
177+
## Implement the configuration for Lottery
176178

177179
Now that you have seen an example of how the `Config` trait is implemented for the Balances pallet, you're ready to implement the `Config` trait for the Nicks pallet.
178180

179-
To implement the `nicks` pallet in your runtime:
181+
To implement the `lottery` pallet in your runtime:
180182

181183
1. Open the `runtime/src/lib.rs` file in a text editor.
182184

183185
1. Locate the last line of the Balances code block.
184186

185-
1. Add the following code block for the Nicks pallet:
187+
1. Before adding Lottery pallet's config, `pallet_random_collective_flip` is instantiated as `RandomnessCollectiveFlip`. For more information, you can see [Incorporate randomness](https://docs.substrate.io/reference/how-to-guides/pallet-design/incorporate-randomness/). Note that add `pallet_random_collective_flip` in `runtime/Cargo.toml` as above.
186188

187189
```rust
188-
impl pallet_nicks::Config for Runtime {
189-
// The Balances pallet implements the ReservableCurrency trait.
190-
// `Balances` is defined in `construct_runtime!` macro.
191-
type Currency = Balances;
192-
193-
// Set ReservationFee to a value.
194-
type ReservationFee = ConstU128<100>;
195-
196-
// No action is taken when deposits are forfeited.
197-
type Slashed = ();
198-
199-
// Configure the FRAME System Root origin as the Nick pallet admin.
200-
// https://paritytech.github.io/substrate/master/frame_system/enum.RawOrigin.html#variant.Root
201-
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
190+
impl pallet_insecure_randomness_collective_flip::Config for Runtime {}
191+
```
202192

203-
// Set MinLength of nick name to a desired value.
204-
type MinLength = ConstU32<8>;
193+
1. Add the following code block for the Lottery pallet:
205194

206-
// Set MaxLength of nick name to a desired value.
207-
type MaxLength = ConstU32<32>;
195+
```rust
196+
parameter_types! {
197+
pub const LotteryPalletId: PalletId = PalletId(*b"py/lotto");
198+
pub const MaxCalls: u32 = 10;
199+
pub const MaxGenerateRandom: u32 = 10;
200+
}
208201

209-
// The ubiquitous event type.
210-
type RuntimeEvent = RuntimeEvent;
202+
impl pallet_lottery::Config for Runtime {
203+
type PalletId = LotteryPalletId;
204+
type RuntimeCall = RuntimeCall;
205+
type Currency = Balances;
206+
type Randomness = RandomnessCollectiveFlip;
207+
type RuntimeEvent = RuntimeEvent;
208+
type ManagerOrigin = EnsureRoot<AccountId>;
209+
type MaxCalls = MaxCalls;
210+
type ValidateCall = Lottery;
211+
type MaxGenerateRandom = MaxGenerateRandom;
212+
type WeightInfo = pallet_lottery::weights::SubstrateWeight<Runtime>;
211213
}
214+
```
212215

213-
1. Add Nicks to the `construct_runtime!` macro.
216+
1. Add Lottery to the `runtime` module.
214217

215218
For example:
216219

217220
```rust
218-
construct_runtime!(
219-
pub enum Runtime where
220-
Block = Block,
221-
NodeBlock = opaque::Block,
222-
UncheckedExtrinsic = UncheckedExtrinsic
223-
{
224-
/* --snip-- */
225-
Balances: pallet_balances,
226-
227-
/*** Add This Line ***/
228-
Nicks: pallet_nicks,
229-
}
230-
);
221+
#[frame_support::runtime]
222+
mod runtime {
223+
#[runtime::runtime]
224+
#[runtime::derive(
225+
RuntimeCall,
226+
RuntimeEvent,
227+
RuntimeError,
228+
RuntimeOrigin,
229+
RuntimeFreezeReason,
230+
RuntimeHoldReason,
231+
RuntimeSlashReason,
232+
RuntimeLockId,
233+
RuntimeTask
234+
)]
235+
...
236+
237+
#[runtime::pallet_index(8)]
238+
pub type Lottery = pallet_lottery;
239+
240+
#[runtime::pallet_index(9)]
241+
pub type RandomnessCollectiveFlip = pallet_insecure_randomness_collective_flip;
242+
}
231243
```
232244

233245
1. Save your changes and close the file.
@@ -248,7 +260,7 @@ To implement the `nicks` pallet in your runtime:
248260

249261
## Start the blockchain node
250262

251-
After your node compiles, you are ready to start the node that has been enhanced with nickname capabilities from the [Nicks pallet](https://paritytech.github.io/substrate/master/pallet_nicks/index.html) and interact with it using the front-end template.
263+
After your node compiles, you are ready to start the node that has been enhanced with lanch a lottery from the [Lottery pallet](https://paritytech.github.io/polkadot-sdk/master/pallet_lottery/) and interact with it using the front-end template.
252264

253265
To start the local Substrate node:
254266

example.env.development

-2
This file was deleted.

example.env.production

-2
This file was deleted.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"lint:fix": "eslint --fix --ext js --ext jsx .",
2222
"extract-locales": "yarn run babel --config-file ./babel-extract-locales.config.js -o tmp/chunk.js 'src/**/*.{js,jsx}' && rm -rf tmp",
2323
"checklinks": "yarn blc --filter-level=3 -rof --requests=10 --exclude='/crates.io' --exclude='/localhost:8000' --exclude='/localhost:8100' --exclude='/localhost:8300' --exclude='/paritytech.github.io/' --exclude='undefined' http://localhost:9000/quick-start/ | tee checklinks.log",
24-
"checklinks-external": "find content/md -name \\*.md -print0 | xargs -0 -n1 markdown-link-check -p -c .github/workflows/mlc_config.json"
25-
},
24+
"checklinks-external": "find content/md -name \\*.md -print0 | xargs -0 -n1 markdown-link-check -p -c .github/workflows/mlc_config.json"
25+
},
2626
"dependencies": {
2727
"aos": "^2.3.4",
2828
"classnames": "^2.3.1",
@@ -102,4 +102,4 @@
102102
"url": "https://github.com/gatsbyjs/gatsby/issues"
103103
},
104104
"packageManager": "[email protected]"
105-
}
105+
}

0 commit comments

Comments
 (0)