Archived
Add Side to PriceLevelUpdate.
This commit is contained in:
1 parent
cd02a89303
commit
97360aefac
9 files changed
+82
-1
No files matched your search
@@ -8,6 +8,7 @@ build = "src/util/build.rs"
|
||||
anyhow = "1"
|
||||
chrono = "0.4"
|
||||
crossbeam = "0.8.4"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
tempfile = "3"
|
||||
|
||||
[build-dependencies]
|
||||
|
||||
@@ -18,3 +18,4 @@ If you're curious to see the YAML schema, take a look at [challenge_orderbook.ya
|
||||
We also use a custom timestamp field, so that the Rust struct can use
|
||||
`chrono::DateTime` in the generated struct, and in SBE schema, it is a uint64
|
||||
representing the timestamp as the number of microseconds since unix epoch.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
id: 1
|
||||
version: 1
|
||||
version: 2
|
||||
description: Voluntas engineering challenge orderbook update schema.
|
||||
|
||||
messages:
|
||||
@@ -49,4 +49,8 @@ messages:
|
||||
id: 5
|
||||
type: int64
|
||||
description: The absolute available quantity, in fixed point; 1_000_000 = 1 share/contract
|
||||
side:
|
||||
id: 6
|
||||
type: SideEncoding
|
||||
description: The side being updated; Buy = bid, Sell = ask
|
||||
symbol: { id: 3, type: string }
|
||||
@@ -47,6 +47,11 @@
|
||||
<type name="bytes32" primitiveType="uint8" length="32"/>
|
||||
<type name="address" primitiveType="uint8" length="20"/>
|
||||
|
||||
<!-- The shared Side domain enum owns and validates the Buy/Sell values. -->
|
||||
<composite name="SideEncoding" description="Orderbook side: 0 = Buy (bid), 1 = Sell (ask)">
|
||||
<type name="value" primitiveType="uint8"/>
|
||||
</composite>
|
||||
|
||||
<!-- FIX-standard boolean: one byte on the wire. -->
|
||||
<enum name="BooleanType" encodingType="uint8">
|
||||
<validValue name="False">0</validValue>
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
pub mod util;
|
||||
|
||||
mod side;
|
||||
pub use side::{InvalidSide, Side};
|
||||
@@ -1,4 +1,5 @@
|
||||
use chrono::DateTime;
|
||||
use engineering_challenge::Side;
|
||||
use engineering_challenge::util::dtos::challenge_orderbook::{
|
||||
AnyMessage, PriceLevelUpdateDomain, SnapshotAsksEntryDomain, SnapshotBidsEntryDomain,
|
||||
SnapshotDomain,
|
||||
@@ -35,6 +36,7 @@ fn main() -> anyhow::Result<()> {
|
||||
exchange_timestamp: timestamp,
|
||||
price_level: 400_000,
|
||||
quantity: 1_250_000,
|
||||
side: Side::Buy,
|
||||
symbol: symbol.clone(),
|
||||
};
|
||||
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// The side of an order or price level, shared by DTOs and application code.
|
||||
/// Buy means bid; Sell means ask. The discriminants are SBE wire values and
|
||||
/// must not be renumbered.
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub enum Side {
|
||||
Buy = 0,
|
||||
Sell = 1,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Side {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Buy => "Buy",
|
||||
Self::Sell => "Sell",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for Side {
|
||||
type Error = InvalidSide;
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
0 => Ok(Self::Buy),
|
||||
1 => Ok(Self::Sell),
|
||||
_ => Err(InvalidSide(value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct InvalidSide(pub u8);
|
||||
|
||||
impl std::fmt::Display for InvalidSide {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "invalid side {}; expected 0 (Buy) or 1 (Sell)", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for InvalidSide {}
|
||||
@@ -26,6 +26,10 @@ fn main() -> Result<()> {
|
||||
.with_domain_type(
|
||||
ConversionSelector::named_type("UTCTimestampMicrosEncoding"),
|
||||
"chrono::DateTime<chrono::Utc>",
|
||||
)
|
||||
.with_domain_type(
|
||||
ConversionSelector::named_type("SideEncoding"),
|
||||
"crate::Side",
|
||||
);
|
||||
generate_str_to_out_dir(&xml, config)?;
|
||||
Ok(())
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
use crate::{InvalidSide, Side};
|
||||
|
||||
macro_rules! schemas {
|
||||
($($schema:ident),* $(,)?) => {$(
|
||||
pub mod $schema {
|
||||
@@ -76,6 +78,22 @@ macro_rules! sbe_encode {
|
||||
|
||||
schemas!(challenge_orderbook);
|
||||
|
||||
impl challenge_orderbook::TryFromSbe<challenge_orderbook::SideEncoding> for Side {
|
||||
type Error = InvalidSide;
|
||||
|
||||
fn try_from_sbe(wire: challenge_orderbook::SideEncoding) -> Result<Self, Self::Error> {
|
||||
Self::try_from(wire.value())
|
||||
}
|
||||
}
|
||||
|
||||
impl challenge_orderbook::TryToSbe<challenge_orderbook::SideEncoding> for Side {
|
||||
type Error = std::convert::Infallible;
|
||||
|
||||
fn try_to_sbe(&self) -> Result<challenge_orderbook::SideEncoding, Self::Error> {
|
||||
Ok(challenge_orderbook::SideEncoding::new(*self as u8))
|
||||
}
|
||||
}
|
||||
|
||||
sbe_encode!(
|
||||
challenge_orderbook::SnapshotDomain,
|
||||
challenge_orderbook::PriceLevelUpdateDomain,
|
||||
|
||||
Reference in new issue
Block a user