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