From 4cdf87dba557d5f5fd0cc078bd9dddf30fa0fdbc Mon Sep 17 00:00:00 2001 From: yrong Date: Mon, 17 May 2021 21:38:00 +0800 Subject: [PATCH] transact xcm test --- .gitignore | 1 + Cargo.lock | 1 + rococo-parachains/pallets/ping/Cargo.toml | 2 +- rococo-parachains/pallets/ping/src/lib.rs | 50 +++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1269ff2824e..07e2dd7f6c1 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .vscode .DS_Store /.cargo/config +data diff --git a/Cargo.lock b/Cargo.lock index f352e9595d3..f2537ed1d67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1447,6 +1447,7 @@ dependencies = [ "cumulus-primitives-core", "frame-support", "frame-system", + "log", "parity-scale-codec", "serde", "sp-runtime", diff --git a/rococo-parachains/pallets/ping/Cargo.toml b/rococo-parachains/pallets/ping/Cargo.toml index 301e0debf09..056c0ee800c 100644 --- a/rococo-parachains/pallets/ping/Cargo.toml +++ b/rococo-parachains/pallets/ping/Cargo.toml @@ -7,7 +7,7 @@ version = "0.1.0" [dependencies] codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] } serde = { version = "1.0.101", optional = true, features = ["derive"] } - +log = "0.4.14" sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-v1" } sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-v1" } frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "rococo-v1" } diff --git a/rococo-parachains/pallets/ping/src/lib.rs b/rococo-parachains/pallets/ping/src/lib.rs index 56d7dc2ea5a..1933c258dfb 100644 --- a/rococo-parachains/pallets/ping/src/lib.rs +++ b/rococo-parachains/pallets/ping/src/lib.rs @@ -24,9 +24,22 @@ use frame_system::Config as SystemConfig; use cumulus_primitives_core::ParaId; use cumulus_pallet_xcm::{Origin as CumulusOrigin, ensure_sibling_para}; use xcm::v0::{Xcm, Error as XcmError, SendXcm, OriginKind, MultiLocation, Junction}; +use codec::{Decode, Encode}; pub use pallet::*; +#[derive(Encode, Decode)] +pub enum RelayTemplatePalletCall { + #[codec(index = 100)] // the index should match the position of the module in `construct_runtime!` + DoSomething(DoSomethingCall), +} + +#[derive(Encode, Decode)] +pub enum DoSomethingCall { + #[codec(index = 0)] // the index should match the position of the dispatchable in the target pallet + Something(u32), +} + #[frame_support::pallet] pub mod pallet { use frame_support::pallet_prelude::*; @@ -88,6 +101,8 @@ pub mod pallet { ErrorSendingPing(XcmError, ParaId, u32, Vec), ErrorSendingPong(XcmError, ParaId, u32, Vec), UnknownPong(ParaId, u32, Vec), + TestMsg(u32), + ErrorSendingTest(), } #[pallet::error] @@ -117,6 +132,41 @@ pub mod pallet { } } } + log::info!( + target: "ping", + "Begin construct relay transact" + ); + let some_value:u32 = 10; + let call = RelayTemplatePalletCall::DoSomething(DoSomethingCall::Something(some_value)).encode(); + + let msg = Xcm::Transact { + origin_type: OriginKind::SovereignAccount, + require_weight_at_most: 1_000, + call:call.into(), + }; + + log::info!( + target: "ping", + "Relay transact {:?}", + msg, + ); + + match T::XcmSender::send_xcm(MultiLocation::X1(Junction::Parent), msg){ + Ok(()) => { + Self::deposit_event(Event::TestMsg(some_value)); + log::info!( + target: "ping", + "Relay transact sent success!" + ); + }, + Err(e) => { + Self::deposit_event(Event::ErrorSendingTest()); + log::error!( + target: "ping", + "Relay transact sent failed!" + ); + } + } } }