Skip to main content

bpx_api_client/routes/
rfq.rs

1use bpx_api_types::rfq::{
2    OpenRfqsQuery, Quote, QuoteAcceptPayload, QuotePayload, RequestForQuote,
3    RequestForQuoteCancelPayload, RequestForQuotePayload, RequestForQuoteRefreshPayload,
4    RfqWithQuotes,
5};
6
7#[cfg(feature = "ws")]
8use bpx_api_types::rfq::RequestForQuoteUpdate;
9#[cfg(feature = "ws")]
10use tokio::sync::mpsc::Sender;
11
12use crate::BpxClient;
13use crate::error::{Error, Result};
14
15#[doc(hidden)]
16pub const API_RFQ: &str = "/api/v1/rfq";
17#[doc(hidden)]
18pub const API_RFQS: &str = "/api/v1/rfqs";
19#[doc(hidden)]
20pub const API_RFQ_QUOTE: &str = "/api/v1/rfq/quote";
21#[doc(hidden)]
22pub const API_RFQ_CANCEL: &str = "/api/v1/rfq/cancel";
23#[doc(hidden)]
24pub const API_RFQ_REFRESH: &str = "/api/v1/rfq/refresh";
25#[doc(hidden)]
26pub const API_RFQ_ACCEPT: &str = "/api/v1/rfq/accept";
27
28#[cfg(feature = "ws")]
29const API_RFQ_STREAM: &str = "account.rfqUpdate";
30
31impl BpxClient {
32    /// Fetches the account's open RFQs and their quotes.
33    pub async fn get_open_rfqs(&self, params: OpenRfqsQuery) -> Result<Vec<RfqWithQuotes>> {
34        let mut url = self.base_url.join(API_RFQS)?;
35        let query_string = serde_qs::to_string(&params)
36            .map_err(|e| Error::UrlParseError(e.to_string().into_boxed_str()))?;
37        if !query_string.is_empty() {
38            url.set_query(Some(&query_string));
39        }
40        let res = self.get(url).await?;
41        res.json().await.map_err(Into::into)
42    }
43
44    /// Submits an RFQ. `order_expiry_time` makes it a resting RFQ.
45    pub async fn submit_rfq(&self, payload: RequestForQuotePayload) -> Result<RequestForQuote> {
46        let endpoint = self.base_url.join(API_RFQ)?;
47        let res = self.post(endpoint, payload).await?;
48        res.json().await.map_err(Into::into)
49    }
50
51    /// Cancels an RFQ. A bound resting RFQ is only marked (`cancel_requested_at`)
52    /// and stays active until the quoter or system settles or cancels it.
53    pub async fn cancel_rfq(
54        &self,
55        payload: RequestForQuoteCancelPayload,
56    ) -> Result<RequestForQuote> {
57        let endpoint = self.base_url.join(API_RFQ_CANCEL)?;
58        let res = self.post(endpoint, payload).await?;
59        res.json().await.map_err(Into::into)
60    }
61
62    pub async fn refresh_rfq(
63        &self,
64        payload: RequestForQuoteRefreshPayload,
65    ) -> Result<RequestForQuote> {
66        let endpoint = self.base_url.join(API_RFQ_REFRESH)?;
67        let res = self.post(endpoint, payload).await?;
68        res.json().await.map_err(Into::into)
69    }
70
71    pub async fn accept_quote(&self, payload: QuoteAcceptPayload) -> Result<RequestForQuote> {
72        let endpoint = self.base_url.join(API_RFQ_ACCEPT)?;
73        let res = self.post(endpoint, payload).await?;
74        res.json().await.map_err(Into::into)
75    }
76
77    pub async fn submit_quote(&self, payload: QuotePayload) -> Result<Quote> {
78        let endpoint = self.base_url.join(API_RFQ_QUOTE)?;
79        let res = self.post(endpoint, payload).await?;
80        res.json().await.map_err(Into::into)
81    }
82
83    #[cfg(feature = "ws")]
84    pub async fn subscribe_to_rfqs(&self, tx: Sender<RequestForQuoteUpdate>) -> Result<()> {
85        self.subscribe(API_RFQ_STREAM, tx).await
86    }
87}