237: Swap CLI buys xmr by default if no subcommand given r=rishflab a=rishflab

- [x] Needs watch for deposit functionality from #236 


Closes #234 

Co-authored-by: rishflab <rishflab@hotmail.com>
This commit is contained in:
bors[bot] 2021-03-01 00:18:46 +00:00 committed by GitHub
commit 6cffb86c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 51 deletions

View File

@ -21,7 +21,7 @@ use swap::{
bitcoin, bitcoin,
bitcoin::Amount, bitcoin::Amount,
cli::{ cli::{
command::{Arguments, Cancel, Command, Refund, Resume}, command::{Arguments, Command},
config::{read_config, Config}, config::{read_config, Config},
}, },
database::Database, database::Database,
@ -79,7 +79,7 @@ async fn main() -> Result<()> {
.run(monero_network, "stagenet.community.xmr.to") .run(monero_network, "stagenet.community.xmr.to")
.await?; .await?;
match opt.cmd { match opt.cmd.unwrap_or_default() {
Command::BuyXmr { Command::BuyXmr {
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
@ -94,6 +94,8 @@ async fn main() -> Result<()> {
) )
.await?; .await?;
let swap_id = Uuid::new_v4();
// TODO: Also wait for more funds if balance < dust // TODO: Also wait for more funds if balance < dust
if bitcoin_wallet.balance().await? == Amount::ZERO { if bitcoin_wallet.balance().await? == Amount::ZERO {
debug!( debug!(
@ -117,7 +119,7 @@ async fn main() -> Result<()> {
let bob_factory = Builder::new( let bob_factory = Builder::new(
seed, seed,
db, db,
Uuid::new_v4(), swap_id,
Arc::new(bitcoin_wallet), Arc::new(bitcoin_wallet),
Arc::new(monero_wallet), Arc::new(monero_wallet),
alice_addr, alice_addr,
@ -141,11 +143,11 @@ async fn main() -> Result<()> {
// Print the table to stdout // Print the table to stdout
table.printstd(); table.printstd();
} }
Command::Resume(Resume::BuyXmr { Command::Resume {
swap_id, swap_id,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
}) => { } => {
let (bitcoin_wallet, monero_wallet) = init_wallets( let (bitcoin_wallet, monero_wallet) = init_wallets(
config, config,
bitcoin_network, bitcoin_network,
@ -171,12 +173,12 @@ async fn main() -> Result<()> {
tokio::spawn(async move { event_loop.run().await }); tokio::spawn(async move { event_loop.run().await });
bob::run(swap).await?; bob::run(swap).await?;
} }
Command::Cancel(Cancel::BuyXmr { Command::Cancel {
swap_id, swap_id,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
force, force,
}) => { } => {
// TODO: Optimization: Only init the Bitcoin wallet, Monero wallet unnecessary // TODO: Optimization: Only init the Bitcoin wallet, Monero wallet unnecessary
let (bitcoin_wallet, monero_wallet) = init_wallets( let (bitcoin_wallet, monero_wallet) = init_wallets(
config, config,
@ -223,12 +225,12 @@ async fn main() -> Result<()> {
} }
} }
} }
Command::Refund(Refund::BuyXmr { Command::Refund {
swap_id, swap_id,
alice_peer_id, alice_peer_id,
alice_addr, alice_addr,
force, force,
}) => { } => {
let (bitcoin_wallet, monero_wallet) = init_wallets( let (bitcoin_wallet, monero_wallet) = init_wallets(
config, config,
bitcoin_network, bitcoin_network,

View File

@ -2,8 +2,8 @@ use libp2p::{core::Multiaddr, PeerId};
use std::path::PathBuf; use std::path::PathBuf;
use uuid::Uuid; use uuid::Uuid;
const DEFAULT_ALICE_MULTIADDR: &str = "/dns4/xmr-btc-asb.coblox.tech/tcp/9876"; pub const DEFAULT_ALICE_MULTIADDR: &str = "/dns4/xmr-btc-asb.coblox.tech/tcp/9876";
const DEFAULT_ALICE_PEER_ID: &str = "12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi"; pub const DEFAULT_ALICE_PEER_ID: &str = "12D3KooWCdMKjesXMJz1SiZ7HgotrxuqhQJbP5sgBm2BwP1cqThi";
#[derive(structopt::StructOpt, Debug)] #[derive(structopt::StructOpt, Debug)]
pub struct Arguments { pub struct Arguments {
@ -15,7 +15,7 @@ pub struct Arguments {
pub config: Option<PathBuf>, pub config: Option<PathBuf>,
#[structopt(subcommand)] #[structopt(subcommand)]
pub cmd: Command, pub cmd: Option<Command>,
} }
#[derive(structopt::StructOpt, Debug)] #[derive(structopt::StructOpt, Debug)]
@ -26,37 +26,13 @@ pub enum Command {
alice_peer_id: PeerId, alice_peer_id: PeerId,
#[structopt( #[structopt(
long = "connect-addr", long = "connect-addr",
default_value = DEFAULT_ALICE_MULTIADDR default_value = DEFAULT_ALICE_MULTIADDR
)] )]
alice_addr: Multiaddr, alice_addr: Multiaddr,
}, },
History, History,
Resume(Resume), Resume {
Cancel(Cancel),
Refund(Refund),
}
#[derive(structopt::StructOpt, Debug)]
pub enum Resume {
BuyXmr {
#[structopt(long = "swap-id")]
swap_id: Uuid,
#[structopt(long = "counterpart-peer-id", default_value = DEFAULT_ALICE_PEER_ID)]
alice_peer_id: PeerId,
#[structopt(
long = "counterpart-addr",
default_value = DEFAULT_ALICE_MULTIADDR
)]
alice_addr: Multiaddr,
},
}
#[derive(structopt::StructOpt, Debug)]
pub enum Cancel {
BuyXmr {
#[structopt(long = "swap-id")] #[structopt(long = "swap-id")]
swap_id: Uuid, swap_id: Uuid,
@ -66,19 +42,12 @@ pub enum Cancel {
alice_peer_id: PeerId, alice_peer_id: PeerId,
#[structopt( #[structopt(
long = "counterpart-addr", long = "counterpart-addr",
default_value = DEFAULT_ALICE_MULTIADDR default_value = DEFAULT_ALICE_MULTIADDR
)] )]
alice_addr: Multiaddr, alice_addr: Multiaddr,
#[structopt(short, long)]
force: bool,
}, },
} Cancel {
#[derive(structopt::StructOpt, Debug)]
pub enum Refund {
BuyXmr {
#[structopt(long = "swap-id")] #[structopt(long = "swap-id")]
swap_id: Uuid, swap_id: Uuid,
@ -88,8 +57,26 @@ pub enum Refund {
alice_peer_id: PeerId, alice_peer_id: PeerId,
#[structopt( #[structopt(
long = "counterpart-addr", long = "counterpart-addr",
default_value = DEFAULT_ALICE_MULTIADDR default_value = DEFAULT_ALICE_MULTIADDR
)]
alice_addr: Multiaddr,
#[structopt(short, long)]
force: bool,
},
Refund {
#[structopt(long = "swap-id")]
swap_id: Uuid,
// TODO: Remove Alice peer-id/address, it should be saved in the database when running swap
// and loaded from the database when running resume/cancel/refund
#[structopt(long = "counterpart-peer-id", default_value = DEFAULT_ALICE_PEER_ID)]
alice_peer_id: PeerId,
#[structopt(
long = "counterpart-addr",
default_value = DEFAULT_ALICE_MULTIADDR
)] )]
alice_addr: Multiaddr, alice_addr: Multiaddr,
@ -97,3 +84,41 @@ pub enum Refund {
force: bool, force: bool,
}, },
} }
impl Default for Command {
fn default() -> Self {
Self::BuyXmr {
alice_peer_id: DEFAULT_ALICE_PEER_ID
.parse()
.expect("default alice peer id str is a valid Multiaddr>"),
alice_addr: DEFAULT_ALICE_MULTIADDR
.parse()
.expect("default alice multiaddr str is a valid PeerId"),
}
}
}
#[cfg(test)]
mod tests {
use crate::cli::command::{Command, DEFAULT_ALICE_MULTIADDR, DEFAULT_ALICE_PEER_ID};
use libp2p::{core::Multiaddr, PeerId};
#[test]
fn parse_default_alice_peer_id_success() {
DEFAULT_ALICE_PEER_ID
.parse::<PeerId>()
.expect("default alice peer id str is a valid PeerId");
}
#[test]
fn parse_default_alice_multiaddr_success() {
DEFAULT_ALICE_MULTIADDR
.parse::<Multiaddr>()
.expect("default alice multiaddr str is a valid Multiaddr>");
}
#[test]
fn default_command_success() {
Command::default();
}
}