Skip to content

Commit

Permalink
move walletconnect rpc to mm2_main rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Sep 26, 2024
1 parent 5457d80 commit 9d12079
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 14 deletions.
5 changes: 2 additions & 3 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
pub mod chain;
#[allow(unused)] mod error;
#[allow(unused)] pub mod error;
mod handler;
mod inbound_message;
mod metadata;
#[allow(unused)] mod pairing;
pub mod rpc_commands;

mod session;
pub mod session;
mod storage;

use chain::{build_required_namespaces,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/kdf_walletconnect/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod delete;
pub mod delete;
pub(crate) mod event;
pub(crate) mod extend;
pub(crate) mod ping;
pub mod ping;
pub(crate) mod propose;
pub(crate) mod settle;
pub(crate) mod update;
Expand Down
2 changes: 1 addition & 1 deletion mm2src/kdf_walletconnect/src/session/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) async fn reply_session_delete_request(
session_delete_cleanup(ctx, topic).await
}

pub(crate) async fn send_session_delete_request(
pub async fn send_session_delete_request(
ctx: &WalletConnectCtx,
session_topic: &Topic,
) -> MmResult<(), WalletConnectCtxError> {
Expand Down
5 changes: 1 addition & 4 deletions mm2src/kdf_walletconnect/src/session/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ pub(crate) async fn reply_session_ping_request(
Ok(())
}

pub(crate) async fn send_session_ping_request(
ctx: &WalletConnectCtx,
topic: &Topic,
) -> MmResult<(), WalletConnectCtxError> {
pub async fn send_session_ping_request(ctx: &WalletConnectCtx, topic: &Topic) -> MmResult<(), WalletConnectCtxError> {
let param = RequestParams::SessionPing(());
ctx.publish_request(topic, param).await?;

Expand Down
1 change: 1 addition & 0 deletions mm2src/mm2_main/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod dispatcher_legacy;
#[path = "rpc/lp_commands/lp_commands_legacy.rs"]
pub mod lp_commands_legacy;
mod rate_limiter;
#[path = "rpc/wc_commands/wc_commands.rs"] pub mod wc_commands;

/// Lists the RPC method not requiring the "userpass" authentication.
/// None is also public to skip auth and display proper error in case of method is missing
Expand Down
5 changes: 1 addition & 4 deletions mm2src/mm2_main/src/rpc/dispatcher/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::lp_ordermatch::{best_orders_rpc_v2, orderbook_rpc_v2, start_simple_ma
use crate::lp_swap::swap_v2_rpcs::{active_swaps_rpc, my_recent_swaps_rpc, my_swap_status_rpc};
use crate::lp_wallet::get_mnemonic_rpc;
use crate::rpc::rate_limiter::{process_rate_limit, RateLimitContext};
use crate::rpc::wc_commands::{delete_connection, get_chain_id, get_session, new_connection, ping_session};
use crate::{lp_stats::{add_node_to_version_stat, remove_node_from_version_stat, start_version_stat_collection,
stop_version_stat_collection, update_version_stat_collection},
lp_swap::{get_locked_amount_rpc, max_maker_vol, recreate_swap_data, trade_preimage_rpc},
Expand Down Expand Up @@ -54,7 +55,6 @@ use common::log::{error, warn};
use common::HttpStatusCode;
use futures::Future as Future03;
use http::Response;
use kdf_walletconnect::rpc_commands::{delete_connection, get_chain_id, get_session, new_connection, ping_session};
use mm2_core::data_asker::send_asked_data_rpc;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
Expand Down Expand Up @@ -161,9 +161,6 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult<Re
return lightning_dispatcher(request, ctx, &lightning_method).await;
}

// WalletConnect Requests
if let Some(method) = request.method.strip_prefix("wc::") {}

match request.method.as_str() {
"account_balance" => handle_mmrpc(ctx, request, account_balance).await,
"active_swaps" => handle_mmrpc(ctx, request, active_swaps_rpc).await,
Expand Down
30 changes: 30 additions & 0 deletions mm2src/mm2_main/src/rpc/wc_commands/delete_connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use kdf_walletconnect::{session::delete::send_session_delete_request, WalletConnectCtx};
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use serde::{Deserialize, Serialize};

use super::WalletConnectRpcError;

#[derive(Debug, PartialEq, Serialize)]
pub struct DeleteConnectionResponse {
pub successful: bool,
}

#[derive(Deserialize)]
pub struct DeleteConnectionRequest {
topic: String,
}

/// `delete connection` RPC command implementation.
pub async fn delete_connection(
ctx: MmArc,
req: DeleteConnectionRequest,
) -> MmResult<DeleteConnectionResponse, WalletConnectRpcError> {
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?;
send_session_delete_request(&ctx, &req.topic.into())
.await
.mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?;

Ok(DeleteConnectionResponse { successful: true })
}
20 changes: 20 additions & 0 deletions mm2src/mm2_main/src/rpc/wc_commands/get_chain_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use kdf_walletconnect::WalletConnectCtx;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use serde::Serialize;

use super::{EmptyRpcRequst, WalletConnectRpcError};

#[derive(Debug, PartialEq, Serialize)]
pub struct GetChainIdResponse {
pub chain_id: String,
}

/// `delete connection` RPC command implementation.
pub async fn get_chain_id(ctx: MmArc, _req: EmptyRpcRequst) -> MmResult<GetChainIdResponse, WalletConnectRpcError> {
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?;
let chain_id = ctx.get_active_chain_id().await;

Ok(GetChainIdResponse { chain_id })
}
20 changes: 20 additions & 0 deletions mm2src/mm2_main/src/rpc/wc_commands/get_session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use kdf_walletconnect::{session::Session, WalletConnectCtx};
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use serde::Serialize;

use super::{EmptyRpcRequst, WalletConnectRpcError};

#[derive(Debug, PartialEq, Serialize)]
pub struct GetSessionResponse {
pub session: Option<Session>,
}

/// `delete connection` RPC command implementation.
pub async fn get_session(ctx: MmArc, _req: EmptyRpcRequst) -> MmResult<GetSessionResponse, WalletConnectRpcError> {
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?;
let session = ctx.get_session().await;

Ok(GetSessionResponse { session })
}
26 changes: 26 additions & 0 deletions mm2src/mm2_main/src/rpc/wc_commands/new_connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use kdf_walletconnect::WalletConnectCtx;
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use serde::Serialize;

use super::{EmptyRpcRequst, WalletConnectRpcError};

#[derive(Debug, PartialEq, Serialize)]
pub struct CreateConnectionResponse {
pub url: String,
}

/// `new_connection` RPC command implementation.
pub async fn new_connection(
ctx: MmArc,
_req: EmptyRpcRequst,
) -> MmResult<CreateConnectionResponse, WalletConnectRpcError> {
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?;
let url = ctx
.new_connection(None)
.await
.mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?;

Ok(CreateConnectionResponse { url })
}
27 changes: 27 additions & 0 deletions mm2src/mm2_main/src/rpc/wc_commands/ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use kdf_walletconnect::{session::ping::send_session_ping_request, WalletConnectCtx};
use mm2_core::mm_ctx::MmArc;
use mm2_err_handle::prelude::*;
use serde::{Deserialize, Serialize};

use super::WalletConnectRpcError;

#[derive(Debug, PartialEq, Serialize)]
pub struct SessionPingResponse {
pub successful: bool,
}

#[derive(Deserialize)]
pub struct SessionPingRequest {
topic: String,
}

/// `ping session` RPC command implementation.
pub async fn ping_session(ctx: MmArc, req: SessionPingRequest) -> MmResult<SessionPingResponse, WalletConnectRpcError> {
let ctx = WalletConnectCtx::try_from_ctx_or_initialize(&ctx)
.mm_err(|err| WalletConnectRpcError::InitializationError(err.to_string()))?;
send_session_ping_request(&ctx, &req.topic.into())
.await
.mm_err(|err| WalletConnectRpcError::SessionRequestError(err.to_string()))?;

Ok(SessionPingResponse { successful: true })
}
37 changes: 37 additions & 0 deletions mm2src/mm2_main/src/rpc/wc_commands/wc_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
mod delete_connection;
mod get_chain_id;
mod get_session;
mod new_connection;
mod ping;

use common::HttpStatusCode;
pub use delete_connection::delete_connection;
use derive_more::Display;
pub use get_chain_id::get_chain_id;
pub use get_session::get_session;
use http::StatusCode;
pub use new_connection::new_connection;
pub use ping::ping_session;
use serde::Deserialize;

#[derive(Deserialize)]
pub struct EmptyRpcRequst {}

#[derive(Serialize, Display, SerializeErrorType)]
#[serde(tag = "error_type", content = "error_data")]
pub enum WalletConnectRpcError {
InternalError(String),
InitializationError(String),
SessionRequestError(String),
}

impl HttpStatusCode for WalletConnectRpcError {
fn status_code(&self) -> StatusCode {
match self {
WalletConnectRpcError::InitializationError(_) => StatusCode::BAD_REQUEST,
WalletConnectRpcError::SessionRequestError(_) | WalletConnectRpcError::InternalError(_) => {
StatusCode::INTERNAL_SERVER_ERROR
},
}
}
}

0 comments on commit 9d12079

Please sign in to comment.