Skip to content

Commit

Permalink
Split the API into two dry run functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Oct 1, 2024
1 parent 1b04597 commit 724b1c6
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use sui_types::types::Object;
use sui_types::types::SignedTransaction;
use sui_types::types::Transaction;
use sui_types::types::TransactionEffects;
use sui_types::types::TransactionKind;
use sui_types::types::UserSignature;

use anyhow::anyhow;
Expand Down Expand Up @@ -688,30 +689,52 @@ impl Client {
}

// ===========================================================================
// Transaction API
// Dry Run API
// ===========================================================================

/// Dry run a transaction and return the transaction.
///
/// `tx` is either a [`Transaction`] struct or a [`TransactionKind`] struct. The expected type
/// is controlled by the presence or absence of `txMeta`:
///
/// - if present, tx is assumed to be a [`TransactionKind`],
/// - if absent, tx is assumed to be [`Transaction`].
/// Dry run a [`Transaction`] and return the transaction.
///
/// `skipChecks` optional flag disables the usual verification checks that prevent access to
/// objects that are owned by addresses other than the sender, and calling non-public,
/// non-entry functions, and some other checks. Defaults to false.
pub async fn dry_run(
pub async fn dry_run_tx(
&self,
tx: &Transaction,
skip_checks: Option<bool>,
tx_meta: Option<TransactionMetadata>,
) -> Result<Option<Transaction>, Error> {
let skip_checks = skip_checks.unwrap_or(false);
let tx_bytes = base64ct::Base64::encode_string(
&bcs::to_bytes(&tx).map_err(|_| Error::msg("Cannot encode Transaction as BCS"))?,
);
self.dry_run(tx_bytes, skip_checks, None).await
}

/// Dry run a [`TransactionKind`] and return the transaction.
///
/// `skipChecks` optional flag disables the usual verification checks that prevent access to
/// objects that are owned by addresses other than the sender, and calling non-public,
/// non-entry functions, and some other checks. Defaults to false.
///
/// `tx_meta` is the transaction metadata.
pub async fn dry_run_tx_kind(
&self,
tx_kind: &TransactionKind,
skip_checks: Option<bool>,
tx_meta: TransactionMetadata,
) -> Result<Option<Transaction>, Error> {
let tx_bytes = base64ct::Base64::encode_string(
&bcs::to_bytes(&tx_kind).map_err(|_| Error::msg("Cannot encode Transaction as BCS"))?,
);
self.dry_run(tx_bytes, skip_checks, Some(tx_meta)).await
}

/// Internal implementation of the dry run API.
async fn dry_run(
&self,
tx_bytes: String,
skip_checks: Option<bool>,
tx_meta: Option<TransactionMetadata>,
) -> Result<Option<Transaction>, Error> {
let skip_checks = skip_checks.unwrap_or(false);
let operation = DryRunQuery::build(DryRunArgs {
tx_bytes,
skip_checks,
Expand Down Expand Up @@ -741,6 +764,10 @@ impl Client {
}
}

// ===========================================================================
// Transaction API
// ===========================================================================

// TODO: From Brandon: this fails due to SignedTransaction in Sui core type being technically inaccurate but it is fixed in this SDK here. in particular core incorrectly appends the signing intent when it shouldn't so my guess is that's whats wrong
/// Get a transaction by its digest.
pub async fn transaction(&self, digest: &str) -> Result<Option<SignedTransaction>, Error> {
Expand Down

0 comments on commit 724b1c6

Please sign in to comment.