mithril_persistence/database/record/
storable_cardano_transaction.rs

1use sqlite::Row;
2
3use mithril_common::entities::{BlockHash, TransactionHash};
4
5use crate::sqlite::{HydrationError, Projection, SqLiteEntity};
6
7/// The record representation of a cardano transaction that can be stored in the database.
8#[derive(Debug, PartialEq, Clone)]
9pub struct StorableCardanoTransactionRecord {
10    /// Unique hash of the transaction
11    pub transaction_hash: TransactionHash,
12
13    /// Block hash of the transaction
14    pub block_hash: BlockHash,
15}
16
17impl StorableCardanoTransactionRecord {
18    /// StorableCardanoTransactionRecord factory
19    pub fn new<T: Into<TransactionHash>, U: Into<BlockHash>>(hash: T, block_hash: U) -> Self {
20        Self {
21            transaction_hash: hash.into(),
22            block_hash: block_hash.into(),
23        }
24    }
25}
26
27impl SqLiteEntity for StorableCardanoTransactionRecord {
28    fn hydrate(row: Row) -> Result<Self, HydrationError>
29    where
30        Self: Sized,
31    {
32        let transaction_hash = row.read::<&str, _>(0);
33        let block_hash = row.read::<&str, _>(1);
34
35        Ok(Self {
36            transaction_hash: transaction_hash.to_string(),
37            block_hash: block_hash.to_string(),
38        })
39    }
40
41    fn get_projection() -> Projection {
42        Projection::from(&[
43            (
44                "transaction_hash",
45                "{:cardano_tx:}.transaction_hash",
46                "text",
47            ),
48            ("block_hash", "{:cardano_tx:}.block_hash", "text"),
49        ])
50    }
51}