From b6201192c2adeac87c21c95a7e3ab4e4ddb31f3a Mon Sep 17 00:00:00 2001 From: binarybaron <86064887+binarybaron@users.noreply.github.com> Date: Sat, 31 Dec 2022 00:36:34 +0100 Subject: [PATCH] Allow asb to set a bitcoin address that is controlled by the asb itself to redeem/punish Bitcoin to --- CHANGELOG.md | 1 + swap/src/asb/config.rs | 5 +++++ swap/src/asb/event_loop.rs | 5 ++++- swap/src/bin/asb.rs | 1 + swap/src/network/swap_setup/alice.rs | 10 ++++++++-- swap/tests/harness/mod.rs | 1 + 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3715329c..8cc57e17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Changed ASB to quote on Monero unlocked balance instead of total balance +- Allow `asb` to set a bitcoin address that is controlled by the asb itself to redeem/punish bitcoin to ### Added diff --git a/swap/src/asb/config.rs b/swap/src/asb/config.rs index 9c2052bd..0aef46bc 100644 --- a/swap/src/asb/config.rs +++ b/swap/src/asb/config.rs @@ -216,6 +216,7 @@ pub struct Maker { pub max_buy_btc: bitcoin::Amount, pub ask_spread: Decimal, pub price_ticker_ws_url: Url, + pub external_bitcoin_redeem_address: Option, } impl Default for TorConf { @@ -384,6 +385,7 @@ pub fn query_user_for_initial_config(testnet: bool) -> Result { max_buy_btc: max_buy, ask_spread, price_ticker_ws_url: defaults.price_ticker_ws_url, + external_bitcoin_redeem_address: None, }, }) } @@ -429,6 +431,7 @@ mod tests { max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(), ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(), price_ticker_ws_url: defaults.price_ticker_ws_url, + external_bitcoin_redeem_address: None, }, }; @@ -472,6 +475,7 @@ mod tests { max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(), ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(), price_ticker_ws_url: defaults.price_ticker_ws_url, + external_bitcoin_redeem_address: None, }, }; @@ -525,6 +529,7 @@ mod tests { max_buy_btc: bitcoin::Amount::from_btc(DEFAULT_MAX_BUY_AMOUNT).unwrap(), ask_spread: Decimal::from_f64(DEFAULT_SPREAD).unwrap(), price_ticker_ws_url: defaults.price_ticker_ws_url, + external_bitcoin_redeem_address: None, }, }; diff --git a/swap/src/asb/event_loop.rs b/swap/src/asb/event_loop.rs index b90188c0..130494bc 100644 --- a/swap/src/asb/event_loop.rs +++ b/swap/src/asb/event_loop.rs @@ -44,6 +44,7 @@ where latest_rate: LR, min_buy: bitcoin::Amount, max_buy: bitcoin::Amount, + external_redeem_address: Option, swap_sender: mpsc::Sender, @@ -76,6 +77,7 @@ where latest_rate: LR, min_buy: bitcoin::Amount, max_buy: bitcoin::Amount, + external_redeem_address: Option, ) -> Result<(Self, mpsc::Receiver)> { let swap_channel = MpscChannels::default(); @@ -89,6 +91,7 @@ where swap_sender: swap_channel.sender, min_buy, max_buy, + external_redeem_address, recv_encrypted_signature: Default::default(), inflight_encrypted_signatures: Default::default(), send_transfer_proof: Default::default(), @@ -165,7 +168,7 @@ where } }; - let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet, btc).await { + let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet, &self.external_redeem_address, btc).await { Ok(wallet_snapshot) => wallet_snapshot, Err(error) => { tracing::error!("Swap request will be ignored because we were unable to create wallet snapshot for swap: {:#}", error); diff --git a/swap/src/bin/asb.rs b/swap/src/bin/asb.rs index 43381399..d05a0dbb 100644 --- a/swap/src/bin/asb.rs +++ b/swap/src/bin/asb.rs @@ -188,6 +188,7 @@ async fn main() -> Result<()> { kraken_rate.clone(), config.maker.min_buy_btc, config.maker.max_buy_btc, + config.maker.external_bitcoin_redeem_address, ) .unwrap(); diff --git a/swap/src/network/swap_setup/alice.rs b/swap/src/network/swap_setup/alice.rs index 3d8eebcc..25a80e0e 100644 --- a/swap/src/network/swap_setup/alice.rs +++ b/swap/src/network/swap_setup/alice.rs @@ -59,11 +59,17 @@ impl WalletSnapshot { pub async fn capture( bitcoin_wallet: &bitcoin::Wallet, monero_wallet: &monero::Wallet, + external_redeem_address: &Option, transfer_amount: bitcoin::Amount, ) -> Result { let balance = monero_wallet.get_balance().await?; - let redeem_address = bitcoin_wallet.new_address().await?; - let punish_address = bitcoin_wallet.new_address().await?; + let redeem_address = external_redeem_address + .clone() + .unwrap_or(bitcoin_wallet.new_address().await?); + let punish_address = external_redeem_address + .clone() + .unwrap_or(bitcoin_wallet.new_address().await?); + let redeem_fee = bitcoin_wallet .estimate_fee(bitcoin::TxRedeem::weight(), transfer_amount) .await?; diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index 29fcce5c..74afcc83 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -259,6 +259,7 @@ async fn start_alice( FixedRate::default(), min_buy, max_buy, + None, ) .unwrap();