Browse Source

Make term a constant

Switch to `time::Duration` so we can declare it as a constant.
compile-for-aarch64
Thomas Eizinger 3 years ago
committed by Daniel Karzel
parent
commit
285840a856
No known key found for this signature in database GPG Key ID: 30C3FC2E438ADB6E
  1. 21
      daemon/src/model/cfd.rs
  2. 7
      daemon/src/to_sse_event.rs

21
daemon/src/model/cfd.rs

@ -15,7 +15,8 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::ops::{Neg, RangeInclusive}; use std::ops::{Neg, RangeInclusive};
use std::time::{Duration, SystemTime}; use std::time::SystemTime;
use time::Duration;
use uuid::Uuid; use uuid::Uuid;
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] #[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. #[allow(dead_code)] // Only one binary and the tests use this.
impl Order { impl Order {
pub const TERM: Duration = Duration::hours(8);
pub fn new( pub fn new(
price: Usd, price: Usd,
min_quantity: Usd, min_quantity: Usd,
@ -123,7 +126,7 @@ impl Order {
liquidation_price, liquidation_price,
position: Position::Sell, position: Position::Sell,
creation_timestamp: SystemTime::now(), creation_timestamp: SystemTime::now(),
term: Duration::from_secs(60 * 60 * 8), // 8 hours term: Self::TERM,
origin, origin,
oracle_event_id, oracle_event_id,
}) })
@ -482,11 +485,7 @@ impl Cfd {
#[allow(dead_code)] #[allow(dead_code)]
pub fn refund_timelock_in_blocks(&self) -> u32 { pub fn refund_timelock_in_blocks(&self) -> u32 {
self.order (self.order.term * Cfd::REFUND_THRESHOLD).as_blocks().ceil() as u32
.term
.mul_f32(Cfd::REFUND_THRESHOLD)
.as_blocks()
.ceil() as u32
} }
/// A factor to be added to the CFD order term for calculating the refund timelock. /// 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 { impl AsBlocks for Duration {
fn as_blocks(&self) -> f32 { 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() { fn test_secs_into_blocks() {
let error_margin = f32::EPSILON; let error_margin = f32::EPSILON;
let duration = Duration::from_secs(600); let duration = Duration::seconds(600);
let blocks = duration.as_blocks(); let blocks = duration.as_blocks();
assert!(blocks - error_margin < 1.0 && blocks + error_margin > 1.0); 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(); let blocks = duration.as_blocks();
assert!(blocks - error_margin < 0.0 && blocks + error_margin > 0.0); 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(); let blocks = duration.as_blocks();
assert!(blocks - error_margin < 0.1 && blocks + error_margin > 0.1); assert!(blocks - error_margin < 0.1 && blocks + error_margin > 0.1);
} }

7
daemon/src/to_sse_event.rs

@ -6,6 +6,7 @@ use rocket::request::FromParam;
use rocket::response::stream::Event; use rocket::response::stream::Event;
use rust_decimal::Decimal; use rust_decimal::Decimal;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::watch; use tokio::sync::watch;
@ -200,7 +201,11 @@ impl ToSseEvent for Option<model::cfd::Order> {
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.expect("timestamp to be convertible to duration since epoch") .expect("timestamp to be convertible to duration since epoch")
.as_secs(), .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") Event::json(&order).event("order")

Loading…
Cancel
Save