Skip to content

Commit

Permalink
types: improve format of UnresolvedTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwill committed Oct 15, 2024
1 parent 2699101 commit cbae283
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 27 deletions.
2 changes: 2 additions & 0 deletions crates/sui-sdk-types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-sdk-types/src/types/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
148 changes: 121 additions & 27 deletions crates/sui-sdk-types/src/types/transaction/unresolved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnresolvedGasPayment>,
pub expiration: TransactionExpiration,
}
Expand All @@ -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<UnresolvedInputArgument>,
pub commands: Vec<Command>,
Expand All @@ -32,61 +35,152 @@ pub struct UnresolvedProgrammableTransaction {
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct UnresolvedGasPayment {
pub objects: Vec<UnresolvedObjectReference>,
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<crate::_schemars::U64>"))]
pub price: Option<u64>,
#[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<crate::_schemars::U64>"))]
pub budget: Option<u64>,
}

#[cfg_attr(
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<crate::_schemars::U64>"))]
pub version: Option<Version>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
pub digest: Option<ObjectDigest>,
}

#[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<u8>,
},
// 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<u64>,
mutable: Option<bool>,
},
// 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<UnresolvedInputArgumentKind>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
pub value: Option<UnresolvedValue>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
pub object_id: Option<ObjectId>,
/// 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<crate::_schemars::U64>"))]
pub version: Option<Version>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
pub digest: Option<ObjectDigest>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
pub mutable: Option<bool>,
}

#[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))]
pub enum UnresolvedValue {
Null,
Bool(bool),
Number(u64),
String(String),
Array(Vec<UnresolvedValue>),
}

#[cfg(feature = "serde")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
impl TryFrom<serde_json::Value> for UnresolvedValue {
type Error = &'static str;

fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
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::<Result<_, _>>()?,
),
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<UnresolvedValue> 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()),
}
}
}

0 comments on commit cbae283

Please sign in to comment.