use rusqlite::types::Value;
use darkfi::{tx::Transaction, Error, Result};
use darkfi_serial::{deserialize_async, serialize_async};
use crate::{
convert_named_params,
error::{WalletDbError, WalletDbResult},
Drk,
};
const WALLET_TXS_HISTORY_TABLE: &str = "transactions_history";
const WALLET_TXS_HISTORY_COL_TX_HASH: &str = "transaction_hash";
const WALLET_TXS_HISTORY_COL_STATUS: &str = "status";
const WALLET_TXS_HISTORY_COL_TX: &str = "tx";
impl Drk {
pub async fn put_tx_history_record(
&self,
tx: &Transaction,
status: &str,
) -> WalletDbResult<String> {
let query = format!(
"INSERT OR REPLACE INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);",
WALLET_TXS_HISTORY_TABLE,
WALLET_TXS_HISTORY_COL_TX_HASH,
WALLET_TXS_HISTORY_COL_STATUS,
WALLET_TXS_HISTORY_COL_TX,
);
let tx_hash = tx.hash().to_string();
let inverse = self.wallet.create_prepared_statement(
&format!(
"UPDATE {} SET {} = ?1 WHERE {} = ?2;",
WALLET_TXS_HISTORY_TABLE,
WALLET_TXS_HISTORY_COL_STATUS,
WALLET_TXS_HISTORY_COL_TX_HASH
),
rusqlite::params!["Reverted", tx_hash],
)?;
self.wallet
.exec_sql(&query, rusqlite::params![tx_hash, status, &serialize_async(tx).await,])?;
self.wallet.cache_inverse(inverse)?;
Ok(tx_hash)
}
pub async fn put_tx_history_records(
&self,
txs: &[&Transaction],
status: &str,
) -> WalletDbResult<Vec<String>> {
let mut ret = Vec::with_capacity(txs.len());
for tx in txs {
ret.push(self.put_tx_history_record(tx, status).await?);
}
Ok(ret)
}
pub async fn get_tx_history_record(
&self,
tx_hash: &str,
) -> Result<(String, String, Transaction)> {
let row = match self.wallet.query_single(
WALLET_TXS_HISTORY_TABLE,
&[],
convert_named_params! {(WALLET_TXS_HISTORY_COL_TX_HASH, tx_hash)},
) {
Ok(r) => r,
Err(e) => {
return Err(Error::DatabaseError(format!(
"[get_tx_history_record] Transaction history record retrieval failed: {e:?}"
)))
}
};
let Value::Text(ref tx_hash) = row[0] else {
return Err(Error::ParseFailed(
"[get_tx_history_record] Transaction hash parsing failed",
))
};
let Value::Text(ref status) = row[1] else {
return Err(Error::ParseFailed("[get_tx_history_record] Status parsing failed"))
};
let Value::Blob(ref bytes) = row[2] else {
return Err(Error::ParseFailed(
"[get_tx_history_record] Transaction bytes parsing failed",
))
};
let tx: Transaction = deserialize_async(bytes).await?;
Ok((tx_hash.clone(), status.clone(), tx))
}
pub fn get_txs_history(&self) -> WalletDbResult<Vec<(String, String)>> {
let rows = self.wallet.query_multiple(
WALLET_TXS_HISTORY_TABLE,
&[WALLET_TXS_HISTORY_COL_TX_HASH, WALLET_TXS_HISTORY_COL_STATUS],
&[],
)?;
let mut ret = Vec::with_capacity(rows.len());
for row in rows {
let Value::Text(ref tx_hash) = row[0] else {
return Err(WalletDbError::ParseColumnValueError)
};
let Value::Text(ref status) = row[1] else {
return Err(WalletDbError::ParseColumnValueError)
};
ret.push((tx_hash.clone(), status.clone()));
}
Ok(ret)
}
pub fn reset_tx_history(&self) -> WalletDbResult<()> {
println!("Resetting transactions history");
let query = format!("DELETE FROM {};", WALLET_TXS_HISTORY_TABLE);
self.wallet.exec_sql(&query, &[])?;
println!("Successfully reset transactions history");
Ok(())
}
pub fn remove_reverted_txs(&self) -> WalletDbResult<()> {
println!("Removing reverted transactions history records");
let query = format!(
"DELETE FROM {} WHERE {} = 'Reverted';",
WALLET_TXS_HISTORY_TABLE, WALLET_TXS_HISTORY_COL_STATUS
);
self.wallet.exec_sql(&query, &[])?;
println!("Successfully removed reverted transactions history records");
Ok(())
}
}