Skip to content

Commit c0fb8ac

Browse files
feat: Command improvements (#1118)
* feat: Command improvements (#968) * feat: Command improvements * add methods * add ctors * Display * clean and fix displays * add delimiters * add parenthesis to Some * binary rename * remove ref * impl PartialEq<Identifier> for String * add new_ prefix * remove too specific methods * Add comments * add write_sep doc
1 parent b4b40f5 commit c0fb8ac

3 files changed

Lines changed: 214 additions & 8 deletions

File tree

crates/iota-sdk-types/src/move_core/identifier.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ impl PartialEq<Identifier> for str {
246246
}
247247
}
248248

249+
impl PartialEq<Identifier> for String {
250+
fn eq(&self, other: &Identifier) -> bool {
251+
self == other.as_str()
252+
}
253+
}
254+
249255
impl PartialEq<String> for Identifier {
250256
fn eq(&self, other: &String) -> bool {
251257
self.as_str() == other

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

Lines changed: 206 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ impl SharedObjectReference {
913913
/// command-make-move-vector = %d05 make-move-vector
914914
/// command-upgrade = %d06 upgrade
915915
/// ```
916-
#[derive(Clone, Debug, PartialEq, Eq)]
916+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
917917
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
918918
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
919919
#[non_exhaustive]
@@ -959,6 +959,204 @@ impl Command {
959959
MakeMoveVector,
960960
Upgrade,
961961
);
962+
963+
/// Create a command to call a Move function.
964+
pub fn new_move_call(
965+
package: ObjectId,
966+
module: Identifier,
967+
function: Identifier,
968+
type_arguments: Vec<TypeTag>,
969+
arguments: Vec<Argument>,
970+
) -> Self {
971+
Command::MoveCall(MoveCall {
972+
package,
973+
module,
974+
function,
975+
type_arguments,
976+
arguments,
977+
})
978+
}
979+
980+
/// Create a command to transfer objects to an address.
981+
pub fn new_transfer_objects(objects: Vec<Argument>, address: Argument) -> Self {
982+
Command::TransferObjects(TransferObjects { objects, address })
983+
}
984+
985+
/// Create a command to split a coin into multiple coins by amounts.
986+
pub fn new_split_coins(coin: Argument, amounts: Vec<Argument>) -> Self {
987+
Command::SplitCoins(SplitCoins { coin, amounts })
988+
}
989+
990+
/// Create a command to merge multiple coins into one.
991+
pub fn new_merge_coins(coin: Argument, coins_to_merge: Vec<Argument>) -> Self {
992+
Command::MergeCoins(MergeCoins {
993+
coin,
994+
coins_to_merge,
995+
})
996+
}
997+
998+
/// Create a command to publish a new Move package.
999+
pub fn new_publish(modules: Vec<Vec<u8>>, dependencies: Vec<ObjectId>) -> Self {
1000+
Command::Publish(Publish {
1001+
modules,
1002+
dependencies,
1003+
})
1004+
}
1005+
1006+
/// Create a command to construct a Move vector from elements.
1007+
pub fn new_make_move_vector(type_: Option<TypeTag>, elements: Vec<Argument>) -> Self {
1008+
Command::MakeMoveVector(MakeMoveVector { type_, elements })
1009+
}
1010+
1011+
/// Create a command to upgrade an existing Move package.
1012+
pub fn new_upgrade(
1013+
modules: Vec<Vec<u8>>,
1014+
dependencies: Vec<ObjectId>,
1015+
package: ObjectId,
1016+
ticket: Argument,
1017+
) -> Self {
1018+
Command::Upgrade(Upgrade {
1019+
modules,
1020+
dependencies,
1021+
package,
1022+
ticket,
1023+
})
1024+
}
1025+
}
1026+
1027+
/// Writes `items` to `f`, separated by `sep` and optionally wrapped in
1028+
/// `delimiters` (e.g. `("[", "]")`). If the iterator is empty, nothing is
1029+
/// written — not even the delimiters.
1030+
pub fn write_sep<T: core::fmt::Display>(
1031+
f: &mut core::fmt::Formatter<'_>,
1032+
items: impl IntoIterator<Item = T>,
1033+
delimiters: Option<(&str, &str)>,
1034+
sep: &str,
1035+
) -> std::fmt::Result {
1036+
let mut xs = items.into_iter();
1037+
let Some(x) = xs.next() else {
1038+
return Ok(());
1039+
};
1040+
if let Some((l, _)) = delimiters {
1041+
write!(f, "{l}")?;
1042+
}
1043+
write!(f, "{x}")?;
1044+
for x in xs {
1045+
write!(f, "{sep}{x}")?;
1046+
}
1047+
if let Some((_, r)) = delimiters {
1048+
write!(f, "{r}")?;
1049+
}
1050+
Ok(())
1051+
}
1052+
1053+
impl core::fmt::Display for MoveCall {
1054+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1055+
let Self {
1056+
package,
1057+
module,
1058+
function,
1059+
type_arguments,
1060+
arguments,
1061+
} = self;
1062+
write!(f, "MoveCall(")?;
1063+
write!(f, "{package}::{module}::{function}")?;
1064+
if !type_arguments.is_empty() {
1065+
write_sep(f, type_arguments, Some(("<", ">")), ",")?;
1066+
}
1067+
write_sep(f, arguments, Some(("(", ")")), ",")?;
1068+
write!(f, ")")
1069+
}
1070+
}
1071+
1072+
impl core::fmt::Display for TransferObjects {
1073+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1074+
let Self { objects, address } = self;
1075+
1076+
write!(f, "TransferObjects(")?;
1077+
write_sep(f, objects, Some(("[", "]")), ",")?;
1078+
write!(f, ",{address})")
1079+
}
1080+
}
1081+
1082+
impl core::fmt::Display for SplitCoins {
1083+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1084+
let Self { coin, amounts } = self;
1085+
1086+
write!(f, "SplitCoins({coin},")?;
1087+
write_sep(f, amounts, Some(("[", "]")), ",")?;
1088+
write!(f, ")")
1089+
}
1090+
}
1091+
1092+
impl core::fmt::Display for MergeCoins {
1093+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1094+
let Self {
1095+
coin,
1096+
coins_to_merge,
1097+
} = self;
1098+
1099+
write!(f, "MergeCoins({coin},")?;
1100+
write_sep(f, coins_to_merge, Some(("[", "]")), ",")?;
1101+
write!(f, ")")
1102+
}
1103+
}
1104+
1105+
impl core::fmt::Display for Publish {
1106+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1107+
let Self { dependencies, .. } = self;
1108+
1109+
write!(f, "Publish(_,")?;
1110+
write_sep(f, dependencies, Some(("[", "]")), ",")?;
1111+
write!(f, ")")
1112+
}
1113+
}
1114+
1115+
impl core::fmt::Display for MakeMoveVector {
1116+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1117+
let Self { type_, elements } = self;
1118+
1119+
write!(f, "MakeMoveVector(")?;
1120+
if let Some(ty) = type_ {
1121+
write!(f, "Some({ty})")?;
1122+
} else {
1123+
write!(f, "None")?;
1124+
}
1125+
write!(f, ",")?;
1126+
write_sep(f, elements, Some(("[", "]")), ",")?;
1127+
write!(f, ")")
1128+
}
1129+
}
1130+
1131+
impl core::fmt::Display for Upgrade {
1132+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1133+
let Self {
1134+
dependencies,
1135+
package,
1136+
ticket,
1137+
..
1138+
} = self;
1139+
1140+
write!(f, "Upgrade(_,")?;
1141+
write_sep(f, dependencies, Some(("[", "]")), ",")?;
1142+
write!(f, ", {package}")?;
1143+
write!(f, ", {ticket}")?;
1144+
write!(f, ")")
1145+
}
1146+
}
1147+
1148+
impl core::fmt::Display for Command {
1149+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
1150+
match self {
1151+
Command::MoveCall(cmd) => write!(f, "{cmd}"),
1152+
Command::TransferObjects(cmd) => write!(f, "{cmd}"),
1153+
Command::SplitCoins(cmd) => write!(f, "{cmd}"),
1154+
Command::MergeCoins(cmd) => write!(f, "{cmd}"),
1155+
Command::Publish(cmd) => write!(f, "{cmd}"),
1156+
Command::MakeMoveVector(cmd) => write!(f, "{cmd}"),
1157+
Command::Upgrade(cmd) => write!(f, "{cmd}"),
1158+
}
1159+
}
9621160
}
9631161

9641162
/// Command to transfer ownership of a set of objects to an address
@@ -970,7 +1168,7 @@ impl Command {
9701168
/// ```text
9711169
/// transfer-objects = (vector argument) argument
9721170
/// ```
973-
#[derive(Clone, Debug, PartialEq, Eq)]
1171+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
9741172
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9751173
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
9761174
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
@@ -991,7 +1189,7 @@ pub struct TransferObjects {
9911189
/// ```text
9921190
/// split-coins = argument (vector argument)
9931191
/// ```
994-
#[derive(Clone, Debug, PartialEq, Eq)]
1192+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
9951193
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9961194
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
9971195
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
@@ -1012,7 +1210,7 @@ pub struct SplitCoins {
10121210
/// ```text
10131211
/// merge-coins = argument (vector argument)
10141212
/// ```
1015-
#[derive(Clone, Debug, PartialEq, Eq)]
1213+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10161214
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10171215
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
10181216
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
@@ -1036,7 +1234,7 @@ pub struct MergeCoins {
10361234
/// publish = (vector bytes) ; the serialized move modules
10371235
/// (vector object-id) ; the set of package dependencies
10381236
/// ```
1039-
#[derive(Clone, Debug, PartialEq, Eq)]
1237+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10401238
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10411239
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
10421240
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
@@ -1062,7 +1260,7 @@ pub struct Publish {
10621260
/// ```text
10631261
/// make-move-vector = (option type-tag) (vector argument)
10641262
/// ```
1065-
#[derive(Clone, Debug, PartialEq, Eq)]
1263+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10661264
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10671265
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
10681266
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
@@ -1090,7 +1288,7 @@ pub struct MakeMoveVector {
10901288
/// object-id ; package-id of the package
10911289
/// argument ; upgrade ticket
10921290
/// ```
1093-
#[derive(Clone, Debug, PartialEq, Eq)]
1291+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10941292
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10951293
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
10961294
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]
@@ -1225,7 +1423,7 @@ impl Argument {
12251423
/// (vector type-tag) ; type arguments, if any
12261424
/// (vector argument) ; input arguments
12271425
/// ```
1228-
#[derive(Clone, Debug, PartialEq, Eq)]
1426+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
12291427
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12301428
#[cfg_attr(feature = "proptest", derive(test_strategy::Arbitrary))]
12311429
#[cfg_attr(feature = "bcs-schema", derive(iota_bcs_schema::BcsSchema))]

crates/iota-sdk-types/src/transaction/serialization.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ mod command {
703703
}
704704

705705
#[derive(serde::Serialize)]
706+
#[serde(rename = "Command")]
706707
enum BinaryCommandRef<'a> {
707708
MoveCall(&'a MoveCall),
708709
TransferObjects(&'a TransferObjects),
@@ -714,6 +715,7 @@ mod command {
714715
}
715716

716717
#[derive(serde::Deserialize)]
718+
#[serde(rename = "Command")]
717719
enum BinaryCommand {
718720
MoveCall(MoveCall),
719721
TransferObjects(TransferObjects),

0 commit comments

Comments
 (0)