use std::convert::TryFrom;
use std::fmt::Debug;
use tonic::transport::Channel;
use tracing::{instrument, trace};
use crate::data::DamlTransaction;
use crate::data::filter::{DamlEventFormat, DamlTransactionFormat};
use crate::data::offset::DamlLedgerOffset;
use crate::data::reassignment::{DamlReassignment, DamlReassignmentCommands};
use crate::data::{DamlCommands, DamlResult};
use crate::grpc_protobuf::com::daml::ledger::api::v2::command_service_client::CommandServiceClient;
use crate::grpc_protobuf::com::daml::ledger::api::v2::{
Commands, ReassignmentCommands, SubmitAndWaitForReassignmentRequest, SubmitAndWaitForTransactionRequest,
SubmitAndWaitRequest,
};
use crate::service::common::make_request;
use crate::util::Required;
#[derive(Debug)]
pub struct DamlCommandService<'a> {
channel: Channel,
auth_token: Option<&'a str>,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct DamlSubmitAndWaitOutcome {
pub update_id: String,
pub completion_offset: DamlLedgerOffset,
}
impl<'a> DamlCommandService<'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 submit_and_wait(
&self,
commands: impl Into<DamlCommands> + Debug,
) -> DamlResult<DamlSubmitAndWaitOutcome> {
let payload = SubmitAndWaitRequest {
commands: Some(Commands::try_from(commands.into())?),
};
trace!(payload = ?payload, token = ?self.auth_token);
let response = self.client().submit_and_wait(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
Ok(DamlSubmitAndWaitOutcome {
update_id: response.update_id,
completion_offset: DamlLedgerOffset::new(response.completion_offset),
})
}
#[instrument(skip(self))]
pub async fn submit_and_wait_for_transaction(
&self,
commands: impl Into<DamlCommands> + Debug,
transaction_format: Option<DamlTransactionFormat>,
) -> DamlResult<DamlTransaction> {
let payload = SubmitAndWaitForTransactionRequest {
commands: Some(Commands::try_from(commands.into())?),
transaction_format: transaction_format.map(Into::into),
};
trace!(payload = ?payload, token = ?self.auth_token);
let response =
self.client().submit_and_wait_for_transaction(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
DamlTransaction::try_from(response.transaction.req()?)
}
#[instrument(skip(self))]
pub async fn submit_and_wait_for_reassignment(
&self,
commands: impl Into<DamlReassignmentCommands> + Debug,
event_format: Option<DamlEventFormat>,
) -> DamlResult<DamlReassignment> {
let payload = SubmitAndWaitForReassignmentRequest {
reassignment_commands: Some(ReassignmentCommands::from(commands.into())),
event_format: event_format.map(Into::into),
};
trace!(payload = ?payload, token = ?self.auth_token);
let response =
self.client().submit_and_wait_for_reassignment(make_request(payload, self.auth_token)?).await?.into_inner();
trace!(?response);
DamlReassignment::try_from(response.reassignment.req()?)
}
fn client(&self) -> CommandServiceClient<Channel> {
CommandServiceClient::new(self.channel.clone())
}
}