xmr-btc-swap/swap/src/protocol/bob.rs

101 lines
2.5 KiB
Rust
Raw Normal View History

use crate::database::Database;
use crate::{bitcoin, env, monero};
use anyhow::Result;
use std::sync::Arc;
use uuid::Uuid;
pub use self::behaviour::{Behaviour, OutEvent};
pub use self::cancel::cancel;
pub use self::event_loop::{EventLoop, EventLoopHandle};
pub use self::refund::refund;
pub use self::state::*;
pub use self::swap::{run, run_until};
2021-01-08 01:33:23 +00:00
mod behaviour;
2021-02-01 05:10:43 +00:00
pub mod cancel;
2020-12-10 03:20:27 +00:00
pub mod event_loop;
mod execution_setup;
2021-02-01 05:25:33 +00:00
pub mod refund;
pub mod state;
2020-12-10 03:20:27 +00:00
pub mod swap;
pub struct Swap {
pub state: BobState,
pub event_loop_handle: EventLoopHandle,
pub db: Database,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
pub env_config: env::Config,
pub swap_id: Uuid,
pub receive_monero_address: monero::Address,
}
pub struct Builder {
swap_id: Uuid,
db: Database,
2021-01-18 10:57:17 +00:00
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
init_params: InitParams,
env_config: env::Config,
2021-03-04 00:40:28 +00:00
event_loop_handle: EventLoopHandle,
receive_monero_address: monero::Address,
}
enum InitParams {
None,
New { btc_amount: bitcoin::Amount },
2021-01-18 10:57:17 +00:00
}
impl Builder {
#[allow(clippy::too_many_arguments)]
2021-01-18 10:57:17 +00:00
pub fn new(
db: Database,
2021-01-18 10:57:17 +00:00
swap_id: Uuid,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
env_config: env::Config,
2021-03-04 00:40:28 +00:00
event_loop_handle: EventLoopHandle,
receive_monero_address: monero::Address,
2021-01-18 10:57:17 +00:00
) -> Self {
Self {
swap_id,
db,
bitcoin_wallet,
monero_wallet,
init_params: InitParams::None,
env_config,
event_loop_handle,
receive_monero_address,
2021-01-18 10:57:17 +00:00
}
}
pub fn with_init_params(self, btc_amount: bitcoin::Amount) -> Self {
Self {
init_params: InitParams::New { btc_amount },
..self
}
2021-01-18 10:57:17 +00:00
}
pub fn build(self) -> Result<Swap> {
let state = match self.init_params {
InitParams::New { btc_amount } => BobState::Started { btc_amount },
InitParams::None => self.db.get_state(self.swap_id)?.try_into_bob()?.into(),
};
Ok(Swap {
state,
event_loop_handle: self.event_loop_handle,
db: self.db,
bitcoin_wallet: self.bitcoin_wallet.clone(),
monero_wallet: self.monero_wallet.clone(),
swap_id: self.swap_id,
env_config: self.env_config,
receive_monero_address: self.receive_monero_address,
})
}
2021-01-18 10:57:17 +00:00
}