Skip to content

Commit

Permalink
sui-graphql-client: use typed digests for queries (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten authored Dec 12, 2024
1 parent b3537da commit d1ebef5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
19 changes: 12 additions & 7 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ use streams::stream_paginated_query;

use sui_types::types::framework::Coin;
use sui_types::types::Address;
use sui_types::types::CheckpointDigest;
use sui_types::types::CheckpointSequenceNumber;
use sui_types::types::CheckpointSummary;
use sui_types::types::Digest;
use sui_types::types::Event;
use sui_types::types::MovePackage;
use sui_types::types::Object;
Expand Down Expand Up @@ -483,7 +483,10 @@ impl Client {

/// The total number of transaction blocks in the network by the end of the provided
/// checkpoint digest.
pub async fn total_transaction_blocks_by_digest(&self, digest: Digest) -> Result<Option<u64>> {
pub async fn total_transaction_blocks_by_digest(
&self,
digest: CheckpointDigest,
) -> Result<Option<u64>> {
self.internal_total_transaction_blocks(Some(digest.to_string()), None)
.await
}
Expand Down Expand Up @@ -643,7 +646,7 @@ impl Client {
/// provided, it will use the last known checkpoint id.
pub async fn checkpoint(
&self,
digest: Option<Digest>,
digest: Option<CheckpointDigest>,
seq_num: Option<u64>,
) -> Result<Option<CheckpointSummary>> {
if digest.is_some() && seq_num.is_some() {
Expand Down Expand Up @@ -1434,7 +1437,10 @@ impl Client {
// ===========================================================================

/// Get a transaction by its digest.
pub async fn transaction(&self, digest: Digest) -> Result<Option<SignedTransaction>> {
pub async fn transaction(
&self,
digest: TransactionDigest,
) -> Result<Option<SignedTransaction>> {
let operation = TransactionBlockQuery::build(TransactionBlockArgs {
digest: digest.to_string(),
});
Expand Down Expand Up @@ -2147,9 +2153,8 @@ mod tests {
.unwrap();

let digest = chckp.digest();
let total_transaction_blocks_by_digest = client
.total_transaction_blocks_by_digest(digest.into())
.await;
let total_transaction_blocks_by_digest =
client.total_transaction_blocks_by_digest(digest).await;
assert!(total_transaction_blocks_by_digest.is_ok());
assert_eq!(
total_transaction_blocks_by_digest.unwrap().unwrap(),
Expand Down
26 changes: 13 additions & 13 deletions crates/sui-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ mod tests {
use crate::Function;
use crate::Serialized;
use crate::TransactionBuilder;
use sui_types::types::Digest;
use sui_types::types::TransactionDigest;

/// Type corresponding to the output of `sui move build --dump-bytecode-as-base64`
#[derive(serde::Deserialize, Debug)]
Expand Down Expand Up @@ -580,7 +580,7 @@ mod tests {
.unwrap()
.sent;
let tx_digest = coins.first().unwrap().transfer_tx_digest;
wait_for_tx(client, tx_digest.into()).await;
wait_for_tx(client, tx_digest).await;

let gas = coins.last().unwrap().id;
// TODO when we have tx resolution, we can just pass an ObjectId
Expand All @@ -595,7 +595,7 @@ mod tests {

/// Wait for the transaction to be finalized and indexed. This queries the GraphQL server until
/// it retrieves the requested transaction.
async fn wait_for_tx(client: &Client, digest: Digest) {
async fn wait_for_tx(client: &Client, digest: TransactionDigest) {
while client.transaction(digest).await.unwrap().is_none() {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
Expand All @@ -605,7 +605,7 @@ mod tests {
/// transaction was successfully executed.
async fn wait_for_tx_and_check_effects_status_success(
client: &Client,
digest: Digest,
digest: TransactionDigest,
effects: Result<Option<TransactionEffects>, sui_graphql_client::error::Error>,
) {
assert!(effects.is_ok(), "Execution failed. Effects: {:?}", effects);
Expand Down Expand Up @@ -675,7 +675,7 @@ mod tests {
let sig = pk.sign_transaction(&tx).unwrap();

let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;

// check that recipient has 1 coin
let recipient_coins = client
Expand Down Expand Up @@ -704,7 +704,7 @@ mod tests {
let tx = tx.finish().unwrap();
let sig = pk.sign_transaction(&tx).unwrap();
let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;
}

#[tokio::test]
Expand All @@ -724,7 +724,7 @@ mod tests {
let sig = pk.sign_transaction(&tx).unwrap();

let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;

// check that recipient has 1 coin
let recipient_coins = client
Expand Down Expand Up @@ -756,7 +756,7 @@ mod tests {

// wait for the transaction to be finalized
loop {
let tx_digest = client.transaction(tx.digest().into()).await.unwrap();
let tx_digest = client.transaction(tx.digest()).await.unwrap();
if tx_digest.is_some() {
break;
}
Expand Down Expand Up @@ -790,7 +790,7 @@ mod tests {
let sig = pk.sign_transaction(&tx).unwrap();

let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;

// check that there are two coins
let coins_after = client
Expand All @@ -813,7 +813,7 @@ mod tests {
let sig = pk.sign_transaction(&tx).unwrap();

let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;
}

#[tokio::test]
Expand All @@ -829,7 +829,7 @@ mod tests {
let tx = tx.finish().unwrap();
let sig = pk.sign_transaction(&tx).unwrap();
let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;
}

#[tokio::test]
Expand Down Expand Up @@ -868,7 +868,7 @@ mod tests {
_ => panic!("Expected V2 effects"),
}
}
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;

let mut tx = TransactionBuilder::new();
let mut upgrade_cap = None;
Expand Down Expand Up @@ -939,6 +939,6 @@ mod tests {
let tx = tx.finish().unwrap();
let sig = pk.sign_transaction(&tx).unwrap();
let effects = client.execute_tx(vec![sig], &tx).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest().into(), effects).await;
wait_for_tx_and_check_effects_status_success(&client, tx.digest(), effects).await;
}
}

0 comments on commit d1ebef5

Please sign in to comment.