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