Skip to content

Commit

Permalink
Only iterate once
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Dec 11, 2024
1 parent 9278344 commit d850b16
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,45 +940,28 @@ impl Client {
if let Some(events) = response.data {
let ec = events.events;
let page_info = ec.page_info;
let nodes = ec
.nodes
.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>>()?;

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::from_error(

let events_with_digests = ec.nodes.into_iter()
.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<_>>>()?
.iter()
.map(|x| TransactionDigest::from_base58(x).map_err(Error::from))
.collect::<Result<Vec<_>>>()?;

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

Ok(Page::new(page_info, events))
Ok(Page::new(page_info, events_with_digests))
} else {
Ok(Page::new_empty())
}
Expand Down

0 comments on commit d850b16

Please sign in to comment.