renegade_sdk/external_match_client/
v1_client.rs

1//! v1-compatible client methods on `ExternalMatchClient`
2//!
3//! These methods accept v1 types and shim them through to the v2 API under the
4//! hood.
5
6use crate::{
7    ExternalMatchOptions, RequestQuoteOptions,
8    api_types::v1_types::{
9        ExternalMatchResponse, ExternalOrder, GetDepthByMintResponse, GetDepthForAllPairsResponse,
10        GetSupportedTokensResponse, GetTokenPricesResponse, SignedExternalQuote,
11    },
12};
13
14use super::{
15    ExternalMatchClient, ExternalMatchClientError,
16    options::AssembleQuoteOptions,
17    v1_conversions::{
18        market_depth_to_v1, market_depths_to_v1, markets_to_supported_tokens,
19        markets_to_token_prices, v1_assemble_options_to_v2, v1_order_to_v2, v1_quote_to_v2,
20        v2_quote_to_v1, v2_response_to_v1_non_malleable,
21    },
22};
23
24impl ExternalMatchClient {
25    // -------------------------
26    // | Market / Token Routes |
27    // -------------------------
28
29    /// Get a list of supported tokens for external matches
30    #[deprecated(
31        since = "2.0.0",
32        note = "Use get_markets instead, which returns all supported tokens along with their current price"
33    )]
34    pub async fn get_supported_tokens(
35        &self,
36    ) -> Result<GetSupportedTokensResponse, ExternalMatchClientError> {
37        let resp = self.get_markets().await?;
38        Ok(markets_to_supported_tokens(&resp))
39    }
40
41    /// Get token prices for all supported tokens
42    #[deprecated(
43        since = "2.0.0",
44        note = "Use get_markets instead, which returns all supported tokens along with their current price"
45    )]
46    pub async fn get_token_prices(
47        &self,
48    ) -> Result<GetTokenPricesResponse, ExternalMatchClientError> {
49        let resp = self.get_markets().await?;
50        markets_to_token_prices(&resp)
51    }
52
53    /// Get the order book depth for a token
54    ///
55    /// The address is the address of the token
56    #[deprecated(since = "2.0.0", note = "Use get_market_depth instead")]
57    pub async fn get_order_book_depth(
58        &self,
59        address: &str,
60    ) -> Result<GetDepthByMintResponse, ExternalMatchClientError> {
61        let resp = self.get_market_depth(address).await?;
62        market_depth_to_v1(&resp)
63    }
64
65    /// Get the order book depth for all supported tokens
66    #[deprecated(since = "2.0.0", note = "Use get_market_depths_all_pairs instead")]
67    pub async fn get_order_book_depth_all_pairs(
68        &self,
69    ) -> Result<GetDepthForAllPairsResponse, ExternalMatchClientError> {
70        let resp = self.get_market_depths_all_pairs().await?;
71        market_depths_to_v1(&resp)
72    }
73
74    // ---------------------
75    // | Quote Routes (v1) |
76    // ---------------------
77
78    /// Request a quote for an external match (v1 API)
79    pub async fn request_quote(
80        &self,
81        order: ExternalOrder,
82    ) -> Result<Option<SignedExternalQuote>, ExternalMatchClientError> {
83        self.request_quote_with_options(order, RequestQuoteOptions::default()).await
84    }
85
86    /// Request a quote for an external match, with options (v1 API)
87    pub async fn request_quote_with_options(
88        &self,
89        order: ExternalOrder,
90        options: RequestQuoteOptions,
91    ) -> Result<Option<SignedExternalQuote>, ExternalMatchClientError> {
92        let v2_order = v1_order_to_v2(&order);
93        let v2_quote = self.request_quote_with_options_v2(v2_order, options).await?;
94        v2_quote.map(|q| v2_quote_to_v1(q, &order)).transpose()
95    }
96
97    /// Assemble a quote into a match bundle, ready for settlement (v1 API)
98    ///
99    /// Returns a non-malleable `ExternalMatchResponse`
100    pub async fn assemble_quote(
101        &self,
102        quote: SignedExternalQuote,
103    ) -> Result<Option<ExternalMatchResponse>, ExternalMatchClientError> {
104        self.assemble_quote_with_options(quote, AssembleQuoteOptions::default()).await
105    }
106
107    /// Assemble a quote into a match bundle with options (v1 API)
108    ///
109    /// Returns a non-malleable `ExternalMatchResponse`
110    pub async fn assemble_quote_with_options(
111        &self,
112        quote: SignedExternalQuote,
113        options: AssembleQuoteOptions,
114    ) -> Result<Option<ExternalMatchResponse>, ExternalMatchClientError> {
115        let direction = quote.quote.order.side.clone();
116        let v2_quote = v1_quote_to_v2(&quote);
117        let v2_options = v1_assemble_options_to_v2(&options);
118        let v2_resp = self.assemble_quote_with_options_v2(v2_quote, v2_options).await?;
119        v2_resp.map(|r| v2_response_to_v1_non_malleable(r, direction)).transpose()
120    }
121
122    // ---------------------
123    // | Match Routes (v1) |
124    // ---------------------
125
126    /// Request an external match (v1 API)
127    ///
128    /// Returns a non-malleable `ExternalMatchResponse`
129    pub async fn request_external_match(
130        &self,
131        order: ExternalOrder,
132    ) -> Result<Option<ExternalMatchResponse>, ExternalMatchClientError> {
133        self.request_external_match_with_options(order, Default::default()).await
134    }
135
136    /// Request an external match with options (v1 API)
137    ///
138    /// Returns a non-malleable `ExternalMatchResponse`
139    pub async fn request_external_match_with_options(
140        &self,
141        order: ExternalOrder,
142        options: ExternalMatchOptions,
143    ) -> Result<Option<ExternalMatchResponse>, ExternalMatchClientError> {
144        let direction = order.side.clone();
145        let v2_order = v1_order_to_v2(&order);
146        let v2_resp = self.request_external_match_with_options_v2(v2_order, options).await?;
147        v2_resp.map(|r| v2_response_to_v1_non_malleable(r, direction)).transpose()
148    }
149}