xcm_tools/
command.rs

1// This file is part of Astar.
2
3// Copyright (C) Stake Technologies Pte.Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later
5
6// Astar is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// Astar is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with Astar. If not, see <http://www.gnu.org/licenses/>.
18
19//! Astar XCM CLI handlers.
20
21use crate::cli::*;
22
23use clap::Parser;
24use cumulus_primitives_core::ParaId;
25use polkadot_parachain::primitives::Sibling;
26use polkadot_primitives::AccountId;
27use sp_core::hexdisplay::HexDisplay;
28use sp_runtime::traits::AccountIdConversion;
29use xcm::latest::prelude::*;
30use xcm_builder::{
31    DescribeAllTerminal, DescribeFamily, HashedDescription, ParentIsPreset,
32    SiblingParachainConvertsVia,
33};
34use xcm_executor::traits::ConvertLocation;
35
36/// CLI error type.
37pub type Error = String;
38
39/// Parse command line arguments into service configuration.
40pub fn run() -> Result<(), Error> {
41    let cli = Cli::parse();
42
43    match &cli.subcommand {
44        Some(Subcommand::RelayChainAccount) => {
45            let relay_account =
46                ParentIsPreset::<AccountId>::convert_location(&Location::parent()).unwrap();
47            println!("{}", relay_account);
48        }
49        Some(Subcommand::SovereignAccount(cmd)) => {
50            let parachain_account = if cmd.sibling {
51                let location = Location {
52                    parents: 1,
53                    interior: Parachain(cmd.parachain_id).into(),
54                };
55                SiblingParachainConvertsVia::<Sibling, AccountId>::convert_location(&location)
56                    .unwrap()
57            } else {
58                let para_id = ParaId::from(cmd.parachain_id);
59                AccountIdConversion::<AccountId>::into_account_truncating(&para_id)
60            };
61            println!("{}", parachain_account);
62        }
63        Some(Subcommand::AssetId(cmd)) => {
64            const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
65            let mut data = [0u8; 20];
66            data[0..4].copy_from_slice(ASSET_PRECOMPILE_ADDRESS_PREFIX);
67            data[4..20].copy_from_slice(&cmd.asset_id.to_be_bytes());
68            println!("pallet_assets: {}", cmd.asset_id);
69            println!("EVM XC20: 0x{}", HexDisplay::from(&data));
70        }
71        Some(Subcommand::RemoteAccount(cmd)) => {
72            let mut sender_multilocation = Location::parent();
73
74            if let Some(parachain_id) = cmd.parachain_id {
75                sender_multilocation
76                    .append_with(Parachain(parachain_id))
77                    .expect("infallible, short sequence");
78            }
79
80            match cmd.account_key {
81                AccountWrapper::SS58(id) => {
82                    sender_multilocation
83                        .append_with(AccountId32 {
84                            id,
85                            // network is not relevant for account derivation
86                            network: None,
87                        })
88                        .expect("infallible, short sequence");
89                }
90                AccountWrapper::H160(key) => {
91                    sender_multilocation
92                        .append_with(AccountKey20 {
93                            key,
94                            // network is not relevant for account derivation
95                            network: None,
96                        })
97                        .expect("infallible, short sequence");
98                }
99            }
100
101            let derived_acc =
102                HashedDescription::<AccountId, DescribeFamily<DescribeAllTerminal>>::convert_location(
103                    &sender_multilocation,
104                );
105            if let Some(derived_acc) = derived_acc {
106                println!("{}", derived_acc);
107            } else {
108                println!("Failed to derive account Id.");
109            }
110        }
111        None => {}
112    }
113    Ok(())
114}