riftenlabs-indexer/src/db/cauldron/utxo_spending.rs
Dagur Valberg Johannsson d85bc2c9db
Update copyright headers
2026-01-21 12:37:13 +01:00

39 lines
1.1 KiB
Rust

// Copyright (C) 2024-2026 Whiterun LLC
//
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
use anyhow::Result;
use bitcoin_hashes::hex::ToHex;
use bitcoincash::Txid;
use riftenlabs_defi::cauldron::ParsedContract;
use rusqlite::{params, Connection};
pub fn create_table(conn: &Connection) {
conn.execute(
"CREATE TABLE utxo_spending (
spent_utxo_hash TEXT PRIMARY KEY,
txid TEXT,
FOREIGN KEY(txid) REFERENCES tx(txid) ON DELETE CASCADE
)",
[],
)
.unwrap();
}
pub fn insert_utxo_spending(
tx: &Connection,
cauldrons: &Vec<ParsedContract>,
txid: &Txid,
replace: bool,
) -> Result<()> {
let mut statement = tx.prepare(&format!(
"INSERT OR {} INTO utxo_spending (spent_utxo_hash, txid) VALUES (?, ?)",
if replace { "REPLACE" } else { "IGNORE " }
))?;
for c in cauldrons {
statement.execute(params![c.spent_utxo_hash.to_hex(), txid.to_hex(),])?;
}
Ok(())
}