ogmios_client/ledger_state_query/
client.rs1use crate::connection::{
4 create_interaction_context, ConnectionConfig, InteractionContext, InteractionContextOptions,
5 InteractionType,
6};
7use crate::error::Result;
8use crate::schema::{
9 Address, BlockHeight, Epoch, EraStart, EraSummary, EraWithGenesis,
10 Constitution, GenesisConfiguration, GovernanceProposalState,
11 LiveStakeDistributionEntry, Point, ProjectedRewards, ProtocolParameters,
12 RewardAccount, RewardAccountSummary, Slot, StakeAddress, StakePoolId,
13 StakePoolPerformance, StakePoolView, Tip, TransactionOutputReference, UtcTime, Utxo,
14};
15use std::collections::HashMap;
16use std::sync::Arc;
17
18use super::query::{self, *};
19
20#[derive(Debug, Clone, Default)]
22pub struct LedgerStateQueryClientOptions {
23 pub point: Option<Point>,
25}
26
27pub struct LedgerStateQueryClient {
62 context: Arc<InteractionContext>,
64}
65
66impl LedgerStateQueryClient {
67 pub fn new(context: InteractionContext) -> Self {
69 Self {
70 context: Arc::new(context),
71 }
72 }
73
74 pub async fn connect(
81 connection: ConnectionConfig,
82 options: Option<LedgerStateQueryClientOptions>,
83 ) -> Result<Self> {
84 let context = create_interaction_context(InteractionContextOptions {
85 connection,
86 interaction_type: InteractionType::LongRunning,
87 ..Default::default()
88 })
89 .await?;
90
91 let client = Self::new(context);
92
93 if let Some(opts) = options {
95 if let Some(point) = opts.point {
96 client.acquire_ledger_state(Some(point)).await?;
97 }
98 }
99
100 Ok(client)
101 }
102
103 pub fn context(&self) -> &InteractionContext {
105 &self.context
106 }
107
108 pub async fn acquire_ledger_state(&self, point: Option<Point>) -> Result<Slot> {
110 query::acquire_ledger_state(&self.context, point).await
111 }
112
113 pub async fn release_ledger_state(&self) -> Result<()> {
115 query::release_ledger_state(&self.context).await
116 }
117
118 pub async fn constitution(&self) -> Result<Constitution> {
120 query::constitution(&self.context).await
121 }
122
123 pub async fn epoch(&self) -> Result<Epoch> {
125 query::epoch(&self.context).await
126 }
127
128 pub async fn era_start(&self) -> Result<EraStart> {
130 query::era_start(&self.context).await
131 }
132
133 pub async fn era_summaries(&self) -> Result<Vec<EraSummary>> {
135 query::era_summaries(&self.context).await
136 }
137
138 pub async fn genesis_configuration(&self, era: EraWithGenesis) -> Result<GenesisConfiguration> {
140 query::genesis_configuration(&self.context, era).await
141 }
142
143 pub async fn governance_proposals(
145 &self,
146 filter: Option<GovernanceProposalFilter>,
147 ) -> Result<Vec<GovernanceProposalState>> {
148 query::governance_proposals(&self.context, filter).await
149 }
150
151 pub async fn ledger_tip(&self) -> Result<Point> {
153 query::ledger_tip(&self.context).await
154 }
155
156 pub async fn network_tip(&self) -> Result<Tip> {
158 query::network_tip(&self.context).await
159 }
160
161 pub async fn network_block_height(&self) -> Result<BlockHeight> {
163 query::network_block_height(&self.context).await
164 }
165
166 pub async fn live_stake_distribution(
168 &self,
169 ) -> Result<HashMap<StakePoolId, LiveStakeDistributionEntry>> {
170 query::live_stake_distribution(&self.context).await
171 }
172
173 pub async fn network_start_time(&self) -> Result<UtcTime> {
175 query::network_start_time(&self.context).await
176 }
177
178 pub async fn projected_rewards(
180 &self,
181 stake_addresses: Vec<StakeAddress>,
182 ) -> Result<Vec<ProjectedRewards>> {
183 query::projected_rewards(
184 &self.context,
185 ProjectedRewardsFilter { stake_addresses },
186 )
187 .await
188 }
189
190 pub async fn protocol_parameters(&self) -> Result<ProtocolParameters> {
192 query::protocol_parameters(&self.context).await
193 }
194
195 pub async fn reward_account_summaries(
197 &self,
198 keys: Vec<StakeAddress>,
199 ) -> Result<HashMap<RewardAccount, RewardAccountSummary>> {
200 query::reward_account_summaries(&self.context, RewardAccountSummariesFilter { keys }).await
201 }
202
203 pub async fn stake_pools(
205 &self,
206 filter: Option<StakePoolsFilter>,
207 include_stake: bool,
208 ) -> Result<HashMap<StakePoolId, StakePoolView>> {
209 query::stake_pools(&self.context, filter, include_stake).await
210 }
211
212 pub async fn stake_pools_performances(
214 &self,
215 ) -> Result<HashMap<StakePoolId, StakePoolPerformance>> {
216 query::stake_pools_performances(&self.context).await
217 }
218
219 pub async fn utxo(&self, filter: Option<UtxoFilter>) -> Result<Vec<Utxo>> {
221 query::utxo(&self.context, filter).await
222 }
223
224 pub async fn utxo_by_addresses(&self, addresses: Vec<Address>) -> Result<Vec<Utxo>> {
226 query::utxo_by_addresses(&self.context, addresses).await
227 }
228
229 pub async fn utxo_by_output_references(
231 &self,
232 output_references: Vec<TransactionOutputReference>,
233 ) -> Result<Vec<Utxo>> {
234 query::utxo_by_output_references(&self.context, output_references).await
235 }
236
237 pub async fn shutdown(&self) -> Result<()> {
239 self.context.shutdown().await
240 }
241}
242
243pub async fn create_ledger_state_query_client(
247 connection: ConnectionConfig,
248 options: Option<LedgerStateQueryClientOptions>,
249) -> Result<LedgerStateQueryClient> {
250 LedgerStateQueryClient::connect(connection, options).await
251}