use crate::{address::Address, block_id::BlockId, operation::OperationId, slot::Slot};
use serde::{Deserialize, Serialize};
use std::{collections::VecDeque, fmt::Display};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SCOutputEvent {
pub context: EventExecutionContext,
pub data: String,
}
impl Display for SCOutputEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Context: {}", self.context)?;
writeln!(f, "Data: {}", self.data)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventExecutionContext {
pub slot: Slot,
pub block: Option<BlockId>,
pub read_only: bool,
pub index_in_slot: u64,
pub call_stack: VecDeque<Address>,
pub origin_operation_id: Option<OperationId>,
pub is_final: bool,
pub is_error: bool,
}
impl Display for EventExecutionContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Slot: {} at index: {}", self.slot, self.index_in_slot)?;
writeln!(
f,
"{}",
if self.read_only {
"Read only execution"
} else {
"On chain execution"
}
)?;
if let Some(id) = self.block {
writeln!(f, "Block id: {}", id)?;
}
if let Some(id) = self.origin_operation_id {
writeln!(f, "Origin operation id: {}", id)?;
}
writeln!(
f,
"Call stack: {}",
self.call_stack
.iter()
.map(|a| format!("{}", a))
.collect::<Vec<_>>()
.join(",")
)
}
}