Reclaim unspent proofs by reverting transaction (#774)

* Reclaim unspent proofs by reverting transaction

* Change fn signatore to return unit
This commit is contained in:
David Caseria
2025-05-29 12:12:10 -04:00
committed by GitHub
parent 9beb0b4256
commit 30d6b20c99
2 changed files with 29 additions and 0 deletions

View File

@@ -234,6 +234,9 @@ pub enum Error {
/// Invalid transaction id
#[error("Invalid transaction id")]
InvalidTransactionId,
/// Transaction not found
#[error("Transaction not found")]
TransactionNotFound,
/// Custom Error
#[error("`{0}`")]
Custom(String),

View File

@@ -28,4 +28,30 @@ impl Wallet {
Ok(transaction)
}
/// Revert a transaction
pub async fn revert_transaction(&self, id: TransactionId) -> Result<(), Error> {
let tx = self
.localstore
.get_transaction(id)
.await?
.ok_or(Error::TransactionNotFound)?;
if tx.direction != TransactionDirection::Outgoing {
return Err(Error::InvalidTransactionDirection);
}
let pending_spent_proofs = self
.get_pending_spent_proofs()
.await?
.into_iter()
.filter(|p| match p.y() {
Ok(y) => tx.ys.contains(&y),
Err(_) => false,
})
.collect::<Vec<_>>();
self.reclaim_unspent(pending_spent_proofs).await?;
Ok(())
}
}