Skip to content

Commit cbae283

Browse files
committed
types: improve format of UnresolvedTransaction
1 parent 2699101 commit cbae283

File tree

3 files changed

+125
-27
lines changed

3 files changed

+125
-27
lines changed

crates/sui-sdk-types/src/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ pub use transaction::TransactionKind;
136136
pub use transaction::TransferObjects;
137137
pub use transaction::UnresolvedGasPayment;
138138
pub use transaction::UnresolvedInputArgument;
139+
pub use transaction::UnresolvedInputArgumentKind;
139140
pub use transaction::UnresolvedObjectReference;
140141
pub use transaction::UnresolvedProgrammableTransaction;
141142
pub use transaction::UnresolvedTransaction;
143+
pub use transaction::UnresolvedValue;
142144
pub use transaction::Upgrade;
143145
pub use transaction::VersionAssignment;
144146
pub use type_tag::Identifier;

crates/sui-sdk-types/src/types/transaction/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ pub(crate) use serialization::SignedTransactionWithIntentMessage;
2424
mod unresolved;
2525
pub use unresolved::UnresolvedGasPayment;
2626
pub use unresolved::UnresolvedInputArgument;
27+
pub use unresolved::UnresolvedInputArgumentKind;
2728
pub use unresolved::UnresolvedObjectReference;
2829
pub use unresolved::UnresolvedProgrammableTransaction;
2930
pub use unresolved::UnresolvedTransaction;
31+
pub use unresolved::UnresolvedValue;
3032

3133
#[derive(Clone, Debug, PartialEq, Eq)]
3234
#[cfg_attr(test, derive(test_strategy::Arbitrary))]

crates/sui-sdk-types/src/types/transaction/unresolved.rs

Lines changed: 121 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ use super::TransactionExpiration;
1111
feature = "serde",
1212
derive(serde_derive::Serialize, serde_derive::Deserialize)
1313
)]
14+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1415
pub struct UnresolvedTransaction {
1516
#[cfg_attr(feature = "serde", serde(flatten))]
1617
pub ptb: UnresolvedProgrammableTransaction,
1718
pub sender: Address,
19+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
1820
pub gas_payment: Option<UnresolvedGasPayment>,
1921
pub expiration: TransactionExpiration,
2022
}
@@ -23,6 +25,7 @@ pub struct UnresolvedTransaction {
2325
feature = "serde",
2426
derive(serde_derive::Serialize, serde_derive::Deserialize)
2527
)]
28+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
2629
pub struct UnresolvedProgrammableTransaction {
2730
pub inputs: Vec<UnresolvedInputArgument>,
2831
pub commands: Vec<Command>,
@@ -32,61 +35,152 @@ pub struct UnresolvedProgrammableTransaction {
3235
feature = "serde",
3336
derive(serde_derive::Serialize, serde_derive::Deserialize)
3437
)]
38+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3539
pub struct UnresolvedGasPayment {
3640
pub objects: Vec<UnresolvedObjectReference>,
3741
pub owner: Address,
3842
#[cfg_attr(
3943
feature = "serde",
40-
serde(with = "crate::_serde::OptionReadableDisplay")
44+
serde(
45+
with = "crate::_serde::OptionReadableDisplay",
46+
default,
47+
skip_serializing_if = "Option::is_none",
48+
)
4149
)]
50+
#[cfg_attr(feature = "schemars", schemars(with = "Option<crate::_schemars::U64>"))]
4251
pub price: Option<u64>,
4352
#[cfg_attr(
4453
feature = "serde",
45-
serde(with = "crate::_serde::OptionReadableDisplay")
54+
serde(
55+
with = "crate::_serde::OptionReadableDisplay",
56+
default,
57+
skip_serializing_if = "Option::is_none",
58+
)
4659
)]
60+
#[cfg_attr(feature = "schemars", schemars(with = "Option<crate::_schemars::U64>"))]
4761
pub budget: Option<u64>,
4862
}
4963

5064
#[cfg_attr(
5165
feature = "serde",
5266
derive(serde_derive::Serialize, serde_derive::Deserialize)
5367
)]
68+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
5469
pub struct UnresolvedObjectReference {
5570
pub object_id: ObjectId,
5671
#[cfg_attr(
5772
feature = "serde",
58-
serde(with = "crate::_serde::OptionReadableDisplay")
73+
serde(
74+
with = "crate::_serde::OptionReadableDisplay",
75+
default,
76+
skip_serializing_if = "Option::is_none",
77+
)
5978
)]
79+
#[cfg_attr(feature = "schemars", schemars(with = "Option<crate::_schemars::U64>"))]
6080
pub version: Option<Version>,
81+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
6182
pub digest: Option<ObjectDigest>,
6283
}
6384

85+
#[derive(Clone, Debug, PartialEq, Eq)]
86+
#[cfg_attr(
87+
feature = "serde",
88+
derive(serde_derive::Serialize, serde_derive::Deserialize),
89+
serde(rename_all = "snake_case")
90+
)]
91+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
92+
pub enum UnresolvedInputArgumentKind {
93+
Pure,
94+
Shared,
95+
Receiving,
96+
ImmutableOrOwned,
97+
Immutable,
98+
Owned,
99+
Literal,
100+
}
101+
102+
#[derive(Clone, Debug, Default, PartialEq, Eq)]
64103
#[cfg_attr(
65104
feature = "serde",
66105
derive(serde_derive::Serialize, serde_derive::Deserialize)
67106
)]
68-
pub enum UnresolvedInputArgument {
69-
// contains no structs or objects
70-
Pure {
71-
#[cfg_attr(
72-
feature = "serde",
73-
serde(with = "::serde_with::As::<::serde_with::Bytes>")
74-
)]
75-
value: Vec<u8>,
76-
},
77-
// A Move object, either immutable, or owned mutable.
78-
ImmutableOrOwned(UnresolvedObjectReference),
79-
// A Move object that's shared.
80-
// SharedObject::mutable controls whether caller asks for a mutable reference to shared object.
81-
Shared {
82-
object_id: ObjectId,
83-
#[cfg_attr(
84-
feature = "serde",
85-
serde(with = "crate::_serde::OptionReadableDisplay")
86-
)]
87-
initial_shared_version: Option<u64>,
88-
mutable: Option<bool>,
89-
},
90-
// A Move object that can be received in this transaction.
91-
Receiving(UnresolvedObjectReference),
107+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
108+
pub struct UnresolvedInputArgument {
109+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
110+
pub kind: Option<UnresolvedInputArgumentKind>,
111+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
112+
pub value: Option<UnresolvedValue>,
113+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
114+
pub object_id: Option<ObjectId>,
115+
/// Either the `initial_shared_version` if object is a shared object, or the `version` if
116+
/// this is an owned object
117+
#[cfg_attr(
118+
feature = "serde",
119+
serde(
120+
with = "crate::_serde::OptionReadableDisplay",
121+
default,
122+
skip_serializing_if = "Option::is_none",
123+
alias = "initial_shared_version",
124+
)
125+
)]
126+
#[cfg_attr(feature = "schemars", schemars(with = "Option<crate::_schemars::U64>"))]
127+
pub version: Option<Version>,
128+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
129+
pub digest: Option<ObjectDigest>,
130+
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none",))]
131+
pub mutable: Option<bool>,
132+
}
133+
134+
#[derive(Clone, Debug, PartialEq, Eq)]
135+
#[cfg_attr(
136+
feature = "serde",
137+
derive(serde_derive::Serialize, serde_derive::Deserialize),
138+
serde(try_from = "serde_json::Value", into = "serde_json::Value")
139+
)]
140+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
141+
pub enum UnresolvedValue {
142+
Null,
143+
Bool(bool),
144+
Number(u64),
145+
String(String),
146+
Array(Vec<UnresolvedValue>),
147+
}
148+
149+
#[cfg(feature = "serde")]
150+
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
151+
impl TryFrom<serde_json::Value> for UnresolvedValue {
152+
type Error = &'static str;
153+
154+
fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
155+
let v = match value {
156+
serde_json::Value::Null => Self::Null,
157+
serde_json::Value::Bool(b) => Self::Bool(b),
158+
serde_json::Value::Number(n) => {
159+
Self::Number(n.as_u64().ok_or("expected unsigned integer")?)
160+
}
161+
serde_json::Value::String(s) => Self::String(s),
162+
serde_json::Value::Array(a) => Self::Array(
163+
a.into_iter()
164+
.map(Self::try_from)
165+
.collect::<Result<_, _>>()?,
166+
),
167+
serde_json::Value::Object(_) => return Err("objects are not supported"),
168+
};
169+
170+
Ok(v)
171+
}
172+
}
173+
174+
#[cfg(feature = "serde")]
175+
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
176+
impl From<UnresolvedValue> for serde_json::Value {
177+
fn from(value: UnresolvedValue) -> Self {
178+
match value {
179+
UnresolvedValue::Null => Self::Null,
180+
UnresolvedValue::Bool(b) => Self::Bool(b),
181+
UnresolvedValue::Number(n) => Self::Number(n.into()),
182+
UnresolvedValue::String(s) => Self::String(s),
183+
UnresolvedValue::Array(a) => Self::Array(a.into_iter().map(Into::into).collect()),
184+
}
185+
}
92186
}

0 commit comments

Comments
 (0)