Skip to content

Commit

Permalink
fix(legacy-swap): taker failed spend maker payment marked as failed (#…
Browse files Browse the repository at this point in the history
…2199)

This fixes the issue where taker doesn't wait for maker payment spend confirmation. Two new events were added:
- "MakerPaymentSpendConfirmed": Success event, fired after "MakerPaymentSpent".
- "MakerPaymentSpendConfirmFailed": Error event, means that the taker spend maker payment transaction confirmation was failed.
  • Loading branch information
laruh authored Oct 4, 2024
1 parent 122112a commit 359cb5c
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@
"Started",
"Negotiated",
"TakerFeeSent",
"TakerPaymentInstructionsReceived",
"MakerPaymentReceived",
"MakerPaymentWaitConfirmStarted",
"MakerPaymentValidatedAndConfirmed",
"TakerPaymentSent",
"TakerPaymentSpent",
"MakerPaymentSpent",
"MakerPaymentSpendConfirmed",
"Finished"
],
"error_events": [
Expand All @@ -164,8 +166,12 @@
"TakerPaymentDataSendFailed",
"TakerPaymentWaitForSpendFailed",
"MakerPaymentSpendFailed",
"MakerPaymentSpendConfirmFailed",
"TakerPaymentWaitRefundStarted",
"TakerPaymentRefundStarted",
"TakerPaymentRefunded",
"TakerPaymentRefundFailed"
"TakerPaymentRefundedByWatcher",
"TakerPaymentRefundFailed",
"TakerPaymentRefundFinished"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@
"taker_coin":"MORTY",
"gui":"atomicDEX 0.5.1 iOS",
"mm_version":"1b065636a",
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","Finished"],
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","MakerPaymentSpendConfirmed","Finished"],
"error_events":["StartFailed","NegotiateFailed","TakerFeeSendFailed","MakerPaymentValidateFailed","MakerPaymentWaitConfirmFailed","TakerPaymentTransactionFailed","TakerPaymentWaitConfirmFailed","TakerPaymentDataSendFailed","TakerPaymentWaitForSpendFailed","MakerPaymentSpendFailed","TakerPaymentWaitRefundStarted","TakerPaymentRefunded","TakerPaymentRefundedByWatcher","TakerPaymentRefundFailed"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"taker_coin":"MORTY",
"gui":"atomicDEX 0.5.1 iOS",
"mm_version":"1b065636a",
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","Finished"],
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","MakerPaymentSpendConfirmed","Finished"],
"error_events":["StartFailed","NegotiateFailed","TakerFeeSendFailed","MakerPaymentValidateFailed","MakerPaymentWaitConfirmFailed","TakerPaymentTransactionFailed","TakerPaymentWaitConfirmFailed","TakerPaymentDataSendFailed","TakerPaymentWaitForSpendFailed","MakerPaymentSpendFailed","TakerPaymentWaitRefundStarted","TakerPaymentRefunded","TakerPaymentRefundedByWatcher","TakerPaymentRefundFailed"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"taker_coin":"MORTY",
"gui":null,
"mm_version":"",
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","Finished"],
"error_events":["StartFailed","NegotiateFailed","TakerFeeSendFailed","MakerPaymentValidateFailed","MakerPaymentWaitConfirmFailed","TakerPaymentTransactionFailed","TakerPaymentWaitConfirmFailed","TakerPaymentDataSendFailed","TakerPaymentWaitForSpendFailed","MakerPaymentSpendFailed","TakerPaymentWaitRefundStarted","TakerPaymentRefundStarted","TakerPaymentRefunded","TakerPaymentRefundedByWatcher","TakerPaymentRefundFailed","TakerPaymentRefundFinished"]
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","MakerPaymentSpendConfirmed","Finished"],
"error_events":["StartFailed","NegotiateFailed","TakerFeeSendFailed","MakerPaymentValidateFailed","MakerPaymentWaitConfirmFailed","TakerPaymentTransactionFailed","TakerPaymentWaitConfirmFailed","TakerPaymentDataSendFailed","TakerPaymentWaitForSpendFailed","MakerPaymentSpendFailed","MakerPaymentSpendConfirmFailed","TakerPaymentWaitRefundStarted","TakerPaymentRefundStarted","TakerPaymentRefunded","TakerPaymentRefundedByWatcher","TakerPaymentRefundFailed","TakerPaymentRefundFinished"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@
"taker_coin":"MORTY",
"gui":null,
"mm_version":"",
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","Finished"],
"error_events":["StartFailed","NegotiateFailed","TakerFeeSendFailed","MakerPaymentValidateFailed","MakerPaymentWaitConfirmFailed","TakerPaymentTransactionFailed","TakerPaymentWaitConfirmFailed","TakerPaymentDataSendFailed","TakerPaymentWaitForSpendFailed","MakerPaymentSpendFailed","TakerPaymentWaitRefundStarted","TakerPaymentRefundStarted","TakerPaymentRefunded","TakerPaymentRefundedByWatcher","TakerPaymentRefundFailed","TakerPaymentRefundFinished"]
"success_events":["Started","Negotiated","TakerFeeSent","TakerPaymentInstructionsReceived","MakerPaymentReceived","MakerPaymentWaitConfirmStarted","MakerPaymentValidatedAndConfirmed","TakerPaymentSent","TakerPaymentSpent","MakerPaymentSpent","MakerPaymentSpendConfirmed","Finished"],
"error_events":["StartFailed","NegotiateFailed","TakerFeeSendFailed","MakerPaymentValidateFailed","MakerPaymentWaitConfirmFailed","TakerPaymentTransactionFailed","TakerPaymentWaitConfirmFailed","TakerPaymentDataSendFailed","TakerPaymentWaitForSpendFailed","MakerPaymentSpendFailed","MakerPaymentSpendConfirmFailed","TakerPaymentWaitRefundStarted","TakerPaymentRefundStarted","TakerPaymentRefunded","TakerPaymentRefundedByWatcher","TakerPaymentRefundFailed","TakerPaymentRefundFinished"]
}
8 changes: 4 additions & 4 deletions mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,12 +1141,12 @@ impl MakerSwap {
}

async fn confirm_taker_payment_spend(&self) -> Result<(Option<MakerSwapCommand>, Vec<MakerSwapEvent>), String> {
// we should wait for only one confirmation to make sure our spend transaction is not failed
let confirmations = std::cmp::min(1, self.r().data.taker_payment_confirmations);
// We should wait for only one confirmation to make sure our spend transaction is not failed.
// However, we allow the user to use 0 confirmations if specified.
let requires_nota = false;
let confirm_taker_payment_spend_input = ConfirmPaymentInput {
payment_tx: self.r().taker_payment_spend.clone().unwrap().tx_hex.0,
confirmations,
confirmations: std::cmp::min(1, self.r().data.taker_payment_confirmations),
requires_nota,
wait_until: self.wait_refund_until(),
check_every: WAIT_CONFIRM_INTERVAL_SEC,
Expand Down Expand Up @@ -1679,7 +1679,7 @@ impl MakerSwapEvent {
},
MakerSwapEvent::TakerPaymentSpent(_) => "Taker payment spent...".to_owned(),
MakerSwapEvent::TakerPaymentSpendFailed(_) => "Taker payment spend failed...".to_owned(),
MakerSwapEvent::TakerPaymentSpendConfirmStarted => "Taker payment send wait confirm started...".to_owned(),
MakerSwapEvent::TakerPaymentSpendConfirmStarted => "Taker payment spend confirm started...".to_owned(),
MakerSwapEvent::TakerPaymentSpendConfirmed => "Taker payment spend confirmed...".to_owned(),
MakerSwapEvent::TakerPaymentSpendConfirmFailed(_) => "Taker payment spend confirm failed...".to_owned(),
MakerSwapEvent::MakerPaymentWaitRefundStarted { wait_until } => {
Expand Down
2 changes: 2 additions & 0 deletions mm2src/mm2_main/src/lp_swap/recreate_swap_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ fn convert_taker_to_maker_events(
| TakerSwapEvent::MakerPaymentWaitConfirmStarted
| TakerSwapEvent::MakerPaymentValidatedAndConfirmed
| TakerSwapEvent::MakerPaymentSpent(_)
| TakerSwapEvent::MakerPaymentSpendConfirmed
| TakerSwapEvent::MakerPaymentSpendConfirmFailed(_)
| TakerSwapEvent::MakerPaymentSpentByWatcher(_)
| TakerSwapEvent::MakerPaymentSpendFailed(_)
// We don't know the reason at the moment, so we rely on the errors handling above.
Expand Down
3 changes: 2 additions & 1 deletion mm2src/mm2_main/src/lp_swap/taker_restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub async fn get_command_based_on_maker_or_watcher_activity(
Err(e) => ERR!("Error {} when trying to find taker payment spend", e),
},
TakerSwapCommand::SpendMakerPayment => check_maker_payment_spend_and_add_event(ctx, swap, saved).await,
TakerSwapCommand::ConfirmMakerPaymentSpend => Ok(command),
TakerSwapCommand::PrepareForTakerPaymentRefund | TakerSwapCommand::RefundTakerPayment => {
#[cfg(not(any(test, feature = "run-docker-tests")))]
{
Expand Down Expand Up @@ -141,7 +142,7 @@ pub async fn check_maker_payment_spend_and_add_event(
let new_swap = SavedSwap::Taker(saved);
try_s!(new_swap.save_to_db(ctx).await);
info!("{}", MAKER_PAYMENT_SPENT_BY_WATCHER_LOG);
Ok(TakerSwapCommand::Finish)
Ok(TakerSwapCommand::ConfirmMakerPaymentSpend)
}

pub async fn check_taker_payment_spend(swap: &TakerSwap) -> Result<Option<FoundSwapTxSpend>, String> {
Expand Down
101 changes: 80 additions & 21 deletions mm2src/mm2_main/src/lp_swap/taker_swap.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions mm2src/mm2_main/tests/docker_tests/swap_watcher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ fn test_taker_saves_the_swap_as_successful_after_restart_panic_at_wait_for_taker
"WatcherMessageSent",
"TakerPaymentSpent",
"MakerPaymentSpentByWatcher",
"MakerPaymentSpendConfirmed",
"Finished",
];
check_actual_events(&mm_alice, &uuids[0], &expected_events);
Expand Down Expand Up @@ -467,6 +468,7 @@ fn test_taker_saves_the_swap_as_successful_after_restart_panic_at_maker_payment_
"WatcherMessageSent",
"TakerPaymentSpent",
"MakerPaymentSpentByWatcher",
"MakerPaymentSpendConfirmed",
"Finished",
];
check_actual_events(&mm_alice, &uuids[0], &expected_events);
Expand Down
Loading

0 comments on commit 359cb5c

Please sign in to comment.