From fcd23c211b6f0623e96fc0dc08c3723bd7e26b6c Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Fri, 8 Oct 2021 10:54:53 +1100 Subject: [PATCH] Chose announcement time based on cfd term --- daemon/src/maker_cfd.rs | 11 ++--------- daemon/src/oracle.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/daemon/src/maker_cfd.rs b/daemon/src/maker_cfd.rs index 40fa778..d01eeaa 100644 --- a/daemon/src/maker_cfd.rs +++ b/daemon/src/maker_cfd.rs @@ -176,15 +176,8 @@ impl Actor { min_quantity: Usd, max_quantity: Usd, ) -> Result<()> { - let oracle_event_id = self - .latest_announcements - .clone() - .context("Cannot create order because no announcement from oracle")? - .iter() - .next_back() - .context("Empty list of announcements")? - .0 - .clone(); + let oracle_event_id = + oracle::next_announcement_after(time::OffsetDateTime::now_utc() + Order::TERM); let order = Order::new( price, diff --git a/daemon/src/oracle.rs b/daemon/src/oracle.rs index c45e96c..0a6afdb 100644 --- a/daemon/src/oracle.rs +++ b/daemon/src/oracle.rs @@ -224,6 +224,16 @@ fn next_24_hours(datetime: OffsetDateTime) -> Vec { (1..=24).map(|i| adjusted + Duration::hours(i)).collect() } +#[allow(dead_code)] +pub fn next_announcement_after(timestamp: OffsetDateTime) -> OracleEventId { + // always ceil to next hour + let adjusted = + timestamp.replace_time(Time::from_hms(timestamp.hour() + 1, 0, 0).expect("in_range")); + let event_id = event_url(adjusted); + + OracleEventId(event_id) +} + /// Construct the URL of `olivia`'s `BitMEX/BXBT` event to be attested /// for at the time indicated by the argument `datetime`. fn event_url(datetime: OffsetDateTime) -> String { @@ -559,6 +569,16 @@ mod tests { ); } + #[test] + fn next_event_url_after_timestamp() { + let url = next_announcement_after(datetime!(2021-09-23 10:40:00).assume_utc()); + + assert_eq!( + url.0, + "https://h00.ooo/x/BitMEX/BXBT/2021-09-23T11:00:00.price[n:20]" + ); + } + #[test] fn next_24() { let datetime = datetime!(2021-09-23 10:43:12);