Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asb: subtract reserved monero from quote volume #245

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- ASB: The maker will take Monero funds needed for ongoing swaps into consideration when making a quote. A warning will be displayed if the Monero funds do not cover all ongoing swaps.

## [1.0.0-rc.11] - 2024-12-22

- ASB: The `history` command will now display additional information about each swap such as the amounts involved, the current state and the txid of the Bitcoin lock transaction.
Expand Down
5 changes: 4 additions & 1 deletion dev-docs/asb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ Consider joining the designated [Matrix chat](https://matrix.to/#/%23unstoppable

### Using Docker

Running the ASB and its required services (Bitcoin node, Monero node, wallet RPC) can be complex to set up manually. We provide a Docker Compose solution that handles all of this automatically. See our [docker-compose repository](https://github.com/UnstoppableSwap/asb-docker-compose) for setup instructions and configuration details.
Running the ASB and its required services (Bitcoin node, Monero node, wallet RPC) can be complex to set up manually.
We provide a Docker Compose solution that handles all of this automatically.
See our [docker-compose repository](https://github.com/UnstoppableSwap/asb-docker-compose) for setup instructions and configuration details.

## ASB Details

The ASB is a long running daemon that acts as the trading partner to the swap CLI.
Expand Down
45 changes: 34 additions & 11 deletions swap/src/asb/event_loop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::asb::{Behaviour, OutEvent, Rate};
use crate::monero::Amount;
use crate::monero::{Amount, MONERO_FEE};
use crate::network::cooperative_xmr_redeem_after_punish::CooperativeXmrRedeemRejectReason;
use crate::network::cooperative_xmr_redeem_after_punish::Response::{Fullfilled, Rejected};
use crate::network::quote::BidQuote;
Expand Down Expand Up @@ -479,16 +479,39 @@ where
// use unlocked monero balance for quote
let xmr_balance = Amount::from_piconero(balance.unlocked_balance);

let max_bitcoin_for_monero =
xmr_balance
.max_bitcoin_for_price(ask_price)
.ok_or_else(|| {
anyhow!(
"Bitcoin price ({}) x Monero ({}) overflow",
ask_price,
xmr_balance
)
})?;
// From our full balance we need to subtract any Monero that is 'reserved' for ongoing swaps
// (where the Bitcoin has been (or is being) locked but we haven't sent the Monero yet).
//
// TODO: Better manage monero funds. Currently we store all Monero in one UTXO, then the change
// address is blocked for 10 blocks before we can use it again.
// We should distribute the monero funds into multiple UTXOs, to avoid blocking large amounts of the total balance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a TODO comment that this is "naive" to some degree due to the 20min lock of outputs / utxo issue

let reserved: Amount = self
.db
.all()
.await?
.iter()
.filter_map(|(_, state)| match state {
State::Alice(AliceState::BtcLockTransactionSeen { state3 })
| State::Alice(AliceState::BtcLocked { state3 }) => Some(state3.xmr + MONERO_FEE),
_ => None,
})
.fold(Amount::ZERO, |acc, amount| acc + amount);

let free_monero_balance = xmr_balance.checked_sub(reserved).unwrap_or_else(|_| {
tracing::warn!(%xmr_balance, missing=%(reserved - xmr_balance), "Monero balance is too low for ongoing swaps, need more funds to complete all ongoing swaps.");
Amount::ZERO
});

let max_bitcoin_for_monero = free_monero_balance
.max_bitcoin_for_price(ask_price)
.ok_or_else(|| {
anyhow!(
"Bitcoin price ({}) x Monero ({}) overflow",
ask_price,
xmr_balance
)
})?;

tracing::trace!(%ask_price, %xmr_balance, %max_bitcoin_for_monero, "Computed quote");

Expand Down
13 changes: 11 additions & 2 deletions swap/src/monero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use wallet::Wallet;
pub use wallet_rpc::{WalletRpc, WalletRpcProcess};

use crate::bitcoin;
use anyhow::Result;
use anyhow::{bail, Result};
use rand::{CryptoRng, RngCore};
use rust_decimal::prelude::*;
use rust_decimal::Decimal;
Expand Down Expand Up @@ -164,6 +164,15 @@ impl Amount {
.ok_or_else(|| OverflowError(amount.to_string()))?;
Ok(Amount(piconeros))
}

/// Subtract but throw an error on underflow.
pub fn checked_sub(self, rhs: Amount) -> Result<Self> {
if self.0 < rhs.0 {
bail!("checked sub would underflow");
}

Ok(Amount::from_piconero(self.0 - rhs.0))
}
}

impl Add for Amount {
Expand All @@ -174,7 +183,7 @@ impl Add for Amount {
}
}

impl Sub for Amount {
impl Sub<Amount> for Amount {
type Output = Amount;

fn sub(self, rhs: Self) -> Self::Output {
Expand Down