Skip to content

Commit

Permalink
sui-graphql-client: return a tuple of Event, TxDigest when querying e…
Browse files Browse the repository at this point in the history
…vents (#68)
  • Loading branch information
stefan-mysten authored Dec 11, 2024
1 parent 8ae5b61 commit b6f1a87
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
37 changes: 26 additions & 11 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,12 +915,12 @@ impl Client {
// Events API
// ===========================================================================

/// Return a page of events based on the (optional) event filter.
/// Return a page of tuple (event, transaction digest) based on the (optional) event filter.
pub async fn events(
&self,
filter: Option<EventFilter>,
pagination_filter: PaginationFilter,
) -> Result<Page<Event>> {
) -> Result<Page<(Event, TransactionDigest)>> {
let (after, before, first, last) = self.pagination_filter(pagination_filter).await;

let operation = EventsQuery::build(EventsQueryArgs {
Expand All @@ -940,17 +940,32 @@ impl Client {
if let Some(events) = response.data {
let ec = events.events;
let page_info = ec.page_info;
let nodes = ec

let events_with_digests = ec
.nodes
.into_iter()
.map(|e| e.bcs.0)
.map(|b| base64ct::Base64::decode_vec(&b))
.collect::<Result<Vec<_>, base64ct::Error>>()?
.iter()
.map(|b| bcs::from_bytes::<Event>(b))
.collect::<Result<Vec<_>, bcs::Error>>()?;
.map(|node| -> Result<(Event, TransactionDigest)> {
let event =
bcs::from_bytes::<Event>(&base64ct::Base64::decode_vec(&node.bcs.0)?)?;

let tx_digest = node
.transaction_block
.ok_or_else(Error::empty_response_error)?
.digest
.ok_or_else(|| {
Error::from_error(
Kind::Deserialization,
"Expected a transaction digest for this event, but it is missing.",
)
})?;

let tx_digest = TransactionDigest::from_base58(&tx_digest)?;

Ok((event, tx_digest))
})
.collect::<Result<Vec<_>>>()?;

Ok(Page::new(page_info, nodes))
Ok(Page::new(page_info, events_with_digests))
} else {
Ok(Page::new_empty())
}
Expand All @@ -961,7 +976,7 @@ impl Client {
&self,
filter: Option<EventFilter>,
streaming_direction: Direction,
) -> impl Stream<Item = Result<Event>> + '_ {
) -> impl Stream<Item = Result<(Event, TransactionDigest)>> + '_ {
stream_paginated_query(
move |pag_filter| self.events(filter.clone(), pag_filter),
streaming_direction,
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-graphql-client/src/query_types/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::query_types::schema;
use crate::query_types::transaction::TransactionBlockDigest;
use crate::query_types::Address;
use crate::query_types::Base64;
use crate::query_types::PageInfo;
Expand Down Expand Up @@ -54,4 +55,5 @@ pub struct EventFilter {
#[cynic(schema = "rpc", graphql_type = "Event")]
pub struct Event {
pub bcs: Base64,
pub transaction_block: Option<TransactionBlockDigest>,
}
6 changes: 6 additions & 0 deletions crates/sui-graphql-client/src/query_types/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ pub struct TransactionBlock {
pub signatures: Option<Vec<Base64>>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "TransactionBlock")]
pub struct TransactionBlockDigest {
pub digest: Option<String>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "TransactionBlock")]
pub struct TxBlockEffects {
Expand Down

0 comments on commit b6f1a87

Please sign in to comment.