Skip to content

Commit

Permalink
refactor(torii-client): cleanup & remove rpc (#3019)
Browse files Browse the repository at this point in the history
* refactor(torii-client): cleanup

* f
  • Loading branch information
Larkooo authored Feb 14, 2025
1 parent 5747193 commit 0a186fa
Showing 1 changed file with 11 additions and 41 deletions.
52 changes: 11 additions & 41 deletions crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<RwLock<WorldMetadata>>,
/// The grpc client.
inner: AsyncRwLock<torii_grpc::client::WorldClient>,
inner: RwLock<torii_grpc::client::WorldClient>,
/// Relay client.
relay_client: torii_relay::client::RelayClient,
/// The subscription client handle.
/// World contract reader.
world_reader: WorldContractReader<JsonRpcClient<HttpTransport>>,
}

impl Client {
/// Returns a initialized [Client].
pub async fn new(
torii_url: String,
rpc_url: String,
relay_url: String,
world: Felt,
) -> Result<Self, Error> {
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<Self, Error> {
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.
Expand All @@ -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<WorldMetadata, Error> {
let mut grpc_client = self.inner.write().await;
let metadata = grpc_client.metadata().await?;
Ok(metadata)
}

/// Retrieves controllers matching contract addresses.
Expand Down Expand Up @@ -230,9 +202,7 @@ impl Client {
contract_address: Option<Felt>,
) -> Result<IndexerUpdateStreaming, Error> {
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)
}

Expand Down

0 comments on commit 0a186fa

Please sign in to comment.