From 131d131689650254bd0ca0ebab2ca8cac641f13d Mon Sep 17 00:00:00 2001 From: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com> Date: Wed, 4 Dec 2024 20:06:02 -0800 Subject: [PATCH] Final fixes --- crates/sui-sdk-types/src/types/object.rs | 2 + .../src/types/transaction/unresolved.rs | 26 +++++------- crates/sui-transaction-builder/src/lib.rs | 40 +++---------------- 3 files changed, 19 insertions(+), 49 deletions(-) diff --git a/crates/sui-sdk-types/src/types/object.rs b/crates/sui-sdk-types/src/types/object.rs index eb3828e85..69d9a464d 100644 --- a/crates/sui-sdk-types/src/types/object.rs +++ b/crates/sui-sdk-types/src/types/object.rs @@ -1,5 +1,6 @@ use std::collections::BTreeMap; +#[cfg(all(feature = "serde", feature = "hash"))] use super::unresolved::Input; use super::Address; use super::Identifier; @@ -325,6 +326,7 @@ impl Object { } /// Convert this object into an [`unresolved::Input`] with the object id, digest, and version. + #[cfg(all(feature = "serde", feature = "hash"))] pub fn as_input(&self) -> Input { Input::by_id(self.object_id()) .with_digest(self.digest()) diff --git a/crates/sui-sdk-types/src/types/transaction/unresolved.rs b/crates/sui-sdk-types/src/types/transaction/unresolved.rs index 1e984de31..6801112b2 100644 --- a/crates/sui-sdk-types/src/types/transaction/unresolved.rs +++ b/crates/sui-sdk-types/src/types/transaction/unresolved.rs @@ -1,8 +1,10 @@ use crate::types::object::Version; use crate::types::Address; +#[cfg(all(feature = "serde", feature = "hash"))] use crate::types::Object; use crate::types::ObjectDigest; use crate::types::ObjectId; +#[cfg(all(feature = "serde", feature = "hash"))] use crate::types::Owner; use super::Command; @@ -280,8 +282,8 @@ impl Input { /// Set the initial shared version. pub fn with_initial_shared_version(self, initial_version: u64) -> Self { Self { - version: Some(initial_version), kind: Some(InputKind::Shared), + version: Some(initial_version), ..self } } @@ -355,26 +357,20 @@ impl From for serde_json::Value { } } +#[cfg(all(feature = "serde", feature = "hash"))] impl From<&Object> for Input { fn from(object: &Object) -> Self { match object.owner() { - Owner::Address(_) => Input::by_id(object.object_id()) - .with_version(object.version()) - .with_digest(object.digest()), - Owner::Object(_) => Input::owned(object.object_id(), object.version(), object.digest()), - - Owner::Shared(at_version) => { - Input::by_id(object.object_id()).with_initial_shared_version(*at_version) - } - Owner::Immutable => { - Input::immutable(object.object_id(), object.version(), object.digest()) - } + Owner::Address(_) => object.as_input(), + Owner::Object(_) => object.as_input().with_owned_kind(), + Owner::Shared(at_version) => object.as_input().with_initial_shared_version(*at_version), + Owner::Immutable => object.as_input().with_immutable_kind(), } } } -impl From<&ObjectId> for Input { - fn from(object_id: &ObjectId) -> Self { - Input::by_id(*object_id) +impl From for Input { + fn from(object_id: ObjectId) -> Self { + Input::by_id(object_id) } } diff --git a/crates/sui-transaction-builder/src/lib.rs b/crates/sui-transaction-builder/src/lib.rs index 370e1fa52..e39a251fe 100644 --- a/crates/sui-transaction-builder/src/lib.rs +++ b/crates/sui-transaction-builder/src/lib.rs @@ -70,14 +70,6 @@ pub struct Function { type_args: Vec, } -/// A trait to convert a type into an [`unresolved::Input`]. This is mostly used for gas objects, -/// to pass them either as literals, as an object id, as a reference to an object, or as a -/// [`unresolved::Input`] object. -// pub trait IntoInput { -// /// Convert the type into an [`unresolved::Input`]. -// fn into_input(self) -> unresolved::Input; -// } - /// A transaction builder to build transactions. impl TransactionBuilder { /// Create a new transaction builder and initialize its elements to default. @@ -760,14 +752,8 @@ mod tests { let coin = coins.first().unwrap().id; let coin_obj = client.object(coin.into(), None).await.unwrap().unwrap(); - let coin_digest = coin_obj.digest(); - let coin_version = coin_obj.version(); + let coin_input = tx.input(coin_obj.as_input().with_owned_kind()); - let coin_input = tx.input(unresolved::Input::owned( - coin_obj.object_id(), - coin_version, - coin_digest, - )); // transfer 1 SUI let amount = tx.input(Serialized(&1_000_000_000u64)); tx.split_coins(coin_input, vec![amount]); @@ -800,26 +786,13 @@ mod tests { let coin1 = coins.first().unwrap().id; let coin1_obj = client.object(coin1.into(), None).await.unwrap().unwrap(); - let coin1_digest = coin1_obj.digest(); - let coin1_version = coin1_obj.version(); - - let coin_to_merge = tx.input(unresolved::Input::owned( - coin1_obj.object_id(), - coin1_version, - coin1_digest, - )); + let coin_to_merge = tx.input(coin1_obj.as_input().with_owned_kind()); let mut coins_to_merge = vec![]; // last coin is used for gas, first coin is the one we merge into for c in coins[1..&coins.len() - 1].iter() { let coin = client.object(c.id.into(), None).await.unwrap().unwrap(); - let coin_digest = coin.digest(); - let coin_version = coin.version(); - coins_to_merge.push(tx.input(unresolved::Input::owned( - coin.object_id(), - coin_version, - coin_digest, - ))); + coins_to_merge.push(tx.input(coin.as_input().with_owned_kind())); } tx.merge_coins(coin_to_merge, coins_to_merge); @@ -915,11 +888,10 @@ mod tests { ObjectType::Struct(x) if x.name.to_string() == "UpgradeCap" => { match obj.owner() { sui_types::types::Owner::Address(_) => { - upgrade_cap = Some( - tx.input(unresolved::Input::immutable(o, obj.version(), obj.digest()))); + upgrade_cap = Some(tx.input(obj.as_input().with_owned_kind())); } - sui_types::types::Owner::Shared(x) => { - upgrade_cap = Some(tx.input(unresolved::Input::shared(o, *x, true))); + sui_types::types::Owner::Shared(_) => { + upgrade_cap = Some(tx.input(&obj)) } // If the capability is owned by an object, then the module defining the owning // object gets to decide how the upgrade capability should be used.