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

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