heliosphere_core/
transaction.rs1#![allow(missing_docs)]
3use crate::util::{as_hex_array, as_hex_buffer};
4use alloc::{
5 string::{String, ToString},
6 vec::Vec,
7};
8use serde::{Deserialize, Serialize};
9
10#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
12#[repr(transparent)]
13pub struct TransactionId(#[serde(with = "as_hex_array")] pub [u8; 32]);
14
15impl core::fmt::Display for TransactionId {
16 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17 write!(f, "{}", hex::encode(self.0))
18 }
19}
20
21impl core::fmt::Debug for TransactionId {
22 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
23 write!(f, "{}", hex::encode(self.0))
24 }
25}
26
27impl core::str::FromStr for TransactionId {
28 type Err = crate::Error;
29 fn from_str(s: &str) -> Result<Self, Self::Err> {
30 Ok(Self(
31 hex::decode(s)
32 .map_err(|_| crate::Error::InvalidTransactionId)?
33 .try_into()
34 .map_err(|_| crate::Error::InvalidTransactionId)?,
35 ))
36 }
37}
38
39impl From<alloy_primitives::TxHash> for TransactionId {
40 fn from(value: alloy_primitives::TxHash) -> Self {
41 Self(value.0)
42 }
43}
44
45impl From<TransactionId> for alloy_primitives::TxHash {
46 fn from(value: TransactionId) -> Self {
47 Self(value.0)
48 }
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
53pub struct Contract {
54 pub parameter: serde_json::Value,
56 pub r#type: String,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
62pub struct RawTxData {
63 #[serde(skip_serializing_if = "Option::is_none")]
64 pub data: Option<String>,
65 pub contract: Vec<Contract>,
66 pub expiration: u64,
67 pub timestamp: u64,
68 #[serde(default)]
69 pub fee_limit: u64,
70 #[serde(with = "as_hex_buffer")]
71 pub ref_block_bytes: Vec<u8>,
72 #[serde(with = "as_hex_buffer")]
73 pub ref_block_hash: Vec<u8>,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
78pub struct Transaction {
79 pub raw_data: RawTxData,
80 #[serde(with = "as_hex_buffer", rename = "raw_data_hex")]
81 pub raw_data_bytes: Vec<u8>,
82 #[serde(rename = "txID")]
83 pub tx_id: TransactionId,
84 #[serde(default)]
85 pub signature: Vec<String>,
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub contract_address: Option<String>,
88 #[serde(skip_serializing_if = "Option::is_none")]
89 pub visible: Option<bool>,
90}
91
92impl Transaction {
93 pub fn tx_id_hex(&self) -> String {
95 self.tx_id.to_string()
96 }
97
98 pub fn raw_data_hex(&self) -> String {
100 hex::encode(&self.raw_data_bytes)
101 }
102}