2021-03-04 00:28:58 +00:00
|
|
|
use crate::bitcoin::ExpiredTimelocks;
|
|
|
|
use crate::database::{Database, Swap};
|
|
|
|
use crate::execution_params::ExecutionParams;
|
|
|
|
use crate::monero::InsufficientFunds;
|
|
|
|
use crate::protocol::bob;
|
|
|
|
use crate::protocol::bob::event_loop::EventLoopHandle;
|
|
|
|
use crate::protocol::bob::state::*;
|
|
|
|
use crate::{bitcoin, monero};
|
2021-01-21 00:20:57 +00:00
|
|
|
use anyhow::{bail, Result};
|
|
|
|
use async_recursion::async_recursion;
|
2021-02-15 02:01:38 +00:00
|
|
|
use rand::rngs::OsRng;
|
2021-01-21 00:20:57 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::select;
|
2021-02-26 06:03:07 +00:00
|
|
|
use tracing::{trace, warn};
|
2021-01-21 00:20:57 +00:00
|
|
|
use uuid::Uuid;
|
2020-12-02 01:09:43 +00:00
|
|
|
|
|
|
|
pub fn is_complete(state: &BobState) -> bool {
|
2020-12-02 01:36:47 +00:00
|
|
|
matches!(
|
|
|
|
state,
|
2020-12-11 05:49:19 +00:00
|
|
|
BobState::BtcRefunded(..)
|
2021-01-18 03:45:47 +00:00
|
|
|
| BobState::XmrRedeemed { .. }
|
|
|
|
| BobState::BtcPunished { .. }
|
2020-12-02 01:36:47 +00:00
|
|
|
| BobState::SafelyAborted
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-18 08:56:43 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub async fn run(swap: bob::Swap) -> Result<BobState> {
|
|
|
|
run_until(swap, is_complete).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_until(
|
|
|
|
swap: bob::Swap,
|
|
|
|
is_target_state: fn(&BobState) -> bool,
|
|
|
|
) -> Result<BobState> {
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2021-01-18 08:56:43 +00:00
|
|
|
swap.state,
|
|
|
|
is_target_state,
|
|
|
|
swap.event_loop_handle,
|
|
|
|
swap.db,
|
|
|
|
swap.bitcoin_wallet,
|
|
|
|
swap.monero_wallet,
|
|
|
|
swap.swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
swap.execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
swap.receive_monero_address,
|
2021-01-18 08:56:43 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2020-11-12 00:06:34 +00:00
|
|
|
// State machine driver for swap execution
|
2020-12-02 01:36:47 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-11-12 00:06:34 +00:00
|
|
|
#[async_recursion]
|
2021-02-03 04:25:05 +00:00
|
|
|
async fn run_until_internal(
|
2020-11-12 00:06:34 +00:00
|
|
|
state: BobState,
|
2020-12-02 01:36:47 +00:00
|
|
|
is_target_state: fn(&BobState) -> bool,
|
2020-12-10 02:55:29 +00:00
|
|
|
mut event_loop_handle: EventLoopHandle,
|
2020-11-12 00:06:34 +00:00
|
|
|
db: Database,
|
2021-01-14 22:45:00 +00:00
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
2020-11-26 02:55:56 +00:00
|
|
|
swap_id: Uuid,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params: ExecutionParams,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address: monero::Address,
|
2021-02-03 04:25:05 +00:00
|
|
|
) -> Result<BobState> {
|
2021-02-26 06:03:07 +00:00
|
|
|
trace!("Current state: {}", state);
|
2020-12-02 01:36:47 +00:00
|
|
|
if is_target_state(&state) {
|
2020-12-02 01:09:43 +00:00
|
|
|
Ok(state)
|
|
|
|
} else {
|
|
|
|
match state {
|
2021-02-15 02:01:38 +00:00
|
|
|
BobState::Started { btc_amount } => {
|
|
|
|
let bitcoin_refund_address = bitcoin_wallet.new_address().await?;
|
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
event_loop_handle.dial().await?;
|
2020-12-18 06:39:04 +00:00
|
|
|
|
2021-03-03 04:45:43 +00:00
|
|
|
let state2 = request_price_and_setup(
|
2021-02-15 02:01:38 +00:00
|
|
|
btc_amount,
|
|
|
|
&mut event_loop_handle,
|
|
|
|
execution_params,
|
|
|
|
bitcoin_refund_address,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-12-08 03:54:08 +00:00
|
|
|
|
2021-02-15 02:01:38 +00:00
|
|
|
let state = BobState::ExecutionSetupDone(state2);
|
2020-12-08 03:54:08 +00:00
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-08 03:54:08 +00:00
|
|
|
state,
|
2020-12-02 01:36:47 +00:00
|
|
|
is_target_state,
|
2020-12-10 02:55:29 +00:00
|
|
|
event_loop_handle,
|
2020-12-02 01:09:43 +00:00
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-02 01:09:43 +00:00
|
|
|
)
|
2020-12-02 01:36:47 +00:00
|
|
|
.await
|
2020-12-02 01:09:43 +00:00
|
|
|
}
|
2021-02-15 02:01:38 +00:00
|
|
|
BobState::ExecutionSetupDone(state2) => {
|
2020-12-18 06:39:04 +00:00
|
|
|
// Do not lock Bitcoin if not connected to Alice.
|
2020-12-22 02:47:05 +00:00
|
|
|
event_loop_handle.dial().await?;
|
2020-12-02 01:09:43 +00:00
|
|
|
// Alice and Bob have exchanged info
|
|
|
|
let state3 = state2.lock_btc(bitcoin_wallet.as_ref()).await?;
|
2020-12-08 03:54:08 +00:00
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
let state = BobState::BtcLocked(state3);
|
2020-12-08 03:54:08 +00:00
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-08 03:54:08 +00:00
|
|
|
state,
|
2020-12-02 01:36:47 +00:00
|
|
|
is_target_state,
|
2020-12-10 02:55:29 +00:00
|
|
|
event_loop_handle,
|
2020-12-02 01:09:43 +00:00
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-02 01:09:43 +00:00
|
|
|
)
|
2020-12-02 01:36:47 +00:00
|
|
|
.await
|
2020-12-02 01:09:43 +00:00
|
|
|
}
|
|
|
|
// Bob has locked Btc
|
2020-12-22 04:47:09 +00:00
|
|
|
// Watch for Alice to Lock Xmr or for cancel timelock to elapse
|
2020-12-22 02:47:05 +00:00
|
|
|
BobState::BtcLocked(state3) => {
|
2020-12-22 04:47:09 +00:00
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
state3.current_epoch(bitcoin_wallet.as_ref()).await?
|
2020-12-21 06:33:18 +00:00
|
|
|
{
|
|
|
|
event_loop_handle.dial().await?;
|
2020-12-18 06:39:04 +00:00
|
|
|
|
2021-01-27 03:14:20 +00:00
|
|
|
let transfer_proof_watcher = event_loop_handle.recv_transfer_proof();
|
2020-12-22 04:47:09 +00:00
|
|
|
let cancel_timelock_expires =
|
|
|
|
state3.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
2020-12-07 02:31:14 +00:00
|
|
|
|
2021-01-06 05:31:47 +00:00
|
|
|
// Record the current monero wallet block height so we don't have to scan from
|
|
|
|
// block 0 once we create the redeem wallet.
|
2021-02-24 05:34:04 +00:00
|
|
|
let monero_wallet_restore_blockheight = monero_wallet.block_height().await?;
|
2021-01-06 05:31:47 +00:00
|
|
|
|
2020-12-22 06:08:17 +00:00
|
|
|
select! {
|
2021-01-27 03:14:20 +00:00
|
|
|
transfer_proof = transfer_proof_watcher => {
|
|
|
|
let transfer_proof = transfer_proof?;
|
2021-01-21 05:39:30 +00:00
|
|
|
BobState::XmrLockProofReceived {
|
|
|
|
state: state3,
|
2021-01-27 03:14:20 +00:00
|
|
|
lock_transfer_proof: transfer_proof.tx_lock_proof,
|
2021-01-21 05:39:30 +00:00
|
|
|
monero_wallet_restore_blockheight
|
|
|
|
}
|
2020-12-22 06:08:17 +00:00
|
|
|
},
|
2020-12-22 04:47:09 +00:00
|
|
|
_ = cancel_timelock_expires => {
|
2021-02-24 06:36:37 +00:00
|
|
|
let state4 = state3.cancel();
|
2020-12-22 04:47:09 +00:00
|
|
|
BobState::CancelTimelockExpired(state4)
|
2020-12-22 06:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 06:33:18 +00:00
|
|
|
} else {
|
2021-02-24 06:36:37 +00:00
|
|
|
let state4 = state3.cancel();
|
2020-12-22 04:47:09 +00:00
|
|
|
BobState::CancelTimelockExpired(state4)
|
2020-12-21 06:33:18 +00:00
|
|
|
};
|
2020-12-08 03:54:08 +00:00
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-08 03:54:08 +00:00
|
|
|
state,
|
2020-12-02 01:36:47 +00:00
|
|
|
is_target_state,
|
2020-12-10 02:55:29 +00:00
|
|
|
event_loop_handle,
|
2020-12-02 01:09:43 +00:00
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-02 01:09:43 +00:00
|
|
|
)
|
2020-12-02 01:36:47 +00:00
|
|
|
.await
|
2020-12-02 01:09:43 +00:00
|
|
|
}
|
2021-01-21 05:39:30 +00:00
|
|
|
BobState::XmrLockProofReceived {
|
|
|
|
state,
|
|
|
|
lock_transfer_proof,
|
|
|
|
monero_wallet_restore_blockheight,
|
|
|
|
} => {
|
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
state.current_epoch(bitcoin_wallet.as_ref()).await?
|
|
|
|
{
|
|
|
|
event_loop_handle.dial().await?;
|
|
|
|
|
|
|
|
let xmr_lock_watcher = state.clone().watch_for_lock_xmr(
|
|
|
|
monero_wallet.as_ref(),
|
|
|
|
lock_transfer_proof,
|
2021-02-24 06:38:35 +00:00
|
|
|
monero_wallet_restore_blockheight,
|
2021-01-21 05:39:30 +00:00
|
|
|
);
|
|
|
|
let cancel_timelock_expires =
|
|
|
|
state.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
|
|
|
|
|
|
|
select! {
|
|
|
|
state4 = xmr_lock_watcher => {
|
2021-02-16 05:00:27 +00:00
|
|
|
match state4? {
|
|
|
|
Ok(state4) => BobState::XmrLocked(state4),
|
|
|
|
Err(InsufficientFunds {..}) => {
|
2021-02-26 06:03:07 +00:00
|
|
|
warn!("The other party has locked insufficient Monero funds! Waiting for refund...");
|
2021-02-16 05:00:27 +00:00
|
|
|
state.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref()).await?;
|
2021-02-24 06:36:37 +00:00
|
|
|
let state4 = state.cancel();
|
2021-02-16 05:00:27 +00:00
|
|
|
BobState::CancelTimelockExpired(state4)
|
|
|
|
},
|
|
|
|
}
|
2021-01-21 05:39:30 +00:00
|
|
|
},
|
|
|
|
_ = cancel_timelock_expires => {
|
2021-02-24 06:36:37 +00:00
|
|
|
let state4 = state.cancel();
|
2021-01-21 05:39:30 +00:00
|
|
|
BobState::CancelTimelockExpired(state4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-24 06:36:37 +00:00
|
|
|
let state4 = state.cancel();
|
2021-01-21 05:39:30 +00:00
|
|
|
BobState::CancelTimelockExpired(state4)
|
|
|
|
};
|
|
|
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
run_until_internal(
|
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2021-01-21 05:39:30 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-12-22 02:47:05 +00:00
|
|
|
BobState::XmrLocked(state) => {
|
2020-12-22 04:47:09 +00:00
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
state.expired_timelock(bitcoin_wallet.as_ref()).await?
|
|
|
|
{
|
2020-12-21 06:33:18 +00:00
|
|
|
event_loop_handle.dial().await?;
|
2020-12-17 04:17:15 +00:00
|
|
|
// Alice has locked Xmr
|
|
|
|
// Bob sends Alice his key
|
|
|
|
let tx_redeem_encsig = state.tx_redeem_encsig();
|
|
|
|
|
|
|
|
let state4_clone = state.clone();
|
2021-01-11 05:59:22 +00:00
|
|
|
|
2021-01-27 03:25:45 +00:00
|
|
|
let enc_sig_sent_watcher =
|
|
|
|
event_loop_handle.send_encrypted_signature(tx_redeem_encsig);
|
2020-12-17 04:17:15 +00:00
|
|
|
let bitcoin_wallet = bitcoin_wallet.clone();
|
2020-12-22 04:47:09 +00:00
|
|
|
let cancel_timelock_expires =
|
|
|
|
state4_clone.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
2020-12-17 04:17:15 +00:00
|
|
|
|
2020-12-18 00:37:02 +00:00
|
|
|
select! {
|
|
|
|
_ = enc_sig_sent_watcher => {
|
2020-12-18 06:39:04 +00:00
|
|
|
BobState::EncSigSent(state)
|
2020-12-18 00:37:02 +00:00
|
|
|
},
|
2020-12-22 04:47:09 +00:00
|
|
|
_ = cancel_timelock_expires => {
|
|
|
|
BobState::CancelTimelockExpired(state)
|
2020-12-18 00:37:02 +00:00
|
|
|
}
|
2020-12-17 04:17:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-22 04:47:09 +00:00
|
|
|
BobState::CancelTimelockExpired(state)
|
2020-12-17 04:17:15 +00:00
|
|
|
};
|
2020-12-08 03:54:08 +00:00
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-08 03:54:08 +00:00
|
|
|
state,
|
2020-12-02 01:36:47 +00:00
|
|
|
is_target_state,
|
2020-12-10 02:55:29 +00:00
|
|
|
event_loop_handle,
|
2020-12-02 01:09:43 +00:00
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-02 01:09:43 +00:00
|
|
|
)
|
2020-12-02 01:36:47 +00:00
|
|
|
.await
|
2020-12-02 01:09:43 +00:00
|
|
|
}
|
2020-12-18 06:39:04 +00:00
|
|
|
BobState::EncSigSent(state) => {
|
2020-12-22 04:47:09 +00:00
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
state.expired_timelock(bitcoin_wallet.as_ref()).await?
|
|
|
|
{
|
2020-12-17 04:17:15 +00:00
|
|
|
let state_clone = state.clone();
|
|
|
|
let redeem_watcher = state_clone.watch_for_redeem_btc(bitcoin_wallet.as_ref());
|
2020-12-22 04:47:09 +00:00
|
|
|
let cancel_timelock_expires =
|
|
|
|
state_clone.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
2020-12-15 05:44:13 +00:00
|
|
|
|
2020-12-18 00:37:02 +00:00
|
|
|
select! {
|
|
|
|
state5 = redeem_watcher => {
|
|
|
|
BobState::BtcRedeemed(state5?)
|
|
|
|
},
|
2020-12-22 04:47:09 +00:00
|
|
|
_ = cancel_timelock_expires => {
|
|
|
|
BobState::CancelTimelockExpired(state)
|
2020-12-18 00:37:02 +00:00
|
|
|
}
|
2020-12-02 01:36:47 +00:00
|
|
|
}
|
2020-12-17 04:17:15 +00:00
|
|
|
} else {
|
2020-12-22 04:47:09 +00:00
|
|
|
BobState::CancelTimelockExpired(state)
|
2020-12-15 05:44:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-15 05:44:13 +00:00
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-15 05:44:13 +00:00
|
|
|
)
|
|
|
|
.await
|
2020-11-12 00:06:34 +00:00
|
|
|
}
|
2020-12-02 01:09:43 +00:00
|
|
|
BobState::BtcRedeemed(state) => {
|
|
|
|
// Bob redeems XMR using revealed s_a
|
|
|
|
state.claim_xmr(monero_wallet.as_ref()).await?;
|
2020-12-08 03:54:08 +00:00
|
|
|
|
2021-03-02 11:07:48 +00:00
|
|
|
// Ensure that the generated wallet is synced so we have a proper balance
|
|
|
|
monero_wallet.refresh().await?;
|
|
|
|
// Sweep (transfer all funds) to the given address
|
2021-03-04 02:54:13 +00:00
|
|
|
let tx_hashes = monero_wallet.sweep_all(receive_monero_address).await?;
|
|
|
|
|
|
|
|
for tx_hash in tx_hashes {
|
|
|
|
tracing::info!("Sent XMR to {} in tx {}", receive_monero_address, tx_hash.0);
|
|
|
|
}
|
2021-03-02 11:07:48 +00:00
|
|
|
|
2021-01-18 04:26:06 +00:00
|
|
|
let state = BobState::XmrRedeemed {
|
|
|
|
tx_lock_id: state.tx_lock_id(),
|
|
|
|
};
|
2020-12-08 03:54:08 +00:00
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-08 03:54:08 +00:00
|
|
|
state,
|
2020-12-02 01:36:47 +00:00
|
|
|
is_target_state,
|
2020-12-10 02:55:29 +00:00
|
|
|
event_loop_handle,
|
2020-12-02 01:09:43 +00:00
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-02 01:09:43 +00:00
|
|
|
)
|
2020-12-02 01:36:47 +00:00
|
|
|
.await
|
|
|
|
}
|
2020-12-22 04:47:09 +00:00
|
|
|
BobState::CancelTimelockExpired(state4) => {
|
2020-12-17 04:17:15 +00:00
|
|
|
if state4
|
|
|
|
.check_for_tx_cancel(bitcoin_wallet.as_ref())
|
|
|
|
.await
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
|
|
|
}
|
|
|
|
|
2020-12-22 04:49:30 +00:00
|
|
|
let state = BobState::BtcCancelled(state4);
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(state.clone().into()))
|
2020-12-17 04:17:15 +00:00
|
|
|
.await?;
|
|
|
|
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-17 04:17:15 +00:00
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-17 04:17:15 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2020-12-22 04:49:30 +00:00
|
|
|
BobState::BtcCancelled(state) => {
|
2020-12-02 01:36:47 +00:00
|
|
|
// Bob has cancelled the swap
|
2020-12-22 04:47:09 +00:00
|
|
|
let state = match state.expired_timelock(bitcoin_wallet.as_ref()).await? {
|
|
|
|
ExpiredTimelocks::None => {
|
|
|
|
bail!("Internal error: canceled state reached before cancel timelock was expired");
|
|
|
|
}
|
|
|
|
ExpiredTimelocks::Cancel => {
|
2021-01-27 02:33:32 +00:00
|
|
|
state
|
|
|
|
.refund_btc(bitcoin_wallet.as_ref(), execution_params)
|
|
|
|
.await?;
|
2020-12-08 03:54:08 +00:00
|
|
|
BobState::BtcRefunded(state)
|
2020-12-11 01:34:20 +00:00
|
|
|
}
|
2021-01-18 04:26:06 +00:00
|
|
|
ExpiredTimelocks::Punish => BobState::BtcPunished {
|
|
|
|
tx_lock_id: state.tx_lock_id(),
|
|
|
|
},
|
2020-12-08 03:54:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let db_state = state.clone().into();
|
2021-01-05 03:08:36 +00:00
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
2021-01-19 23:37:16 +00:00
|
|
|
run_until_internal(
|
2020-12-08 03:54:08 +00:00
|
|
|
state,
|
|
|
|
is_target_state,
|
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
swap_id,
|
2021-01-27 02:33:32 +00:00
|
|
|
execution_params,
|
2021-03-02 11:07:48 +00:00
|
|
|
receive_monero_address,
|
2020-12-08 03:54:08 +00:00
|
|
|
)
|
|
|
|
.await
|
2020-12-02 01:36:47 +00:00
|
|
|
}
|
2020-12-11 05:49:19 +00:00
|
|
|
BobState::BtcRefunded(state4) => Ok(BobState::BtcRefunded(state4)),
|
2021-01-18 04:26:06 +00:00
|
|
|
BobState::BtcPunished { tx_lock_id } => Ok(BobState::BtcPunished { tx_lock_id }),
|
2020-12-10 00:51:58 +00:00
|
|
|
BobState::SafelyAborted => Ok(BobState::SafelyAborted),
|
2021-01-18 04:26:06 +00:00
|
|
|
BobState::XmrRedeemed { tx_lock_id } => Ok(BobState::XmrRedeemed { tx_lock_id }),
|
2020-11-12 00:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 05:53:50 +00:00
|
|
|
|
2021-03-03 04:45:43 +00:00
|
|
|
pub async fn request_price_and_setup(
|
|
|
|
btc: bitcoin::Amount,
|
2021-02-04 00:03:13 +00:00
|
|
|
event_loop_handle: &mut EventLoopHandle,
|
2021-02-15 02:01:38 +00:00
|
|
|
execution_params: ExecutionParams,
|
|
|
|
bitcoin_refund_address: bitcoin::Address,
|
2021-02-03 04:25:05 +00:00
|
|
|
) -> Result<bob::state::State2> {
|
2021-03-03 04:45:43 +00:00
|
|
|
let xmr = event_loop_handle.request_spot_price(btc).await?;
|
2021-01-14 02:23:03 +00:00
|
|
|
|
2021-03-03 04:45:43 +00:00
|
|
|
tracing::info!("Spot price for {} is {}", btc, xmr);
|
2021-02-15 02:01:38 +00:00
|
|
|
|
|
|
|
let state0 = State0::new(
|
|
|
|
&mut OsRng,
|
2021-03-03 04:45:43 +00:00
|
|
|
btc,
|
|
|
|
xmr,
|
2021-02-15 02:01:38 +00:00
|
|
|
execution_params.bitcoin_cancel_timelock,
|
|
|
|
execution_params.bitcoin_punish_timelock,
|
|
|
|
bitcoin_refund_address,
|
|
|
|
execution_params.monero_finality_confirmations,
|
|
|
|
);
|
2020-12-15 05:53:50 +00:00
|
|
|
|
2021-02-03 05:45:43 +00:00
|
|
|
let state2 = event_loop_handle.execution_setup(state0).await?;
|
2020-12-15 05:53:50 +00:00
|
|
|
|
2020-12-22 02:47:05 +00:00
|
|
|
Ok(state2)
|
2020-12-15 05:53:50 +00:00
|
|
|
}
|