Skip to content

Commit

Permalink
Rename MarketHours->WeeklySchedule, market_hours->weekly_schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
drozdziak1 committed Nov 28, 2023
1 parent c5bce62 commit 5ab9e94
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 58 deletions.
2 changes: 1 addition & 1 deletion integration-tests/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"nasdaq_symbol": "AAPL",
"symbol": "Equity.US.AAPL/USD",
"base": "AAPL",
"market_hours": "America/New_York,C,C,C,C,C,C,C" # Should never be published due to all-closed market hours
"weekly_schedule": "America/New_York,C,C,C,C,C,C,C" # Should never be published due to all-closed market hours
},
"metadata": {"jump_id": "186", "jump_symbol": "AAPL", "price_exp": -5, "min_publishers": 1},
}
Expand Down
40 changes: 22 additions & 18 deletions src/agent/market_hours.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
DateTime,
Datelike,
Duration,
TimeZone,
Utc,
Weekday,
},
chrono_tz::Tz,
Expand All @@ -29,7 +29,7 @@ lazy_static! {

/// Weekly market hours schedule
#[derive(Clone, Default, Debug, Eq, PartialEq)]
pub struct MarketHours {
pub struct WeeklySchedule {
pub timezone: Tz,
pub mon: MHKind,
pub tue: MHKind,
Expand All @@ -40,7 +40,7 @@ pub struct MarketHours {
pub sun: MHKind,
}

impl MarketHours {
impl WeeklySchedule {
pub fn all_closed() -> Self {
Self {
timezone: Default::default(),
Expand All @@ -54,7 +54,7 @@ impl MarketHours {
}
}

pub fn can_publish_at<Tz: TimeZone>(&self, when: &DateTime<Tz>) -> bool {
pub fn can_publish_at(&self, when: &DateTime<Utc>) -> bool {
// Convert to time local to the market
let when_market_local = when.with_timezone(&self.timezone);

Expand All @@ -76,7 +76,7 @@ impl MarketHours {
}
}

impl FromStr for MarketHours {
impl FromStr for WeeklySchedule {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
let mut split_by_commas = s.split(",");
Expand Down Expand Up @@ -235,9 +235,9 @@ mod tests {
// Mon-Fri 9-5, inconsistent leading space on Tuesday, leading 0 on Friday (expected to be fine)
let s = "Europe/Warsaw,9:00-17:00, 9:00-17:00,9:00-17:00,9:00-17:00,09:00-17:00,C,C";

let parsed: MarketHours = s.parse()?;
let parsed: WeeklySchedule = s.parse()?;

let expected = MarketHours {
let expected = WeeklySchedule {
timezone: Tz::Europe__Warsaw,
mon: MHKind::TimeRange(
NaiveTime::from_hms_opt(9, 0, 0).unwrap(),
Expand Down Expand Up @@ -273,7 +273,7 @@ mod tests {
// Valid but missing a timezone
let s = "O,C,O,C,O,C,O";

let parsing_result: Result<MarketHours> = s.parse();
let parsing_result: Result<WeeklySchedule> = s.parse();

dbg!(&parsing_result);
assert!(parsing_result.is_err());
Expand All @@ -284,7 +284,7 @@ mod tests {
// One day short
let s = "Asia/Hong_Kong,C,O,C,O,C,O";

let parsing_result: Result<MarketHours> = s.parse();
let parsing_result: Result<WeeklySchedule> = s.parse();

dbg!(&parsing_result);
assert!(parsing_result.is_err());
Expand All @@ -294,7 +294,7 @@ mod tests {
fn test_parsing_gibberish_timezone_is_error() {
// Pretty sure that one's extinct
let s = "Pangea/New_Dino_City,O,O,O,O,O,O,O";
let parsing_result: Result<MarketHours> = s.parse();
let parsing_result: Result<WeeklySchedule> = s.parse();

dbg!(&parsing_result);
assert!(parsing_result.is_err());
Expand All @@ -303,7 +303,7 @@ mod tests {
#[test]
fn test_parsing_gibberish_day_schedule_is_error() {
let s = "Europe/Amsterdam,mondays are alright I guess,O,O,O,O,O,O";
let parsing_result: Result<MarketHours> = s.parse();
let parsing_result: Result<WeeklySchedule> = s.parse();

dbg!(&parsing_result);
assert!(parsing_result.is_err());
Expand All @@ -313,7 +313,7 @@ mod tests {
fn test_parsing_too_many_days_is_error() {
// One day too many
let s = "Europe/Lisbon,O,O,O,O,O,O,O,O,C";
let parsing_result: Result<MarketHours> = s.parse();
let parsing_result: Result<WeeklySchedule> = s.parse();

dbg!(&parsing_result);
assert!(parsing_result.is_err());
Expand All @@ -322,7 +322,7 @@ mod tests {
#[test]
fn test_market_hours_happy_path() -> Result<()> {
// Prepare a schedule of narrow ranges
let mh: MarketHours = "America/New_York,00:00-1:00,1:00-2:00,2:00-3:00,3:00-4:00,4:00-5:00,5:00-6:00,6:00-7:00".parse()?;
let mh: WeeklySchedule = "America/New_York,00:00-1:00,1:00-2:00,2:00-3:00,3:00-4:00,4:00-5:00,5:00-6:00,6:00-7:00".parse()?;

// Prepare UTC datetimes that fall before, within and after market hours
let format = "%Y-%m-%d %H:%M";
Expand Down Expand Up @@ -379,32 +379,36 @@ mod tests {
#[test]
fn test_market_hours_midnight_00_24() -> Result<()> {
// Prepare a schedule of midnight-neighboring ranges
let mh: MarketHours = "Europe/Amsterdam,23:00-24:00,00:00-01:00,O,C,C,C,C".parse()?;
let mh: WeeklySchedule = "Europe/Amsterdam,23:00-24:00,00:00-01:00,O,C,C,C,C".parse()?;

let format = "%Y-%m-%d %H:%M";
let ok_datetimes = vec![
NaiveDate::from_ymd_opt(2023, 11, 20)
.unwrap()
.and_time(MAX_TIME_INSTANT.clone())
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
.unwrap()
.with_timezone(&Utc),
NaiveDateTime::parse_from_str("2023-11-21 00:00", format)?
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
.unwrap()
.with_timezone(&Utc),
];

let bad_datetimes = vec![
// Start of Monday Nov 20th, must not be confused for MAX_TIME_INSTANT on that day
NaiveDateTime::parse_from_str("2023-11-20 00:00", format)?
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
.unwrap()
.with_timezone(&Utc),
// End of Tuesday Nov 21st, borders Wednesday, must not be
// confused for Wednesday 00:00 which is open.
NaiveDate::from_ymd_opt(2023, 11, 21)
.unwrap()
.and_time(MAX_TIME_INSTANT.clone())
.and_local_timezone(Tz::Europe__Amsterdam)
.unwrap(),
.unwrap()
.with_timezone(&Utc),
];

dbg!(&mh);
Expand Down
4 changes: 3 additions & 1 deletion src/agent/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ use {
},
warp::{
hyper::StatusCode,
reply::{self,},
reply::{
self,
},
Filter,
Rejection,
Reply,
Expand Down
12 changes: 6 additions & 6 deletions src/agent/pythd/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ mod tests {
)
.unwrap(),
solana::oracle::ProductEntry {
account_data: pyth_sdk_solana::state::ProductAccount {
account_data: pyth_sdk_solana::state::ProductAccount {
magic: 0xa1b2c3d4,
ver: 6,
atype: 4,
Expand Down Expand Up @@ -992,8 +992,8 @@ mod tests {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
},
market_hours: Default::default(),
price_accounts: vec![
weekly_schedule: Default::default(),
price_accounts: vec![
solana_sdk::pubkey::Pubkey::from_str(
"GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU",
)
Expand All @@ -1015,7 +1015,7 @@ mod tests {
)
.unwrap(),
solana::oracle::ProductEntry {
account_data: pyth_sdk_solana::state::ProductAccount {
account_data: pyth_sdk_solana::state::ProductAccount {
magic: 0xa1b2c3d4,
ver: 5,
atype: 3,
Expand Down Expand Up @@ -1052,8 +1052,8 @@ mod tests {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
},
market_hours: Default::default(),
price_accounts: vec![
weekly_schedule: Default::default(),
price_accounts: vec![
solana_sdk::pubkey::Pubkey::from_str(
"GG3FTE7xhc9Diy7dn9P6BWzoCrAEE4D3p5NBYrDAm5DD",
)
Expand Down
26 changes: 12 additions & 14 deletions src/agent/solana/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use {
network::Network,
},
crate::agent::{
market_hours::MarketHours,
market_hours::WeeklySchedule,
remote_keypair_loader::{
KeypairRequest,
RemoteKeypairLoader,
Expand All @@ -22,10 +22,7 @@ use {
Result,
},
bincode::Options,
chrono::{
Local,
Utc,
},
chrono::Utc,
futures_util::future::{
self,
join_all,
Expand Down Expand Up @@ -63,7 +60,6 @@ use {
collections::{
BTreeMap,
HashMap,
HashSet,
},
time::Duration,
},
Expand Down Expand Up @@ -175,7 +171,7 @@ pub fn spawn_exporter(
network: Network,
rpc_url: &str,
rpc_timeout: Duration,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketHours>>>,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, WeeklySchedule>>>,
key_store: KeyStore,
local_store_tx: Sender<store::local::Message>,
global_store_tx: Sender<store::global::Lookup>,
Expand Down Expand Up @@ -263,10 +259,10 @@ pub struct Exporter {
inflight_transactions_tx: Sender<Signature>,

/// publisher => { permissioned_price => market hours } as read by the oracle module
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketHours>>>,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, WeeklySchedule>>>,

/// Currently known permissioned prices of this publisher along with their market hours
our_prices: HashMap<Pubkey, MarketHours>,
our_prices: HashMap<Pubkey, WeeklySchedule>,

/// Interval to update the dynamic price (if enabled)
dynamic_compute_unit_price_update_interval: Interval,
Expand All @@ -290,7 +286,7 @@ impl Exporter {
global_store_tx: Sender<store::global::Lookup>,
network_state_rx: watch::Receiver<NetworkState>,
inflight_transactions_tx: Sender<Signature>,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, MarketHours>>>,
publisher_permissions_rx: mpsc::Receiver<HashMap<Pubkey, HashMap<Pubkey, WeeklySchedule>>>,
keypair_request_tx: mpsc::Sender<KeypairRequest>,
logger: Logger,
) -> Self {
Expand Down Expand Up @@ -474,20 +470,22 @@ impl Exporter {
"publish_pubkey" => publish_keypair.pubkey().to_string(),
);

let now = Local::now();
// Get a fresh system time
let now = Utc::now();

// Filter out price accounts we're not permissioned to update
Ok(fresh_updates
.into_iter()
.filter(|(id, _data)| {
let key_from_id = Pubkey::from((*id).clone().to_bytes());
if let Some(market_hours) = self.our_prices.get(&key_from_id) {
let ret = market_hours.can_publish_at(&now);
if let Some(weekly_schedule) = self.our_prices.get(&key_from_id) {
let ret = weekly_schedule.can_publish_at(&now);

if !ret {
debug!(self.logger, "Exporter: Attempted to publish price outside market hours";
"price_account" => key_from_id.to_string(),
"market_hours" => format!("{:?}", market_hours),
"weekly_schedule" => format!("{:?}", weekly_schedule),
"utc_time" => now.format("%c").to_string(),
);
}

Expand Down
Loading

0 comments on commit 5ab9e94

Please sign in to comment.