From b6f1a87488ec6d5b497b5a892288ef7f00164ebc Mon Sep 17 00:00:00 2001 From: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com> Date: Wed, 11 Dec 2024 12:44:57 -0800 Subject: [PATCH] sui-graphql-client: return a tuple of Event, TxDigest when querying events (#68) --- crates/sui-graphql-client/src/lib.rs | 37 +++++++++++++------ .../src/query_types/events.rs | 2 + .../src/query_types/transaction.rs | 6 +++ 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/crates/sui-graphql-client/src/lib.rs b/crates/sui-graphql-client/src/lib.rs index 0b01e4dc1..55809590e 100644 --- a/crates/sui-graphql-client/src/lib.rs +++ b/crates/sui-graphql-client/src/lib.rs @@ -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, pagination_filter: PaginationFilter, - ) -> Result> { + ) -> Result> { let (after, before, first, last) = self.pagination_filter(pagination_filter).await; let operation = EventsQuery::build(EventsQueryArgs { @@ -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::, base64ct::Error>>()? - .iter() - .map(|b| bcs::from_bytes::(b)) - .collect::, bcs::Error>>()?; + .map(|node| -> Result<(Event, TransactionDigest)> { + let event = + bcs::from_bytes::(&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::>>()?; - Ok(Page::new(page_info, nodes)) + Ok(Page::new(page_info, events_with_digests)) } else { Ok(Page::new_empty()) } @@ -961,7 +976,7 @@ impl Client { &self, filter: Option, streaming_direction: Direction, - ) -> impl Stream> + '_ { + ) -> impl Stream> + '_ { stream_paginated_query( move |pag_filter| self.events(filter.clone(), pag_filter), streaming_direction, diff --git a/crates/sui-graphql-client/src/query_types/events.rs b/crates/sui-graphql-client/src/query_types/events.rs index 591a76ada..1d3f4b8ed 100644 --- a/crates/sui-graphql-client/src/query_types/events.rs +++ b/crates/sui-graphql-client/src/query_types/events.rs @@ -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; @@ -54,4 +55,5 @@ pub struct EventFilter { #[cynic(schema = "rpc", graphql_type = "Event")] pub struct Event { pub bcs: Base64, + pub transaction_block: Option, } diff --git a/crates/sui-graphql-client/src/query_types/transaction.rs b/crates/sui-graphql-client/src/query_types/transaction.rs index 38be0490f..a384c2238 100644 --- a/crates/sui-graphql-client/src/query_types/transaction.rs +++ b/crates/sui-graphql-client/src/query_types/transaction.rs @@ -91,6 +91,12 @@ pub struct TransactionBlock { pub signatures: Option>, } +#[derive(cynic::QueryFragment, Debug)] +#[cynic(schema = "rpc", graphql_type = "TransactionBlock")] +pub struct TransactionBlockDigest { + pub digest: Option, +} + #[derive(cynic::QueryFragment, Debug)] #[cynic(schema = "rpc", graphql_type = "TransactionBlock")] pub struct TxBlockEffects {