-
Notifications
You must be signed in to change notification settings - Fork 1.1k
XCM Cookbook #2633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
XCM Cookbook #2633
Changes from 8 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
6bcd6a0
Move XCM docs to rust docs
franciscoaguirre 8bbde01
Add an XCM configuration guide
franciscoaguirre 7ac4e96
Move configuration to cookbook instead
franciscoaguirre 1475b56
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre 6556e06
Add relay_token_transactor example to cookbook
franciscoaguirre ddb2ea8
Put back what got deleted from xcm-simulator-example
franciscoaguirre 9d7d8df
Publish and license
franciscoaguirre 51aec90
".git/.scripts/commands/fmt/fmt.sh"
cb50a66
fix(xcm-docs): Update to XCM v4
franciscoaguirre dc97b47
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre 7619a74
chore(xcm-docs): Replace deprecated CurrencyAdapter with FungibleAdapter
franciscoaguirre d6badb1
Update polkadot/xcm/docs/src/cookbook/relay_token_transactor/mod.rs
franciscoaguirre 2df362a
Update polkadot/xcm/docs/src/cookbook/relay_token_transactor/mod.rs
franciscoaguirre 4d78127
Update polkadot/xcm/docs/src/cookbook/relay_token_transactor/mod.rs
franciscoaguirre 38b9533
Update polkadot/xcm/docs/src/glossary.rs
franciscoaguirre f038965
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre 834492f
Update polkadot/xcm/docs/src/lib.rs
franciscoaguirre f7278b6
Update polkadot/xcm/docs/src/glossary.rs
franciscoaguirre 0d3092f
Update polkadot/xcm/docs/src/cookbook/relay_token_transactor/tests.rs
franciscoaguirre cd6746d
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre 3001aa8
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre d2605cf
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre a2b8978
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre 3d022af
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre 1863b25
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre 17aeea0
Update polkadot/xcm/docs/src/fundamentals.rs
franciscoaguirre 845cb40
docs(xcm-docs): Improve relay token transactor recipe
franciscoaguirre 521be8c
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre 6f5e5b1
WIP
franciscoaguirre e21a588
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre a07ef52
fix: Cargo.lock
franciscoaguirre 7a658d4
doc(xcm-docs): more explanations and diagrams
franciscoaguirre 0de7bd4
feat: move mock_message_queue to xcm-simulator
franciscoaguirre 3c603e4
fix: fmt
franciscoaguirre ae33cc7
feat: address feedback
franciscoaguirre bb564e2
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre b918c90
fix(xcm-cookbook): add license to every file
franciscoaguirre e9a1a58
fix(xcm-docs): fmt
franciscoaguirre 8c0721c
fix(xcm-simulator): getters were removed
franciscoaguirre 8ce59e4
fix(xcm-simulator-example): fmt
franciscoaguirre c919085
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre f35fe7a
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre 2f469b3
fix: revert Cargo.lock
franciscoaguirre c657f5c
Merge branch 'master' into cis-xcm-rust-docs
franciscoaguirre 3b76222
fix: update Cargo.lock
franciscoaguirre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| [package] | ||
| name = "xcm-docs" | ||
| description = "Documentation and guides for XCM" | ||
| version = "0.0.1" | ||
| license = "GPL-3.0-or-later WITH Classpath-exception-2.0" | ||
| repository.workspace = true | ||
| authors.workspace = true | ||
| edition.workspace = true | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| # For XCM stuff | ||
| xcm = { path = "../../xcm", package = "staging-xcm" } | ||
| xcm-executor = { path = "../../xcm/xcm-executor", package = "staging-xcm-executor" } | ||
| xcm-builder = { path = "../../xcm/xcm-builder", package = "staging-xcm-builder" } | ||
| xcm-simulator = { path = "../../xcm/xcm-simulator" } | ||
| pallet-xcm = { path = "../../xcm/pallet-xcm" } | ||
|
|
||
| # For building FRAME runtimes | ||
| frame = { path = "../../../substrate/frame", features = ["experimental", "runtime"] } | ||
| codec = { package = "parity-scale-codec", version = "3.6.9" } | ||
| scale-info = { version = "2.6.0", default-features = false } | ||
| polkadot-parachain-primitives = { path = "../../../polkadot/parachain" } | ||
| polkadot-runtime-parachains = { path = "../../../polkadot/runtime/parachains" } | ||
| polkadot-primitives = { path = "../../../polkadot/primitives" } | ||
| sp-runtime = { path = "../../../substrate/primitives/runtime" } | ||
| sp-std = { path = "../../../substrate/primitives/std" } | ||
| sp-io = { path = "../../../substrate/primitives/io" } | ||
|
|
||
| # Some pallets | ||
| pallet-message-queue = { path = "../../../substrate/frame/message-queue" } | ||
| pallet-balances = { path = "../../../substrate/frame/balances" } | ||
|
|
||
| # For building docs | ||
| simple-mermaid = { git = "https://github.com/kianenigma/simple-mermaid.git", branch = "main" } | ||
| docify = "0.2.6" | ||
|
|
||
| [dev-dependencies] | ||
| test-log = "0.2.14" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| flowchart | ||
| relay[Relaychain] --> paraA["Parachain(1000)"] | ||
| relay --> paraB["Parachain(2000)"] | ||
|
|
||
| paraA --> pallet[Pallet] | ||
| pallet --> indexA[Index 1] | ||
| pallet --> indexB[Index 2] | ||
|
|
||
| paraA --> account[Account] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| flowchart | ||
| docs[xcm_docs] --> fundamentals | ||
| docs --> guides | ||
| docs --> cookbook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| flowchart | ||
| universe[Universal Location] --> polkadot[Polkadot] | ||
| universe --> ethereum[Ethereum] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| flowchart | ||
| relay[Polkadot] --> assetHub["Asset Hub"] | ||
| relay --> anotherPara["Another parachain"] | ||
|
|
||
| assetHub --> assetsPallet["Foreign Assets Pallet"] | ||
| assetsPallet --> usdt[1984] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //! # XCM Cookbook | ||
| //! | ||
| //! A collection of tested examples to do useful things in XCM. | ||
|
|
||
| /// Configuring a parachain that only uses the relay chain token. | ||
| /// Useful for a parachain that only wants to deal with DOT. | ||
| pub mod relay_token_transactor; | ||
35 changes: 35 additions & 0 deletions
35
polkadot/xcm/docs/src/cookbook/relay_token_transactor/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| //! # Relay asset transactor | ||
| //! | ||
| //! This example shows how to configure a parachain to only deal with the relay chain token. | ||
franciscoaguirre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! The first step is using the [`xcm_builder::CurrencyAdapter`] to create an `AssetTransactor` that | ||
| //! can handle the relay chain token. | ||
| #![doc = docify::embed!("src/cookbook/relay_token_transactor/parachain/xcm_config.rs", asset_transactor)] | ||
| //! | ||
| //! The second step is to configure `IsReserve` to recognize the relay chain as a reserve for its | ||
| //! own asset. | ||
| //! With this, you'll be able to easily get derivatives from the relay chain by using the xcm | ||
| //! pallet's `transfer_assets` extrinsic. | ||
franciscoaguirre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //! | ||
| //! The `IsReserve` type takes a type that implements `ContainsPair<MultiAsset, MultiLocation>`. | ||
| //! In this case, we want a type that contains the pair `(relay_chain_native_token, relay_chain)`. | ||
| #![doc = docify::embed!("src/cookbook/relay_token_transactor/parachain/xcm_config.rs", is_reserve)] | ||
| //! | ||
| //! With this setup, we are able to do a reserve asset transfer to and from the parachain and relay | ||
| //! chain. | ||
| #![doc = docify::embed!("src/cookbook/relay_token_transactor/tests.rs", reserve_asset_transfers_work)] | ||
| //! | ||
| //! For the rest of the code, be sure to check the contents of this module. | ||
|
|
||
| /// The parachain runtime for this example | ||
| pub mod parachain; | ||
|
|
||
| /// The relay chain runtime for this example | ||
| pub mod relay_chain; | ||
|
|
||
| /// The network for this example | ||
| pub mod network; | ||
|
|
||
| /// Tests for this example | ||
| #[cfg(test)] | ||
| pub mod tests; | ||
franciscoaguirre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
74 changes: 74 additions & 0 deletions
74
polkadot/xcm/docs/src/cookbook/relay_token_transactor/network.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //! Mock network | ||
|
|
||
| use frame::deps::{ | ||
| frame_system, | ||
| sp_io::TestExternalities, | ||
| sp_runtime::{AccountId32, BuildStorage}, | ||
| }; | ||
| use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt}; | ||
|
|
||
| use super::{parachain, relay_chain}; | ||
|
|
||
| pub const ALICE: AccountId32 = AccountId32::new([0u8; 32]); | ||
| pub const BOB: AccountId32 = AccountId32::new([1u8; 32]); | ||
| pub const UNITS: u64 = 10_000_000_000; | ||
| pub const CENTS: u64 = 100_000_000; | ||
| pub const INITIAL_BALANCE: u64 = 1 * UNITS; | ||
|
|
||
| decl_test_parachain! { | ||
| pub struct ParaA { | ||
| Runtime = parachain::Runtime, | ||
| XcmpMessageHandler = parachain::MessageQueue, | ||
| DmpMessageHandler = parachain::MessageQueue, | ||
| new_ext = para_ext(), | ||
| } | ||
| } | ||
|
|
||
| decl_test_relay_chain! { | ||
| pub struct Relay { | ||
| Runtime = relay_chain::Runtime, | ||
| RuntimeCall = relay_chain::RuntimeCall, | ||
| RuntimeEvent = relay_chain::RuntimeEvent, | ||
| XcmConfig = relay_chain::XcmConfig, | ||
| MessageQueue = relay_chain::MessageQueue, | ||
| System = relay_chain::System, | ||
| new_ext = relay_ext(), | ||
| } | ||
| } | ||
|
|
||
| decl_test_network! { | ||
| pub struct MockNet { | ||
| relay_chain = Relay, | ||
| parachains = vec![ | ||
| (2222, ParaA), | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| pub fn para_ext() -> TestExternalities { | ||
| use parachain::{MessageQueue, Runtime, System}; | ||
|
|
||
| let t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap(); | ||
| let mut ext = frame::deps::sp_io::TestExternalities::new(t); | ||
| ext.execute_with(|| { | ||
| System::set_block_number(1); | ||
| MessageQueue::set_para_id(2222.into()); | ||
| }); | ||
| ext | ||
| } | ||
|
|
||
| pub fn relay_ext() -> TestExternalities { | ||
| use relay_chain::{Runtime, System}; | ||
|
|
||
| let mut t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap(); | ||
|
|
||
| pallet_balances::GenesisConfig::<Runtime> { balances: vec![(ALICE, INITIAL_BALANCE)] } | ||
| .assimilate_storage(&mut t) | ||
| .unwrap(); | ||
|
|
||
| let mut ext = TestExternalities::new(t); | ||
| ext.execute_with(|| { | ||
| System::set_block_number(1); | ||
| }); | ||
| ext | ||
| } |
40 changes: 40 additions & 0 deletions
40
polkadot/xcm/docs/src/cookbook/relay_token_transactor/parachain/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| //! # Runtime | ||
|
|
||
| use frame::{deps::frame_system, prelude::*, runtime::prelude::*, traits::IdentityLookup}; | ||
| use xcm_executor::XcmExecutor; | ||
|
|
||
| mod xcm_config; | ||
| use crate::mock_message_queue; | ||
| use xcm_config::XcmConfig; | ||
|
|
||
| pub type Block = frame_system::mocking::MockBlock<Runtime>; | ||
| pub type AccountId = frame::deps::sp_runtime::AccountId32; | ||
| pub type Balance = u64; | ||
|
|
||
| construct_runtime! { | ||
| pub struct Runtime { | ||
| System: frame_system, | ||
| MessageQueue: mock_message_queue, | ||
| Balances: pallet_balances, | ||
| XcmPallet: pallet_xcm, | ||
| } | ||
| } | ||
|
|
||
| #[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)] | ||
| impl frame_system::Config for Runtime { | ||
| type Block = Block; | ||
| type AccountId = AccountId; | ||
| type Lookup = IdentityLookup<AccountId>; | ||
| type AccountData = pallet_balances::AccountData<Balance>; | ||
| } | ||
|
|
||
| impl mock_message_queue::Config for Runtime { | ||
| type RuntimeEvent = RuntimeEvent; | ||
| type XcmExecutor = XcmExecutor<XcmConfig>; | ||
| } | ||
|
|
||
| #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)] | ||
| impl pallet_balances::Config for Runtime { | ||
| type Balance = Balance; | ||
| type AccountStore = System; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.