Skip to content

OrderSide enum should serialize to lowercase for Deribit API compatibility #1

@aaaronme

Description

@aaaronme

Source: order.rs line 40

Problem

The OrderSide enum currently serializes to capitalized values ("Buy" and "Sell"), but the Deribit API expects lowercase values ("buy" and "sell").
This causes serialization/deserialization errors when communicating with the Deribit API.


Current Behavior

#[derive(DebugPretty, DisplaySimple, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderSide {
    /// Buy order
    Buy,
    /// Sell order
    Sell,
}

When serialized, this produces:

  • OrderSide::Buy"Buy"
  • OrderSide::Sell"Sell"

Expected Behavior

The enum should serialize to lowercase to match Deribit’s API specification:

  • OrderSide::Buy"buy"
  • OrderSide::Sell"sell"

Evidence

  • The existing test test_order_side_serialization() at line 275 explicitly expects capitalized values, which is incorrect for Deribit API compatibility.
  • Other enums in the same file (TimeInForce and OrderType) already use #[serde(rename = "...")] attributes to match API expectations.
  • The OrderInfo struct uses a direction: String field that contains lowercase values like "buy" (see test at line 409), indicating the API uses lowercase.

Solution

Add serde rename attributes to match the pattern used by other enums in the file:

#[derive(DebugPretty, DisplaySimple, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderSide {
    #[serde(rename = "buy")]
    Buy,
    #[serde(rename = "sell")]
    Sell,
}

Additional Changes Needed

  • Update the test test_order_side_serialization() to expect lowercase values.
  • Consider adding an as_str() method to OrderSide for consistency with TimeInForce and OrderType:
impl OrderSide {
    pub fn as_str(&self) -> &'static str {
        match self {
            OrderSide::Buy => "buy",
            OrderSide::Sell => "sell",
        }
    }
}

Impact

This is a breaking change that affects serialization/deserialization, but it’s necessary for proper Deribit API compatibility.
Users relying on the current capitalized serialization format will need to update their code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions