fix(tauri_bindings): Emit swap id with WaitingForBtcDeposit event

This commit is contained in:
binarybaron 2024-08-27 15:23:43 +02:00 committed by binarybaron
parent 88cfd6bc3e
commit de1f77bf80
3 changed files with 32 additions and 13 deletions

View file

@ -18,13 +18,20 @@ export const swapSlice = createSlice({
swap, swap,
action: PayloadAction<TauriSwapProgressEventWrapper>, action: PayloadAction<TauriSwapProgressEventWrapper>,
) { ) {
// If either:
// 1. No swap is currently running
// 2. The swap ID of the event does not match the current swap ID
//
// Then we create a new swap state object that stores the current and previous events
if (swap.state === null || action.payload.swap_id !== swap.state.swapId) { if (swap.state === null || action.payload.swap_id !== swap.state.swapId) {
console.log("Creating new swap state object");
swap.state = { swap.state = {
curr: action.payload.event, curr: action.payload.event,
prev: null, prev: null,
swapId: action.payload.swap_id, swapId: action.payload.swap_id,
}; };
} else { } else {
console.log("Updating existing swap state object");
swap.state.prev = swap.state.curr; swap.state.prev = swap.state.curr;
swap.state.curr = action.payload.event; swap.state.curr = action.payload.event;
} }

View file

@ -66,6 +66,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -104,6 +105,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -142,6 +144,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -176,6 +179,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -210,6 +214,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -248,6 +253,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.unwrap(); .unwrap();
@ -291,6 +297,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
), ),
) )
.await .await
@ -341,6 +348,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
), ),
) )
.await .await
@ -376,6 +384,7 @@ mod tests {
|| async { Ok(()) }, || async { Ok(()) },
|_| async { Ok(Amount::from_sat(1000)) }, |_| async { Ok(Amount::from_sat(1000)) },
None, None,
None,
) )
.await .await
.err() .err()

View file

@ -622,7 +622,8 @@ pub async fn buy_xmr(
max_givable, max_givable,
|| bitcoin_wallet.sync(), || bitcoin_wallet.sync(),
estimate_fee, estimate_fee,
context.tauri_handle.clone() context.tauri_handle.clone(),
Some(swap_id)
); );
let (amount, fees) = match determine_amount.await { let (amount, fees) = match determine_amount.await {
@ -1086,6 +1087,7 @@ pub async fn determine_btc_to_swap<FB, TB, FMG, TMG, FS, TS, FFE, TFE>(
sync: FS, sync: FS,
estimate_fee: FFE, estimate_fee: FFE,
event_emitter: Option<TauriHandle>, event_emitter: Option<TauriHandle>,
swap_id: Option<Uuid>,
) -> Result<(bitcoin::Amount, bitcoin::Amount)> ) -> Result<(bitcoin::Amount, bitcoin::Amount)>
where where
TB: Future<Output = Result<bitcoin::Amount>>, TB: Future<Output = Result<bitcoin::Amount>>,
@ -1143,18 +1145,19 @@ where
"Waiting for Bitcoin deposit", "Waiting for Bitcoin deposit",
); );
// TODO: Use the real swap id here if let Some(swap_id) = swap_id {
event_emitter.emit_swap_progress_event( event_emitter.emit_swap_progress_event(
Uuid::new_v4(), swap_id,
TauriSwapProgressEvent::WaitingForBtcDeposit { TauriSwapProgressEvent::WaitingForBtcDeposit {
deposit_address: deposit_address.clone(), deposit_address: deposit_address.clone(),
max_giveable, max_giveable,
min_deposit_until_swap_will_start, min_deposit_until_swap_will_start,
max_deposit_until_maximum_amount_is_reached, max_deposit_until_maximum_amount_is_reached,
min_bitcoin_lock_tx_fee, min_bitcoin_lock_tx_fee,
quote: bid_quote, quote: bid_quote.clone(),
}, },
); );
}
max_giveable = loop { max_giveable = loop {
sync().await?; sync().await?;