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 3 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
1 change: 1 addition & 0 deletions dev-docs/asb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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.

## ASB Details

The ASB is a long running daemon that acts as the trading partner to the swap CLI.
Expand Down
40 changes: 29 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,34 @@ 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).

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)
.context("reserved more monero than we've got")?;
Einliterflasche marked this conversation as resolved.
Show resolved Hide resolved

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
Loading