From a376de159b56014e7baf5eb3c386eea326bff314 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Wed, 7 Jun 2023 09:33:20 +0200 Subject: [PATCH] check_para_leases (#1) --- cli/src/helper.rs | 38 ++++++++++++++++++++++++++++++++++++++ cli/src/lib.rs | 2 ++ cli/src/main.rs | 24 ++---------------------- cli/src/query.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 cli/src/helper.rs create mode 100644 cli/src/lib.rs create mode 100644 cli/src/query.rs diff --git a/cli/src/helper.rs b/cli/src/helper.rs new file mode 100644 index 0000000..436559c --- /dev/null +++ b/cli/src/helper.rs @@ -0,0 +1,38 @@ +use subxt::{OnlineClient, PolkadotConfig}; + +use crate::query::maybe_leases; + +pub enum Chain { + DOT, + KSM, + ROC, +} + +pub type Api = OnlineClient::; + +// Returns if the passed para_id is applicable for a permanent slot in Rococo +pub async fn needs_perm_slot( + para_id: u32 +) -> Result> { + + let polkadot_api = OnlineClient::::from_url("wss://rpc.polkadot.io:443").await?; + let kusama_api = OnlineClient::::from_url("wss://kusama-rpc.polkadot.io:443").await?; + let _rococo_api = OnlineClient::::from_url("wss://rococo-rpc.polkadot.io:443").await?; + + let lease_polkadot = maybe_leases( + polkadot_api, + Chain::DOT, + para_id + ).await; + + let lease_kusama = maybe_leases( + kusama_api, + Chain::KSM, + para_id + ).await; + + if lease_kusama.unwrap() || lease_polkadot.unwrap() { + println!("ParaId: {} needs a permanent slot", para_id); + Ok(true) + } else { Ok(false) } +} \ No newline at end of file diff --git a/cli/src/lib.rs b/cli/src/lib.rs new file mode 100644 index 0000000..6d34be1 --- /dev/null +++ b/cli/src/lib.rs @@ -0,0 +1,2 @@ +pub mod helper; +pub mod query; \ No newline at end of file diff --git a/cli/src/main.rs b/cli/src/main.rs index 3d96990..4551fbe 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,9 +1,6 @@ use clap::Parser; - -mod api; -use api::query::{exists_in_polkadot, exists_in_kusama}; - +use para_onboarding::helper::needs_perm_slot; #[derive(Parser, Debug)] #[command(about = "CLI tool to onboard parachains.")] @@ -20,24 +17,7 @@ async fn main() -> Result<(), Box> { println!("Parachain ID: {}", args.para_id); println!("Manager Address: {}", args.account_address); - let is_in_polkadot = exists_in_polkadot(args.para_id).await; - let is_in_kusama = exists_in_kusama(args.para_id).await; - let is_exists = match is_in_polkadot { - Ok(is_exists) => - match is_in_kusama { - Ok(is_exists_kusama) => is_exists || is_exists_kusama, - Err(v) => Err(format!("Error querying the chain: {}", v))? - }, - Err(v) => Err(format!("Error querying the chain: {}", v))? - }; - if is_exists { - // Chain exists on Polkadot/Kusama -> long term - println!("Parachain exists"); - } - else { - // Chain does not exist on Polkadot/Kusama -> short term - println!("This parachain does not exist"); - } + let _perm_slot: bool = needs_perm_slot(args.para_id).await.unwrap_or(false); Ok(()) } diff --git a/cli/src/query.rs b/cli/src/query.rs new file mode 100644 index 0000000..70a038c --- /dev/null +++ b/cli/src/query.rs @@ -0,0 +1,41 @@ +use crate::helper::{Chain, Api}; + +#[subxt::subxt(runtime_metadata_path = "metadata/polkadot_metadata.scale")] +pub mod polkadot {} + +#[subxt::subxt(runtime_metadata_path = "metadata/kusama_metadata.scale")] +pub mod kusama {} + +#[subxt::subxt(runtime_metadata_path = "metadata/rococo_metadata.scale")] +pub mod rococo {} + +use polkadot::runtime_types::polkadot_parachain::primitives::Id; +use kusama::runtime_types::polkadot_parachain::primitives::Id as KusamaId; +use rococo::runtime_types::polkadot_parachain::primitives::Id as RococoId; + + +// Checks if paraId holds any leases on the specified chain +// +pub async fn maybe_leases( + api: Api, + chain: Chain, + para_id: u32 +) -> Result> { + + let query = match chain { + Chain::DOT => polkadot::storage().slots().leases(Id(para_id)), + Chain::KSM => kusama::storage().slots().leases(KusamaId(para_id)), + Chain::ROC => rococo::storage().slots().leases(RococoId(para_id)), + }; + + match api + .storage() + .at_latest() + .await? + .fetch(&query) + .await? + { + Some(_) => Ok(true), + _ => Ok(false), + } +} \ No newline at end of file