sub_meta_api/collator_selection/
candidate_list.rs1#![allow(missing_docs)]
2use crate::models::collator::Collator;
3use crate::connection::api_connection;
4use crate::errors::pallet;
5
6use std::error::Error;
7
8use subxt::{ utils::Yes, storage::Address };
9use polkadot_asset_hub_metadata::collator_selection::storage::types::candidate_list::CandidateList;
10
11use crate::enums::chains::ChainNames;
12
13#[subxt::subxt(runtime_metadata_path = "src/artifacts/metadata/polkadot-assethub.scale")]
14mod polkadot_asset_hub_metadata {}
15
16#[subxt::subxt(runtime_metadata_path = "src/artifacts/metadata/polkadot-bridgehub.scale")]
17mod polkadot_bridge_hub_metadata {}
18
19pub async fn execute(chain:ChainNames)->Result<Vec<Collator>, Box<dyn Error>> {
20
21 match chain{
22 ChainNames::Polkadot => {
23 Err(pallet::PalletNotFound::new("CollatorSelection").into())
24 }
25 ChainNames::Kusama => {
26 Err(pallet::PalletNotFound::new("CollatorSelection").into())
27 }
28 ChainNames::PolkadotAssetHub =>{
29 let query = polkadot_asset_hub_metadata::storage().collator_selection().candidate_list();
30 Ok(get_collators_generic(None,query,ChainNames::PolkadotAssetHub).await?)
31 }
32 ChainNames::PolkadotBridgeHub => {
33 Err(pallet::PalletNotFound::new("CollatorSelection").into())
34 }
35 }
36}
37
38pub async fn execute_with_wss(wss_url:&str, chain:ChainNames)->Result<Vec<Collator>, Box<dyn Error>> {
39
40 match chain{
41 ChainNames::Polkadot => {
42 Err(pallet::PalletNotFound::new("CollatorSelection").into())
43 }
44 ChainNames::Kusama => {
45 Err(pallet::PalletNotFound::new("CollatorSelection").into())
46 }
47 ChainNames::PolkadotAssetHub =>{
48 let query = polkadot_asset_hub_metadata::storage().collator_selection().candidate_list();
49 Ok(get_collators_generic(Some(wss_url),query,ChainNames::PolkadotAssetHub).await?)
50 }
51 ChainNames::PolkadotBridgeHub => {
52 Err(pallet::PalletNotFound::new("CollatorSelection").into())
53 }
54 }
55}
56
57async fn get_collators_generic<T: Address<IsFetchable = Yes, Target=CandidateList>>(wss_url:Option<&str>, query:T, chain:ChainNames) ->Result<Vec<Collator>, Box<dyn Error>> {
58
59 let api_connection_result = api_connection::get_api_connection(wss_url, chain).await;
60 let mut result:Vec<Collator> = vec![];
61
62 match api_connection_result {
63 Ok(api_connection) => {
64
65 match api_connection.storage().at_latest().await {
66 Ok(api_at_latest) => {
67 match api_at_latest.fetch(&query).await {
68 Ok(candidates_option)=>{
70 match candidates_option {
71 Some(candidates) => {
72 for candidate in candidates.0.iter() {
73 result.push(Collator::new()
74 .set_address(candidate.who.clone())
75 .set_bond(candidate.deposit.clone())
76 .set_invulnerable(false)
77 );
78 }
79 }
80 None => {
81 eprintln!("No candidate list found");
82 }
83 }
84 },
85 Err(_) => {
86 todo!("Couldn't fetch query")
87 },
88 }
89 }
90 Err(_) => {
91 todo!("Couldn't fetch query")
92 }
93 }
94 }
95 Err(_err) => {
96 todo!("Couldn't fetch query")
97 }
98 }
99
100 Ok(result)
101}
102