Skip to content

Commit

Permalink
implement bare types for websocket transport
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 Jan 23, 2024
1 parent 6dbd85b commit fbf0dad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mm2src/coins/eth/web3_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ use web3::types::BlockNumber;
use web3::{Error, RequestId, Transport};

pub(crate) mod http_transport;
pub(crate) mod websocket_transport;
#[cfg(target_arch = "wasm32")] pub(crate) mod metamask_transport;

type Web3SendOut = BoxFuture<'static, Result<Json, Error>>;

#[derive(Clone, Debug)]
pub(crate) enum Web3Transport {
Http(http_transport::HttpTransport),
#[allow(dead_code)] // TODO: remove this
Websocket(websocket_transport::WebsocketTransport),
#[cfg(target_arch = "wasm32")]
Metamask(metamask_transport::MetamaskTransport),
}
Expand Down Expand Up @@ -52,6 +55,8 @@ impl Web3Transport {
pub fn gui_auth_validation_generator_as_mut(&mut self) -> Option<&mut GuiAuthValidationGenerator> {
match self {
Web3Transport::Http(http) => http.gui_auth_validation_generator.as_mut(),
// TODO
Web3Transport::Websocket(_) => None,
#[cfg(target_arch = "wasm32")]
Web3Transport::Metamask(_) => None,
}
Expand All @@ -64,6 +69,7 @@ impl Transport for Web3Transport {
fn prepare(&self, method: &str, params: Vec<Value>) -> (RequestId, Call) {
match self {
Web3Transport::Http(http) => http.prepare(method, params),
Web3Transport::Websocket(websocket) => websocket.prepare(method, params),
#[cfg(target_arch = "wasm32")]
Web3Transport::Metamask(metamask) => metamask.prepare(method, params),
}
Expand All @@ -72,6 +78,7 @@ impl Transport for Web3Transport {
fn send(&self, id: RequestId, request: Call) -> Self::Out {
match self {
Web3Transport::Http(http) => http.send(id, request),
Web3Transport::Websocket(websocket) => websocket.send(id, request),
#[cfg(target_arch = "wasm32")]
Web3Transport::Metamask(metamask) => metamask.send(id, request),
}
Expand Down
34 changes: 34 additions & 0 deletions mm2src/coins/eth/web3_transport/websocket_transport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#![allow(unused)] // TODO: remove this

use crate::eth::web3_transport::Web3SendOut;
use futures::lock::Mutex as AsyncMutex;
use jsonrpc_core::Call;
use std::sync::Arc;
use web3::{RequestId, Transport};

#[derive(Clone, Debug)]
pub struct WebsocketTransportNode {
pub(crate) uri: http::Uri,
pub(crate) gui_auth: bool,
}

#[derive(Debug)]
struct WebsocketTransportRpcClient(AsyncMutex<WebsocketTransportRpcClientImpl>);

#[derive(Debug)]
struct WebsocketTransportRpcClientImpl {
nodes: Vec<WebsocketTransportNode>,
}

#[derive(Clone, Debug)]
pub struct WebsocketTransport {
client: Arc<WebsocketTransportRpcClient>,
}

impl Transport for WebsocketTransport {
type Out = Web3SendOut;

fn prepare(&self, method: &str, params: Vec<serde_json::Value>) -> (RequestId, Call) { todo!() }

fn send(&self, id: RequestId, request: Call) -> Self::Out { todo!() }
}

0 comments on commit fbf0dad

Please sign in to comment.