Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
* Rename `task::init_metamask::*` to `task::connect_metamask::*`
  • Loading branch information
sergeyboyko0791 committed Nov 23, 2022
1 parent bf3fa88 commit 3ef732e
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 48 deletions.
22 changes: 3 additions & 19 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ use serialization::{CompactInteger, Serializable, Stream};
use sha3::{Digest, Keccak256};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt;
use std::ops::Deref;
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -345,33 +344,18 @@ pub enum EthPrivKeyPolicy {
Metamask(MetamaskWeak),
}

impl fmt::Debug for EthPrivKeyPolicy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EthPrivKeyPolicy::KeyPair(_) => write!(f, "KeyPair"),
#[cfg(target_arch = "wasm32")]
EthPrivKeyPolicy::Metamask(_) => write!(f, "Metamask"),
}
}
}

impl From<KeyPair> for EthPrivKeyPolicy {
fn from(key_pair: KeyPair) -> Self { EthPrivKeyPolicy::KeyPair(key_pair) }
}

impl EthPrivKeyPolicy {
pub fn key_pair(&self) -> Option<&KeyPair> {
pub fn key_pair_or_err(&self) -> MmResult<&KeyPair, PrivKeyPolicyNotAllowed> {
match self {
EthPrivKeyPolicy::KeyPair(key_pair) => Some(key_pair),
EthPrivKeyPolicy::KeyPair(key_pair) => Ok(key_pair),
#[cfg(target_arch = "wasm32")]
EthPrivKeyPolicy::Metamask(_) => None,
EthPrivKeyPolicy::Metamask(_) => MmError::err(PrivKeyPolicyNotAllowed::HardwareWalletNotSupported),
}
}

pub fn key_pair_or_err(&self) -> MmResult<&KeyPair, PrivKeyPolicyNotAllowed> {
self.key_pair()
.or_mm_err(|| PrivKeyPolicyNotAllowed::HardwareWalletNotSupported)
}
}

/// pImpl idiom.
Expand Down
8 changes: 3 additions & 5 deletions mm2src/coins/eth/v2_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ pub enum EthPrivKeyActivationPolicy {
Metamask,
}

impl EthPrivKeyActivationPolicy {
/// The function can be used as a default deserialization constructor:
/// `#[serde(default = "EthPrivKeyActivationPolicy::context_priv_key")]`
pub fn context_priv_key() -> EthPrivKeyActivationPolicy { EthPrivKeyActivationPolicy::ContextPrivKey }
impl Default for EthPrivKeyActivationPolicy {
fn default() -> Self { EthPrivKeyActivationPolicy::ContextPrivKey }
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -79,7 +77,7 @@ pub struct EthActivationV2Request {
pub gas_station_policy: GasStationPricePolicy,
pub mm2: Option<u8>,
pub required_confirmations: Option<u64>,
#[serde(default = "EthPrivKeyActivationPolicy::context_priv_key")]
#[serde(default)]
pub priv_key_policy: EthPrivKeyActivationPolicy,
}

Expand Down
7 changes: 3 additions & 4 deletions mm2src/coins/eth/web3_transport/metamask_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use web3::error::{Error, ErrorKind};
use web3::helpers::build_request;
use web3::{RequestId, Transport};

const REQUEST_ID: RequestId = 0;

#[derive(Clone)]
pub struct MetamaskTransport {
metamask_ctx: MetamaskWeak,
Expand Down Expand Up @@ -60,9 +58,10 @@ impl Transport for MetamaskTransport {
type Out = Web3SendOut;

fn prepare(&self, method: &str, params: Vec<Json>) -> (RequestId, Call) {
let request = build_request(REQUEST_ID, method, params);

// RequestId doesn't make sense for `MetamaskProvider`.
const REQUEST_ID: RequestId = 0;

let request = build_request(REQUEST_ID, method, params);
(REQUEST_ID, request)
}

Expand Down
1 change: 0 additions & 1 deletion mm2src/coins/eth/web3_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ impl Web3Transport {
}

#[cfg(test)]
#[inline]
pub fn with_nodes(nodes: Vec<http_transport::HttpTransportNode>) -> Web3Transport {
http_transport::HttpTransport::new(nodes).into()
}
Expand Down
10 changes: 2 additions & 8 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2196,14 +2196,8 @@ pub enum PrivKeyActivationPolicy {
Trezor,
}

impl PrivKeyActivationPolicy {
/// The function can be used as a default deserialization constructor:
/// `#[serde(default = "PrivKeyActivationPolicy::context_priv_key")]`
pub fn context_priv_key() -> PrivKeyActivationPolicy { PrivKeyActivationPolicy::ContextPrivKey }

/// The function can be used as a default deserialization constructor:
/// `#[serde(default = "PrivKeyActivationPolicy::trezor")]`
pub fn trezor() -> PrivKeyActivationPolicy { PrivKeyActivationPolicy::Trezor }
impl Default for PrivKeyActivationPolicy {
fn default() -> Self { PrivKeyActivationPolicy::ContextPrivKey }
}

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ pub struct UtxoActivationParams {
pub gap_limit: Option<u32>,
#[serde(flatten)]
pub enable_params: EnabledCoinBalanceParams,
#[serde(default = "PrivKeyActivationPolicy::context_priv_key")]
#[serde(default)]
pub priv_key_policy: PrivKeyActivationPolicy,
/// The flag determines whether to use mature unspent outputs *only* to generate transactions.
/// https://github.com/KomodoPlatform/atomicDEX-API/issues/1181
Expand Down
9 changes: 6 additions & 3 deletions mm2src/mm2_main/src/lp_init/init_metamask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,18 @@ impl RpcTask for InitMetamaskTask {
}
}

pub async fn init_metamask(ctx: MmArc, req: InitMetamaskRequest) -> MmResult<InitRpcTaskResponse, InitMetamaskError> {
pub async fn connect_metamask(
ctx: MmArc,
req: InitMetamaskRequest,
) -> MmResult<InitRpcTaskResponse, InitMetamaskError> {
let init_ctx = MmInitContext::from_ctx(&ctx).map_to_mm(InitMetamaskError::Internal)?;
let spawner = ctx.spawner();
let task = InitMetamaskTask { ctx, req };
let task_id = RpcTaskManager::spawn_rpc_task(&init_ctx.init_metamask_manager, &spawner, task)?;
Ok(InitRpcTaskResponse { task_id })
}

pub async fn init_metamask_status(
pub async fn connect_metamask_status(
ctx: MmArc,
req: RpcTaskStatusRequest,
) -> MmResult<InitMetamaskStatus, RpcTaskStatusError> {
Expand All @@ -152,7 +155,7 @@ pub async fn init_metamask_status(
.or_mm_err(|| RpcTaskStatusError::NoSuchTask(req.task_id))
}

pub async fn cancel_init_metamask(
pub async fn cancel_connect_metamask(
ctx: MmArc,
req: CancelRpcTaskRequest,
) -> MmResult<SuccessResponse, CancelRpcTaskError> {
Expand Down
8 changes: 4 additions & 4 deletions mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{DispatcherError, DispatcherResult, PUBLIC_METHODS};
use crate::mm2::lp_native_dex::init_hw::{cancel_init_trezor, init_trezor, init_trezor_status, init_trezor_user_action};
#[cfg(target_arch = "wasm32")]
use crate::mm2::lp_native_dex::init_metamask::{cancel_init_metamask, init_metamask, init_metamask_status};
use crate::mm2::lp_native_dex::init_metamask::{cancel_connect_metamask, connect_metamask, connect_metamask_status};
use crate::mm2::lp_ordermatch::{best_orders_rpc_v2, orderbook_rpc_v2, start_simple_market_maker_bot,
stop_simple_market_maker_bot};
use crate::mm2::rpc::rate_limiter::{process_rate_limit, RateLimitContext};
Expand Down Expand Up @@ -247,9 +247,9 @@ async fn rpc_task_dispatcher(
},
#[cfg(target_arch = "wasm32")]
wasm_only_methods => match wasm_only_methods {
"init_metamask::cancel" => handle_mmrpc(ctx, request, cancel_init_metamask).await,
"init_metamask::init" => handle_mmrpc(ctx, request, init_metamask).await,
"init_metamask::status" => handle_mmrpc(ctx, request, init_metamask_status).await,
"connect_metamask::cancel" => handle_mmrpc(ctx, request, cancel_connect_metamask).await,
"connect_metamask::init" => handle_mmrpc(ctx, request, connect_metamask).await,
"connect_metamask::status" => handle_mmrpc(ctx, request, connect_metamask_status).await,
_ => MmError::err(DispatcherError::NoSuchMethod),
},
}
Expand Down
3 changes: 0 additions & 3 deletions mm2src/mm2_metamask/src/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ impl EthProvider {
}
}

// pub async fn detect_ethereum_provider(must_be_metamask: bool, timeout_s: f64) -> {
// }

#[wasm_bindgen]
extern "C" {
/// An EIP-1193 provider object. Available by convention at `window.ethereum`.
Expand Down

0 comments on commit 3ef732e

Please sign in to comment.