mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-10-16 15:31:04 -04:00
prettify error, when bob tries to cancel and refund after disconnecting
This commit is contained in:
parent
21fed8c291
commit
4f8907a4b0
2 changed files with 35 additions and 23 deletions
|
@ -1,4 +1,3 @@
|
||||||
use crate::bitcoin::wallet::Subscription;
|
|
||||||
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Wallet};
|
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Wallet};
|
||||||
use crate::protocol::bob::BobState;
|
use crate::protocol::bob::BobState;
|
||||||
use crate::protocol::Database;
|
use crate::protocol::Database;
|
||||||
|
@ -12,8 +11,15 @@ pub async fn cancel_and_refund(
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Arc<dyn Database + Send + Sync>,
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<BobState> {
|
) -> Result<BobState> {
|
||||||
if let Err(err) = cancel(swap_id, bitcoin_wallet.clone(), db.clone()).await {
|
match cancel(swap_id, bitcoin_wallet.clone(), db.clone()).await {
|
||||||
tracing::info!(%err, "Could not submit cancel transaction");
|
Ok((_, state)) => {
|
||||||
|
if matches!(state, BobState::BtcCancelled { .. }) {
|
||||||
|
return Ok(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
tracing::info!(%err, "Could not submit cancel transaction");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = match refund(swap_id, bitcoin_wallet, db).await {
|
let state = match refund(swap_id, bitcoin_wallet, db).await {
|
||||||
|
@ -29,7 +35,7 @@ pub async fn cancel(
|
||||||
swap_id: Uuid,
|
swap_id: Uuid,
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Arc<dyn Database + Send + Sync>,
|
db: Arc<dyn Database + Send + Sync>,
|
||||||
) -> Result<(Txid, Subscription, BobState)> {
|
) -> Result<(Txid, BobState)> {
|
||||||
let state = db.get_state(swap_id).await?.try_into()?;
|
let state = db.get_state(swap_id).await?.try_into()?;
|
||||||
|
|
||||||
let state6 = match state {
|
let state6 = match state {
|
||||||
|
@ -55,26 +61,30 @@ pub async fn cancel(
|
||||||
|
|
||||||
tracing::info!(%swap_id, "Manually cancelling swap");
|
tracing::info!(%swap_id, "Manually cancelling swap");
|
||||||
|
|
||||||
let (txid, subscription) = match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
||||||
Ok(txid) => txid,
|
Ok((txid, _)) => {
|
||||||
|
let state = BobState::BtcCancelled(state6);
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
return Ok((txid, state));
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if let Ok(error_code) = parse_rpc_error_code(&err) {
|
if let Ok(error_code) = parse_rpc_error_code(&err) {
|
||||||
tracing::debug!(%error_code, "parse rpc error");
|
tracing::debug!(%error_code, "parse rpc error");
|
||||||
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
|
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain)
|
||||||
tracing::info!("Cancel transaction has already been confirmed on chain");
|
|| error_code == i64::from(RpcErrorCode::RpcVerifyError)
|
||||||
} else if error_code == i64::from(RpcErrorCode::RpcVerifyError) {
|
{
|
||||||
tracing::info!("General error trying to submit cancel transaction");
|
let txid = state6.construct_tx_cancel().unwrap().txid();
|
||||||
|
let state = BobState::BtcCancelled(state6);
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
tracing::info!("Cancel transaction has already been confirmed on chain. The swap has therefore already been cancelled by Alice");
|
||||||
|
return Ok((txid, state));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bail!(err);
|
bail!(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = BobState::BtcCancelled(state6);
|
|
||||||
db.insert_latest_state(swap_id, state.clone().into())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok((txid, subscription, state))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refund(
|
pub async fn refund(
|
||||||
|
|
|
@ -663,12 +663,8 @@ impl State6 {
|
||||||
|
|
||||||
Ok(tx)
|
Ok(tx)
|
||||||
}
|
}
|
||||||
|
pub fn construct_tx_cancel(&self) -> Result<Transaction> {
|
||||||
pub async fn submit_tx_cancel(
|
bitcoin::TxCancel::new(
|
||||||
&self,
|
|
||||||
bitcoin_wallet: &bitcoin::Wallet,
|
|
||||||
) -> Result<(Txid, Subscription)> {
|
|
||||||
let transaction = bitcoin::TxCancel::new(
|
|
||||||
&self.tx_lock,
|
&self.tx_lock,
|
||||||
self.cancel_timelock,
|
self.cancel_timelock,
|
||||||
self.A,
|
self.A,
|
||||||
|
@ -676,7 +672,13 @@ impl State6 {
|
||||||
self.tx_cancel_fee,
|
self.tx_cancel_fee,
|
||||||
)?
|
)?
|
||||||
.complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone())
|
.complete_as_bob(self.A, self.b.clone(), self.tx_cancel_sig_a.clone())
|
||||||
.context("Failed to complete Bitcoin cancel transaction")?;
|
.context("Failed to complete Bitcoin cancel transaction")
|
||||||
|
}
|
||||||
|
pub async fn submit_tx_cancel(
|
||||||
|
&self,
|
||||||
|
bitcoin_wallet: &bitcoin::Wallet,
|
||||||
|
) -> Result<(Txid, Subscription)> {
|
||||||
|
let transaction = self.construct_tx_cancel()?;
|
||||||
|
|
||||||
let (tx_id, subscription) = bitcoin_wallet.broadcast(transaction, "cancel").await?;
|
let (tx_id, subscription) = bitcoin_wallet.broadcast(transaction, "cancel").await?;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue