use std::convert::TryFrom;
use chrono::{DateTime, Utc};
use crate::data::completion::DamlStatus;
use crate::data::identifier::DamlIdentifier;
use crate::data::offset::DamlLedgerOffset;
use crate::data::value::{DamlRecord, DamlValue};
use crate::data::{DamlError, DamlResult};
use crate::grpc_protobuf::com::daml::ledger::api::v2::{CreatedEvent, InterfaceView};
use crate::util;
use crate::util::Required;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DamlCreatedEvent {
pub offset: DamlLedgerOffset,
pub node_id: i32,
pub contract_id: String,
pub template_id: DamlIdentifier,
pub contract_key: Option<DamlValue>,
pub contract_key_hash: Vec<u8>,
pub create_arguments: DamlRecord,
pub created_event_blob: Vec<u8>,
pub interface_views: Vec<DamlInterfaceView>,
pub witness_parties: Vec<String>,
pub signatories: Vec<String>,
pub observers: Vec<String>,
pub created_at: DateTime<Utc>,
pub package_name: String,
pub acs_delta: bool,
pub representative_package_id: String,
}
impl TryFrom<CreatedEvent> for DamlCreatedEvent {
type Error = DamlError;
fn try_from(e: CreatedEvent) -> DamlResult<Self> {
Ok(Self {
offset: DamlLedgerOffset::new(e.offset),
node_id: e.node_id,
contract_id: e.contract_id,
template_id: DamlIdentifier::from(e.template_id.req()?),
contract_key: e.contract_key.map(DamlValue::try_from).transpose()?,
contract_key_hash: e.contract_key_hash,
create_arguments: DamlRecord::try_from(e.create_arguments.req()?)?,
created_event_blob: e.created_event_blob,
interface_views: e
.interface_views
.into_iter()
.map(DamlInterfaceView::try_from)
.collect::<DamlResult<_>>()?,
witness_parties: e.witness_parties,
signatories: e.signatories,
observers: e.observers,
created_at: util::from_grpc_timestamp(&e.created_at.req()?)?,
package_name: e.package_name,
acs_delta: e.acs_delta,
representative_package_id: e.representative_package_id,
})
}
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct DamlInterfaceView {
pub interface_id: DamlIdentifier,
pub view_status: DamlStatus,
pub view_value: Option<DamlRecord>,
pub implementation_package_id: String,
}
impl TryFrom<InterfaceView> for DamlInterfaceView {
type Error = DamlError;
fn try_from(v: InterfaceView) -> DamlResult<Self> {
Ok(Self {
interface_id: DamlIdentifier::from(v.interface_id.req()?),
view_status: DamlStatus::from(v.view_status.req()?),
view_value: v.view_value.map(DamlRecord::try_from).transpose()?,
implementation_package_id: v.implementation_package_id,
})
}
}