mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-06 13:34:38 -04:00
Separate wallet settings and protocol settings
This commit is contained in:
parent
049b7bc329
commit
4cc74bad62
3 changed files with 108 additions and 58 deletions
|
@ -17,6 +17,7 @@ use anyhow::{Context, Result};
|
||||||
use database::Database;
|
use database::Database;
|
||||||
use prettytable::{row, Table};
|
use prettytable::{row, Table};
|
||||||
use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
use protocol::{alice, bob, bob::Builder, SwapAmounts};
|
||||||
|
use settings::Settings;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use trace::init_tracing;
|
use trace::init_tracing;
|
||||||
|
@ -45,7 +46,6 @@ async fn main() -> Result<()> {
|
||||||
init_tracing(LevelFilter::Info).expect("initialize tracing");
|
init_tracing(LevelFilter::Info).expect("initialize tracing");
|
||||||
|
|
||||||
let opt = Options::from_args();
|
let opt = Options::from_args();
|
||||||
let settings = settings::Protocol::testnet();
|
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Database and Seed will be stored in directory: {}",
|
"Database and Seed will be stored in directory: {}",
|
||||||
|
@ -67,18 +67,15 @@ async fn main() -> Result<()> {
|
||||||
send_monero,
|
send_monero,
|
||||||
receive_bitcoin,
|
receive_bitcoin,
|
||||||
} => {
|
} => {
|
||||||
|
let settings =
|
||||||
|
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
|
||||||
|
|
||||||
let swap_amounts = SwapAmounts {
|
let swap_amounts = SwapAmounts {
|
||||||
xmr: send_monero,
|
xmr: send_monero,
|
||||||
btc: receive_bitcoin,
|
btc: receive_bitcoin,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
||||||
bitcoind_url,
|
|
||||||
bitcoin_wallet_name.as_str(),
|
|
||||||
monero_wallet_rpc_url,
|
|
||||||
settings,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
@ -89,7 +86,7 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let alice_factory = alice::Builder::new(
|
let alice_factory = alice::Builder::new(
|
||||||
seed,
|
seed,
|
||||||
settings,
|
settings.protocol,
|
||||||
swap_id,
|
swap_id,
|
||||||
Arc::new(bitcoin_wallet),
|
Arc::new(bitcoin_wallet),
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
|
@ -112,18 +109,15 @@ async fn main() -> Result<()> {
|
||||||
send_bitcoin,
|
send_bitcoin,
|
||||||
receive_monero,
|
receive_monero,
|
||||||
} => {
|
} => {
|
||||||
|
let settings =
|
||||||
|
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
|
||||||
|
|
||||||
let swap_amounts = SwapAmounts {
|
let swap_amounts = SwapAmounts {
|
||||||
btc: send_bitcoin,
|
btc: send_bitcoin,
|
||||||
xmr: receive_monero,
|
xmr: receive_monero,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
||||||
bitcoind_url,
|
|
||||||
bitcoin_wallet_name.as_str(),
|
|
||||||
monero_wallet_rpc_url,
|
|
||||||
settings,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let swap_id = Uuid::new_v4();
|
let swap_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
@ -140,7 +134,7 @@ async fn main() -> Result<()> {
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
alice_addr,
|
alice_addr,
|
||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
settings,
|
settings.protocol,
|
||||||
);
|
);
|
||||||
let (swap, event_loop) = bob_factory.with_init_params(swap_amounts).build().await?;
|
let (swap, event_loop) = bob_factory.with_init_params(swap_amounts).build().await?;
|
||||||
|
|
||||||
|
@ -168,17 +162,14 @@ async fn main() -> Result<()> {
|
||||||
monero_wallet_rpc_url,
|
monero_wallet_rpc_url,
|
||||||
listen_addr,
|
listen_addr,
|
||||||
}) => {
|
}) => {
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
let settings =
|
||||||
bitcoind_url,
|
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
|
||||||
bitcoin_wallet_name.as_str(),
|
|
||||||
monero_wallet_rpc_url,
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
||||||
settings,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let alice_factory = alice::Builder::new(
|
let alice_factory = alice::Builder::new(
|
||||||
seed,
|
seed,
|
||||||
settings,
|
settings.protocol,
|
||||||
swap_id,
|
swap_id,
|
||||||
Arc::new(bitcoin_wallet),
|
Arc::new(bitcoin_wallet),
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
|
@ -199,13 +190,10 @@ async fn main() -> Result<()> {
|
||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
alice_addr,
|
alice_addr,
|
||||||
}) => {
|
}) => {
|
||||||
let (bitcoin_wallet, monero_wallet) = setup_wallets(
|
let settings =
|
||||||
bitcoind_url,
|
Settings::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url);
|
||||||
bitcoin_wallet_name.as_str(),
|
|
||||||
monero_wallet_rpc_url,
|
let (bitcoin_wallet, monero_wallet) = setup_wallets(settings.wallets).await?;
|
||||||
settings,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let bob_factory = Builder::new(
|
let bob_factory = Builder::new(
|
||||||
seed,
|
seed,
|
||||||
|
@ -215,7 +203,7 @@ async fn main() -> Result<()> {
|
||||||
Arc::new(monero_wallet),
|
Arc::new(monero_wallet),
|
||||||
alice_addr,
|
alice_addr,
|
||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
settings,
|
settings.protocol,
|
||||||
);
|
);
|
||||||
let (swap, event_loop) = bob_factory.build().await?;
|
let (swap, event_loop) = bob_factory.build().await?;
|
||||||
|
|
||||||
|
@ -227,21 +215,21 @@ async fn main() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn setup_wallets(
|
async fn setup_wallets(settings: settings::Wallets) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
||||||
bitcoind_url: url::Url,
|
let bitcoin_wallet = bitcoin::Wallet::new(
|
||||||
bitcoin_wallet_name: &str,
|
settings.bitcoin.wallet_name.as_str(),
|
||||||
monero_wallet_rpc_url: url::Url,
|
settings.bitcoin.bitcoind_url,
|
||||||
settings: settings::Protocol,
|
settings.bitcoin.network,
|
||||||
) -> Result<(bitcoin::Wallet, monero::Wallet)> {
|
)
|
||||||
let bitcoin_wallet =
|
.await?;
|
||||||
bitcoin::Wallet::new(bitcoin_wallet_name, bitcoind_url, settings.bitcoin_network).await?;
|
|
||||||
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
let bitcoin_balance = bitcoin_wallet.balance().await?;
|
||||||
info!(
|
info!(
|
||||||
"Connection to Bitcoin wallet succeeded, balance: {}",
|
"Connection to Bitcoin wallet succeeded, balance: {}",
|
||||||
bitcoin_balance
|
bitcoin_balance
|
||||||
);
|
);
|
||||||
|
|
||||||
let monero_wallet = monero::Wallet::new(monero_wallet_rpc_url, settings.monero_network);
|
let monero_wallet =
|
||||||
|
monero::Wallet::new(settings.monero.wallet_rpc_url, settings.monero.network);
|
||||||
let monero_balance = monero_wallet.get_balance().await?;
|
let monero_balance = monero_wallet.get_balance().await?;
|
||||||
info!(
|
info!(
|
||||||
"Connection to Monero wallet succeeded, balance: {}",
|
"Connection to Monero wallet succeeded, balance: {}",
|
||||||
|
|
|
@ -1,6 +1,79 @@
|
||||||
use crate::bitcoin::Timelock;
|
use crate::bitcoin::Timelock;
|
||||||
use conquer_once::Lazy;
|
use conquer_once::Lazy;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
pub struct Settings {
|
||||||
|
pub wallets: Wallets,
|
||||||
|
pub protocol: Protocol,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Settings {
|
||||||
|
pub fn testnet(
|
||||||
|
bitcoind_url: Url,
|
||||||
|
bitcoin_wallet_name: String,
|
||||||
|
monero_wallet_rpc_url: Url,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
wallets: Wallets::testnet(bitcoind_url, bitcoin_wallet_name, monero_wallet_rpc_url),
|
||||||
|
protocol: Protocol::testnet(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Wallets {
|
||||||
|
pub bitcoin: Bitcoin,
|
||||||
|
pub monero: Monero,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Wallets {
|
||||||
|
pub fn mainnet(
|
||||||
|
bitcoind_url: Url,
|
||||||
|
bitcoin_wallet_name: String,
|
||||||
|
monero_wallet_rpc_url: Url,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
bitcoin: Bitcoin {
|
||||||
|
bitcoind_url,
|
||||||
|
wallet_name: bitcoin_wallet_name,
|
||||||
|
network: bitcoin::Network::Bitcoin,
|
||||||
|
},
|
||||||
|
monero: Monero {
|
||||||
|
wallet_rpc_url: monero_wallet_rpc_url,
|
||||||
|
network: monero::Network::Mainnet,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn testnet(
|
||||||
|
bitcoind_url: Url,
|
||||||
|
bitcoin_wallet_name: String,
|
||||||
|
monero_wallet_rpc_url: Url,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
bitcoin: Bitcoin {
|
||||||
|
bitcoind_url,
|
||||||
|
wallet_name: bitcoin_wallet_name,
|
||||||
|
network: bitcoin::Network::Testnet,
|
||||||
|
},
|
||||||
|
monero: Monero {
|
||||||
|
wallet_rpc_url: monero_wallet_rpc_url,
|
||||||
|
network: monero::Network::Stagenet,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Bitcoin {
|
||||||
|
pub bitcoind_url: Url,
|
||||||
|
pub wallet_name: String,
|
||||||
|
pub network: bitcoin::Network,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Monero {
|
||||||
|
pub wallet_rpc_url: Url,
|
||||||
|
pub network: monero::Network,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct Protocol {
|
pub struct Protocol {
|
||||||
|
@ -10,8 +83,6 @@ pub struct Protocol {
|
||||||
pub monero_finality_confirmations: u32,
|
pub monero_finality_confirmations: u32,
|
||||||
pub bitcoin_cancel_timelock: Timelock,
|
pub bitcoin_cancel_timelock: Timelock,
|
||||||
pub bitcoin_punish_timelock: Timelock,
|
pub bitcoin_punish_timelock: Timelock,
|
||||||
pub bitcoin_network: bitcoin::Network,
|
|
||||||
pub monero_network: monero::Network,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Protocol {
|
impl Protocol {
|
||||||
|
@ -23,8 +94,6 @@ impl Protocol {
|
||||||
monero_finality_confirmations: mainnet::MONERO_FINALITY_CONFIRMATIONS,
|
monero_finality_confirmations: mainnet::MONERO_FINALITY_CONFIRMATIONS,
|
||||||
bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK,
|
bitcoin_cancel_timelock: mainnet::BITCOIN_CANCEL_TIMELOCK,
|
||||||
bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK,
|
bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK,
|
||||||
bitcoin_network: bitcoin::Network::Bitcoin,
|
|
||||||
monero_network: monero::Network::Mainnet,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +105,6 @@ impl Protocol {
|
||||||
monero_finality_confirmations: testnet::MONERO_FINALITY_CONFIRMATIONS,
|
monero_finality_confirmations: testnet::MONERO_FINALITY_CONFIRMATIONS,
|
||||||
bitcoin_cancel_timelock: testnet::BITCOIN_CANCEL_TIMELOCK,
|
bitcoin_cancel_timelock: testnet::BITCOIN_CANCEL_TIMELOCK,
|
||||||
bitcoin_punish_timelock: testnet::BITCOIN_PUNISH_TIMELOCK,
|
bitcoin_punish_timelock: testnet::BITCOIN_PUNISH_TIMELOCK,
|
||||||
bitcoin_network: bitcoin::Network::Testnet,
|
|
||||||
monero_network: monero::Network::Stagenet,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,8 +116,6 @@ impl Protocol {
|
||||||
monero_finality_confirmations: regtest::MONERO_FINALITY_CONFIRMATIONS,
|
monero_finality_confirmations: regtest::MONERO_FINALITY_CONFIRMATIONS,
|
||||||
bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK,
|
bitcoin_cancel_timelock: regtest::BITCOIN_CANCEL_TIMELOCK,
|
||||||
bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK,
|
bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK,
|
||||||
bitcoin_network: bitcoin::Network::Regtest,
|
|
||||||
monero_network: monero::Network::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -316,12 +316,11 @@ where
|
||||||
.parse()
|
.parse()
|
||||||
.expect("failed to parse Alice's address");
|
.expect("failed to parse Alice's address");
|
||||||
|
|
||||||
let (alice_bitcoin_wallet, alice_monero_wallet) = init_wallets(
|
let (alice_bitcoin_wallet, alice_monero_wallet) = init_test_wallets(
|
||||||
"alice",
|
"alice",
|
||||||
&containers.bitcoind,
|
&containers.bitcoind,
|
||||||
&monero,
|
&monero,
|
||||||
alice_starting_balances.clone(),
|
alice_starting_balances.clone(),
|
||||||
settings,
|
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
@ -340,12 +339,11 @@ where
|
||||||
btc: swap_amounts.btc * 10,
|
btc: swap_amounts.btc * 10,
|
||||||
};
|
};
|
||||||
|
|
||||||
let (bob_bitcoin_wallet, bob_monero_wallet) = init_wallets(
|
let (bob_bitcoin_wallet, bob_monero_wallet) = init_test_wallets(
|
||||||
"bob",
|
"bob",
|
||||||
&containers.bitcoind,
|
&containers.bitcoind,
|
||||||
&monero,
|
&monero,
|
||||||
bob_starting_balances.clone(),
|
bob_starting_balances.clone(),
|
||||||
settings,
|
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
@ -385,12 +383,11 @@ async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
|
||||||
(monero, Containers { bitcoind, monerods })
|
(monero, Containers { bitcoind, monerods })
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_wallets(
|
async fn init_test_wallets(
|
||||||
name: &str,
|
name: &str,
|
||||||
bitcoind: &Bitcoind<'_>,
|
bitcoind: &Bitcoind<'_>,
|
||||||
monero: &Monero,
|
monero: &Monero,
|
||||||
starting_balances: StartingBalances,
|
starting_balances: StartingBalances,
|
||||||
settings: settings::Protocol,
|
|
||||||
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
|
) -> (Arc<bitcoin::Wallet>, Arc<monero::Wallet>) {
|
||||||
monero
|
monero
|
||||||
.init(vec![(name, starting_balances.xmr.as_piconero())])
|
.init(vec![(name, starting_balances.xmr.as_piconero())])
|
||||||
|
@ -399,11 +396,11 @@ async fn init_wallets(
|
||||||
|
|
||||||
let xmr_wallet = Arc::new(swap::monero::Wallet {
|
let xmr_wallet = Arc::new(swap::monero::Wallet {
|
||||||
inner: monero.wallet(name).unwrap().client(),
|
inner: monero.wallet(name).unwrap().client(),
|
||||||
network: settings.monero_network,
|
network: monero::Network::default(),
|
||||||
});
|
});
|
||||||
|
|
||||||
let btc_wallet = Arc::new(
|
let btc_wallet = Arc::new(
|
||||||
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), settings.bitcoin_network)
|
swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), bitcoin::Network::Regtest)
|
||||||
.await
|
.await
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue