From 1eb237faaae6dbe24ab068fc3e6c2ee0a9c60aa8 Mon Sep 17 00:00:00 2001 From: Lucas Soriano del Pino Date: Tue, 21 Sep 2021 10:54:09 +1000 Subject: [PATCH] Take RangeInclusive as argument to Payout constructor --- cfd_protocol/src/interval.rs | 5 +++-- cfd_protocol/src/protocol.rs | 12 +++++------- cfd_protocol/tests/cfds.rs | 25 ++++++++++++++++--------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cfd_protocol/src/interval.rs b/cfd_protocol/src/interval.rs index 29fe898..02df497 100644 --- a/cfd_protocol/src/interval.rs +++ b/cfd_protocol/src/interval.rs @@ -17,7 +17,8 @@ const BASE: usize = 2; pub struct Digits(BitVec); impl Digits { - pub fn new(start: u64, end: u64) -> Result, Error> { + pub fn new(range: RangeInclusive) -> Result, Error> { + let (start, end) = range.into_inner(); if start > MAX_PRICE_DEC || end > MAX_PRICE_DEC { return Err(Error::RangeOverMax); } @@ -127,7 +128,7 @@ mod tests { } fn to_digits(&self) -> Result> { - let digits = Digits::new(*self.0.start(), *self.0.end())?; + let digits = Digits::new(self.0.clone())?; Ok(digits) } diff --git a/cfd_protocol/src/protocol.rs b/cfd_protocol/src/protocol.rs index fac8e3a..fa6cf34 100644 --- a/cfd_protocol/src/protocol.rs +++ b/cfd_protocol/src/protocol.rs @@ -22,6 +22,7 @@ use itertools::Itertools; use secp256k1_zkp::{self, schnorrsig, EcdsaAdaptorSignature, SecretKey, Signature, SECP256K1}; use std::collections::HashMap; use std::iter::FromIterator; +use std::ops::RangeInclusive; mod sighash_ext; mod transaction_ext; @@ -341,12 +342,11 @@ pub struct Payout { impl Payout { pub fn new( - start: u64, - end: u64, + range: RangeInclusive, maker_amount: Amount, taker_amount: Amount, ) -> Result> { - let digits = interval::Digits::new(start, end).context("invalid interval")?; + let digits = interval::Digits::new(range).context("invalid interval")?; Ok(digits .into_iter() .map(|digits| Self { @@ -473,8 +473,7 @@ mod tests { let orig_maker_amount = 1000; let orig_taker_amount = 1000; let payouts = Payout::new( - 0, - 10_000, + 0..=10_000, Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_taker_amount), ) @@ -508,8 +507,7 @@ mod tests { let orig_maker_amount = dummy_dust_limit.as_sat() - 1; let orig_taker_amount = 1000; let payouts = Payout::new( - 0, - 10_000, + 0..=10_000, Amount::from_sat(orig_maker_amount), Amount::from_sat(orig_taker_amount), ) diff --git a/cfd_protocol/tests/cfds.rs b/cfd_protocol/tests/cfds.rs index 976b694..1ec6749 100644 --- a/cfd_protocol/tests/cfds.rs +++ b/cfd_protocol/tests/cfds.rs @@ -32,13 +32,17 @@ fn create_cfd() { let payouts = vec![ Payout::new( - 0, - 10_000, + 0..=10_000, Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), ) .unwrap(), - Payout::new(10_001, 20_000, Amount::ZERO, Amount::from_btc(2.0).unwrap()).unwrap(), + Payout::new( + 10_001..=20_000, + Amount::ZERO, + Amount::from_btc(2.0).unwrap(), + ) + .unwrap(), ] .concat(); @@ -112,8 +116,13 @@ fn renew_cfd() { let (_event, announcement) = announce(&mut rng); let payouts = vec![ - Payout::new(0, 10_000, Amount::from_btc(2.0).unwrap(), Amount::ZERO).unwrap(), - Payout::new(10_001, 20_000, Amount::ZERO, Amount::from_btc(2.0).unwrap()).unwrap(), + Payout::new(0..=10_000, Amount::from_btc(2.0).unwrap(), Amount::ZERO).unwrap(), + Payout::new( + 10_001..=20_000, + Amount::ZERO, + Amount::from_btc(2.0).unwrap(), + ) + .unwrap(), ] .concat(); @@ -140,15 +149,13 @@ fn renew_cfd() { let payouts = vec![ Payout::new( - 0, - 10_000, + 0..=10_000, Amount::from_btc(1.5).unwrap(), Amount::from_btc(0.5).unwrap(), ) .unwrap(), Payout::new( - 10_001, - 20_000, + 10_001..=20_000, Amount::from_btc(0.5).unwrap(), Amount::from_btc(1.5).unwrap(), )