From 31bd9da32a8057edc45da8f5e6a8f25b83919b93 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Tue, 15 Oct 2024 10:27:30 -0500 Subject: [PATCH] types: improve format of UnresolvedTransaction --- crates/sui-sdk-types/src/types/mod.rs | 2 + .../src/types/transaction/mod.rs | 2 + .../src/types/transaction/unresolved.rs | 152 ++++++++++++++---- 3 files changed, 129 insertions(+), 27 deletions(-) diff --git a/crates/sui-sdk-types/src/types/mod.rs b/crates/sui-sdk-types/src/types/mod.rs index bad033ce3..12e97b862 100644 --- a/crates/sui-sdk-types/src/types/mod.rs +++ b/crates/sui-sdk-types/src/types/mod.rs @@ -136,9 +136,11 @@ pub use transaction::TransactionKind; pub use transaction::TransferObjects; pub use transaction::UnresolvedGasPayment; pub use transaction::UnresolvedInputArgument; +pub use transaction::UnresolvedInputArgumentKind; pub use transaction::UnresolvedObjectReference; pub use transaction::UnresolvedProgrammableTransaction; pub use transaction::UnresolvedTransaction; +pub use transaction::UnresolvedValue; pub use transaction::Upgrade; pub use transaction::VersionAssignment; pub use type_tag::Identifier; diff --git a/crates/sui-sdk-types/src/types/transaction/mod.rs b/crates/sui-sdk-types/src/types/transaction/mod.rs index 701957879..ce9e8d352 100644 --- a/crates/sui-sdk-types/src/types/transaction/mod.rs +++ b/crates/sui-sdk-types/src/types/transaction/mod.rs @@ -24,9 +24,11 @@ pub(crate) use serialization::SignedTransactionWithIntentMessage; mod unresolved; pub use unresolved::UnresolvedGasPayment; pub use unresolved::UnresolvedInputArgument; +pub use unresolved::UnresolvedInputArgumentKind; pub use unresolved::UnresolvedObjectReference; pub use unresolved::UnresolvedProgrammableTransaction; pub use unresolved::UnresolvedTransaction; +pub use unresolved::UnresolvedValue; #[derive(Clone, Debug, PartialEq, Eq)] #[cfg_attr(test, derive(test_strategy::Arbitrary))] diff --git a/crates/sui-sdk-types/src/types/transaction/unresolved.rs b/crates/sui-sdk-types/src/types/transaction/unresolved.rs index 8acff69bd..f0ffbfdbf 100644 --- a/crates/sui-sdk-types/src/types/transaction/unresolved.rs +++ b/crates/sui-sdk-types/src/types/transaction/unresolved.rs @@ -11,10 +11,12 @@ use super::TransactionExpiration; feature = "serde", derive(serde_derive::Serialize, serde_derive::Deserialize) )] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct UnresolvedTransaction { #[cfg_attr(feature = "serde", serde(flatten))] pub ptb: UnresolvedProgrammableTransaction, pub sender: Address, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] pub gas_payment: Option, pub expiration: TransactionExpiration, } @@ -23,6 +25,7 @@ pub struct UnresolvedTransaction { feature = "serde", derive(serde_derive::Serialize, serde_derive::Deserialize) )] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct UnresolvedProgrammableTransaction { pub inputs: Vec, pub commands: Vec, @@ -32,18 +35,33 @@ pub struct UnresolvedProgrammableTransaction { feature = "serde", derive(serde_derive::Serialize, serde_derive::Deserialize) )] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct UnresolvedGasPayment { + #[cfg_attr( + feature = "serde", + serde(default, skip_serializing_if = "Vec::is_empty") + )] pub objects: Vec, pub owner: Address, #[cfg_attr( feature = "serde", - serde(with = "crate::_serde::OptionReadableDisplay") + serde( + with = "crate::_serde::OptionReadableDisplay", + default, + skip_serializing_if = "Option::is_none", + ) )] + #[cfg_attr(feature = "schemars", schemars(with = "Option"))] pub price: Option, #[cfg_attr( feature = "serde", - serde(with = "crate::_serde::OptionReadableDisplay") + serde( + with = "crate::_serde::OptionReadableDisplay", + default, + skip_serializing_if = "Option::is_none", + ) )] + #[cfg_attr(feature = "schemars", schemars(with = "Option"))] pub budget: Option, } @@ -51,42 +69,122 @@ pub struct UnresolvedGasPayment { feature = "serde", derive(serde_derive::Serialize, serde_derive::Deserialize) )] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct UnresolvedObjectReference { pub object_id: ObjectId, #[cfg_attr( feature = "serde", - serde(with = "crate::_serde::OptionReadableDisplay") + serde( + with = "crate::_serde::OptionReadableDisplay", + default, + skip_serializing_if = "Option::is_none", + ) )] + #[cfg_attr(feature = "schemars", schemars(with = "Option"))] pub version: Option, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] pub digest: Option, } +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr( + feature = "serde", + derive(serde_derive::Serialize, serde_derive::Deserialize), + serde(rename_all = "snake_case") +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub enum UnresolvedInputArgumentKind { + Pure, + Shared, + Receiving, + ImmutableOrOwned, + Immutable, + Owned, + Literal, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] #[cfg_attr( feature = "serde", derive(serde_derive::Serialize, serde_derive::Deserialize) )] -pub enum UnresolvedInputArgument { - // contains no structs or objects - Pure { - #[cfg_attr( - feature = "serde", - serde(with = "::serde_with::As::<::serde_with::Bytes>") - )] - value: Vec, - }, - // A Move object, either immutable, or owned mutable. - ImmutableOrOwned(UnresolvedObjectReference), - // A Move object that's shared. - // SharedObject::mutable controls whether caller asks for a mutable reference to shared object. - Shared { - object_id: ObjectId, - #[cfg_attr( - feature = "serde", - serde(with = "crate::_serde::OptionReadableDisplay") - )] - initial_shared_version: Option, - mutable: Option, - }, - // A Move object that can be received in this transaction. - Receiving(UnresolvedObjectReference), +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +pub struct UnresolvedInputArgument { + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] + pub kind: Option, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] + pub value: Option, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] + pub object_id: Option, + /// Either the `initial_shared_version` if object is a shared object, or the `version` if + /// this is an owned object + #[cfg_attr( + feature = "serde", + serde( + with = "crate::_serde::OptionReadableDisplay", + default, + skip_serializing_if = "Option::is_none", + alias = "initial_shared_version", + ) + )] + #[cfg_attr(feature = "schemars", schemars(with = "Option"))] + pub version: Option, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] + pub digest: Option, + #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))] + pub mutable: Option, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +#[cfg_attr( + feature = "serde", + derive(serde_derive::Serialize, serde_derive::Deserialize), + serde(try_from = "serde_json::Value", into = "serde_json::Value") +)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema), schemars(untagged))] +pub enum UnresolvedValue { + Null, + Bool(bool), + Number(u64), + String(String), + Array(Vec), +} + +#[cfg(feature = "serde")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))] +impl TryFrom for UnresolvedValue { + type Error = &'static str; + + fn try_from(value: serde_json::Value) -> Result { + let v = match value { + serde_json::Value::Null => Self::Null, + serde_json::Value::Bool(b) => Self::Bool(b), + serde_json::Value::Number(n) => { + Self::Number(n.as_u64().ok_or("expected unsigned integer")?) + } + serde_json::Value::String(s) => Self::String(s), + serde_json::Value::Array(a) => Self::Array( + a.into_iter() + .map(Self::try_from) + .collect::>()?, + ), + serde_json::Value::Object(_) => return Err("objects are not supported"), + }; + + Ok(v) + } +} + +#[cfg(feature = "serde")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))] +impl From for serde_json::Value { + fn from(value: UnresolvedValue) -> Self { + match value { + UnresolvedValue::Null => Self::Null, + UnresolvedValue::Bool(b) => Self::Bool(b), + UnresolvedValue::Number(n) => Self::Number(n.into()), + UnresolvedValue::String(s) => Self::String(s), + UnresolvedValue::Array(a) => Self::Array(a.into_iter().map(Into::into).collect()), + } + } }