Skip to content

BFP-1154: Command Failure WS events#48

Merged
nikita-seedlabs merged 5 commits intomainfrom
nikita/BFP-1154
Mar 31, 2025
Merged

BFP-1154: Command Failure WS events#48
nikita-seedlabs merged 5 commits intomainfrom
nikita/BFP-1154

Conversation

@nikita-seedlabs
Copy link
Copy Markdown
Contributor

@nikita-seedlabs nikita-seedlabs commented Mar 31, 2025

User description

  • Adds command failure feedback in web sockets + example

PR Type

Enhancement, Tests, Documentation


Description

  • Introduced AccountCommandFailureUpdate to handle command failure events.

    • Added support for AccountCommandFailureUpdate in TypeScript, Rust, and Python SDKs.
    • Updated WebSocket API to include AccountCommandFailureUpdate in event streams.
  • Added example in Rust SDK for handling leverage update failures.

  • Updated documentation for new event types and payloads.


Changes walkthrough 📝

Relevant files
Enhancement
15 files
api.ts
Added `AccountCommandFailureUpdate` interface and updated enums.
+32/-4   
account_command_failure_update.rs
Added Rust model for `AccountCommandFailureUpdate`.           
+38/-0   
account_data_stream.rs
Updated `AccountDataStream` enum with `AccountCommandFailureUpdate`.
+3/-0     
account_event_reason.rs
Added `IsolatedMarginUpdated` to `AccountEventReason`.     
+3/-0     
account_event_type.rs
Added `AccountLeverageFailureUpdate` to `AccountEventType`.
+3/-0     
account_stream_message.rs
Added `AccountCommandFailureUpdate` to `AccountStreamMessage`.
+8/-1     
account_stream_message_payload.rs
Added `AccountCommandFailureUpdate` to `AccountStreamMessagePayload`.
+1/-0     
mod.rs
Registered `AccountCommandFailureUpdate` in Rust models. 
+2/-0     
__init__.py
Added `AccountCommandFailureUpdate` to Python SDK imports.
+1/-0     
__init__.py
Registered `AccountCommandFailureUpdate` in Python models.
+1/-0     
account_command_failure_update.py
Added Python model for `AccountCommandFailureUpdate`.       
+92/-0   
account_data_stream.py
Updated `AccountDataStream` enum with `AccountCommandFailureUpdate`.
+1/-0     
account_event_reason.py
Added `IsolatedMarginUpdated` to `AccountEventReason`.     
+1/-0     
account_event_type.py
Added `AccountLeverageFailureUpdate` to `AccountEventType`.
+1/-0     
account_stream_message_payload.py
Added `AccountCommandFailureUpdate` to `AccountStreamMessagePayload`.
+22/-8   
Tests
2 files
ws-leverage-update-failure.rs
Added example for handling leverage update failures.         
+211/-0 
test_account_command_failure_update.py
Added unit test for `AccountCommandFailureUpdate`.             
+56/-0   
Configuration changes
2 files
apigen.rs
Updated OpenAPI generator command to `openapi-generator-cli`.
+1/-1     
openapitools.json
Enforced OpenAPI generator version 7.11.0.                             
+7/-0     
Documentation
1 files
websocket-api.yaml
Updated WebSocket API schema for `AccountCommandFailureUpdate`.
+25/-1   
Additional files
14 files
FILES +3/-0     
AccountCommandFailureUpdate.md +32/-0   
AccountDataStream.md +2/-0     
AccountEventReason.md +2/-0     
AccountEventType.md +2/-0     
AccountStreamMessagePayload.md +3/-0     
openapi_client_README.md +1/-0     
FILES +2/-0     
README.md +1/-0     
AccountCommandFailureUpdate.md +13/-0   
AccountDataStream.md +1/-0     
AccountEventReason.md +1/-0     
AccountEventType.md +1/-0     
AccountStreamMessagePayload.md +1/-0     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @qodo-code-review
    Copy link
    Copy Markdown

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling

    The example code doesn't properly handle the case when the websocket connection fails or when the leverage update request fails with an error other than through the command failure update.

    send_invalid_leverage_update_request(&auth_token).await?;
    
    // Listen to the mpsc channel for 5 seconds (for the sake of this example) to get the account
    // update. Normally, you would listen to this channel indefinitely to wait for messages.
    while let Some(websocket_message) = receiver.recv().await {
        if let AccountStreamMessage::AccountCommandFailureUpdate {
            payload:
                AccountStreamMessagePayload::AccountCommandFailureUpdate(account_command_failure_update),
            ..
        } = websocket_message
        {
            println!("{account_command_failure_update:#?}");
        }
    }
    Resource Cleanup

    The example doesn't properly clean up resources. The websocket connection is never explicitly closed, and the receiver channel might not be properly drained if an error occurs.

    async fn main() -> Result<()> {
        // First, we log into the desired environment.
        let auth_token = authenticate(Environment::Devnet).await?;
    
        // Stream websocket messages for 10 seconds
        let shutdown_flag = Arc::new(AtomicBool::new(false));
        let (sender, mut receiver) = tokio::sync::mpsc::channel::<AccountStreamMessage>(100);
        listen_to_command_failures(
            &auth_token,
            Environment::Devnet,
            sender,
            Duration::from_secs(20),
            Arc::clone(&shutdown_flag),
        )
        .await?;
    
        send_invalid_leverage_update_request(&auth_token).await?;
    
        // Listen to the mpsc channel for 5 seconds (for the sake of this example) to get the account
        // update. Normally, you would listen to this channel indefinitely to wait for messages.
        while let Some(websocket_message) = receiver.recv().await {
            if let AccountStreamMessage::AccountCommandFailureUpdate {
                payload:
                    AccountStreamMessagePayload::AccountCommandFailureUpdate(account_command_failure_update),
                ..
            } = websocket_message
            {
                println!("{account_command_failure_update:#?}");
            }
        }
    
        shutdown_flag.store(true, std::sync::atomic::Ordering::SeqCst);
    
        Ok(())

    @qodo-code-review
    Copy link
    Copy Markdown

    qodo-code-review Bot commented Mar 31, 2025

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Restore previous default implementation

    The default implementation for AccountStreamMessage has been changed to use
    AccountCommandFailureUpdate instead of the previous default. This could break
    existing code that relies on the default being AccountOrderUpdate. Consider
    keeping the previous default or providing a more neutral default.

    rust/gen/bluefin_api/src/models/account_stream_message.rs [62-67]

     impl Default for AccountStreamMessage {
         fn default() -> Self {
    -        Self::AccountCommandFailureUpdate {
    +        Self::AccountOrderUpdate {
                 reason: Default::default(),
                 payload: Default::default(),
             }
         }
     }

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 8

    __

    Why: This is an important suggestion that identifies a potential breaking change. Changing the default implementation from AccountOrderUpdate to AccountCommandFailureUpdate could break existing code that relies on the previous default behavior.

    Medium
    General
    Make generator command configurable

    The command has been changed from "openapi-generator" to
    "openapi-generator-cli", which might cause compatibility issues if users don't
    have the CLI version installed. Consider making this configurable or providing a
    fallback mechanism to support both command names.

    rust/src/bin/apigen.rs [125]

    -let command = "openapi-generator-cli";
    +let command = std::env::var("OPENAPI_GENERATOR_COMMAND").unwrap_or_else(|_| "openapi-generator-cli".to_string());
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: This is a good suggestion that improves flexibility by making the OpenAPI generator command configurable via an environment variable. This allows users to specify their preferred command name, supporting both the new "openapi-generator-cli" and potentially the old "openapi-generator" command.

    Medium
    • Update

    @nikita-seedlabs nikita-seedlabs merged commit ed183f1 into main Mar 31, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    3 participants