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

1//! Insert TXO Native Assets into the DB.
2
3use std::sync::Arc;
4
5use cardano_blockchain_types::{Slot, StakeAddress, TxnIndex, TxnOutputOffset};
6use scylla::{SerializeRow, Session};
7use tracing::error;
8
9use crate::{
10    db::{
11        index::queries::{PreparedQueries, SizedBatch},
12        types::{DbSlot, DbStakeAddress, DbTxnIndex, DbTxnOutputOffset},
13    },
14    settings::cassandra_db,
15};
16
17/// TXO Asset by Stake Address Indexing Query
18const INSERT_TXO_ASSET_QUERY: &str = include_str!("./cql/insert_txo_asset.cql");
19
20/// Insert TXO Asset Query Parameters
21/// (Superset of data to support both Staked and Unstaked TXO records.)
22#[derive(SerializeRow, Debug)]
23pub(crate) struct Params {
24    /// Stake Address - Binary 29 bytes.
25    stake_address: DbStakeAddress,
26    /// Block Slot Number
27    slot_no: DbSlot,
28    /// Transaction Offset inside the block.
29    txn_index: DbTxnIndex,
30    /// Transaction Output Offset inside the transaction.
31    txo: DbTxnOutputOffset,
32    /// Policy hash of the asset
33    policy_id: Vec<u8>,
34    /// Name of the asset, within the Policy.
35    asset_name: Vec<u8>,
36    /// Value of the asset
37    value: num_bigint::BigInt,
38}
39
40impl Params {
41    /// Create a new record for this transaction.
42    ///
43    /// Note Value can be either a u64 or an i64, so use a i128 to represent all possible
44    /// values.
45    #[allow(clippy::too_many_arguments)]
46    pub(crate) fn new(
47        stake_address: StakeAddress, slot_no: Slot, txn_index: TxnIndex, txo: TxnOutputOffset,
48        policy_id: &[u8], asset_name: &[u8], value: i128,
49    ) -> Self {
50        Self {
51            stake_address: stake_address.into(),
52            slot_no: slot_no.into(),
53            txn_index: txn_index.into(),
54            txo: txo.into(),
55            policy_id: policy_id.to_vec(),
56            asset_name: asset_name.to_vec(),
57            value: value.into(),
58        }
59    }
60
61    /// Prepare Batch of Staked Insert TXO Asset Index Data Queries
62    pub(crate) async fn prepare_batch(
63        session: &Arc<Session>, cfg: &cassandra_db::EnvVars,
64    ) -> anyhow::Result<SizedBatch> {
65        PreparedQueries::prepare_batch(
66            session.clone(),
67            INSERT_TXO_ASSET_QUERY,
68            cfg,
69            scylla::statement::Consistency::Any,
70            true,
71            false,
72        )
73        .await
74        .inspect_err(|error| error!(error=%error,"Failed to prepare Insert TXO Asset Query."))
75        .map_err(|error| anyhow::anyhow!("{error}\n--\n{INSERT_TXO_ASSET_QUERY}"))
76    }
77}