use std::convert::TryFrom;
use std::fmt::Debug;
use tonic::transport::Channel;
use tracing::{instrument, trace};
use crate::data::DamlResult;
use crate::data::event::DamlCreatedEvent;
use crate::grpc_protobuf::com::daml::ledger::api::v2::GetContractRequest;
use crate::grpc_protobuf::com::daml::ledger::api::v2::contract_service_client::ContractServiceClient;
use crate::service::common::make_request;
use crate::util::Required;
#[derive(Debug)]
pub struct DamlContractService<'a> {
channel: Channel,
auth_token: Option<&'a str>,
}
impl<'a> DamlContractService<'a> {
pub fn new(channel: Channel, auth_token: Option<&'a str>) -> Self {
Self {
channel,
auth_token,
}
}
pub fn with_token(self, auth_token: &'a str) -> Self {
Self {
auth_token: Some(auth_token),
..self
}
}
#[instrument(skip(self))]
pub async fn get_contract(
&self,
contract_id: impl Into<String> + Debug,
querying_parties: impl Into<Vec<String>> + Debug,
) -> DamlResult<DamlCreatedEvent> {
let payload = GetContractRequest {
contract_id: contract_id.into(),
querying_parties: querying_parties.into(),
};
trace!(payload = ?payload, token = ?self.auth_token);
let response = self.client().get_contract(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
DamlCreatedEvent::try_from(response.created_event.req()?)
}
fn client(&self) -> ContractServiceClient<Channel> {
ContractServiceClient::new(self.channel.clone())
}
}