Skip to content

Commit 0131af9

Browse files
authored
Utility to generate hex bytes for send-message command (paritytech#1658)
1 parent 86265db commit 0131af9

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

primitives/test-utils/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch
1515
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1616
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
1717

18+
[dev-dependencies]
19+
xcm = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false }
20+
1821
[features]
1922
default = ["std"]
2023
std = [

primitives/test-utils/src/lib.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,47 @@ macro_rules! generate_owned_bridge_module_tests {
299299
}
300300
};
301301
}
302+
303+
#[cfg(test)]
304+
mod tests {
305+
use codec::Encode;
306+
use sp_application_crypto::sp_core::{hexdisplay, hexdisplay::HexDisplay};
307+
use xcm::VersionedXcm;
308+
309+
fn print_xcm<RuntimeCall>(xcm: &VersionedXcm<RuntimeCall>) {
310+
println!("-----------------");
311+
println!("xcm (plain): {:?}", xcm);
312+
println!("xcm (bytes): {:?}", xcm.encode());
313+
println!("xcm (hex): {:?}", hexdisplay::HexDisplay::from(&xcm.encode()));
314+
}
315+
316+
fn as_hex<RuntimeCall>(xcm: &VersionedXcm<RuntimeCall>) -> String {
317+
HexDisplay::from(&xcm.encode()).to_string()
318+
}
319+
320+
pub type RuntimeCall = ();
321+
322+
#[test]
323+
fn generate_versioned_xcm_message_hex_bytes() {
324+
let xcm: xcm::v2::Xcm<RuntimeCall> = xcm::v2::Xcm(vec![xcm::v2::Instruction::Trap(43)]);
325+
let xcm: VersionedXcm<RuntimeCall> = From::from(xcm);
326+
print_xcm(&xcm);
327+
assert_eq!("020419ac", format!("{}", as_hex(&xcm)));
328+
329+
let xcm: xcm::v3::Xcm<RuntimeCall> = vec![xcm::v3::Instruction::Trap(43)].into();
330+
let xcm: VersionedXcm<RuntimeCall> = From::from(xcm);
331+
print_xcm(&xcm);
332+
assert_eq!("030419ac", format!("{}", as_hex(&xcm)));
333+
334+
let xcm: xcm::v3::Xcm<RuntimeCall> = vec![
335+
xcm::v3::Instruction::ClearError,
336+
xcm::v3::Instruction::ClearTopic,
337+
xcm::v3::Instruction::ClearTransactStatus,
338+
xcm::v3::Instruction::Trap(43),
339+
]
340+
.into();
341+
let xcm: VersionedXcm<RuntimeCall> = From::from(xcm);
342+
print_xcm(&xcm);
343+
assert_eq!("0310172c2319ac", format!("{}", as_hex(&xcm)));
344+
}
345+
}

0 commit comments

Comments
 (0)