Skip to content

Commit

Permalink
Update add-pallets-to-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucca committed Jan 7, 2025
1 parent d77895b commit 231f17b
Show file tree
Hide file tree
Showing 35 changed files with 6,358 additions and 988 deletions.
5,041 changes: 4,207 additions & 834 deletions .snippets/code/Cargo.lock

Large diffs are not rendered by default.

100 changes: 95 additions & 5 deletions .snippets/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,108 @@ repository = "https://github.com/paritytech/polkadot-sdk-parachain-template.git"
edition = "2021"

[workspace]
members = ["tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet"]
members = [
"tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet",
"tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-unit-testing",
"tutorials/polkadot-sdk/parachains/zero-to-hero/add-pallets-to-runtime/runtime",
]
resolver = "2"

[workspace.dependencies]
clap = { version = "4.5.10" }
codec = { version = "3.6.12", default-features = false, package = "parity-scale-codec" }
frame-support = { version = "38.0.0", default-features = false }
frame-system = { version = "38.0.0", default-features = false }
scale-info = { version = "2.11.1", default-features = false }
sp-runtime = { version = "39.0.5", default-features = false }
color-print = { version = "0.3.4" }
cumulus-client-cli = { version = "0.18.0", default-features = false }
cumulus-client-collator = { version = "0.18.0", default-features = false }
cumulus-client-consensus-aura = { version = "0.18.0", default-features = false }
cumulus-client-consensus-common = { version = "0.18.0", default-features = false }
cumulus-client-consensus-proposer = { version = "0.16.0", default-features = false }
cumulus-client-service = { version = "0.19.0", default-features = false }
cumulus-primitives-core = { version = "0.16.0", default-features = false }
cumulus-primitives-parachain-inherent = { version = "0.16.0", default-features = false }
cumulus-relay-chain-interface = { version = "0.18.0", default-features = false }
docify = { version = "0.2.8" }
frame-benchmarking = { version = "38.0.0", default-features = false }
frame-benchmarking-cli = { version = "43.0.0", default-features = false }
futures = { version = "0.3.30" }
jsonrpsee = { version = "0.24.3" }
log = { version = "0.4.22", default-features = false }
pallet-transaction-payment-rpc = { version = "41.0.0", default-features = false }
polkadot-cli = { version = "19.0.0", default-features = false }
polkadot-primitives = { version = "16.0.0", default-features = false }
prometheus-endpoint = { version = "0.17.0", default-features = false, package = "substrate-prometheus-endpoint" }
sc-basic-authorship = { version = "0.45.0", default-features = false }
sc-chain-spec = { version = "38.0.0", default-features = false }
sc-cli = { version = "0.47.0", default-features = false }
sc-client-api = { version = "37.0.0", default-features = false }
sc-consensus = { version = "0.44.0", default-features = false }
sc-executor = { version = "0.40.1", default-features = false }
sc-network = { version = "0.45.0", default-features = false }
sc-network-sync = { version = "0.44.0", default-features = false }
sc-offchain = { version = "40.0.0", default-features = false }
sc-rpc = { version = "40.0.0", default-features = false }
sc-service = { version = "0.46.0", default-features = false }
sc-sysinfo = { version = "38.0.0", default-features = false }
sc-telemetry = { version = "25.0.0", default-features = false }
sc-tracing = { version = "37.0.1", default-features = false }
sc-transaction-pool = { version = "37.0.0", default-features = false }
sc-transaction-pool-api = { version = "37.0.0", default-features = false }
serde = { version = "1.0.209", default-features = false }
serde_json = { version = "1.0.127", default-features = false }
sp-api = { version = "34.0.0", default-features = false }
sp-block-builder = { version = "34.0.0", default-features = false }
sp-blockchain = { version = "37.0.1", default-features = false }
sp-consensus-aura = { version = "0.40.0", default-features = false }
sp-core = { version = "34.0.0", default-features = false }
sp-genesis-builder = { version = "0.15.1", default-features = false }
sp-io = { version = "38.0.0", default-features = false }
sp-keystore = { version = "0.40.0", default-features = false }
sp-runtime = { version = "39.0.1", default-features = false }
sp-timestamp = { version = "34.0.0", default-features = false }
substrate-frame-rpc-system = { version = "39.0.0", default-features = false }
xcm = { version = "14.2.0", default-features = false, package = "staging-xcm" }
substrate-build-script-utils = { version = "11.0.0", default-features = false }
cumulus-pallet-aura-ext = { version = "0.17.0", default-features = false }
cumulus-pallet-parachain-system = { version = "0.17.0", default-features = false }
cumulus-pallet-session-benchmarking = { version = "19.0.0", default-features = false }
cumulus-pallet-xcm = { version = "0.17.0", default-features = false }
cumulus-pallet-xcmp-queue = { version = "0.17.0", default-features = false }
cumulus-primitives-aura = { version = "0.15.0", default-features = false }
cumulus-primitives-storage-weight-reclaim = { version = "8.0.0", default-features = false }
cumulus-primitives-utility = { version = "0.17.0", default-features = false }
frame-executive = { version = "38.0.0", default-features = false }
frame-metadata-hash-extension = { version = "0.6.0", default-features = false }
frame-support = { version = "38.0.0", default-features = false }
frame-system = { version = "38.0.0", default-features = false }
frame-system-benchmarking = { version = "38.0.0", default-features = false }
frame-system-rpc-runtime-api = { version = "34.0.0", default-features = false }
frame-try-runtime = { version = "0.44.0", default-features = false }
hex-literal = { version = "0.4.1", default-features = false }
pallet-aura = { version = "37.0.0", default-features = false }
pallet-authorship = { version = "38.0.0", default-features = false }
pallet-balances = { version = "39.0.0", default-features = false }
pallet-collator-selection = { version = "19.0.0", default-features = false }
pallet-message-queue = { version = "41.0.0", default-features = false }
pallet-session = { version = "38.0.0", default-features = false }
pallet-sudo = { version = "38.0.0", default-features = false }
pallet-timestamp = { version = "37.0.0", default-features = false }
pallet-transaction-payment = { version = "38.0.0", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { version = "38.0.0", default-features = false }
pallet-xcm = { version = "17.0.0", default-features = false }
parachain-info = { version = "0.17.0", default-features = false, package = "staging-parachain-info" }
parachains-common = { version = "18.0.0", default-features = false }
polkadot-parachain-primitives = { version = "14.0.0", default-features = false }
polkadot-runtime-common = { version = "17.0.0", default-features = false }
scale-info = { version = "2.11.1", default-features = false }
smallvec = { version = "1.11.0", default-features = false }
sp-inherents = { version = "34.0.0", default-features = false }
sp-offchain = { version = "34.0.0", default-features = false }
sp-session = { version = "36.0.0", default-features = false }
sp-transaction-pool = { version = "34.0.0", default-features = false }
sp-version = { version = "37.0.0", default-features = false }
xcm-builder = { version = "17.0.0", default-features = false, package = "staging-xcm-builder" }
xcm-executor = { version = "17.0.0", default-features = false, package = "staging-xcm-executor" }
substrate-wasm-builder = { version = "24.0.1", default-features = false }

[workspace.lints.rust]
suspicious_double_ref_op = { level = "allow", priority = 2 }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "custom-pallet-2"
version = "0.1.0"
license.workspace = true
authors.workspace = true
homepage.workspace = true
repository.workspace = true
edition.workspace = true

[dependencies]
codec = { features = ["derive"], workspace = true }
scale-info = { features = ["derive"], workspace = true }
frame-support.workspace = true
frame-system.workspace = true

[dev-dependencies]
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }

[features]
default = ["std"]
std = ["codec/std", "frame-support/std", "frame-system/std", "scale-info/std"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);

// Configuration trait for the pallet
#[pallet::config]
pub trait Config: frame_system::Config {
// Defines the event type for the pallet
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

// Defines the maximum value the counter can hold
#[pallet::constant]
type CounterMaxValue: Get<u32>;
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// The counter value has been set to a new value by Root.
CounterValueSet {
/// The new value set.
counter_value: u32,
},
/// A user has successfully incremented the counter.
CounterIncremented {
/// The new value set.
counter_value: u32,
/// The account who incremented the counter.
who: T::AccountId,
/// The amount by which the counter was incremented.
incremented_amount: u32,
},
/// A user has successfully decremented the counter.
CounterDecremented {
/// The new value set.
counter_value: u32,
/// The account who decremented the counter.
who: T::AccountId,
/// The amount by which the counter was decremented.
decremented_amount: u32,
},
}

/// Storage for the current value of the counter.
#[pallet::storage]
pub type CounterValue<T> = StorageValue<_, u32>;

/// Storage map to track the number of interactions performed by each account.
#[pallet::storage]
pub type UserInteractions<T: Config> = StorageMap<_, Twox64Concat, T::AccountId, u32>;

#[pallet::error]
pub enum Error<T> {
/// The counter value exceeds the maximum allowed value.
CounterValueExceedsMax,
/// The counter value cannot be decremented below zero.
CounterValueBelowZero,
/// Overflow occurred in the counter.
CounterOverflow,
/// Overflow occurred in user interactions.
UserInteractionOverflow,
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Set the value of the counter.
///
/// The dispatch origin of this call must be _Root_.
///
/// - `new_value`: The new value to set for the counter.
///
/// Emits `CounterValueSet` event when successful.
#[pallet::call_index(0)]
#[pallet::weight(0)]
pub fn set_counter_value(origin: OriginFor<T>, new_value: u32) -> DispatchResult {
ensure_root(origin)?;

ensure!(
new_value <= T::CounterMaxValue::get(),
Error::<T>::CounterValueExceedsMax
);

CounterValue::<T>::put(new_value);

Self::deposit_event(Event::<T>::CounterValueSet {
counter_value: new_value,
});

Ok(())
}

/// Increment the counter by a specified amount.
///
/// This function can be called by any signed account.
///
/// - `amount_to_increment`: The amount by which to increment the counter.
///
/// Emits `CounterIncremented` event when successful.
#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn increment(origin: OriginFor<T>, amount_to_increment: u32) -> DispatchResult {
let who = ensure_signed(origin)?;

let current_value = CounterValue::<T>::get().unwrap_or(0);

let new_value = current_value
.checked_add(amount_to_increment)
.ok_or(Error::<T>::CounterOverflow)?;

ensure!(
new_value <= T::CounterMaxValue::get(),
Error::<T>::CounterValueExceedsMax
);

CounterValue::<T>::put(new_value);

UserInteractions::<T>::try_mutate(&who, |interactions| -> Result<_, Error<T>> {
let new_interactions = interactions
.unwrap_or(0)
.checked_add(1)
.ok_or(Error::<T>::UserInteractionOverflow)?;
*interactions = Some(new_interactions); // Store the new value

Ok(())
})?;

Self::deposit_event(Event::<T>::CounterIncremented {
counter_value: new_value,
who,
incremented_amount: amount_to_increment,
});

Ok(())
}

/// Decrement the counter by a specified amount.
///
/// This function can be called by any signed account.
///
/// - `amount_to_decrement`: The amount by which to decrement the counter.
///
/// Emits `CounterDecremented` event when successful.
#[pallet::call_index(2)]
#[pallet::weight(0)]
pub fn decrement(origin: OriginFor<T>, amount_to_decrement: u32) -> DispatchResult {
let who = ensure_signed(origin)?;

let current_value = CounterValue::<T>::get().unwrap_or(0);

let new_value = current_value
.checked_sub(amount_to_decrement)
.ok_or(Error::<T>::CounterValueBelowZero)?;

CounterValue::<T>::put(new_value);

UserInteractions::<T>::try_mutate(&who, |interactions| -> Result<_, Error<T>> {
let new_interactions = interactions
.unwrap_or(0)
.checked_add(1)
.ok_or(Error::<T>::UserInteractionOverflow)?;
*interactions = Some(new_interactions); // Store the new value

Ok(())
})?;

Self::deposit_event(Event::<T>::CounterDecremented {
counter_value: new_value,
who,
decremented_amount: amount_to_decrement,
});

Ok(())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[package]
name = "pallet-parachain-template"
description = "FRAME pallet template for defining custom runtime logic."
version = "0.1.0"
license = "Unlicense"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
edition.workspace = true
publish = false

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { features = ["derive"], workspace = true }
scale-info = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support.workspace = true
frame-system.workspace = true
sp-runtime.workspace = true

[dev-dependencies]
sp-core = { default-features = true, workspace = true }
sp-io = { default-features = true, workspace = true }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = [
"codec/std",
"scale-info/std",

"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",

"sp-runtime/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


## Release

Polkadot SDK stable2409
Loading

0 comments on commit 231f17b

Please sign in to comment.