Skip to content

Commit 32912eb

Browse files
Merge #394
394: Add a configurable spread to the ASB r=thomaseizinger a=thomaseizinger Fixes #381. Co-authored-by: Thomas Eizinger <[email protected]>
2 parents f0a8be6 + a99d12b commit 32912eb

File tree

11 files changed

+172
-72
lines changed

11 files changed

+172
-72
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- A changelog file.
1818
- Automatic resume of unfinished swaps for the `asb` upon startup.
1919
Unfinished swaps from earlier versions will be skipped.
20+
- A configurable spread for the ASB that is applied to the asking price received from the Kraken price ticker.
21+
The default value is 2% and can be configured using the `--ask-spread` parameter.
22+
See `./asb --help` for details.
2023

2124
### Changed
2225

swap/src/asb.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
pub mod command;
22
pub mod config;
3-
mod fixed_rate;
43
mod rate;
54

6-
pub use self::fixed_rate::FixedRate;
7-
pub use self::rate::Rate;
5+
pub use rate::Rate;

swap/src/asb/command.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::bitcoin::Amount;
22
use bitcoin::util::amount::ParseAmountError;
33
use bitcoin::{Address, Denomination};
4+
use rust_decimal::Decimal;
45
use std::path::PathBuf;
56

67
#[derive(structopt::StructOpt, Debug)]
@@ -27,6 +28,12 @@ pub enum Command {
2728
Start {
2829
#[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))]
2930
max_buy: Amount,
31+
#[structopt(
32+
long = "ask-spread",
33+
help = "The spread in percent that should be applied to the asking price.",
34+
default_value = "0.02"
35+
)]
36+
ask_spread: Decimal,
3037
},
3138
History,
3239
WithdrawBtc {

swap/src/asb/fixed_rate.rs

-20
This file was deleted.

swap/src/asb/rate.rs

+69-10
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,47 @@ use rust_decimal::prelude::ToPrimitive;
44
use rust_decimal::Decimal;
55
use std::fmt::{Debug, Display, Formatter};
66

7-
/// Prices at which 1 XMR will be traded, in BTC (XMR/BTC pair)
8-
/// The `ask` represents the minimum price in BTC for which we are willing to
9-
/// sell 1 XMR.
7+
/// Represents the rate at which we are willing to trade 1 XMR.
108
#[derive(Debug, Clone, Copy, PartialEq)]
119
pub struct Rate {
12-
pub ask: bitcoin::Amount,
10+
/// Represents the asking price from the market.
11+
ask: bitcoin::Amount,
12+
/// The spread which should be applied to the market asking price.
13+
ask_spread: Decimal,
1314
}
1415

16+
const ZERO_SPREAD: Decimal = Decimal::from_parts(0, 0, 0, false, 0);
17+
1518
impl Rate {
1619
pub const ZERO: Rate = Rate {
1720
ask: bitcoin::Amount::ZERO,
21+
ask_spread: ZERO_SPREAD,
1822
};
1923

20-
// This function takes the quote amount as it is what Bob sends to Alice in the
21-
// swap request
24+
pub fn new(ask: bitcoin::Amount, ask_spread: Decimal) -> Self {
25+
Self { ask, ask_spread }
26+
}
27+
28+
/// Computes the asking price at which we are willing to sell 1 XMR.
29+
///
30+
/// This applies the spread to the market asking price.
31+
pub fn ask(&self) -> Result<bitcoin::Amount> {
32+
let sats = self.ask.as_sat();
33+
let sats = Decimal::from(sats);
34+
35+
let additional_sats = sats * self.ask_spread;
36+
let additional_sats = bitcoin::Amount::from_sat(
37+
additional_sats
38+
.to_u64()
39+
.context("Failed to fit spread into u64")?,
40+
);
41+
42+
Ok(self.ask + additional_sats)
43+
}
44+
45+
/// Calculate a sell quote for a given BTC amount.
2246
pub fn sell_quote(&self, quote: bitcoin::Amount) -> Result<monero::Amount> {
23-
Self::quote(self.ask, quote)
47+
Self::quote(self.ask()?, quote)
2448
}
2549

2650
fn quote(rate: bitcoin::Amount, quote: bitcoin::Amount) -> Result<monero::Amount> {
@@ -59,16 +83,51 @@ impl Display for Rate {
5983
mod tests {
6084
use super::*;
6185

86+
const TWO_PERCENT: Decimal = Decimal::from_parts(2, 0, 0, false, 2);
87+
const ONE: Decimal = Decimal::from_parts(1, 0, 0, false, 0);
88+
6289
#[test]
6390
fn sell_quote() {
64-
let rate = Rate {
65-
ask: bitcoin::Amount::from_btc(0.002_500).unwrap(),
66-
};
91+
let asking_price = bitcoin::Amount::from_btc(0.002_500).unwrap();
92+
let rate = Rate::new(asking_price, ZERO_SPREAD);
6793

6894
let btc_amount = bitcoin::Amount::from_btc(2.5).unwrap();
6995

7096
let xmr_amount = rate.sell_quote(btc_amount).unwrap();
7197

7298
assert_eq!(xmr_amount, monero::Amount::from_monero(1000.0).unwrap())
7399
}
100+
101+
#[test]
102+
fn applies_spread_to_asking_price() {
103+
let asking_price = bitcoin::Amount::from_sat(100);
104+
let rate = Rate::new(asking_price, TWO_PERCENT);
105+
106+
let amount = rate.ask().unwrap();
107+
108+
assert_eq!(amount.as_sat(), 102);
109+
}
110+
111+
#[test]
112+
fn given_spread_of_two_percent_when_caluclating_sell_quote_factor_between_should_be_two_percent(
113+
) {
114+
let asking_price = bitcoin::Amount::from_btc(0.004).unwrap();
115+
116+
let rate_no_spread = Rate::new(asking_price, ZERO_SPREAD);
117+
let rate_with_spread = Rate::new(asking_price, TWO_PERCENT);
118+
119+
let xmr_no_spread = rate_no_spread.sell_quote(bitcoin::Amount::ONE_BTC).unwrap();
120+
let xmr_with_spread = rate_with_spread
121+
.sell_quote(bitcoin::Amount::ONE_BTC)
122+
.unwrap();
123+
124+
let xmr_factor =
125+
xmr_no_spread.as_piconero_decimal() / xmr_with_spread.as_piconero_decimal() - ONE;
126+
127+
assert!(xmr_with_spread < xmr_no_spread);
128+
assert_eq!(xmr_factor.round_dp(8), TWO_PERCENT); // round to 8 decimal
129+
// places to show that
130+
// it is really close
131+
// to two percent
132+
}
74133
}

swap/src/bin/asb.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use swap::env::GetConfig;
2626
use swap::fs::default_config_path;
2727
use swap::monero::Amount;
2828
use swap::network::swarm;
29+
use swap::protocol::alice::event_loop::KrakenRate;
2930
use swap::protocol::alice::{run, Behaviour, EventLoop};
3031
use swap::seed::Seed;
3132
use swap::trace::init_tracing;
@@ -74,7 +75,10 @@ async fn main() -> Result<()> {
7475
let env_config = env::Testnet::get_config();
7576

7677
match opt.cmd {
77-
Command::Start { max_buy } => {
78+
Command::Start {
79+
max_buy,
80+
ask_spread,
81+
} => {
7882
let bitcoin_wallet = init_bitcoin_wallet(&config, &seed, env_config).await?;
7983
let monero_wallet = init_monero_wallet(&config, env_config).await?;
8084

@@ -92,7 +96,7 @@ async fn main() -> Result<()> {
9296
info!("Monero balance: {}", monero_balance);
9397
}
9498

95-
let kraken_rate_updates = kraken::connect()?;
99+
let kraken_price_updates = kraken::connect()?;
96100

97101
let mut swarm = swarm::new::<Behaviour>(&seed)?;
98102
Swarm::listen_on(&mut swarm, config.network.listen)
@@ -104,7 +108,7 @@ async fn main() -> Result<()> {
104108
Arc::new(bitcoin_wallet),
105109
Arc::new(monero_wallet),
106110
Arc::new(db),
107-
kraken_rate_updates,
111+
KrakenRate::new(ask_spread, kraken_price_updates),
108112
max_buy,
109113
)
110114
.unwrap();

swap/src/bin/kraken_ticker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ async fn main() -> Result<()> {
99
let mut ticker = swap::kraken::connect().context("Failed to connect to kraken")?;
1010

1111
loop {
12-
match ticker.wait_for_update().await? {
13-
Ok(rate) => println!("Rate update: {}", rate),
12+
match ticker.wait_for_next_update().await? {
13+
Ok(update) => println!("Price update: {}", update.ask),
1414
Err(e) => println!("Error: {:#}", e),
1515
}
1616
}

0 commit comments

Comments
 (0)