Skip to content

Commit

Permalink
refactor on review notes (more)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimxy committed Sep 20, 2024
1 parent 1c31a7a commit 3610147
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
21 changes: 8 additions & 13 deletions mm2src/mm2_main/src/ext_api/one_inch/rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 13 additions & 6 deletions mm2src/mm2_main/src/ext_api/one_inch/types.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -24,9 +25,12 @@ pub struct ClassicSwapQuoteRequest {
pub parts: Option<u32>,
pub main_route_parts: Option<u32>,
pub gas_limit: Option<u128>,
pub include_tokens_info: Option<bool>,
pub include_protocols: Option<bool>,
pub include_gas: Option<bool>,
#[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<String>,
}

Expand All @@ -44,9 +48,12 @@ pub struct ClassicSwapCreateRequest {
pub parts: Option<u32>,
pub main_route_parts: Option<u32>,
pub gas_limit: Option<u128>,
pub include_tokens_info: Option<bool>,
pub include_protocols: Option<bool>,
pub include_gas: Option<bool>,
#[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<String>,
pub permit: Option<String>,
pub receiver: Option<String>,
Expand Down
21 changes: 5 additions & 16 deletions mm2src/trading_api/src/one_inch_api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value, ApiClientError> {
let (status_code, _, body) = slurp_url_with_headers(api_url.as_str(), Self::get_headers())
Expand All @@ -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<ClassicSwapData, ApiClientError> {
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<ClassicSwapData, ApiClientError> {
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()?;

Expand Down

0 comments on commit 3610147

Please sign in to comment.