Skip to content

Commit

Permalink
sui-sdk-types: rename types and add constructors for unresolved::Value (
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten authored Nov 19, 2024
1 parent ff5ccfe commit 83feb73
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 88 deletions.
2 changes: 1 addition & 1 deletion crates/sui-sdk-types/src/types/address.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(
feature = "serde",
derive(serde_derive::Serialize, serde_derive::Deserialize)
Expand Down
28 changes: 28 additions & 0 deletions crates/sui-sdk-types/src/types/effects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub use v2::TransactionEffectsV2;
pub use v2::UnchangedSharedKind;
pub use v2::UnchangedSharedObject;

use crate::types::execution_status::ExecutionStatus;

/// The response from processing a transaction or a certified transaction
#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(
Expand All @@ -28,6 +30,32 @@ pub enum TransactionEffects {
V2(Box<TransactionEffectsV2>),
}

impl TransactionEffects {
/// Return the status of the transaction.
pub fn status(&self) -> &ExecutionStatus {
match self {
TransactionEffects::V1(e) => e.status(),
TransactionEffects::V2(e) => e.status(),
}
}

/// Return the epoch in which this transaction was executed.
pub fn epoch(&self) -> u64 {
match self {
TransactionEffects::V1(e) => e.epoch(),
TransactionEffects::V2(e) => e.epoch(),
}
}

/// Return the gas cost summary of the transaction.
pub fn gas_summary(&self) -> &crate::types::gas::GasCostSummary {
match self {
TransactionEffects::V1(e) => e.gas_summary(),
TransactionEffects::V2(e) => e.gas_summary(),
}
}
}

#[cfg(feature = "serde")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
mod serialization {
Expand Down
17 changes: 17 additions & 0 deletions crates/sui-sdk-types/src/types/effects/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ pub struct ObjectReferenceWithOwner {
pub owner: Owner,
}

impl TransactionEffectsV1 {
/// The status of the execution
pub fn status(&self) -> &ExecutionStatus {
&self.status
}

/// The epoch when this transaction was executed.
pub fn epoch(&self) -> EpochId {
self.epoch
}

/// The gas used in this transaction.
pub fn gas_summary(&self) -> &GasCostSummary {
&self.gas_used
}
}

#[cfg(feature = "serde")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
mod serialization {
Expand Down
23 changes: 20 additions & 3 deletions crates/sui-sdk-types/src/types/effects/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use crate::types::TransactionEventsDigest;
pub struct TransactionEffectsV2 {
/// The status of the execution
#[cfg_attr(feature = "schemars", schemars(flatten))]
pub status: ExecutionStatus,
status: ExecutionStatus,
/// The epoch when this transaction was executed.
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::U64"))]
pub epoch: EpochId,
pub gas_used: GasCostSummary,
epoch: EpochId,
gas_used: GasCostSummary,
/// The transaction digest
pub transaction_digest: TransactionDigest,
/// The updated gas object reference, as an index into the `changed_objects` vector.
Expand Down Expand Up @@ -185,6 +185,23 @@ pub enum IdOperation {
Deleted,
}

impl TransactionEffectsV2 {
/// The status of the execution
pub fn status(&self) -> &ExecutionStatus {
&self.status
}

/// The epoch when this transaction was executed.
pub fn epoch(&self) -> EpochId {
self.epoch
}

/// The gas used in this transaction.
pub fn gas_summary(&self) -> &GasCostSummary {
&self.gas_used
}
}

#[cfg(feature = "serde")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
mod serialization {
Expand Down
10 changes: 2 additions & 8 deletions crates/sui-sdk-types/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub use object::TypeOrigin;
pub use object::UpgradeInfo;
pub use object::Version;
pub use object_id::ObjectId;
pub use transaction::unresolved;
pub use transaction::ActiveJwk;
pub use transaction::Argument;
pub use transaction::AuthenticatorStateExpire;
Expand All @@ -122,7 +123,7 @@ pub use transaction::ConsensusDeterminedVersionAssignments;
pub use transaction::EndOfEpochTransactionKind;
pub use transaction::GasPayment;
pub use transaction::GenesisTransaction;
pub use transaction::InputArgument;
pub use transaction::Input;
pub use transaction::MakeMoveVector;
pub use transaction::MergeCoins;
pub use transaction::MoveCall;
Expand All @@ -136,13 +137,6 @@ pub use transaction::Transaction;
pub use transaction::TransactionExpiration;
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: 1 addition & 1 deletion crates/sui-sdk-types/src/types/serialization_proptests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ serialization_test!(VersionAssignment);
serialization_test!(EndOfEpochTransactionKind);
serialization_test!(GasPayment);
serialization_test!(GenesisTransaction);
serialization_test!(InputArgument);
serialization_test!(Input);
serialization_test!(MakeMoveVector);
serialization_test!(MergeCoins);
serialization_test!(MoveCall);
Expand Down
26 changes: 15 additions & 11 deletions crates/sui-sdk-types/src/types/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ mod serialization;
#[cfg_attr(doc_cfg, doc(cfg(feature = "serde")))]
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;
pub mod unresolved;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
Expand All @@ -51,10 +44,11 @@ pub struct SignedTransaction {
pub signatures: Vec<UserSignature>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub enum TransactionExpiration {
/// The transaction has no expiration
#[default]
None,
/// Validators wont sign a transaction unless the expiration Epoch
/// is greater than or equal to the current epoch
Expand Down Expand Up @@ -442,7 +436,7 @@ pub struct GenesisTransaction {
pub struct ProgrammableTransaction {
/// Input objects or primitive values
#[cfg_attr(test, any(proptest::collection::size_range(0..=10).lift()))]
pub inputs: Vec<InputArgument>,
pub inputs: Vec<Input>,
/// The commands to be executed sequentially. A failure in any command will
/// result in the failure of the entire transaction.
#[cfg_attr(test, any(proptest::collection::size_range(0..=10).lift()))]
Expand All @@ -456,7 +450,7 @@ pub struct ProgrammableTransaction {
schemars(tag = "type", rename_all = "snake_case")
)]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub enum InputArgument {
pub enum Input {
// contains no structs or objects
Pure {
#[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::Base64"))]
Expand Down Expand Up @@ -626,6 +620,16 @@ pub enum Argument {
NestedResult(u16, u16),
}

impl Argument {
/// Turn a Result into a NestedResult. If the argument is not a Result, returns None.
pub fn nested(&self, ix: u16) -> Option<Argument> {
match self {
Argument::Result(i) => Some(Argument::NestedResult(*i, ix)),
_ => None,
}
}
}

/// The command for calling a Move function, either an entry function or a public
/// function (which cannot return references).
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
68 changes: 30 additions & 38 deletions crates/sui-sdk-types/src/types/transaction/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,13 @@ mod version_assignments {
}

mod input_argument {
use crate::types::transaction::InputArgument;
use crate::types::transaction::Input;

use super::*;

#[derive(serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum ReadableInputArgument {
enum ReadableInput {
Pure {
#[serde(with = "::serde_with::As::<crate::_serde::Base64Encoded>")]
value: Vec<u8>,
Expand Down Expand Up @@ -645,38 +645,36 @@ mod input_argument {
Receiving(ObjectReference),
}

impl Serialize for InputArgument {
impl Serialize for Input {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if serializer.is_human_readable() {
let readable = match self.clone() {
InputArgument::Pure { value } => ReadableInputArgument::Pure { value },
InputArgument::ImmutableOrOwned(object_ref) => {
ReadableInputArgument::ImmutableOrOwned(object_ref)
Input::Pure { value } => ReadableInput::Pure { value },
Input::ImmutableOrOwned(object_ref) => {
ReadableInput::ImmutableOrOwned(object_ref)
}
InputArgument::Shared {
Input::Shared {
object_id,
initial_shared_version,
mutable,
} => ReadableInputArgument::Shared {
} => ReadableInput::Shared {
object_id,
initial_shared_version,
mutable,
},
InputArgument::Receiving(object_ref) => {
ReadableInputArgument::Receiving(object_ref)
}
Input::Receiving(object_ref) => ReadableInput::Receiving(object_ref),
};
readable.serialize(serializer)
} else {
let binary = match self.clone() {
InputArgument::Pure { value } => CallArg::Pure(value),
InputArgument::ImmutableOrOwned(object_ref) => {
Input::Pure { value } => CallArg::Pure(value),
Input::ImmutableOrOwned(object_ref) => {
CallArg::Object(ObjectArg::ImmutableOrOwned(object_ref))
}
InputArgument::Shared {
Input::Shared {
object_id,
initial_shared_version,
mutable,
Expand All @@ -685,7 +683,7 @@ mod input_argument {
initial_shared_version,
mutable,
}),
InputArgument::Receiving(object_ref) => {
Input::Receiving(object_ref) => {
CallArg::Object(ObjectArg::Receiving(object_ref))
}
};
Expand All @@ -694,47 +692,45 @@ mod input_argument {
}
}

impl<'de> Deserialize<'de> for InputArgument {
impl<'de> Deserialize<'de> for Input {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
if deserializer.is_human_readable() {
ReadableInputArgument::deserialize(deserializer).map(|readable| match readable {
ReadableInputArgument::Pure { value } => InputArgument::Pure { value },
ReadableInputArgument::ImmutableOrOwned(object_ref) => {
InputArgument::ImmutableOrOwned(object_ref)
ReadableInput::deserialize(deserializer).map(|readable| match readable {
ReadableInput::Pure { value } => Input::Pure { value },
ReadableInput::ImmutableOrOwned(object_ref) => {
Input::ImmutableOrOwned(object_ref)
}
ReadableInputArgument::Shared {
ReadableInput::Shared {
object_id,
initial_shared_version,
mutable,
} => InputArgument::Shared {
} => Input::Shared {
object_id,
initial_shared_version,
mutable,
},
ReadableInputArgument::Receiving(object_ref) => {
InputArgument::Receiving(object_ref)
}
ReadableInput::Receiving(object_ref) => Input::Receiving(object_ref),
})
} else {
CallArg::deserialize(deserializer).map(|binary| match binary {
CallArg::Pure(value) => InputArgument::Pure { value },
CallArg::Pure(value) => Input::Pure { value },
CallArg::Object(ObjectArg::ImmutableOrOwned(object_ref)) => {
InputArgument::ImmutableOrOwned(object_ref)
Input::ImmutableOrOwned(object_ref)
}
CallArg::Object(ObjectArg::Shared {
object_id,
initial_shared_version,
mutable,
}) => InputArgument::Shared {
}) => Input::Shared {
object_id,
initial_shared_version,
mutable,
},
CallArg::Object(ObjectArg::Receiving(object_ref)) => {
InputArgument::Receiving(object_ref)
Input::Receiving(object_ref)
}
})
}
Expand Down Expand Up @@ -1227,7 +1223,7 @@ mod test {
use base64ct::Encoding;

use crate::types::transaction::Argument;
use crate::types::transaction::InputArgument;
use crate::types::transaction::Input;
use crate::types::transaction::Transaction;
use crate::types::ObjectDigest;
use crate::types::ObjectId;
Expand Down Expand Up @@ -1262,7 +1258,7 @@ mod test {
fn input_argument() {
let test_cases = [
(
InputArgument::Pure {
Input::Pure {
value: vec![1, 2, 3, 4],
},
serde_json::json!({
Expand All @@ -1271,7 +1267,7 @@ mod test {
}),
),
(
InputArgument::ImmutableOrOwned(ObjectReference::new(
Input::ImmutableOrOwned(ObjectReference::new(
ObjectId::ZERO,
1,
ObjectDigest::ZERO,
Expand All @@ -1284,7 +1280,7 @@ mod test {
}),
),
(
InputArgument::Shared {
Input::Shared {
object_id: ObjectId::ZERO,
initial_shared_version: 1,
mutable: true,
Expand All @@ -1297,11 +1293,7 @@ mod test {
}),
),
(
InputArgument::Receiving(ObjectReference::new(
ObjectId::ZERO,
1,
ObjectDigest::ZERO,
)),
Input::Receiving(ObjectReference::new(ObjectId::ZERO, 1, ObjectDigest::ZERO)),
serde_json::json!({
"type": "receiving",
"object_id": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down
Loading

0 comments on commit 83feb73

Please sign in to comment.