From 3610147ad88ce67926cb1ae32bc2ea71eb01c7ac Mon Sep 17 00:00:00 2001 From: dimxy Date: Fri, 20 Sep 2024 19:04:24 +0500 Subject: [PATCH] refactor on review notes (more) --- mm2src/mm2_main/src/ext_api/one_inch/rpcs.rs | 21 +++++++------------ mm2src/mm2_main/src/ext_api/one_inch/types.rs | 19 +++++++++++------ mm2src/trading_api/src/one_inch_api/client.rs | 21 +++++-------------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/mm2src/mm2_main/src/ext_api/one_inch/rpcs.rs b/mm2src/mm2_main/src/ext_api/one_inch/rpcs.rs index bc7059a9da..b860a86d84 100644 --- a/mm2src/mm2_main/src/ext_api/one_inch/rpcs.rs +++ b/mm2src/mm2_main/src/ext_api/one_inch/rpcs.rs @@ -7,11 +7,6 @@ use mm2_err_handle::prelude::*; use trading_api::one_inch_api::client::ApiClient; use trading_api::one_inch_api::types::{ClassicSwapCreateParams, ClassicSwapQuoteParams}; -// Default swap optional params: -const INCLUDE_GAS: bool = true; -const INCLUDE_PROTOCOLS: bool = true; -const INCLUDE_TOKENS_INFO: bool = true; - /// "1inch_v6_0_classic_swap_contract" rpc impl /// used to get contract address (for e.g. to approve funds) pub async fn one_inch_v6_0_classic_swap_contract_rpc( @@ -39,15 +34,15 @@ pub async fn one_inch_v6_0_classic_swap_quote_rpc( .with_parts(req.parts) .with_main_route_parts(req.main_route_parts) .with_gas_limit(req.gas_limit) - .with_include_tokens_info(Some(req.include_tokens_info.unwrap_or(INCLUDE_TOKENS_INFO))) - .with_include_protocols(Some(req.include_protocols.unwrap_or(INCLUDE_PROTOCOLS))) - .with_include_gas(Some(req.include_gas.unwrap_or(INCLUDE_GAS))) + .with_include_tokens_info(Some(req.include_tokens_info)) + .with_include_protocols(Some(req.include_protocols)) + .with_include_gas(Some(req.include_gas)) .with_connector_tokens(req.connector_tokens) .build_query_params() .mm_err(|api_err| ApiIntegrationRpcError::from_api_error(api_err, base.decimals()))?; let quote = ApiClient::new(ctx) .mm_err(|api_err| ApiIntegrationRpcError::from_api_error(api_err, base.decimals()))? - .get_classic_swap_quote(base.chain_id(), query_params) + .call_swap_api(base.chain_id(), ApiClient::get_quote_method().to_owned(), query_params) .await .mm_err(|api_err| ApiIntegrationRpcError::from_api_error(api_err, base.decimals()))?; // use 'base' as amount in errors is in the src coin ClassicSwapResponse::from_api_value(quote, rel.decimals()) // use 'rel' as quote value is in the dst coin @@ -83,9 +78,9 @@ pub async fn one_inch_v6_0_classic_swap_create_rpc( .with_parts(req.parts) .with_main_route_parts(req.main_route_parts) .with_gas_limit(req.gas_limit) - .with_include_tokens_info(Some(req.include_tokens_info.unwrap_or(INCLUDE_TOKENS_INFO))) - .with_include_protocols(Some(req.include_protocols.unwrap_or(INCLUDE_PROTOCOLS))) - .with_include_gas(Some(req.include_gas.unwrap_or(INCLUDE_GAS))) + .with_include_tokens_info(Some(req.include_tokens_info)) + .with_include_protocols(Some(req.include_protocols)) + .with_include_gas(Some(req.include_gas)) .with_connector_tokens(req.connector_tokens) .with_permit(req.permit) .with_receiver(req.receiver) @@ -96,7 +91,7 @@ pub async fn one_inch_v6_0_classic_swap_create_rpc( .mm_err(|api_err| ApiIntegrationRpcError::from_api_error(api_err, base.decimals()))?; let swap_with_tx = ApiClient::new(ctx) .mm_err(|api_err| ApiIntegrationRpcError::from_api_error(api_err, base.decimals()))? - .get_classic_swap_tx(base.chain_id(), query_params) + .call_swap_api(base.chain_id(), ApiClient::get_swap_method().to_owned(), query_params) .await .mm_err(|api_err| ApiIntegrationRpcError::from_api_error(api_err, base.decimals()))?; // use 'base' as amount in errors is in the src coin ClassicSwapResponse::from_api_value(swap_with_tx, base.decimals()) // use 'base' as we spend in the src coin diff --git a/mm2src/mm2_main/src/ext_api/one_inch/types.rs b/mm2src/mm2_main/src/ext_api/one_inch/types.rs index 198fbbdd34..7bf5d61f1d 100644 --- a/mm2src/mm2_main/src/ext_api/one_inch/types.rs +++ b/mm2src/mm2_main/src/ext_api/one_inch/types.rs @@ -1,5 +1,6 @@ use crate::ext_api::one_inch::errors::FromApiValueError; use coins::eth::{u256_to_big_decimal, wei_to_gwei_decimal}; +use common::true_f; use ethereum_types::{Address, U256}; use mm2_err_handle::prelude::*; use mm2_number::{BigDecimal, MmNumber}; @@ -24,9 +25,12 @@ pub struct ClassicSwapQuoteRequest { pub parts: Option, pub main_route_parts: Option, pub gas_limit: Option, - pub include_tokens_info: Option, - pub include_protocols: Option, - pub include_gas: Option, + #[serde(default = "true_f")] + pub include_tokens_info: bool, + #[serde(default = "true_f")] + pub include_protocols: bool, + #[serde(default = "true_f")] + pub include_gas: bool, pub connector_tokens: Option, } @@ -44,9 +48,12 @@ pub struct ClassicSwapCreateRequest { pub parts: Option, pub main_route_parts: Option, pub gas_limit: Option, - pub include_tokens_info: Option, - pub include_protocols: Option, - pub include_gas: Option, + #[serde(default = "true_f")] + pub include_tokens_info: bool, + #[serde(default = "true_f")] + pub include_protocols: bool, + #[serde(default = "true_f")] + pub include_gas: bool, pub connector_tokens: Option, pub permit: Option, pub receiver: Option, diff --git a/mm2src/trading_api/src/one_inch_api/client.rs b/mm2src/trading_api/src/one_inch_api/client.rs index a220c30339..d7b901913a 100644 --- a/mm2src/trading_api/src/one_inch_api/client.rs +++ b/mm2src/trading_api/src/one_inch_api/client.rs @@ -132,9 +132,9 @@ impl ApiClient { fn get_swap_endpoint() -> &'static str { ONE_INCH_API_ENDPOINT_V6_0 } - pub(crate) const fn get_swap_method() -> &'static str { SWAP_METHOD } + pub const fn get_swap_method() -> &'static str { SWAP_METHOD } - pub(crate) const fn get_quote_method() -> &'static str { QUOTE_METHOD } + pub const fn get_quote_method() -> &'static str { QUOTE_METHOD } pub(crate) async fn call_api(api_url: Url) -> MmResult { let (status_code, _, body) = slurp_url_with_headers(api_url.as_str(), Self::get_headers()) @@ -148,24 +148,13 @@ impl ApiClient { Ok(body) } - pub async fn get_classic_swap_quote( + pub async fn call_swap_api( &self, chain_id: u64, + method: String, params: QueryParams<'_>, ) -> MmResult { - let api_url = UrlBuilder::new(self, chain_id, ApiClient::get_quote_method().to_owned()) - .with_query_params(params) - .build()?; - let value = Self::call_api(api_url).await?; - serde_json::from_value(value).map_err(|err| ApiClientError::ParseBodyError(err.to_string()).into()) - } - - pub async fn get_classic_swap_tx( - &self, - chain_id: u64, - params: QueryParams<'_>, - ) -> MmResult { - let api_url = UrlBuilder::new(self, chain_id, ApiClient::get_swap_method().to_owned()) + let api_url = UrlBuilder::new(self, chain_id, method) .with_query_params(params) .build()?;