2021-02-11 04:21:34 +00:00
|
|
|
use std::sync::Arc;
|
2021-06-25 03:40:33 +00:00
|
|
|
|
|
|
|
use anyhow::Result;
|
2021-01-14 02:23:03 +00:00
|
|
|
use uuid::Uuid;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-09-28 00:15:31 +00:00
|
|
|
use crate::protocol::Database;
|
2021-06-25 03:40:33 +00:00
|
|
|
use crate::{bitcoin, cli, env, monero};
|
|
|
|
|
2021-03-04 00:28:58 +00:00
|
|
|
pub use self::state::*;
|
|
|
|
pub use self::swap::{run, run_until};
|
2021-09-28 00:15:31 +00:00
|
|
|
use std::convert::TryInto;
|
2021-01-08 01:33:23 +00:00
|
|
|
|
2021-01-05 03:08:36 +00:00
|
|
|
pub mod state;
|
2020-12-10 03:20:27 +00:00
|
|
|
pub mod swap;
|
2020-10-15 22:14:39 +00:00
|
|
|
|
2021-01-18 08:56:43 +00:00
|
|
|
pub struct Swap {
|
|
|
|
pub state: BobState,
|
2021-06-25 03:40:33 +00:00
|
|
|
pub event_loop_handle: cli::EventLoopHandle,
|
2021-09-28 00:15:31 +00:00
|
|
|
pub db: Arc<dyn Database + Send + Sync>,
|
2021-01-18 08:56:43 +00:00
|
|
|
pub bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
pub monero_wallet: Arc<monero::Wallet>,
|
2021-04-16 01:45:05 +00:00
|
|
|
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,
|
2021-01-18 08:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 02:00:11 +00:00
|
|
|
impl Swap {
|
2021-01-19 01:43:20 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-01-18 10:57:17 +00:00
|
|
|
pub fn new(
|
2021-09-28 00:15:31 +00:00
|
|
|
db: Arc<dyn Database + Send + Sync>,
|
2021-04-16 02:00:11 +00:00
|
|
|
id: Uuid,
|
2021-01-18 10:57:17 +00:00
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
2021-04-16 01:45:05 +00:00
|
|
|
env_config: env::Config,
|
2021-06-25 03:40:33 +00:00
|
|
|
event_loop_handle: cli::EventLoopHandle,
|
2021-07-06 08:34:09 +00:00
|
|
|
monero_receive_address: monero::Address,
|
2021-07-06 09:17:17 +00:00
|
|
|
bitcoin_change_address: bitcoin::Address,
|
2021-04-16 02:00:11 +00:00
|
|
|
btc_amount: bitcoin::Amount,
|
2021-01-18 10:57:17 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2021-07-06 09:17:17 +00:00
|
|
|
state: BobState::Started {
|
|
|
|
btc_amount,
|
|
|
|
change_address: bitcoin_change_address,
|
|
|
|
},
|
2021-04-16 02:00:11 +00:00
|
|
|
event_loop_handle,
|
2021-02-11 04:21:34 +00:00
|
|
|
db,
|
2021-01-18 22:43:50 +00:00
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
2021-03-17 03:55:42 +00:00
|
|
|
env_config,
|
2021-04-16 02:00:11 +00:00
|
|
|
id,
|
2021-07-06 08:34:09 +00:00
|
|
|
monero_receive_address,
|
2021-01-18 10:57:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 09:17:17 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2021-09-28 00:15:31 +00:00
|
|
|
pub async fn from_db(
|
|
|
|
db: Arc<dyn Database + Send + Sync>,
|
2021-04-16 02:00:11 +00:00
|
|
|
id: Uuid,
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
env_config: env::Config,
|
2021-06-25 03:40:33 +00:00
|
|
|
event_loop_handle: cli::EventLoopHandle,
|
2021-07-06 08:34:09 +00:00
|
|
|
monero_receive_address: monero::Address,
|
2021-04-16 02:00:11 +00:00
|
|
|
) -> Result<Self> {
|
2021-09-28 00:15:31 +00:00
|
|
|
let state = db.get_state(id).await?.try_into()?;
|
2021-03-03 02:56:25 +00:00
|
|
|
|
2021-04-16 02:00:11 +00:00
|
|
|
Ok(Self {
|
2021-03-03 02:56:25 +00:00
|
|
|
state,
|
2021-04-16 02:00:11 +00:00
|
|
|
event_loop_handle,
|
|
|
|
db,
|
|
|
|
bitcoin_wallet,
|
|
|
|
monero_wallet,
|
|
|
|
env_config,
|
|
|
|
id,
|
2021-07-06 08:34:09 +00:00
|
|
|
monero_receive_address,
|
2021-03-03 02:56:25 +00:00
|
|
|
})
|
2021-01-19 03:22:09 +00:00
|
|
|
}
|
2021-01-18 10:57:17 +00:00
|
|
|
}
|