diff --git a/content/md/en/docs/tutorials/build-application-logic/add-a-pallet.md b/content/md/en/docs/tutorials/build-application-logic/add-a-pallet.md index a920de241..9379d3882 100644 --- a/content/md/en/docs/tutorials/build-application-logic/add-a-pallet.md +++ b/content/md/en/docs/tutorials/build-application-logic/add-a-pallet.md @@ -15,12 +15,8 @@ As you saw in [Build a local blockchain](/tutorials/build-a-blockchain/build-loc This tutorial introduces the basic steps for adding a new pallet to the runtime for the node template. The steps are similar any time you want to add a new FRAME pallet to the runtime. However, each pallet requires specific configuration settings—for example, the specific parameters and types required to perform the functions that the pallet implements. -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. -The Nicks pallet allows blockchain users to pay a deposit to reserve a nickname for an account they control. It implements the following functions: - -- The `set_name` function to collect a deposit and set the name of an account if the name is not already taken. -- The `clear_name` function to remove the name associated with an account and return the deposit. -- The `kill_name` function to forcibly remove an account name without returning the deposit. +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. +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. 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. @@ -46,7 +42,7 @@ By completing this tutorial, you will use the Nicks pallet to accomplish the fol - See changes to the runtime by interacting with the new pallet using the front-end template. -## Add the Nicks pallet dependencies +## Add the Lottery pallet dependencies 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. @@ -59,7 +55,7 @@ Because the Substrate runtime compiles to both a native platform binary that inc For information about adding dependencies in `Cargo.toml` files, see [Dependencies](https://doc.rust-lang.org/cargo/guide/dependencies.html) in the Cargo documentation. 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. -To add the dependencies for the Nicks pallet to the runtime: +To add the dependencies for the Lottery pallet to the runtime: 1. Open a terminal shell and change to the root directory for the node template. @@ -67,24 +63,24 @@ To add the dependencies for the Nicks pallet to the runtime: 1. Locate the [dependencies] section and note how other pallets are imported. -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. +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. For example, add a line similar to the following: ```toml - pallet-nicks = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", branch = "polkadot-v1.0.0" } + pallet-lottery = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-v1.9.0", default-features = false } ``` - This line imports the `pallet-nicks` crate as a dependency and specifies the following: + This line imports the `pallet-lottery` crate as a dependency and specifies the following: - Version to identify which version of the crate you want to import. - The default behavior for including pallet features when compiling the runtime with the standard Rust libraries. - - Repository location for retrieving the `pallet-nicks` crate. - - 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. + - Repository location for retrieving the `pallet-lottery` crate. + - 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. These details should be the same for every pallet in any given version of the node template. -1. Add the `pallet-nicks/std` features to the list of `features` to enable when compiling the runtime. +1. Add the `pallet-lottery/std` features to the list of `features` to enable when compiling the runtime. ```toml [features] @@ -93,7 +89,7 @@ To add the dependencies for the Nicks pallet to the runtime: ... "pallet-aura/std", "pallet-balances/std", - "pallet-nicks/std", + "pallet-lottery/std", ... ] ``` @@ -117,19 +113,25 @@ The `Config` trait is used to identify the parameters and types that the pallet Most of the pallet-specific code required to add a pallet is implemented using the `Config` trait. 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. -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). +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). -For this tutorial, you can see that the `Config` trait in the `nicks` pallet declares the following types: +For this tutorial, you can see that the `Config` trait in the `lottery` pallet declares the following types: ```rust -pub trait Config: Config { - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - type Currency: ReservableCurrency; - type ReservationFee: Get<<::Currency as Currency<::AccountId>>::Balance>; - type Slashed: OnUnbalanced<<::Currency as Currency<::AccountId>>::NegativeImbalance>; - type ForceOrigin: EnsureOrigin; - type MinLength: Get; - type MaxLength: Get; +pub trait Config: frame_system::Config { + type PalletId: Get; + type RuntimeCall: Parameter + + Dispatchable + + GetDispatchInfo + + From>; + type Currency: ReservableCurrency; + type Randomness: Randomness>; + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + type ManagerOrigin: EnsureOrigin; + type MaxCalls: Get; + type ValidateCall: ValidateCall; + type MaxGenerateRandom: Get; + type WeightInfo: WeightInfo; } ``` @@ -172,62 +174,72 @@ To review the `Config` trait for the Balances pallet: 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. For example, this `impl` block configures the Balances pallet to use the `u128` type to track balances. -## Implement the configuration for Nicks +## Implement the configuration for Lottery 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. -To implement the `nicks` pallet in your runtime: +To implement the `lottery` pallet in your runtime: 1. Open the `runtime/src/lib.rs` file in a text editor. 1. Locate the last line of the Balances code block. -1. Add the following code block for the Nicks pallet: +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. ```rust - impl pallet_nicks::Config for Runtime { - // The Balances pallet implements the ReservableCurrency trait. - // `Balances` is defined in `construct_runtime!` macro. - type Currency = Balances; - - // Set ReservationFee to a value. - type ReservationFee = ConstU128<100>; - - // 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; + impl pallet_insecure_randomness_collective_flip::Config for Runtime {} + ``` - // Set MinLength of nick name to a desired value. - type MinLength = ConstU32<8>; +1. Add the following code block for the Lottery pallet: - // Set MaxLength of nick name to a desired value. - type MaxLength = ConstU32<32>; + ```rust + parameter_types! { + pub const LotteryPalletId: PalletId = PalletId(*b"py/lotto"); + pub const MaxCalls: u32 = 10; + pub const MaxGenerateRandom: u32 = 10; + } - // The ubiquitous event type. - type RuntimeEvent = RuntimeEvent; + impl pallet_lottery::Config for Runtime { + type PalletId = LotteryPalletId; + type RuntimeCall = RuntimeCall; + type Currency = Balances; + type Randomness = RandomnessCollectiveFlip; + type RuntimeEvent = RuntimeEvent; + type ManagerOrigin = EnsureRoot; + type MaxCalls = MaxCalls; + type ValidateCall = Lottery; + type MaxGenerateRandom = MaxGenerateRandom; + type WeightInfo = pallet_lottery::weights::SubstrateWeight; } + ``` -1. Add Nicks to the `construct_runtime!` macro. +1. Add Lottery to the `runtime` module. For example: ```rust - construct_runtime!( - pub enum Runtime where - Block = Block, - NodeBlock = opaque::Block, - UncheckedExtrinsic = UncheckedExtrinsic - { - /* --snip-- */ - Balances: pallet_balances, - - /*** Add This Line ***/ - Nicks: pallet_nicks, - } - ); + #[frame_support::runtime] + mod runtime { + #[runtime::runtime] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeFreezeReason, + RuntimeHoldReason, + RuntimeSlashReason, + RuntimeLockId, + RuntimeTask + )] + ... + + #[runtime::pallet_index(8)] + pub type Lottery = pallet_lottery; + + #[runtime::pallet_index(9)] + pub type RandomnessCollectiveFlip = pallet_insecure_randomness_collective_flip; + } ``` 1. Save your changes and close the file. @@ -248,7 +260,7 @@ To implement the `nicks` pallet in your runtime: ## Start the blockchain node -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. +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. To start the local Substrate node: @@ -274,7 +286,7 @@ To start the local Substrate node: ## Start the front-end template -Now that you have added a new pallet to your runtime, you can use the [Substrate front-end template](/tutorials/build-a-blockchain/build-local-blockchain/#install-the-front-end-template) to interact with the node template and access the Nicks pallet. +Now that you have added a new pallet to your runtime, you can use the [Substrate front-end template](/tutorials/build-a-blockchain/build-local-blockchain/#install-the-front-end-template) or [Polkadot/Substrate Portal](https://polkadot.js.org/apps/#/explorer?rpc=ws://localhost:9944) (used in this tutorial) to interact with the node template and access the Lottery pallet. To start the front-end template: @@ -290,62 +302,41 @@ To start the front-end template: 1. Open `http://localhost:8000/` in a browser to view the front-end template. -## Set a nickname using the Nicks pallet - -After you start the front-end template, you can use it to interact with the Nicks pallet you just added to the runtime. - -To set a nickname for an account: - -1. Check the account selection list to verify that the Alice account is currently selected. - -1. In the Pallet Interactor component, verify that **Extrinsic** is selected. - -2. Select **nicks** from the list of pallets available to call. - -3. Select [**setName**](https://paritytech.github.io/substrate/master/pallet_nicks/pallet/enum.Call.html#variant.set_name) as the function to call from the nicks pallet. - -4. Type a **name** that is longer than the `MinNickLength` (8 characters) and no longer than the `MaxNickLength` (32 characters). - - ![Select the pallet and the function to call](/media/images/docs/tutorials/add-a-pallet/set-name-function.png) - -5. Click **Signed** to execute the function. - -6. Observe the status of the call change from Ready to InBlock to Finalized and the note the [events](https://paritytech.github.io/substrate/master/pallet_nicks/pallet/enum.Event.html) emitted by the Nicks pallet. - - ![Successful update to the nickname for Alice](/media/images/docs/tutorials/add-a-pallet/set-name-result.png) +## Launch a lottery using the Lottery pallet -## Query information for an account using the Nicks pallet +After you start the front-end template, you can use it to interact with the Lottery pallet you just added to the runtime. -Next, you can use Query capability to read the value of Alice's nickname from the runtime storage for the Nicks pallet. +To set startLottery's parameters: -To return the information stored for Alice: +1. According to our configuration, we need to have administrative privileges. In the `Developer` menu, verify that **Sudo** is selected. -1. In the Pallet Interactor component, select **Query** as the Interaction Type. +2. Select **lottery** from the list of pallets available to call. -1. Select **nicks** from the list of pallets available to query. +3. Select **startLottery** as the function to call from the lottery pallet. -1. Select [**nameOf**](https://paritytech.github.io/substrate/master/pallet_nicks/pallet/enum.Call.html#variant.set_name) as the function to call. +4. Set up a new lottery by filling up the 4 parameters: -1. Copy and paste the address for the **alice** account in the AccountId field, then click **Query**. + - `price`: The cost of a single ticket. + - `length`: How long the lottery should run for starting at the current block. + - `delay`: How long after the lottery end we should wait before picking a winner. + - `repeat`: If the lottery should repeat when completed. - ![Read a name](/media/images/docs/tutorials/add-a-pallet/Alice-query-result.png) + ![Select the pallet and the function to call](/media/images/docs/tutorials/add-a-pallet/start-lottery-function.png) - The return type is a tuple that contains two values: +5. Click **Submit Sudo** to execute the function. - - The hex-encoded nickname for the Alice account `53756273747261746520737570657273746172202d20416c696365`. - If you convert the hex-encoded value to a string, you'll see the name you specified for the `setName` function. - - The amount that was reserved from Alice's account to secure the nickname (`100`). +6. Observe the status of the call change from Ready to InBlock to Finalized and the note the events emitted by the Lottery pallet. - If you were to query the Nicks pallet for the `nameOf` for Bob's account, you would see the value `None` returned because Bob has not invoked the `setName` function to reserve a nickname. + ![Successfully select winner](/media/images/docs/tutorials/add-a-pallet/start-lottery-result.png) ## Explore additional functions This tutorial illustrates how to add a simple pallet to the runtime and demonstrates how to interact with the new pallet using the predefined front-end template. -In this case, you added the `nicks` pallet to the runtime and called the `set_name` and `nameOf` functions using the front-end template. -The `nicks` pallet also provides two additional functions—the `clear_name` function and the `kill_name` function—that enable an account owner to remove the reserved name or a root-level user to forcibly remove an account name. -You can learn about additional features—such as the use of the Sudo pallet and origin accounts—by exploring how these functions work. +In this case, you added the `lottery` pallet to the runtime and called the `start_lottery` functions using the front-end template. +The `lottery` pallet also provides other functions like the `stop_repeat` function. +You can learn about additional features—such as giving Alice account the Sudo privileges. However, these features are beyond the intended scope of this tutorial. -If you want to explore additional features exposed through the Nicks and Sudo pallets, see [Next steps](#next-steps) and select [Specify the origin for a call](/tutorials/build-application-logic/specify-the-origin-for-a-call). +If you want to explore additional features exposed through the Lottery and Sudo pallets, see [Next steps](#next-steps) and select [Specify the origin for a call](/tutorials/build-application-logic/specify-the-origin-for-a-call). ## Next steps diff --git a/content/media/images/docs/tutorials/add-a-pallet/Alice-query-result.png b/content/media/images/docs/tutorials/add-a-pallet/Alice-query-result.png deleted file mode 100644 index 14b356d86..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/Alice-query-result.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/badOrigin.png b/content/media/images/docs/tutorials/add-a-pallet/badOrigin.png deleted file mode 100644 index 0a4ed5765..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/badOrigin.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/killName-success.png b/content/media/images/docs/tutorials/add-a-pallet/killName-success.png deleted file mode 100644 index 086b32c8e..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/killName-success.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/set-name-events.png b/content/media/images/docs/tutorials/add-a-pallet/set-name-events.png deleted file mode 100644 index c14bfbd1f..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/set-name-events.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/set-name-function.png b/content/media/images/docs/tutorials/add-a-pallet/set-name-function.png deleted file mode 100644 index 6b060f500..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/set-name-function.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/set-name-result.png b/content/media/images/docs/tutorials/add-a-pallet/set-name-result.png deleted file mode 100644 index 3cff0f5f8..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/set-name-result.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/start-lottery-function.png b/content/media/images/docs/tutorials/add-a-pallet/start-lottery-function.png new file mode 100644 index 000000000..20070ca0a Binary files /dev/null and b/content/media/images/docs/tutorials/add-a-pallet/start-lottery-function.png differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/start-lottery-result.png b/content/media/images/docs/tutorials/add-a-pallet/start-lottery-result.png new file mode 100644 index 000000000..b0d3a0c17 Binary files /dev/null and b/content/media/images/docs/tutorials/add-a-pallet/start-lottery-result.png differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/sudo-error.png b/content/media/images/docs/tutorials/add-a-pallet/sudo-error.png deleted file mode 100644 index 4dccd192f..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/sudo-error.png and /dev/null differ diff --git a/content/media/images/docs/tutorials/add-a-pallet/sudo-tx.png b/content/media/images/docs/tutorials/add-a-pallet/sudo-tx.png deleted file mode 100644 index 4a022d167..000000000 Binary files a/content/media/images/docs/tutorials/add-a-pallet/sudo-tx.png and /dev/null differ diff --git a/example.env.development b/example.env.development deleted file mode 100644 index ee31fa503..000000000 --- a/example.env.development +++ /dev/null @@ -1,2 +0,0 @@ -# GATSBY_WEBSITE_URL=http://localhost:8100 -# GATSBY_DOCS_URL=http://localhost:8200 diff --git a/example.env.production b/example.env.production deleted file mode 100644 index ee31fa503..000000000 --- a/example.env.production +++ /dev/null @@ -1,2 +0,0 @@ -# GATSBY_WEBSITE_URL=http://localhost:8100 -# GATSBY_DOCS_URL=http://localhost:8200 diff --git a/package.json b/package.json index f59f8e54e..aab669604 100644 --- a/package.json +++ b/package.json @@ -21,8 +21,8 @@ "lint:fix": "eslint --fix --ext js --ext jsx .", "extract-locales": "yarn run babel --config-file ./babel-extract-locales.config.js -o tmp/chunk.js 'src/**/*.{js,jsx}' && rm -rf tmp", "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", - "checklinks-external": "find content/md -name \\*.md -print0 | xargs -0 -n1 markdown-link-check -p -c .github/workflows/mlc_config.json" - }, + "checklinks-external": "find content/md -name \\*.md -print0 | xargs -0 -n1 markdown-link-check -p -c .github/workflows/mlc_config.json" + }, "dependencies": { "aos": "^2.3.4", "classnames": "^2.3.1", @@ -102,4 +102,4 @@ "url": "https://github.com/gatsbyjs/gatsby/issues" }, "packageManager": "yarn@3.2.1" -} +} \ No newline at end of file diff --git a/plugins/gatsby-plugin-substrate b/plugins/gatsby-plugin-substrate index 798d7c609..ca1dd93b2 160000 --- a/plugins/gatsby-plugin-substrate +++ b/plugins/gatsby-plugin-substrate @@ -1 +1 @@ -Subproject commit 798d7c609fa30f9c1f81f795b6862d9debe64c01 +Subproject commit ca1dd93b20b545abf6dee09332717904f0bbbb07