From 0a186faefcaad5f26a26598a87616253bb1fa9c1 Mon Sep 17 00:00:00 2001 From: Larko <59736843+Larkooo@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:06:29 +0800 Subject: [PATCH] refactor(torii-client): cleanup & remove rpc (#3019) * refactor(torii-client): cleanup * f --- crates/torii/client/src/client/mod.rs | 52 ++++++--------------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/crates/torii/client/src/client/mod.rs b/crates/torii/client/src/client/mod.rs index 1579c98861..3de705ef9c 100644 --- a/crates/torii/client/src/client/mod.rs +++ b/crates/torii/client/src/client/mod.rs @@ -4,13 +4,9 @@ use std::sync::Arc; use crypto_bigint::U256; use dojo_types::WorldMetadata; -use dojo_world::contracts::WorldContractReader; use futures::lock::Mutex; -use parking_lot::{RwLock, RwLockReadGuard}; use starknet::core::types::Felt; -use starknet::providers::jsonrpc::HttpTransport; -use starknet::providers::JsonRpcClient; -use tokio::sync::RwLock as AsyncRwLock; +use tokio::sync::RwLock; use torii_grpc::client::{ EntityUpdateStreaming, EventUpdateStreaming, IndexerUpdateStreaming, TokenBalanceStreaming, TokenUpdateStreaming, @@ -26,50 +22,24 @@ use torii_grpc::types::{ use torii_relay::client::EventLoop; use torii_relay::types::Message; -use crate::client::error::{Error, ParseError}; +use crate::client::error::Error; -// TODO: remove reliance on RPC #[allow(unused)] #[derive(Debug)] pub struct Client { - /// Metadata of the World that the client is connected to. - metadata: Arc>, /// The grpc client. - inner: AsyncRwLock, + inner: RwLock, /// Relay client. relay_client: torii_relay::client::RelayClient, - /// The subscription client handle. - /// World contract reader. - world_reader: WorldContractReader>, } impl Client { /// Returns a initialized [Client]. - pub async fn new( - torii_url: String, - rpc_url: String, - relay_url: String, - world: Felt, - ) -> Result { - let mut grpc_client = torii_grpc::client::WorldClient::new(torii_url, world).await?; - + pub async fn new(torii_url: String, relay_url: String, world: Felt) -> Result { + let grpc_client = torii_grpc::client::WorldClient::new(torii_url, world).await?; let relay_client = torii_relay::client::RelayClient::new(relay_url)?; - let metadata = grpc_client.metadata().await?; - - let shared_metadata: Arc<_> = RwLock::new(metadata).into(); - - // initialize the entities to be synced with the latest values - let rpc_url = url::Url::parse(&rpc_url).map_err(ParseError::Url)?; - let provider = JsonRpcClient::new(HttpTransport::new(rpc_url)); - let world_reader = WorldContractReader::new(world, provider); - - Ok(Self { - world_reader, - metadata: shared_metadata, - inner: AsyncRwLock::new(grpc_client), - relay_client, - }) + Ok(Self { inner: RwLock::new(grpc_client), relay_client }) } /// Starts the relay client event loop. @@ -90,8 +60,10 @@ impl Client { } /// Returns a read lock on the World metadata that the client is connected to. - pub fn metadata(&self) -> RwLockReadGuard<'_, WorldMetadata> { - self.metadata.read() + pub async fn metadata(&self) -> Result { + let mut grpc_client = self.inner.write().await; + let metadata = grpc_client.metadata().await?; + Ok(metadata) } /// Retrieves controllers matching contract addresses. @@ -230,9 +202,7 @@ impl Client { contract_address: Option, ) -> Result { let mut grpc_client = self.inner.write().await; - let stream = grpc_client - .subscribe_indexer(contract_address.unwrap_or(self.world_reader.address)) - .await?; + let stream = grpc_client.subscribe_indexer(contract_address.unwrap_or_default()).await?; Ok(stream) }