Skip to main content

daml_grpc/service/
daml_event_query_service.rs

1use std::convert::TryFrom;
2use std::fmt::Debug;
3
4use tonic::transport::Channel;
5use tracing::{instrument, trace};
6
7use crate::data::DamlResult;
8use crate::data::event_query::DamlEventsByContractId;
9use crate::data::filter::DamlEventFormat;
10use crate::grpc_protobuf::com::daml::ledger::api::v2::GetEventsByContractIdRequest;
11use crate::grpc_protobuf::com::daml::ledger::api::v2::event_query_service_client::EventQueryServiceClient;
12use crate::service::common::make_request;
13
14/// Look up the create + consuming-archive events for a single
15/// contract by id. Distinct from streaming history because the
16/// participant returns just the two endpoint events, not the full
17/// transaction(s) they appeared in.
18///
19/// Contract-key lookup is **not** supported in v2; multi-synchronizer
20/// participants don't (yet) have a globally unique contract-key
21/// notion. Use `get_events_by_contract_id` when the contract id is
22/// in hand.
23#[derive(Debug)]
24pub struct DamlEventQueryService<'a> {
25    channel: Channel,
26    auth_token: Option<&'a str>,
27}
28
29impl<'a> DamlEventQueryService<'a> {
30    pub fn new(channel: Channel, auth_token: Option<&'a str>) -> Self {
31        Self {
32            channel,
33            auth_token,
34        }
35    }
36
37    /// Override the JWT token to use for this service.
38    pub fn with_token(self, auth_token: &'a str) -> Self {
39        Self {
40            auth_token: Some(auth_token),
41            ..self
42        }
43    }
44
45    /// Fetch the create and archive events for `contract_id`.
46    ///
47    /// Events are filtered through `event_format`; results take
48    /// ACS-delta shape regardless of any `transaction_shape` setting.
49    /// Returns `CONTRACT_EVENTS_NOT_FOUND` when the contract is
50    /// unknown to the participant or every matching event has been
51    /// pruned.
52    #[instrument(skip(self))]
53    pub async fn get_events_by_contract_id(
54        &self,
55        contract_id: impl Into<String> + Debug,
56        event_format: DamlEventFormat,
57    ) -> DamlResult<DamlEventsByContractId> {
58        let payload = GetEventsByContractIdRequest {
59            contract_id: contract_id.into(),
60            event_format: Some(event_format.into()),
61        };
62        trace!(payload = ?payload, token = ?self.auth_token);
63        let response =
64            self.client().get_events_by_contract_id(make_request(payload, self.auth_token)?).await?.into_inner();
65        trace!(?response);
66        DamlEventsByContractId::try_from(response)
67    }
68
69    fn client(&self) -> EventQueryServiceClient<Channel> {
70        EventQueryServiceClient::new(self.channel.clone())
71    }
72}