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

79 lines
2.0 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use anyhow::Result;
use uuid::Uuid;
use crate::protocol::Database;
use crate::{bitcoin, cli, env, monero};
pub use self::state::*;
pub use self::swap::{run, run_until};
use std::convert::TryInto;
2021-01-08 01:33:23 +00:00
pub mod state;
2020-12-10 03:20:27 +00:00
pub mod swap;
pub struct Swap {
pub state: BobState,
pub event_loop_handle: cli::EventLoopHandle,
pub db: Arc<dyn Database + Send + Sync>,
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub monero_wallet: Arc<monero::Wallet>,
pub env_config: env::Config,
2021-04-16 01:48:15 +00:00
pub id: Uuid,
2021-07-06 08:34:09 +00:00
pub monero_receive_address: monero::Address,
}
impl Swap {
#[allow(clippy::too_many_arguments)]
2021-01-18 10:57:17 +00:00
pub fn new(
db: Arc<dyn Database + Send + Sync>,
id: Uuid,
2021-01-18 10:57:17 +00:00
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
env_config: env::Config,
event_loop_handle: cli::EventLoopHandle,
2021-07-06 08:34:09 +00:00
monero_receive_address: monero::Address,
bitcoin_change_address: bitcoin::Address,
btc_amount: bitcoin::Amount,
2021-01-18 10:57:17 +00:00
) -> Self {
Self {
state: BobState::Started {
btc_amount,
change_address: bitcoin_change_address,
},
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
env_config,
id,
2021-07-06 08:34:09 +00:00
monero_receive_address,
2021-01-18 10:57:17 +00:00
}
}
#[allow(clippy::too_many_arguments)]
pub async fn from_db(
db: Arc<dyn Database + Send + Sync>,
id: Uuid,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
env_config: env::Config,
event_loop_handle: cli::EventLoopHandle,
2021-07-06 08:34:09 +00:00
monero_receive_address: monero::Address,
) -> Result<Self> {
let state = db.get_state(id).await?.try_into()?;
Ok(Self {
state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
env_config,
id,
2021-07-06 08:34:09 +00:00
monero_receive_address,
})
}
2021-01-18 10:57:17 +00:00
}