Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 54 additions & 31 deletions crates/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use torii_grpc_client::{
EntityUpdateStreaming, EventUpdateStreaming, IndexerUpdateStreaming, TokenBalanceStreaming,
TokenUpdateStreaming, WorldClient,
};
use torii_libp2p_client::EventLoop;
use torii_libp2p_client::RelayClient;
use torii_libp2p_client::{EventLoop, RelayClient};
use torii_libp2p_types::Message;
use torii_proto::proto::world::{
RetrieveControllersResponse, RetrieveEntitiesResponse, RetrieveEventsResponse,
Expand All @@ -25,6 +24,7 @@ use torii_proto::{

use crate::error::Error;

#[allow(unused)]
#[derive(Debug)]
pub struct Client {
/// The grpc client.
Expand Down Expand Up @@ -89,22 +89,25 @@ impl Client {
contract_addresses: Vec<Felt>,
token_ids: Vec<U256>,
limit: Option<u32>,
offset: Option<u32>,
cursor: Option<String>,
) -> Result<Page<Token>, Error> {
let mut grpc_client = self.inner.write().await;
let RetrieveTokensResponse {
tokens,
next_cursor,
} = grpc_client
.retrieve_tokens(contract_addresses, token_ids, limit, offset, cursor)
.retrieve_tokens(contract_addresses, token_ids, limit, cursor)
.await?;
Ok(Page {
items: tokens
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Token>, _>>()?,
next_cursor,
next_cursor: if next_cursor.is_empty() {
None
} else {
Some(next_cursor)
},
})
}

Expand All @@ -115,7 +118,6 @@ impl Client {
contract_addresses: Vec<Felt>,
token_ids: Vec<U256>,
limit: Option<u32>,
offset: Option<u32>,
cursor: Option<String>,
) -> Result<Page<TokenBalance>, Error> {
let mut grpc_client = self.inner.write().await;
Expand All @@ -128,7 +130,6 @@ impl Client {
contract_addresses,
token_ids,
limit,
offset,
cursor,
)
.await?;
Expand All @@ -137,7 +138,11 @@ impl Client {
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<TokenBalance>, _>>()?,
next_cursor,
next_cursor: if next_cursor.is_empty() {
None
} else {
Some(next_cursor)
},
})
}

Expand All @@ -147,43 +152,61 @@ impl Client {
/// entities, this is less efficient as it requires an additional query for each entity's
/// model data. Specifying a clause can optimize the query by limiting the retrieval to specific
/// type of entites matching keys and/or models.
pub async fn entities(&self, query: Query, historical: bool) -> Result<Vec<Entity>, Error> {
pub async fn entities(&self, query: Query) -> Result<Page<Entity>, Error> {
let mut grpc_client = self.inner.write().await;
let RetrieveEntitiesResponse {
entities,
total_count: _,
} = grpc_client.retrieve_entities(query, historical).await?;
Ok(entities
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Entity>, _>>()?)
next_cursor,
} = grpc_client.retrieve_entities(query).await?;
Ok(Page {
items: entities
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Entity>, _>>()?,
next_cursor: if next_cursor.is_empty() {
None
} else {
Some(next_cursor)
},
})
}

/// Similary to entities, this function retrieves event messages matching the query parameter.
pub async fn event_messages(
&self,
query: Query,
historical: bool,
) -> Result<Vec<Entity>, Error> {
pub async fn event_messages(&self, query: Query) -> Result<Page<Entity>, Error> {
let mut grpc_client = self.inner.write().await;
let RetrieveEntitiesResponse {
entities,
total_count: _,
} = grpc_client
.retrieve_event_messages(query, historical)
.await?;
Ok(entities
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Entity>, _>>()?)
next_cursor,
} = grpc_client.retrieve_event_messages(query).await?;
Ok(Page {
items: entities
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Entity>, _>>()?,
next_cursor: if next_cursor.is_empty() {
None
} else {
Some(next_cursor)
},
})
}

/// Retrieve raw starknet events matching the keys provided.
/// If the keys are empty, it will return all events.
pub async fn starknet_events(&self, query: EventQuery) -> Result<Vec<Event>, Error> {
pub async fn starknet_events(&self, query: EventQuery) -> Result<Page<Event>, Error> {
let mut grpc_client = self.inner.write().await;
let RetrieveEventsResponse { events } = grpc_client.retrieve_events(query).await?;
Ok(events.into_iter().map(Event::from).collect::<Vec<Event>>())
let RetrieveEventsResponse {
events,
next_cursor,
} = grpc_client.retrieve_events(query).await?;
Ok(Page {
items: events.into_iter().map(Event::from).collect::<Vec<Event>>(),
next_cursor: if next_cursor.is_empty() {
None
} else {
Some(next_cursor)
},
})
}

/// A direct stream to grpc subscribe entities
Expand Down
8 changes: 0 additions & 8 deletions crates/grpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ impl WorldClient {
contract_addresses: Vec<Felt>,
token_ids: Vec<U256>,
limit: Option<u32>,
offset: Option<u32>,
cursor: Option<String>,
) -> Result<RetrieveTokensResponse, Error> {
self.inner
Expand All @@ -138,7 +137,6 @@ impl WorldClient {
.map(|id| id.to_be_bytes().to_vec())
.collect(),
limit: limit.unwrap_or_default(),
offset: offset.unwrap_or_default(),
cursor: cursor.unwrap_or_default(),
})
.await
Expand Down Expand Up @@ -216,7 +214,6 @@ impl WorldClient {
contract_addresses: Vec<Felt>,
token_ids: Vec<U256>,
limit: Option<u32>,
offset: Option<u32>,
cursor: Option<String>,
) -> Result<RetrieveTokenBalancesResponse, Error> {
self.inner
Expand All @@ -234,7 +231,6 @@ impl WorldClient {
.map(|id| id.to_be_bytes().to_vec())
.collect(),
limit: limit.unwrap_or_default(),
offset: offset.unwrap_or_default(),
cursor: cursor.unwrap_or_default(),
})
.await
Expand All @@ -245,11 +241,9 @@ impl WorldClient {
pub async fn retrieve_entities(
&mut self,
query: Query,
historical: bool,
) -> Result<RetrieveEntitiesResponse, Error> {
let request = RetrieveEntitiesRequest {
query: Some(query.into()),
historical,
};
self.inner
.retrieve_entities(request)
Expand All @@ -261,11 +255,9 @@ impl WorldClient {
pub async fn retrieve_event_messages(
&mut self,
query: Query,
historical: bool,
) -> Result<RetrieveEntitiesResponse, Error> {
let request = RetrieveEventMessagesRequest {
query: Some(query.into()),
historical,
};
self.inner
.retrieve_event_messages(request)
Expand Down
1 change: 1 addition & 0 deletions crates/grpc/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tonic-reflection.workspace = true
tonic-web.workspace = true
tonic.workspace = true
anyhow.workspace = true
base64.workspace = true

[dev-dependencies]
cainome.workspace = true
Expand Down
Loading
Loading