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 /// Closes an existing position
47 async fn close_position(
48 &self,
49 close_request: &ClosePositionRequest,
50 ) -> Result<ClosePositionResponse, AppError>;
51
52 /// Creates a new working order
53 async fn create_working_order(
54 &self,
55 order: &CreateWorkingOrderRequest,
56 ) -> Result<CreateWorkingOrderResponse, AppError>;
57
58 /// Deletes a working order based on the provided deal ID.
59 ///
60 /// # Parameters
61 /// - `deal_id`: A `String` representing the deal ID of the working order that needs to be deleted.
62 ///
63 /// # Returns
64 /// - `Result<(), AppError>`:
65 /// - On success, the function returns `Ok(())` indicating that the working order was successfully deleted.
66 /// - On failure, it returns `Err(AppError)` containing the error details that occurred during the deletion process.
67 ///
68 /// # Errors
69 /// This function will return an `AppError` in the following scenarios:
70 /// - If the deletion operation fails due to invalid deal ID.
71 /// - If there are connectivity issues with the database or external services.
72 /// - If the calling user does not have permission to delete the specified working order.
73 ///
74 async fn delete_working_order(&self, deal_id: &str) -> Result<(), AppError>;
75}