90 lines
3.6 KiB
Rust
90 lines
3.6 KiB
Rust
use tokio::sync::mpsc::UnboundedSender;
|
|
use color_eyre::Result;
|
|
use subxt::{
|
|
backend::{
|
|
legacy::LegacyRpcMethods,
|
|
rpc::RpcClient,
|
|
},
|
|
utils::H256,
|
|
OnlineClient
|
|
};
|
|
|
|
mod legacy_rpc_calls;
|
|
mod predefinded_calls;
|
|
mod subscriptions;
|
|
|
|
use crate::{
|
|
action::Action,
|
|
casper::CasperConfig,
|
|
};
|
|
|
|
pub use subscriptions::{FinalizedSubscription, BestSubscription};
|
|
|
|
pub struct Network {
|
|
action_tx: UnboundedSender<Action>,
|
|
online_client_api: OnlineClient<CasperConfig>,
|
|
legacy_client_api: LegacyRpcMethods<CasperConfig>,
|
|
rpc_client: RpcClient,
|
|
best_hash: Option<H256>,
|
|
finalized_hash: Option<H256>,
|
|
accounts_to_watch: std::collections::HashSet<[u8; 32]>
|
|
}
|
|
|
|
impl Network {
|
|
pub fn new(
|
|
action_tx: UnboundedSender<Action>,
|
|
online_client_api: OnlineClient<CasperConfig>,
|
|
legacy_client_api: LegacyRpcMethods<CasperConfig>,
|
|
rpc_client: RpcClient,
|
|
) -> Self {
|
|
Self {
|
|
action_tx,
|
|
online_client_api,
|
|
legacy_client_api,
|
|
rpc_client,
|
|
best_hash: None,
|
|
finalized_hash: None,
|
|
accounts_to_watch: Default::default(),
|
|
}
|
|
}
|
|
|
|
pub async fn handle_network_event(&mut self, io_event: Action) -> Result<()> {
|
|
match io_event {
|
|
Action::NewBestHash(hash) => {
|
|
self.best_hash = Some(hash);
|
|
for account_id in self.accounts_to_watch.iter() {
|
|
predefinded_calls::get_balance(&self.action_tx, &self.online_client_api, &account_id).await?;
|
|
}
|
|
Ok(())
|
|
},
|
|
Action::NewFinalizedHash(hash) => {
|
|
self.finalized_hash = Some(hash);
|
|
Ok(())
|
|
},
|
|
Action::GetSystemHealth => legacy_rpc_calls::get_system_health(&self.action_tx, &self.legacy_client_api).await,
|
|
Action::GetNodeName => legacy_rpc_calls::get_node_name(&self.action_tx, &self.legacy_client_api).await,
|
|
Action::GetGenesisHash => legacy_rpc_calls::get_genesis_hash(&self.action_tx, &self.legacy_client_api).await,
|
|
Action::GetChainName => legacy_rpc_calls::get_chain_name(&self.action_tx, &self.legacy_client_api).await,
|
|
Action::GetChainVersion => legacy_rpc_calls::get_system_version(&self.action_tx, &self.legacy_client_api).await,
|
|
Action::GetBlockAuthor(hash, logs) => predefinded_calls::get_block_author(&self.action_tx, &self.online_client_api, &logs, &hash).await,
|
|
Action::GetActiveEra => predefinded_calls::get_active_era(&self.action_tx, &self.online_client_api).await,
|
|
Action::GetEpochProgress => predefinded_calls::get_epoch_progress(&self.action_tx, &self.online_client_api).await,
|
|
Action::GetPendingExtrinsics => predefinded_calls::get_pending_extrinsics(&self.action_tx, &self.rpc_client).await,
|
|
|
|
Action::GetExistentialDeposit => predefinded_calls::get_existential_deposit(&self.action_tx, &self.online_client_api).await,
|
|
Action::GetTotalIssuance => predefinded_calls::get_total_issuance(&self.action_tx, &self.online_client_api).await,
|
|
|
|
Action::BalanceRequest(account_id, remove) => {
|
|
if remove {
|
|
let _ = self.accounts_to_watch.remove(&account_id);
|
|
Ok(())
|
|
} else {
|
|
let _ = self.accounts_to_watch.insert(account_id);
|
|
predefinded_calls::get_balance(&self.action_tx, &self.online_client_api, &account_id).await
|
|
}
|
|
}
|
|
_ => Ok(())
|
|
}
|
|
}
|
|
}
|