Skip to content

Commit

Permalink
apply various improvements
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Feb 22, 2024
1 parent df48248 commit 665dc2f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
11 changes: 9 additions & 2 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// Copyright © 2023 Pampex LTD and TillyHK LTD. All rights reserved.
//
use super::eth::Action::{Call, Create};
use crate::eth::eth_rpc::ETH_RPC_REQUEST_TIMEOUT;
use crate::eth::web3_transport::websocket_transport::{WebsocketTransport, WebsocketTransportNode};
use crate::lp_price::get_base_price_in_rel;
use crate::nft::nft_structs::{ContractType, ConvertChain, TransactionNftDetails, WithdrawErc1155, WithdrawErc721};
Expand Down Expand Up @@ -2535,7 +2536,7 @@ impl RpcCommonOps for EthCoin {
.web3
.web3()
.client_version()
.timeout(Duration::from_secs(15))
.timeout(ETH_RPC_REQUEST_TIMEOUT)
.await
{
Ok(Ok(_)) => {
Expand Down Expand Up @@ -5738,11 +5739,17 @@ pub async fn eth_coin_from_conf_and_request(

Web3Transport::Websocket(websocket_transport)
},
_ => {
Some("http") | Some("https") => {
let node = HttpTransportNode { uri, gui_auth: false };

Web3Transport::new_http_with_event_handlers(node, event_handlers.clone())
},
_ => {
return ERR!(
"Invalid node address '{}'. Only http(s) and ws(s) nodes are supported",
uri
);
},
};

let web3 = Web3::new(transport);
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/eth/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use web3::types::{Address, Block, BlockId, BlockNumber, Bytes, CallRequest, FeeH
H520, H64, U256, U64};
use web3::{helpers, Transport};

pub(crate) const ETH_RPC_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);

impl EthCoin {
async fn try_rpc_send(&self, method: &str, params: Vec<jsonrpc_core::Value>) -> Result<Value, web3::Error> {
let mut clients = self.web3_instances.lock().await;
Expand All @@ -27,7 +29,7 @@ impl EthCoin {
Web3Transport::Metamask(metamask) => metamask.execute(method, params.clone()),
};

match execute_fut.timeout(Duration::from_secs(15)).await {
match execute_fut.timeout(ETH_RPC_REQUEST_TIMEOUT).await {
Ok(Ok(r)) => {
// Bring the live client to the front of rpc_clients
clients.rotate_left(i);
Expand Down
9 changes: 7 additions & 2 deletions mm2src/coins/eth/v2_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use instant::Instant;
use mm2_err_handle::common_errors::WithInternal;
#[cfg(target_arch = "wasm32")]
use mm2_metamask::{from_metamask_error, MetamaskError, MetamaskRpcError, WithMetamaskRpcError};
use v2_activation::web3_transport::websocket_transport::WebsocketTransport;
use web3_transport::websocket_transport::WebsocketTransport;

#[derive(Clone, Debug, Deserialize, Display, EnumFromTrait, PartialEq, Serialize, SerializeErrorType)]
#[serde(tag = "error_type", content = "error_data")]
Expand Down Expand Up @@ -440,7 +440,7 @@ async fn build_web3_instances(

Web3Transport::Websocket(websocket_transport)
},
_ => {
Some("http") | Some("https") => {
let node = HttpTransportNode {
uri,
gui_auth: eth_node.gui_auth,
Expand All @@ -454,6 +454,11 @@ async fn build_web3_instances(
event_handlers.clone(),
)
},
_ => {
return MmError::err(EthActivationV2Error::InvalidPayload(format!(
"Invalid node address '{uri}'. Only http(s) and ws(s) nodes are supported"
)));
},
};

let web3 = Web3::new(transport);
Expand Down
5 changes: 1 addition & 4 deletions mm2src/coins/eth/web3_transport/metamask_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ impl MetamaskTransport {
async fn send_impl(&self, id: RequestId, request: Call) -> Result<Json, web3::Error> {
// Hold the mutex guard until the request is finished.
let _rpc_lock = self.request_preparation().await?;
match self.inner.eip1193.send(id, request).await {
Ok(t) => Ok(t),
Err(e) => Err(e),
}
self.inner.eip1193.send(id, request).await
}

/// Ensures that the MetaMask wallet is targeted to [`EthConfig::chain_id`].
Expand Down

0 comments on commit 665dc2f

Please sign in to comment.