233: ASB max sell amount r=thomaseizinger a=da-kami



Co-authored-by: Daniel Karzel <daniel@comit.network>
This commit is contained in:
bors[bot] 2021-03-01 01:47:34 +00:00 committed by GitHub
commit 7251588e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 3 deletions

View File

@ -1,3 +1,5 @@
use crate::monero::Amount;
use anyhow::Result;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(structopt::StructOpt, Debug)] #[derive(structopt::StructOpt, Debug)]
@ -16,6 +18,14 @@ pub struct Arguments {
#[derive(structopt::StructOpt, Debug)] #[derive(structopt::StructOpt, Debug)]
#[structopt(name = "xmr_btc-swap", about = "XMR BTC atomic swap")] #[structopt(name = "xmr_btc-swap", about = "XMR BTC atomic swap")]
pub enum Command { pub enum Command {
Start, 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,
},
History, History,
} }
fn parse_xmr(str: &str) -> Result<Amount> {
let amount = Amount::parse_monero(str)?;
Ok(amount)
}

View File

@ -79,7 +79,7 @@ async fn main() -> Result<()> {
let wallet_data_dir = config.data.dir.join("wallet"); let wallet_data_dir = config.data.dir.join("wallet");
match opt.cmd { match opt.cmd {
Command::Start => { Command::Start { max_sell } => {
let seed = Seed::from_file_or_generate(&config.data.dir) let seed = Seed::from_file_or_generate(&config.data.dir)
.expect("Could not retrieve/initialize seed"); .expect("Could not retrieve/initialize seed");
@ -107,6 +107,7 @@ async fn main() -> Result<()> {
Arc::new(monero_wallet), Arc::new(monero_wallet),
Arc::new(db), Arc::new(db),
rate_service, rate_service,
max_sell,
) )
.unwrap(); .unwrap();

View File

@ -4,7 +4,7 @@ use crate::{
database::Database, database::Database,
execution_params::ExecutionParams, execution_params::ExecutionParams,
monero, monero,
monero::BalanceTooLow, monero::{Amount, BalanceTooLow},
network, network,
network::{transport, TokioExecutor}, network::{transport, TokioExecutor},
protocol::{ protocol::{
@ -88,6 +88,7 @@ pub struct EventLoop<RS> {
db: Arc<Database>, db: Arc<Database>,
listen_address: Multiaddr, listen_address: Multiaddr,
rate_service: RS, rate_service: RS,
max_sell: Amount,
recv_encrypted_signature: broadcast::Sender<EncryptedSignature>, recv_encrypted_signature: broadcast::Sender<EncryptedSignature>,
send_transfer_proof: mpsc::Receiver<(PeerId, TransferProof)>, send_transfer_proof: mpsc::Receiver<(PeerId, TransferProof)>,
@ -102,6 +103,7 @@ impl<RS> EventLoop<RS>
where where
RS: LatestRate, RS: LatestRate,
{ {
#[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
listen_address: Multiaddr, listen_address: Multiaddr,
seed: Seed, seed: Seed,
@ -110,6 +112,7 @@ where
monero_wallet: Arc<monero::Wallet>, monero_wallet: Arc<monero::Wallet>,
db: Arc<Database>, db: Arc<Database>,
rate_service: RS, rate_service: RS,
max_sell: Amount,
) -> Result<(Self, mpsc::Receiver<RemoteHandle<Result<AliceState>>>)> { ) -> Result<(Self, mpsc::Receiver<RemoteHandle<Result<AliceState>>>)> {
let identity = network::Seed::new(seed).derive_libp2p_identity(); let identity = network::Seed::new(seed).derive_libp2p_identity();
let behaviour = Behaviour::default(); let behaviour = Behaviour::default();
@ -142,6 +145,7 @@ where
send_transfer_proof: send_transfer_proof.receiver, send_transfer_proof: send_transfer_proof.receiver,
send_transfer_proof_sender: send_transfer_proof.sender, send_transfer_proof_sender: send_transfer_proof.sender,
swap_handle_sender: swap_handle.sender, swap_handle_sender: swap_handle.sender,
max_sell,
}; };
Ok((event_loop, swap_handle.receiver)) Ok((event_loop, swap_handle.receiver))
} }
@ -216,6 +220,13 @@ where
let btc_amount = quote_request.btc_amount; let btc_amount = quote_request.btc_amount;
let xmr_amount = rate.sell_quote(btc_amount)?; let xmr_amount = rate.sell_quote(btc_amount)?;
if xmr_amount > self.max_sell {
anyhow!(MaximumSellAmountExceeded {
actual: xmr_amount,
max_sell: self.max_sell
});
}
let xmr_balance = monero_wallet.get_balance().await?; let xmr_balance = monero_wallet.get_balance().await?;
let xmr_lock_fees = monero_wallet.static_tx_fee_estimate(); let xmr_lock_fees = monero_wallet.static_tx_fee_estimate();
@ -288,3 +299,10 @@ where
Ok(()) Ok(())
} }
} }
#[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,
}

View File

@ -384,6 +384,7 @@ where
alice_monero_wallet.clone(), alice_monero_wallet.clone(),
alice_db, alice_db,
fixed_rate::RateService::default(), fixed_rate::RateService::default(),
alice_starting_balances.xmr,
) )
.unwrap(); .unwrap();