135 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			3.8 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
use tokio::sync::mpsc::UnboundedSender;
 | 
						|
use color_eyre::Result;
 | 
						|
use subxt::{backend::{legacy::rpc_methods::SystemHealth, rpc::RpcClient}, rpc_params};
 | 
						|
 | 
						|
use crate::{action::Action, types::PeerInformation};
 | 
						|
 | 
						|
pub async fn get_node_name(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let maybe_node_name = rpc_client
 | 
						|
        .request("system_name", rpc_params![])
 | 
						|
        .await
 | 
						|
        .ok();
 | 
						|
    action_tx.send(Action::SetNodeName(maybe_node_name))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_system_health(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let (maybe_peers, is_syncing, should_have_peers) = rpc_client
 | 
						|
        .request("system_health", rpc_params![])
 | 
						|
        .await
 | 
						|
        .ok()
 | 
						|
        .map_or((None, false, false), |health: SystemHealth| (
 | 
						|
                Some(health.peers),
 | 
						|
                health.is_syncing,
 | 
						|
                health.should_have_peers,
 | 
						|
        ));
 | 
						|
    action_tx.send(Action::SetSystemHealth(
 | 
						|
            maybe_peers, 
 | 
						|
            is_syncing, 
 | 
						|
            should_have_peers))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_genesis_hash(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let params = rpc_params![0u32];
 | 
						|
    let maybe_genesis_hash = rpc_client
 | 
						|
        .request("chain_getBlockHash", params)
 | 
						|
        .await
 | 
						|
        .ok();
 | 
						|
    action_tx.send(Action::SetGenesisHash(maybe_genesis_hash))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_chain_name(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let maybe_chain_name = rpc_client
 | 
						|
        .request("system_chain", rpc_params![])
 | 
						|
        .await
 | 
						|
        .ok();
 | 
						|
    action_tx.send(Action::SetChainName(maybe_chain_name))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_system_version(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let maybe_system_version = rpc_client
 | 
						|
        .request("system_version", rpc_params![])
 | 
						|
        .await
 | 
						|
        .ok();
 | 
						|
    action_tx.send(Action::SetChainVersion(maybe_system_version))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_pending_extrinsics(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let pending_extrinsics: Vec<String> = rpc_client
 | 
						|
        .request("author_pendingExtrinsics", rpc_params![])
 | 
						|
        .await
 | 
						|
        .unwrap_or_default();
 | 
						|
    action_tx.send(Action::SetPendingExtrinsicsLength(pending_extrinsics.len()))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_connected_peers(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let connected_peers: Vec<PeerInformation> = rpc_client
 | 
						|
        .request("system_peers", rpc_params![])
 | 
						|
        .await
 | 
						|
        .unwrap_or_default();
 | 
						|
    action_tx.send(Action::SetConnectedPeers(connected_peers))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_listen_addresses(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let listen_addresses: Vec<String> = rpc_client
 | 
						|
        .request("system_localListenAddresses", rpc_params![])
 | 
						|
        .await
 | 
						|
        .unwrap_or_default();
 | 
						|
    action_tx.send(Action::SetListenAddresses(listen_addresses))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn get_local_identity(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let local_peer_id: String = rpc_client
 | 
						|
        .request("system_localPeerId", rpc_params![])
 | 
						|
        .await
 | 
						|
        .unwrap_or_default();
 | 
						|
    action_tx.send(Action::SetLocalIdentity(local_peer_id))?;
 | 
						|
    Ok(())
 | 
						|
}
 | 
						|
 | 
						|
pub async fn rotate_keys(
 | 
						|
    action_tx: &UnboundedSender<Action>,
 | 
						|
    rpc_client: &RpcClient,
 | 
						|
) -> Result<()> {
 | 
						|
    let rotated_keys: String = rpc_client
 | 
						|
        .request("author_rotateKeys", rpc_params![])
 | 
						|
        .await
 | 
						|
        .unwrap_or_default();
 | 
						|
    action_tx.send(Action::StoreRotatedKeys(rotated_keys))?;
 | 
						|
    Ok(())
 | 
						|
}
 |