ghost-eye/src/network/predefinded_calls.rs
Uncle Stretch b3cebfa0a4
fixes for active screen and wallet screen draft added
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
2024-12-02 14:16:39 +03:00

166 lines
5.2 KiB
Rust

use tokio::sync::mpsc::UnboundedSender;
use color_eyre::Result;
use subxt::{
ext::sp_core::crypto::{AccountId32, Ss58Codec, Ss58AddressFormat},
utils::H256,
backend::rpc::RpcClient,
client::OnlineClient,
config::substrate::DigestItem,
rpc_params,
};
use crate::{
action::Action,
casper_network::{
self,
runtime_types::sp_consensus_slots,
},
types::EraInfo,
CasperAccountId, CasperConfig
};
pub async fn get_block_author(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
logs: &Vec<DigestItem>,
at_hash: &H256,
) -> Result<()> {
use codec::Decode;
use crate::casper_network::runtime_types::sp_consensus_babe::digests::PreDigest;
let storage_key = casper_network::storage().session().validators();
let validators = api.storage().at(*at_hash).fetch(&storage_key).await?.unwrap_or_default();
let maybe_author = match logs.iter().find(|item| matches!(item, DigestItem::PreRuntime(..))) {
Some(DigestItem::PreRuntime(engine, data)) if *engine == [b'B', b'A', b'B', b'E'] => {
match PreDigest::decode(&mut &data[..]) {
Ok(PreDigest::Primary(primary)) => validators.get(primary.authority_index as usize),
Ok(PreDigest::SecondaryPlain(secondary)) => validators.get(secondary.authority_index as usize),
Ok(PreDigest::SecondaryVRF(secondary)) => validators.get(secondary.authority_index as usize),
_ => None,
}
},
_ => None,
};
let validator = match maybe_author {
Some(author) => {
let extended_author = CasperAccountId::decode(&mut author.as_ref())
.expect("author should be valid AccountId32; qed");
let account_id = AccountId32::from(extended_author.0);
account_id.to_ss58check_with_version(Ss58AddressFormat::custom(1996))
},
None => "...".to_string(),
};
action_tx.send(Action::SetBlockAuthor(*at_hash, validator))?;
Ok(())
}
pub async fn get_active_era(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
) -> Result<()> {
let storage_key = casper_network::storage().staking().active_era();
if let Some(active_era) = api.storage().at_latest().await?.fetch(&storage_key).await? {
action_tx.send(Action::SetActiveEra(EraInfo {
index: active_era.index,
start: active_era.start,
}))?;
}
Ok(())
}
pub async fn get_epoch_progress(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
) -> Result<()> {
let storage_key = casper_network::storage().babe().current_slot();
let current_slot = api.storage()
.at_latest()
.await?
.fetch(&storage_key)
.await?
.unwrap_or(sp_consensus_slots::Slot(0u64));
let storage_key = casper_network::storage().babe().epoch_index();
let epoch_index = api.storage()
.at_latest()
.await?
.fetch(&storage_key)
.await?
.unwrap_or_default();
let storage_key = casper_network::storage().babe().genesis_slot();
let genesis_slot = api.storage()
.at_latest()
.await?
.fetch(&storage_key)
.await?
.unwrap_or(sp_consensus_slots::Slot(0u64));
let constant_query = casper_network::constants().babe().epoch_duration();
let epoch_duration = api.constants().at(&constant_query)?;
let epoch_start_slot = epoch_index * epoch_duration + genesis_slot.0;
let progress = current_slot.0.saturating_sub(epoch_start_slot);
action_tx.send(Action::SetEpochProgress(epoch_index, progress))?;
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?;
action_tx.send(Action::SetPendingExtrinsicsLength(pending_extrinsics.len()))?;
Ok(())
}
pub async fn get_total_issuance(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
) -> Result<()> {
let storage_key = casper_network::storage().balances().total_issuance();
let total_issuance = api.storage()
.at_latest()
.await?
.fetch(&storage_key)
.await?
.unwrap_or_default();
action_tx.send(Action::SetTotalIssuance(total_issuance))?;
Ok(())
}
pub async fn get_existential_deposit(
action_tx: &UnboundedSender<Action>,
api: &OnlineClient<CasperConfig>,
) -> Result<()> {
let constant_query = casper_network::constants().balances().existential_deposit();
let existential_deposit = api.constants().at(&constant_query)?;
action_tx.send(Action::SetExistentialDeposit(existential_deposit))?;
Ok(())
}
//pub async fn get_balance(
// action_tx: &UnboundedSender<Action>,
// api: &OnlineClient<CasperConfig>,
// account_id: subxt::utils::AccountId32,
//) -> Result<()> {
// let storage_key = casper_network::storage().system().account(&account_id);
// let balance = api.storage()
// .at_latest()
// .await?
// .fetch(&storage_key)
// .await?;
//
// action_tx.send(Action::SetTotalIssuance(total_issuance))?;
// Ok(())
//}