use crate::connection::{
create_interaction_context, ConnectionConfig, InteractionContext, InteractionContextOptions,
InteractionType,
};
use crate::error::Result;
use crate::schema::{
Address, BlockHeight, Epoch, EraStart, EraSummary, EraWithGenesis,
Constitution, GenesisConfiguration, GovernanceProposalState,
LiveStakeDistributionEntry, Point, ProjectedRewards, ProtocolParameters,
RewardAccount, RewardAccountSummary, Slot, StakeAddress, StakePoolId,
StakePoolPerformance, StakePoolView, Tip, TransactionOutputReference, UtcTime, Utxo,
};
use std::collections::HashMap;
use std::sync::Arc;
use super::query::{self, *};
#[derive(Debug, Clone, Default)]
pub struct LedgerStateQueryClientOptions {
pub point: Option<Point>,
}
pub struct LedgerStateQueryClient {
context: Arc<InteractionContext>,
}
impl LedgerStateQueryClient {
pub fn new(context: InteractionContext) -> Self {
Self {
context: Arc::new(context),
}
}
pub async fn connect(
connection: ConnectionConfig,
options: Option<LedgerStateQueryClientOptions>,
) -> Result<Self> {
let context = create_interaction_context(InteractionContextOptions {
connection,
interaction_type: InteractionType::LongRunning,
..Default::default()
})
.await?;
let client = Self::new(context);
if let Some(opts) = options {
if let Some(point) = opts.point {
client.acquire_ledger_state(Some(point)).await?;
}
}
Ok(client)
}
pub fn context(&self) -> &InteractionContext {
&self.context
}
pub async fn acquire_ledger_state(&self, point: Option<Point>) -> Result<Slot> {
query::acquire_ledger_state(&self.context, point).await
}
pub async fn release_ledger_state(&self) -> Result<()> {
query::release_ledger_state(&self.context).await
}
pub async fn constitution(&self) -> Result<Constitution> {
query::constitution(&self.context).await
}
pub async fn epoch(&self) -> Result<Epoch> {
query::epoch(&self.context).await
}
pub async fn era_start(&self) -> Result<EraStart> {
query::era_start(&self.context).await
}
pub async fn era_summaries(&self) -> Result<Vec<EraSummary>> {
query::era_summaries(&self.context).await
}
pub async fn genesis_configuration(&self, era: EraWithGenesis) -> Result<GenesisConfiguration> {
query::genesis_configuration(&self.context, era).await
}
pub async fn governance_proposals(
&self,
filter: Option<GovernanceProposalFilter>,
) -> Result<Vec<GovernanceProposalState>> {
query::governance_proposals(&self.context, filter).await
}
pub async fn ledger_tip(&self) -> Result<Point> {
query::ledger_tip(&self.context).await
}
pub async fn network_tip(&self) -> Result<Tip> {
query::network_tip(&self.context).await
}
pub async fn network_block_height(&self) -> Result<BlockHeight> {
query::network_block_height(&self.context).await
}
pub async fn live_stake_distribution(
&self,
) -> Result<HashMap<StakePoolId, LiveStakeDistributionEntry>> {
query::live_stake_distribution(&self.context).await
}
pub async fn network_start_time(&self) -> Result<UtcTime> {
query::network_start_time(&self.context).await
}
pub async fn projected_rewards(
&self,
stake_addresses: Vec<StakeAddress>,
) -> Result<Vec<ProjectedRewards>> {
query::projected_rewards(
&self.context,
ProjectedRewardsFilter { stake_addresses },
)
.await
}
pub async fn protocol_parameters(&self) -> Result<ProtocolParameters> {
query::protocol_parameters(&self.context).await
}
pub async fn reward_account_summaries(
&self,
keys: Vec<StakeAddress>,
) -> Result<HashMap<RewardAccount, RewardAccountSummary>> {
query::reward_account_summaries(&self.context, RewardAccountSummariesFilter { keys }).await
}
pub async fn stake_pools(
&self,
filter: Option<StakePoolsFilter>,
include_stake: bool,
) -> Result<HashMap<StakePoolId, StakePoolView>> {
query::stake_pools(&self.context, filter, include_stake).await
}
pub async fn stake_pools_performances(
&self,
) -> Result<HashMap<StakePoolId, StakePoolPerformance>> {
query::stake_pools_performances(&self.context).await
}
pub async fn utxo(&self, filter: Option<UtxoFilter>) -> Result<Vec<Utxo>> {
query::utxo(&self.context, filter).await
}
pub async fn utxo_by_addresses(&self, addresses: Vec<Address>) -> Result<Vec<Utxo>> {
query::utxo_by_addresses(&self.context, addresses).await
}
pub async fn utxo_by_output_references(
&self,
output_references: Vec<TransactionOutputReference>,
) -> Result<Vec<Utxo>> {
query::utxo_by_output_references(&self.context, output_references).await
}
pub async fn shutdown(&self) -> Result<()> {
self.context.shutdown().await
}
}
pub async fn create_ledger_state_query_client(
connection: ConnectionConfig,
options: Option<LedgerStateQueryClientOptions>,
) -> Result<LedgerStateQueryClient> {
LedgerStateQueryClient::connect(connection, options).await
}