Skip to content

Commit

Permalink
feat(rpc): extract http client adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-melnychuk committed Aug 29, 2024
1 parent 2f646d3 commit 8492ee7
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 1,158 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ reqwest = { version = "0.12.3", default-features = false, features = [
"json",
"blocking",
] }
ureq = { version = "2.10.1", features = ["json"], default-features = false }
wasm-timer = "0.2.5"
web-sys = "0.3.69"
wasm-bindgen = "0.2.92"
Expand Down
51 changes: 47 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tokio::sync::RwLock;

use crate::eth::{EthereumClient, Helios};
use crate::gen::client::Client as StarknetClient;
use crate::gen::{BlockId, Felt, Rpc};
use crate::gen::{gen, BlockId, Felt, Rpc};
use crate::{config::Config, gen::FunctionCall};

#[derive(Debug, Clone)]
Expand All @@ -15,14 +15,57 @@ pub struct State {
pub root: Felt,
}

#[derive(Clone)]
pub struct AsyncHttp(pub reqwest::Client);

#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
impl gen::client::HttpClient for AsyncHttp {
async fn post(&self, url: &str, request: &iamgroot::jsonrpc::Request) -> std::result::Result<iamgroot::jsonrpc::Response, iamgroot::jsonrpc::Error> {
let response = self.0.post(url)
.json(&request)
.send()
.await
.map_err(|e| iamgroot::jsonrpc::Error::new(0, format!("LOL: {e:?}")))?
.json()
.await
.map_err(|e| iamgroot::jsonrpc::Error::new(0, format!("LOL: {e:?}")))?;
Ok(response)
}
}

#[derive(Clone)]
pub struct SyncHttp;

impl gen::client::blocking::HttpClient for SyncHttp {
fn post(&self, url: &str, request: &iamgroot::jsonrpc::Request) -> std::result::Result<iamgroot::jsonrpc::Response, iamgroot::jsonrpc::Error> {
#[cfg(not(target_arch = "wasm32"))]
{
let response = ureq::post(url)
.send_json(&request)
.map_err(|e| iamgroot::jsonrpc::Error::new(0, format!("LOL: {e:?}")))?
.into_json()
.map_err(|e| iamgroot::jsonrpc::Error::new(0, format!("LOL: {e:?}")))?;
Ok(response)
}

#[cfg(target_arch = "wasm32")]
{
unimplemented!("TODO: FIXME: use wasm-friendly blocking http client")
}

}
}

pub struct Client {
starknet: StarknetClient,
starknet: StarknetClient<AsyncHttp>,
ethereum: EthereumClient,
}

impl Client {
pub async fn new(config: &Config) -> Result<Self> {
let starknet = StarknetClient::new(&config.starknet_rpc);
let http = AsyncHttp(reqwest::Client::new());
let starknet = StarknetClient::new(&config.starknet_rpc, http);
let ethereum = EthereumClient::new(config).await?;
Ok(Self { starknet, ethereum })
}
Expand All @@ -35,7 +78,7 @@ impl Client {
self.ethereum.helios()
}

pub fn starknet(&self) -> &StarknetClient {
pub fn starknet(&self) -> &StarknetClient<AsyncHttp> {
&self.starknet
}

Expand Down
6 changes: 3 additions & 3 deletions src/exe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ use starknet_api::{
};
use starknet_types_core::felt::Felt as StarkFelt;

use crate::gen::{self, blocking::Rpc};
use crate::{client::SyncHttp, gen::{self, blocking::Rpc}};

pub mod err;
pub mod map;

use err::Error;

pub fn call(
client: &gen::client::blocking::Client,
client: &gen::client::blocking::Client<SyncHttp>,
function_call: gen::FunctionCall,
state_root: gen::Felt,
) -> Result<CallInfo, Error> {
Expand Down Expand Up @@ -146,7 +146,7 @@ pub fn call(
}

struct StateProxy {
client: gen::client::blocking::Client,
client: gen::client::blocking::Client<SyncHttp>,
diff: CommitmentStateDiff,
state_root: gen::Felt,
}
Expand Down
Loading

0 comments on commit 8492ee7

Please sign in to comment.