-
Notifications
You must be signed in to change notification settings - Fork 195
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
feat(torii-client): token subscription & update subscription via id #3006
Changes from 8 commits
e1f02b7
c5b5ba4
2e4a23a
2003973
1bab5dc
7e9eb78
3ccb423
70d7fc2
fcb0722
d645aa5
657510e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
use std::num::ParseIntError; | ||||||||||||||||||||||||||||||||||||||||||||||||||
use std::time::Duration; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
use crypto_bigint::U256; | ||||||||||||||||||||||||||||||||||||||||||||||||||
use futures_util::stream::MapOk; | ||||||||||||||||||||||||||||||||||||||||||||||||||
use futures_util::{Stream, StreamExt, TryStreamExt}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
use starknet::core::types::{Felt, FromStrError, StateDiff, StateUpdate}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -17,12 +18,13 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
SubscribeEntityResponse, SubscribeEventMessagesRequest, SubscribeEventsRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||
SubscribeEventsResponse, SubscribeIndexerRequest, SubscribeIndexerResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||
SubscribeModelsRequest, SubscribeModelsResponse, SubscribeTokenBalancesResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||
UpdateEntitiesSubscriptionRequest, UpdateEventMessagesSubscriptionRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||
UpdateTokenBalancesSubscriptionRequest, WorldMetadataRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||
SubscribeTokensResponse, UpdateEntitiesSubscriptionRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||
UpdateEventMessagesSubscriptionRequest, UpdateTokenBalancesSubscriptionRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||
UpdateTokenSubscriptionRequest, WorldMetadataRequest, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::types::schema::{Entity, SchemaError}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
use crate::types::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
EntityKeysClause, Event, EventQuery, IndexerUpdate, ModelKeysClause, Query, TokenBalance, | ||||||||||||||||||||||||||||||||||||||||||||||||||
EntityKeysClause, Event, EventQuery, IndexerUpdate, ModelKeysClause, Query, Token, TokenBalance, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Debug, thiserror::Error)] | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -127,6 +129,55 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
.map(|res| res.into_inner()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn subscribe_tokens( | ||||||||||||||||||||||||||||||||||||||||||||||||||
&mut self, | ||||||||||||||||||||||||||||||||||||||||||||||||||
contract_addresses: Vec<Felt>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<TokenUpdateStreaming, Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
let request = RetrieveTokensRequest { | ||||||||||||||||||||||||||||||||||||||||||||||||||
contract_addresses: contract_addresses | ||||||||||||||||||||||||||||||||||||||||||||||||||
.into_iter() | ||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|c| c.to_bytes_be().to_vec()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.collect(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
let stream = self | ||||||||||||||||||||||||||||||||||||||||||||||||||
.inner | ||||||||||||||||||||||||||||||||||||||||||||||||||
.subscribe_tokens(request) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.await | ||||||||||||||||||||||||||||||||||||||||||||||||||
.map_err(Error::Grpc) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|res| res.into_inner())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(TokenUpdateStreaming(stream.map_ok(Box::new(|res| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
( | ||||||||||||||||||||||||||||||||||||||||||||||||||
res.subscription_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||
match res.token { | ||||||||||||||||||||||||||||||||||||||||||||||||||
Some(token) => token.try_into().expect("must able to serialize"), | ||||||||||||||||||||||||||||||||||||||||||||||||||
None => Token { | ||||||||||||||||||||||||||||||||||||||||||||||||||
id: "".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
contract_address: Felt::ZERO, | ||||||||||||||||||||||||||||||||||||||||||||||||||
name: "".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
symbol: "".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
decimals: 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
metadata: "".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||
})))) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn update_tokens_subscription( | ||||||||||||||||||||||||||||||||||||||||||||||||||
&mut self, | ||||||||||||||||||||||||||||||||||||||||||||||||||
subscription_id: u64, | ||||||||||||||||||||||||||||||||||||||||||||||||||
contract_addresses: Vec<Felt>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) -> Result<(), Error> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
let contract_addresses = | ||||||||||||||||||||||||||||||||||||||||||||||||||
contract_addresses.into_iter().map(|c| c.to_bytes_be().to_vec()).collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
let request = UpdateTokenSubscriptionRequest { subscription_id, contract_addresses }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
self.inner | ||||||||||||||||||||||||||||||||||||||||||||||||||
.update_tokens_subscription(request) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.await | ||||||||||||||||||||||||||||||||||||||||||||||||||
.map_err(Error::Grpc) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|res| res.into_inner()) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
pub async fn retrieve_token_balances( | ||||||||||||||||||||||||||||||||||||||||||||||||||
&mut self, | ||||||||||||||||||||||||||||||||||||||||||||||||||
account_addresses: Vec<Felt>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -338,7 +389,18 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
.map_err(Error::Grpc) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.map(|res| res.into_inner())?; | ||||||||||||||||||||||||||||||||||||||||||||||||||
Ok(TokenBalanceStreaming(stream.map_ok(Box::new(|res| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
(res.subscription_id, res.balance.unwrap().try_into().expect("must able to serialize")) | ||||||||||||||||||||||||||||||||||||||||||||||||||
( | ||||||||||||||||||||||||||||||||||||||||||||||||||
res.subscription_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||
match res.balance { | ||||||||||||||||||||||||||||||||||||||||||||||||||
Some(balance) => balance.try_into().expect("must able to serialize"), | ||||||||||||||||||||||||||||||||||||||||||||||||||
None => TokenBalance { | ||||||||||||||||||||||||||||||||||||||||||||||||||
balance: U256::ZERO, | ||||||||||||||||||||||||||||||||||||||||||||||||||
account_address: Felt::ZERO, | ||||||||||||||||||||||||||||||||||||||||||||||||||
contract_address: Felt::ZERO, | ||||||||||||||||||||||||||||||||||||||||||||||||||
token_id: "".to_string(), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+393
to
+404
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Apply consistent error handling pattern. Similar to the token subscription, let's handle the error gracefully here as well. - Some(balance) => balance.try_into().expect("must able to serialize"),
+ Some(balance) => balance.try_into().map_err(Error::Schema)?, 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
})))) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -368,6 +430,24 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
type TokenMappedStream = MapOk< | ||||||||||||||||||||||||||||||||||||||||||||||||||
tonic::Streaming<SubscribeTokensResponse>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
Box<dyn Fn(SubscribeTokensResponse) -> (SubscriptionId, Token) + Send>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Debug)] | ||||||||||||||||||||||||||||||||||||||||||||||||||
pub struct TokenUpdateStreaming(TokenMappedStream); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
impl Stream for TokenUpdateStreaming { | ||||||||||||||||||||||||||||||||||||||||||||||||||
type Item = <TokenMappedStream as Stream>::Item; | ||||||||||||||||||||||||||||||||||||||||||||||||||
fn poll_next( | ||||||||||||||||||||||||||||||||||||||||||||||||||
mut self: std::pin::Pin<&mut Self>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
cx: &mut std::task::Context<'_>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
) -> std::task::Poll<Option<Self::Item>> { | ||||||||||||||||||||||||||||||||||||||||||||||||||
self.0.poll_next_unpin(cx) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
type TokenBalanceMappedStream = MapOk< | ||||||||||||||||||||||||||||||||||||||||||||||||||
tonic::Streaming<SubscribeTokenBalancesResponse>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
Box<dyn Fn(SubscribeTokenBalancesResponse) -> (SubscriptionId, TokenBalance) + Send>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider throwing an error instead of returning an empty token.
Returning an empty token when
None
is received might hide potential issues. Consider throwing an error to make the failure more explicit.📝 Committable suggestion