From 724b1c6503bf980f2229c3a863d8e36d928447e7 Mon Sep 17 00:00:00 2001 From: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:05:50 -0700 Subject: [PATCH] Split the API into two dry run functions --- crates/sui-graphql-client/src/lib.rs | 49 +++++++++++++++++++++------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/crates/sui-graphql-client/src/lib.rs b/crates/sui-graphql-client/src/lib.rs index 5f465e455..cf2e8a944 100644 --- a/crates/sui-graphql-client/src/lib.rs +++ b/crates/sui-graphql-client/src/lib.rs @@ -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; @@ -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, - tx_meta: Option, ) -> Result, 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, + tx_meta: TransactionMetadata, + ) -> Result, 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, + tx_meta: Option, + ) -> Result, Error> { + let skip_checks = skip_checks.unwrap_or(false); let operation = DryRunQuery::build(DryRunArgs { tx_bytes, skip_checks, @@ -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, Error> {