Skip to main content

daml_grpc/data/event/
created.rs

1use std::convert::TryFrom;
2
3use chrono::{DateTime, Utc};
4
5use crate::data::completion::DamlStatus;
6use crate::data::identifier::DamlIdentifier;
7use crate::data::offset::DamlLedgerOffset;
8use crate::data::value::{DamlRecord, DamlValue};
9use crate::data::{DamlError, DamlResult};
10use crate::grpc_protobuf::com::daml::ledger::api::v2::{CreatedEvent, InterfaceView};
11use crate::util;
12use crate::util::Required;
13
14/// Records that a contract was created.
15///
16/// v2 reshaped this event heavily versus v1:
17///   - `event_id` and `agreement_text` are gone. Events are addressed
18///     by the `(offset, node_id)` pair instead.
19///   - `interface_views`, `created_event_blob`, `contract_key_hash`,
20///     `created_at`, `package_name`, `acs_delta`, and
21///     `representative_package_id` are new.
22#[derive(Debug, Eq, PartialEq, Clone)]
23pub struct DamlCreatedEvent {
24    /// Participant-local offset at which this event was emitted.
25    pub offset: DamlLedgerOffset,
26    /// Position of this event within the originating transaction or
27    /// reassignment.
28    pub node_id: i32,
29    pub contract_id: String,
30    pub template_id: DamlIdentifier,
31    pub contract_key: Option<DamlValue>,
32    /// Hash of the contract key (present iff `template_id` defines a
33    /// contract key).
34    pub contract_key_hash: Vec<u8>,
35    pub create_arguments: DamlRecord,
36    /// Opaque payload for forwarding this event as a `DisclosedContract`
37    /// in a future command submission.
38    pub created_event_blob: Vec<u8>,
39    /// Interface views requested via `InterfaceFilter::include_interface_view`.
40    pub interface_views: Vec<DamlInterfaceView>,
41    pub witness_parties: Vec<String>,
42    pub signatories: Vec<String>,
43    pub observers: Vec<String>,
44    /// Ledger-effective time of the creating transaction.
45    pub created_at: DateTime<Utc>,
46    /// Package-name of the created contract.
47    pub package_name: String,
48    /// Whether this event would appear on an ACS-delta-shaped stream.
49    /// Tracks contract activeness on the client side.
50    pub acs_delta: bool,
51    /// Server-internal: a package-id from the participant's store that
52    /// typechecks the contract's arguments. May differ from the
53    /// template's package-id when upgrades have happened. Documented
54    /// as experimental and "not for client consumption" — surfaced
55    /// anyway for round-trip parity.
56    pub representative_package_id: String,
57}
58
59impl TryFrom<CreatedEvent> for DamlCreatedEvent {
60    type Error = DamlError;
61
62    fn try_from(e: CreatedEvent) -> DamlResult<Self> {
63        Ok(Self {
64            offset: DamlLedgerOffset::new(e.offset),
65            node_id: e.node_id,
66            contract_id: e.contract_id,
67            template_id: DamlIdentifier::from(e.template_id.req()?),
68            contract_key: e.contract_key.map(DamlValue::try_from).transpose()?,
69            contract_key_hash: e.contract_key_hash,
70            create_arguments: DamlRecord::try_from(e.create_arguments.req()?)?,
71            created_event_blob: e.created_event_blob,
72            interface_views: e
73                .interface_views
74                .into_iter()
75                .map(DamlInterfaceView::try_from)
76                .collect::<DamlResult<_>>()?,
77            witness_parties: e.witness_parties,
78            signatories: e.signatories,
79            observers: e.observers,
80            created_at: util::from_grpc_timestamp(&e.created_at.req()?)?,
81            package_name: e.package_name,
82            acs_delta: e.acs_delta,
83            representative_package_id: e.representative_package_id,
84        })
85    }
86}
87
88/// View of a created event matched by an interface filter — the
89/// participant evaluates the interface's `view` method for each
90/// matching event and ships the result alongside.
91#[derive(Debug, Eq, PartialEq, Clone)]
92pub struct DamlInterfaceView {
93    pub interface_id: DamlIdentifier,
94    /// The result of evaluating the view: `code == 0` is success;
95    /// otherwise the view computation failed and `view_value` will be
96    /// `None`.
97    pub view_status: DamlStatus,
98    /// Computed view value. `None` when `view_status` reports an error.
99    pub view_value: Option<DamlRecord>,
100    /// Package that supplied the interface implementation used to
101    /// compute the view. Empty when the computation failed.
102    pub implementation_package_id: String,
103}
104
105impl TryFrom<InterfaceView> for DamlInterfaceView {
106    type Error = DamlError;
107
108    fn try_from(v: InterfaceView) -> DamlResult<Self> {
109        Ok(Self {
110            interface_id: DamlIdentifier::from(v.interface_id.req()?),
111            view_status: DamlStatus::from(v.view_status.req()?),
112            view_value: v.view_value.map(DamlRecord::try_from).transpose()?,
113            implementation_package_id: v.implementation_package_id,
114        })
115    }
116}