daml_grpc/data/
transaction.rs

1use crate::data::event::DamlEvent;
2
3use crate::data::{DamlError, DamlResult};
4use crate::grpc_protobuf::com::daml::ledger::api::v1::Transaction;
5use crate::util;
6use crate::util::Required;
7use chrono::DateTime;
8use chrono::Utc;
9use std::convert::TryFrom;
10
11/// A Daml ledger transaction.
12#[derive(Debug, Eq, PartialEq, Clone)]
13pub struct DamlTransaction {
14    transaction_id: String,
15    command_id: String,
16    workflow_id: String,
17    effective_at: DateTime<Utc>,
18    events: Vec<DamlEvent>,
19    offset: String,
20}
21
22impl DamlTransaction {
23    pub fn new(
24        transaction_id: impl Into<String>,
25        command_id: impl Into<String>,
26        workflow_id: impl Into<String>,
27        effective_at: impl Into<DateTime<Utc>>,
28        events: impl Into<Vec<DamlEvent>>,
29        offset: impl Into<String>,
30    ) -> Self {
31        Self {
32            transaction_id: transaction_id.into(),
33            command_id: command_id.into(),
34            workflow_id: workflow_id.into(),
35            effective_at: effective_at.into(),
36            events: events.into(),
37            offset: offset.into(),
38        }
39    }
40
41    pub fn transaction_id(&self) -> &str {
42        &self.transaction_id
43    }
44
45    pub fn command_id(&self) -> &str {
46        &self.command_id
47    }
48
49    pub fn workflow_id(&self) -> &str {
50        &self.workflow_id
51    }
52
53    pub const fn effective_at(&self) -> &DateTime<Utc> {
54        &self.effective_at
55    }
56
57    pub fn events(&self) -> &[DamlEvent] {
58        &self.events
59    }
60
61    pub fn take_events(self) -> Vec<DamlEvent> {
62        self.events
63    }
64
65    pub fn offset(&self) -> &str {
66        &self.offset
67    }
68}
69
70impl TryFrom<Transaction> for DamlTransaction {
71    type Error = DamlError;
72
73    fn try_from(tx: Transaction) -> Result<Self, Self::Error> {
74        Ok(Self::new(
75            tx.transaction_id,
76            tx.command_id,
77            tx.workflow_id,
78            util::from_grpc_timestamp(&tx.effective_at.req()?),
79            tx.events.into_iter().map(DamlEvent::try_from).collect::<DamlResult<Vec<_>>>()?,
80            tx.offset,
81        ))
82    }
83}