Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(torii-client): cleanup & remove rpc #3019

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions crates/torii/client/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ pub mod error;
use std::sync::Arc;

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 @@ -25,48 +21,29 @@ 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>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ohayo sensei! Ensure references to the removed world_reader field are cleaned up.
Excellent approach to storing WorldClient in a RwLock. However, referencing self.world_reader.address around line 216 looks like a leftover. This will cause a compile error now that world_reader is gone.

As a follow-up fix outside the changed lines, consider updating the code to reference the new client’s address field (assuming one exists). For instance:

- subscribe_indexer(contract_address.unwrap_or(self.world_reader.address))
+ subscribe_indexer(contract_address.unwrap_or(grpc_client.world_address))

Committable suggestion skipped: line range outside the PR's diff.

/// 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?;

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),
inner: RwLock::new(grpc_client),
relay_client,
})
}
Expand All @@ -89,8 +66,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
Loading