Skip to content

Commit

Permalink
Final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Dec 5, 2024
1 parent b6c3db9 commit 131d131
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
2 changes: 2 additions & 0 deletions crates/sui-sdk-types/src/types/object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::BTreeMap;

#[cfg(all(feature = "serde", feature = "hash"))]
use super::unresolved::Input;
use super::Address;
use super::Identifier;
Expand Down Expand Up @@ -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())
Expand Down
26 changes: 11 additions & 15 deletions crates/sui-sdk-types/src/types/transaction/unresolved.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -355,26 +357,20 @@ impl From<Value> 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<ObjectId> for Input {
fn from(object_id: ObjectId) -> Self {
Input::by_id(object_id)
}
}
40 changes: 6 additions & 34 deletions crates/sui-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ pub struct Function {
type_args: Vec<TypeTag>,
}

/// 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.
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 131d131

Please sign in to comment.