use std::fmt::Display;
use borderless_hash::Hash256;
use borderless_id_types::{BlockIdentifier, TxIdentifier};
use serde::{Deserialize, Serialize};
use crate::{events::Sink, BorderlessId, ContractId};
pub mod env;
pub mod ledger;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Role {
pub participant_id: BorderlessId,
pub role: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Info {
pub contract_id: ContractId,
pub participants: Vec<BorderlessId>,
pub roles: Vec<Role>,
pub sinks: Vec<Sink>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxCtx {
pub tx_id: TxIdentifier,
pub index: u64,
}
impl TxCtx {
pub fn dummy() -> Self {
Self {
tx_id: TxIdentifier::new(999, 999, Hash256::empty()),
index: 0,
}
}
pub fn to_bytes(&self) -> Result<Vec<u8>, postcard::Error> {
postcard::to_allocvec(&self)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, postcard::Error> {
postcard::from_bytes(bytes)
}
}
impl Display for TxCtx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Tx-ID: {}, index: {}", self.tx_id, self.index)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockCtx {
pub block_id: BlockIdentifier,
pub timestamp: u64,
}
impl BlockCtx {
pub fn to_bytes(&self) -> Result<Vec<u8>, postcard::Error> {
postcard::to_allocvec(&self)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, postcard::Error> {
postcard::from_bytes(bytes)
}
}
impl Display for BlockCtx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Block-ID: {}, timestamp: {}",
self.block_id, self.timestamp
)
}
}