diff --git a/daemon/src/model/cfd.rs b/daemon/src/model/cfd.rs index 0c910f5..b7dc2a4 100644 --- a/daemon/src/model/cfd.rs +++ b/daemon/src/model/cfd.rs @@ -15,7 +15,8 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::fmt; use std::ops::{Neg, RangeInclusive}; -use std::time::{Duration, SystemTime}; +use std::time::SystemTime; +use time::Duration; use uuid::Uuid; #[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] @@ -101,6 +102,8 @@ pub struct Order { #[allow(dead_code)] // Only one binary and the tests use this. impl Order { + pub const TERM: Duration = Duration::hours(8); + pub fn new( price: Usd, min_quantity: Usd, @@ -123,7 +126,7 @@ impl Order { liquidation_price, position: Position::Sell, creation_timestamp: SystemTime::now(), - term: Duration::from_secs(60 * 60 * 8), // 8 hours + term: Self::TERM, origin, oracle_event_id, }) @@ -482,11 +485,7 @@ impl Cfd { #[allow(dead_code)] pub fn refund_timelock_in_blocks(&self) -> u32 { - self.order - .term - .mul_f32(Cfd::REFUND_THRESHOLD) - .as_blocks() - .ceil() as u32 + (self.order.term * Cfd::REFUND_THRESHOLD).as_blocks().ceil() as u32 } /// A factor to be added to the CFD order term for calculating the refund timelock. @@ -1031,7 +1030,7 @@ pub trait AsBlocks { impl AsBlocks for Duration { fn as_blocks(&self) -> f32 { - self.as_secs_f32() / 60.0 / 10.0 + self.as_seconds_f32() / 60.0 / 10.0 } } @@ -1140,15 +1139,15 @@ mod tests { fn test_secs_into_blocks() { let error_margin = f32::EPSILON; - let duration = Duration::from_secs(600); + let duration = Duration::seconds(600); let blocks = duration.as_blocks(); assert!(blocks - error_margin < 1.0 && blocks + error_margin > 1.0); - let duration = Duration::from_secs(0); + let duration = Duration::seconds(0); let blocks = duration.as_blocks(); assert!(blocks - error_margin < 0.0 && blocks + error_margin > 0.0); - let duration = Duration::from_secs(60); + let duration = Duration::seconds(60); let blocks = duration.as_blocks(); assert!(blocks - error_margin < 0.1 && blocks + error_margin > 0.1); } diff --git a/daemon/src/to_sse_event.rs b/daemon/src/to_sse_event.rs index b4e6fd4..6bb25e9 100644 --- a/daemon/src/to_sse_event.rs +++ b/daemon/src/to_sse_event.rs @@ -6,6 +6,7 @@ use rocket::request::FromParam; use rocket::response::stream::Event; use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; +use std::convert::TryInto; use std::time::{SystemTime, UNIX_EPOCH}; use tokio::sync::watch; @@ -200,7 +201,11 @@ impl ToSseEvent for Option { .duration_since(UNIX_EPOCH) .expect("timestamp to be convertible to duration since epoch") .as_secs(), - term_in_secs: order.term.as_secs(), + term_in_secs: order + .term + .whole_seconds() + .try_into() + .expect("term is always positive number"), }); Event::json(&order).event("order")