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