Skip to main content

ig_client/application/interfaces/
order.rs

1use crate::error::AppError;
2use crate::model::requests::{
3    ClosePositionRequest, CreateOrderRequest, CreateWorkingOrderRequest, UpdatePositionRequest,
4    UpdateWorkingOrderRequest,
5};
6use crate::model::responses::{
7    ClosePositionResponse, CreateOrderResponse, CreateWorkingOrderResponse,
8    OrderConfirmationResponse, SinglePositionResponse, UpdatePositionResponse,
9};
10
11use async_trait::async_trait;
12
13#[async_trait]
14/// Service for creating, updating, and managing trading orders with the IG Markets API
15///
16/// This trait defines the interface for interacting with the IG Markets order endpoints,
17/// allowing clients to create new orders, get order confirmations, update existing positions,
18/// and close positions.
19pub trait OrderService: Send + Sync {
20    /// Creates a new order
21    async fn create_order(
22        &self,
23        order: &CreateOrderRequest,
24    ) -> Result<CreateOrderResponse, AppError>;
25
26    /// Gets the confirmation of an order
27    async fn get_order_confirmation(
28        &self,
29        deal_reference: &str,
30    ) -> Result<OrderConfirmationResponse, AppError>;
31
32    /// Gets the confirmation of an order with retry logic
33    async fn get_order_confirmation_w_retry(
34        &self,
35        deal_reference: &str,
36        retries: u64,
37        delay_ms: u64,
38    ) -> Result<OrderConfirmationResponse, AppError>;
39
40    /// Updates an existing position
41    async fn update_position(
42        &self,
43        deal_id: &str,
44        update: &UpdatePositionRequest,
45    ) -> Result<UpdatePositionResponse, AppError>;
46
47    ///  Asynchronously updates the limit level of a position in a specified deal.
48    ///  
49    ///  # Parameters
50    ///  - `deal_id`: A reference to a string slice representing the unique identifier of the deal
51    ///    whose position is to be updated.
52    ///  - `limit_level`: An optional `f64` value specifying the new limit level for the position.
53    ///    If `None`, the limit level will not be updated.
54    ///  
55    ///  # Returns
56    ///  - `Result<UpdatePositionResponse, AppError>`:
57    ///    - On success, returns an `UpdatePositionResponse` containing details of the updated position.
58    ///    - On failure, returns an `AppError` indicating the error encountered during the operation.
59    ///  
60    ///  # Errors
61    ///  This function returns an `AppError` in case of:
62    ///  - Invalid `deal_id` (e.g., deal doesn't exist).
63    ///  - Backend service issues or database failures.
64    ///  - Input validation errors for the `limit_level`.
65    ///  
66    ///  
67    ///  # Notes
68    ///  Ensure that the passed `deal_id` exists, and the `limit_level` (if provided) adheres
69    ///  to any required constraints specific to the application's domain logic.
70    ///  
71    ///
72    async fn update_level_in_position(
73        &self,
74        deal_id: &str,
75        limit_level: Option<f64>,
76    ) -> Result<UpdatePositionResponse, AppError>;
77
78    /// Closes an existing position
79    async fn close_position(
80        &self,
81        close_request: &ClosePositionRequest,
82    ) -> Result<ClosePositionResponse, AppError>;
83
84    /// Creates a new working order
85    async fn create_working_order(
86        &self,
87        order: &CreateWorkingOrderRequest,
88    ) -> Result<CreateWorkingOrderResponse, AppError>;
89
90    /// Deletes a working order based on the provided deal ID.
91    ///
92    /// # Parameters
93    /// - `deal_id`: A `String` representing the deal ID of the working order that needs to be deleted.
94    ///
95    /// # Returns
96    /// - `Result<(), AppError>`:
97    ///   - On success, the function returns `Ok(())` indicating that the working order was successfully deleted.
98    ///   - On failure, it returns `Err(AppError)` containing the error details that occurred during the deletion process.
99    ///
100    /// # Errors
101    /// This function will return an `AppError` in the following scenarios:
102    /// - If the deletion operation fails due to invalid deal ID.
103    /// - If there are connectivity issues with the database or external services.
104    /// - If the calling user does not have permission to delete the specified working order.
105    ///
106    async fn delete_working_order(&self, deal_id: &str) -> Result<(), AppError>;
107
108    /// Gets a single position by deal ID
109    ///
110    /// # Arguments
111    /// * `deal_id` - The deal ID of the position to retrieve
112    ///
113    /// # Returns
114    /// * `Ok(SinglePositionResponse)` - The position details and market data
115    /// * `Err(AppError)` - If the request fails
116    async fn get_position(&self, deal_id: &str) -> Result<SinglePositionResponse, AppError>;
117
118    /// Updates an existing working order
119    ///
120    /// # Arguments
121    /// * `deal_id` - The deal ID of the working order to update
122    /// * `update` - The update parameters
123    ///
124    /// # Returns
125    /// * `Ok(CreateWorkingOrderResponse)` - The updated working order confirmation
126    /// * `Err(AppError)` - If the request fails
127    async fn update_working_order(
128        &self,
129        deal_id: &str,
130        update: &UpdateWorkingOrderRequest,
131    ) -> Result<CreateWorkingOrderResponse, AppError>;
132}