Skip to content

Commit

Permalink
Return a tuple of Event, TxDigest
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Dec 10, 2024
1 parent 24c3493 commit b53f4f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
39 changes: 33 additions & 6 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 @@ -942,15 +942,42 @@ impl Client {
let page_info = ec.page_info;
let nodes = ec
.nodes
.into_iter()
.map(|e| e.bcs.0)
.iter()
.map(|e| e.bcs.0.clone())
.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>>()?;

Ok(Page::new(page_info, nodes))
let tx_digests = ec
.nodes
.iter()
.map(|e| {
e.transaction_block
.as_ref()
.ok_or_else(|| Error::empty_response_error())
})
.collect::<Result<Vec<_>>>()?
.iter()
.map(|x| {
x.digest
.as_ref()
.ok_or_else(|| Error::empty_response_error())
})
.collect::<Result<Vec<_>>>()?
.iter()
.map(|x| {
TransactionDigest::from_base58(x).map_err(|_| Error::empty_response_error())
})
.collect::<Result<Vec<_>>>()?;

let events = nodes
.into_iter()
.zip(tx_digests.into_iter())
.collect::<Vec<_>>();

Ok(Page::new(page_info, events))
} else {
Ok(Page::new_empty())
}
Expand All @@ -961,7 +988,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 b53f4f3

Please sign in to comment.