Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 170 additions & 15 deletions .ai/categories/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ First, you'll update the runtime's `Cargo.toml` file to include the Utility pall
1. Open the `runtime/Cargo.toml` file and locate the `[dependencies]` section. Add pallet-utility as one of the features for the `polkadot-sdk` dependency with the following line:

```toml hl_lines="4" title="runtime/Cargo.toml"

[dependencies]
...
polkadot-sdk = { workspace = true, features = [
"pallet-utility",
Expand All @@ -267,17 +267,19 @@ First, you'll update the runtime's `Cargo.toml` file to include the Utility pall
2. In the same `[dependencies]` section, add the custom pallet that you built from scratch with the following line:

```toml hl_lines="3" title="Cargo.toml"

[dependencies]
...

custom-pallet = { path = "../pallets/custom-pallet", default-features = false }
```

3. In the `[features]` section, add the custom pallet to the `std` feature list:

```toml hl_lines="5" title="Cargo.toml"

[features]
default = ["std"]
std = [
...

"custom-pallet/std",
...
]
```
Expand Down Expand Up @@ -2699,13 +2701,53 @@ To build the smart contract, follow the steps below:
6. Add the getter and setter functions:

```solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

contract Storage {
// State variable to store our number
uint256 private number;

// Event to notify when the number changes
event NumberChanged(uint256 newNumber);

// Function to store a new number
function store(uint256 newNumber) public {
number = newNumber;
emit NumberChanged(newNumber);
}

// Function to retrieve the stored number
function retrieve() public view returns (uint256) {
return number;
}
}
```

??? code "Complete Storage.sol contract"

```solidity title="Storage.sol"

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

contract Storage {
// State variable to store our number
uint256 private number;

// Event to notify when the number changes
event NumberChanged(uint256 newNumber);

// Function to store a new number
function store(uint256 newNumber) public {
number = newNumber;
emit NumberChanged(newNumber);
}

// Function to retrieve the stored number
function retrieve() public view returns (uint256) {
return number;
}
}
```

## Understanding the Code
Expand Down Expand Up @@ -3343,7 +3385,23 @@ To create the ERC-20 contract, you can follow the steps below:
3. Now, paste the following ERC-20 contract code into the editor:

```solidity title="MyToken.sol"

// SPDX-License-Identifier: MIT
// Compatible with OpenZeppelin Contracts ^5.0.0
pragma solidity ^0.8.22;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20, Ownable {
constructor(address initialOwner)
ERC20("MyToken", "MTK")
Ownable(initialOwner)
{}

function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
```

The key components of the code above are:
Expand Down Expand Up @@ -8224,7 +8282,16 @@ The [`Account` data type](https://paritytech.github.io/polkadot-sdk/master/frame
The code snippet below shows how accounts are defined:

```rs

/// The full account information for a particular account ID.
#[pallet::storage]
#[pallet::getter(fn account)]
pub type Account<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
AccountInfo<T::Nonce, T::AccountData>,
ValueQuery,
>;
```

The preceding code block defines a storage map named `Account`. The `StorageMap` is a type of on-chain storage that maps keys to values. In the `Account` map, the key is an account ID, and the value is the account's information. Here, `T` represents the generic parameter for the runtime configuration, which is defined by the pallet's configuration trait (`Config`).
Expand All @@ -8248,7 +8315,24 @@ For a detailed explanation of storage maps, see the [`StorageMap`](https://parit
The `AccountInfo` structure is another key element within the [System pallet](https://paritytech.github.io/polkadot-sdk/master/src/frame_system/lib.rs.html){target=\_blank}, providing more granular details about each account's state. This structure tracks vital data, such as the number of transactions and the account’s relationships with other modules.

```rs

/// Information of an account.
#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct AccountInfo<Nonce, AccountData> {
/// The number of transactions this account has sent.
pub nonce: Nonce,
/// The number of other modules that currently depend on this account's existence. The account
/// cannot be reaped until this is zero.
pub consumers: RefCount,
/// The number of other modules that allow this account to exist. The account may not be reaped
/// until this and `sufficients` are both zero.
pub providers: RefCount,
/// The number of modules that allow this account to exist for their own purposes only. The
/// account may not be reaped until this and `providers` are both zero.
pub sufficients: RefCount,
/// The additional data that belongs to this account. Used to store the balance(s) in a lot of
/// chains.
pub data: AccountData,
}
```

The `AccountInfo` structure includes the following components:
Expand Down Expand Up @@ -9262,7 +9346,8 @@ The [`XcmRouter`](https://paritytech.github.io/polkadot-sdk/master/pallet_xcm/pa
For instance, the Kusama network employs the [`ChildParachainRouter`](https://paritytech.github.io/polkadot-sdk/master/polkadot_runtime_common/xcm_sender/struct.ChildParachainRouter.html){target=\_blank}, which restricts routing to [Downward Message Passing (DMP)](https://wiki.polkadot.com/learn/learn-xcm-transport/#dmp-downward-message-passing){target=\_blank} from the relay chain to parachains, ensuring secure and controlled communication.

```rust

pub type PriceForChildParachainDelivery =
ExponentialPrice<FeeAssetId, BaseDeliveryFee, TransactionByteFee, Dmp>;
```

For more details about XCM transport protocols, see the [XCM Channels](/develop/interoperability/xcm-channels/){target=\_blank} page.
Expand Down Expand Up @@ -10388,25 +10473,95 @@ The `xcm-emulator` provides macros for defining a mocked testing environment. Ch
- **[`decl_test_relay_chains`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-stable2506-2/cumulus/xcm/xcm-emulator/src/lib.rs#L361){target=\_blank}**: Defines runtime and configuration for the relay chains. Example:

```rust

decl_test_relay_chains! {
#[api_version(13)]
pub struct Westend {
genesis = genesis::genesis(),
on_init = (),
runtime = westend_runtime,
core = {
SovereignAccountOf: westend_runtime::xcm_config::LocationConverter,
},
pallets = {
XcmPallet: westend_runtime::XcmPallet,
Sudo: westend_runtime::Sudo,
Balances: westend_runtime::Balances,
Treasury: westend_runtime::Treasury,
AssetRate: westend_runtime::AssetRate,
Hrmp: westend_runtime::Hrmp,
Identity: westend_runtime::Identity,
IdentityMigrator: westend_runtime::IdentityMigrator,
}
},
}
```

- **[`decl_test_parachains`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-stable2506-2/cumulus/xcm/xcm-emulator/src/lib.rs#L596){target=\_blank}**: Defines runtime and configuration for parachains. Example:

```rust

decl_test_parachains! {
pub struct AssetHubWestend {
genesis = genesis::genesis(),
on_init = {
asset_hub_westend_runtime::AuraExt::on_initialize(1);
},
runtime = asset_hub_westend_runtime,
core = {
XcmpMessageHandler: asset_hub_westend_runtime::XcmpQueue,
LocationToAccountId: asset_hub_westend_runtime::xcm_config::LocationToAccountId,
ParachainInfo: asset_hub_westend_runtime::ParachainInfo,
MessageOrigin: cumulus_primitives_core::AggregateMessageOrigin,
DigestProvider: (),
},
pallets = {
PolkadotXcm: asset_hub_westend_runtime::PolkadotXcm,
Balances: asset_hub_westend_runtime::Balances,
Assets: asset_hub_westend_runtime::Assets,
ForeignAssets: asset_hub_westend_runtime::ForeignAssets,
PoolAssets: asset_hub_westend_runtime::PoolAssets,
AssetConversion: asset_hub_westend_runtime::AssetConversion,
SnowbridgeSystemFrontend: asset_hub_westend_runtime::SnowbridgeSystemFrontend,
Revive: asset_hub_westend_runtime::Revive,
}
},
}
```

- **[`decl_test_bridges`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-stable2506-2/cumulus/xcm/xcm-emulator/src/lib.rs#L1221){target=\_blank}**: Creates bridges between chains, specifying the source, target, and message handler. Example:

```rust

decl_test_bridges! {
pub struct RococoWestendMockBridge {
source = BridgeHubRococoPara,
target = BridgeHubWestendPara,
handler = RococoWestendMessageHandler
},
pub struct WestendRococoMockBridge {
source = BridgeHubWestendPara,
target = BridgeHubRococoPara,
handler = WestendRococoMessageHandler
}
}
```

- **[`decl_test_networks`](https://github.com/paritytech/polkadot-sdk/blob/polkadot-stable2506-2/cumulus/xcm/xcm-emulator/src/lib.rs#L958){target=\_blank}**: Defines a testing network with relay chains, parachains, and bridges, implementing message transport and processing logic. Example:

```rust

decl_test_networks! {
pub struct WestendMockNet {
relay_chain = Westend,
parachains = vec![
AssetHubWestend,
BridgeHubWestend,
CollectivesWestend,
CoretimeWestend,
PeopleWestend,
PenpalA,
PenpalB,
],
bridge = ()
},
}
```

By leveraging these macros, developers can customize their testing networks by defining relay chains and parachains tailored to their needs. For guidance on implementing a mock runtime for a Polkadot SDK-based chain, refer to the [Pallet Testing](/parachains/customize-runtime/pallet-development/pallet-testing/){target=\_blank} article.
Expand Down
Loading
Loading