Skip to content

Commit

Permalink
Merge #257
Browse files Browse the repository at this point in the history
257: Allow ASB to be configured with max BTC buy amount r=thomaseizinger a=thomaseizinger

This will make it easier to also configure the CLI to display an appropriate max amount the user has to deal with.

This is the first step in working towards comit-network/xmr-btc-swap#255.

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
  • Loading branch information
abraham-nixon and thomaseizinger committed Mar 3, 2021
2 parents b68b42d + 108931a commit 2ec1f33
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
13 changes: 6 additions & 7 deletions swap/src/asb/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::monero::Amount;
use anyhow::Result;
use crate::bitcoin::Amount;
use bitcoin::{util::amount::ParseAmountError, Denomination};
use std::path::PathBuf;

#[derive(structopt::StructOpt, Debug)]
Expand All @@ -19,13 +19,12 @@ pub struct Arguments {
#[structopt(name = "xmr_btc-swap", about = "XMR BTC atomic swap")]
pub enum Command {
Start {
#[structopt(long = "max-sell-xmr", help = "The maximum amount of XMR the ASB is willing to sell.", default_value="0.5", parse(try_from_str = parse_xmr))]
max_sell: Amount,
#[structopt(long = "max-buy-btc", help = "The maximum amount of BTC the ASB is willing to buy.", default_value="0.005", parse(try_from_str = parse_btc))]
max_buy: Amount,
},
History,
}

fn parse_xmr(str: &str) -> Result<Amount> {
let amount = Amount::parse_monero(str)?;
Ok(amount)
fn parse_btc(s: &str) -> Result<Amount, ParseAmountError> {
Amount::from_str_in(s, Denomination::Bitcoin)
}
4 changes: 2 additions & 2 deletions swap/src/bin/asb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn main() -> Result<()> {
let wallet_data_dir = config.data.dir.join("wallet");

match opt.cmd {
Command::Start { max_sell } => {
Command::Start { max_buy } => {
let seed = Seed::from_file_or_generate(&config.data.dir)
.expect("Could not retrieve/initialize seed");

Expand Down Expand Up @@ -108,7 +108,7 @@ async fn main() -> Result<()> {
Arc::new(monero_wallet),
Arc::new(db),
rate_service,
max_sell,
max_buy,
)
.unwrap();

Expand Down
26 changes: 13 additions & 13 deletions swap/src/protocol/alice/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
database::Database,
execution_params::ExecutionParams,
monero,
monero::{Amount, BalanceTooLow},
monero::BalanceTooLow,
network::{transport, TokioExecutor},
protocol::{
alice,
Expand Down Expand Up @@ -86,7 +86,7 @@ pub struct EventLoop<RS> {
monero_wallet: Arc<monero::Wallet>,
db: Arc<Database>,
rate_service: RS,
max_sell: Amount,
max_buy: bitcoin::Amount,

recv_encrypted_signature: broadcast::Sender<EncryptedSignature>,
send_transfer_proof: mpsc::Receiver<(PeerId, TransferProof)>,
Expand All @@ -110,7 +110,7 @@ where
monero_wallet: Arc<monero::Wallet>,
db: Arc<Database>,
rate_service: RS,
max_sell: Amount,
max_buy: bitcoin::Amount,
) -> Result<(Self, mpsc::Receiver<RemoteHandle<Result<AliceState>>>)> {
let identity = seed.derive_libp2p_identity();
let behaviour = Behaviour::default();
Expand Down Expand Up @@ -142,7 +142,7 @@ where
send_transfer_proof: send_transfer_proof.receiver,
send_transfer_proof_sender: send_transfer_proof.sender,
swap_handle_sender: swap_handle.sender,
max_sell,
max_buy,
};
Ok((event_loop, swap_handle.receiver))
}
Expand Down Expand Up @@ -215,17 +215,17 @@ where
.context("Failed to get latest rate")?;

let btc_amount = quote_request.btc_amount;
let xmr_amount = rate.sell_quote(btc_amount)?;

if xmr_amount > self.max_sell {
bail!(MaximumSellAmountExceeded {
actual: xmr_amount,
max_sell: self.max_sell
if btc_amount > self.max_buy {
bail!(MaximumBuyAmountExceeded {
actual: btc_amount,
max: self.max_buy
})
}

let xmr_balance = monero_wallet.get_balance().await?;
let xmr_lock_fees = monero_wallet.static_tx_fee_estimate();
let xmr_amount = rate.sell_quote(btc_amount)?;

if xmr_balance < xmr_amount + xmr_lock_fees {
bail!(BalanceTooLow {
Expand Down Expand Up @@ -299,8 +299,8 @@ where
}

#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("The amount {actual} exceeds the configured maximum sell amount of {max_sell} XMR")]
pub struct MaximumSellAmountExceeded {
pub max_sell: Amount,
pub actual: Amount,
#[error("Refusing to buy {actual} because the maximum configured limit is {max}")]
pub struct MaximumBuyAmountExceeded {
pub max: bitcoin::Amount,
pub actual: bitcoin::Amount,
}
2 changes: 1 addition & 1 deletion swap/tests/testutils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ where
alice_monero_wallet.clone(),
alice_db,
fixed_rate::RateService::default(),
alice_starting_balances.xmr,
bitcoin::Amount::ONE_BTC,
)
.unwrap();

Expand Down

0 comments on commit 2ec1f33

Please sign in to comment.