cat_gateway/db/index/block/txo/
insert_txo.rs

1//! Insert TXO Indexed Data Queries.
2//!
3//! Note, there are multiple ways TXO Data is indexed and they all happen in here.
4
5use std::sync::Arc;
6
7use cardano_chain_follower::{
8    hashes::TransactionId, Slot, StakeAddress, TxnIndex, TxnOutputOffset,
9};
10use scylla::{client::session::Session, SerializeRow};
11use tracing::error;
12
13use crate::{
14    db::{
15        index::queries::{PreparedQueries, SizedBatch},
16        types::{DbSlot, DbStakeAddress, DbTransactionId, DbTxnIndex, DbTxnOutputOffset},
17    },
18    settings::cassandra_db,
19};
20
21/// TXO by Stake Address Indexing query
22const INSERT_TXO_QUERY: &str = include_str!("./cql/insert_txo.cql");
23
24/// Insert TXO Query Parameters
25/// (Superset of data to support both Staked and Unstaked TXO records.)
26#[derive(SerializeRow, Debug)]
27pub(crate) struct Params {
28    /// Stake Address - Binary 29 bytes.
29    stake_address: DbStakeAddress,
30    /// Block Slot Number
31    slot_no: DbSlot,
32    /// Transaction Offset inside the block.
33    txn_index: DbTxnIndex,
34    /// Transaction Output Offset inside the transaction.
35    txo: DbTxnOutputOffset,
36    /// Actual full TXO Address
37    address: String,
38    /// Actual TXO Value in lovelace
39    value: num_bigint::BigInt,
40    /// Transactions hash.
41    txn_hash: DbTransactionId,
42}
43
44impl Params {
45    /// Create a new record for this transaction.
46    pub(crate) fn new(
47        stake_address: StakeAddress,
48        slot_no: Slot,
49        txn_index: TxnIndex,
50        txo: TxnOutputOffset,
51        address: &str,
52        value: u64,
53        txn_hash: TransactionId,
54    ) -> Self {
55        Self {
56            stake_address: stake_address.into(),
57            slot_no: slot_no.into(),
58            txn_index: txn_index.into(),
59            txo: txo.into(),
60            address: address.to_string(),
61            value: value.into(),
62            txn_hash: txn_hash.into(),
63        }
64    }
65
66    /// Prepare Batch of Staked Insert TXO Asset Index Data Queries
67    pub(crate) async fn prepare_batch(
68        session: &Arc<Session>,
69        cfg: &cassandra_db::EnvVars,
70    ) -> anyhow::Result<SizedBatch> {
71        PreparedQueries::prepare_batch(
72            session.clone(),
73            INSERT_TXO_QUERY,
74            cfg,
75            scylla::statement::Consistency::Any,
76            true,
77            false,
78        )
79        .await
80        .inspect_err(|error| error!(error=%error,"Failed to prepare Insert TXO Query."))
81        .map_err(|error| anyhow::anyhow!("{error}\n--\n{INSERT_TXO_QUERY}"))
82    }
83}