1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::types::execution_status::ExecutionStatus;
use crate::types::object::Owner;
use crate::types::object::Version;
use crate::types::EpochId;
use crate::types::GasCostSummary;
use crate::types::ObjectId;
use crate::types::ObjectReference;
use crate::types::TransactionDigest;
use crate::types::TransactionEventsDigest;

/// The response from processing a transaction or a certified transaction
#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct TransactionEffectsV1 {
    /// The status of the execution
    #[cfg_attr(feature = "schemars", schemars(flatten))]
    pub 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,
    /// The version that every modified (mutated or deleted) object had before it was modified by
    /// this transaction.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub modified_at_versions: Vec<ModifiedAtVersion>,
    /// The object references of the shared objects used in this transaction. Empty if no shared objects were used.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub shared_objects: Vec<ObjectReference>,
    /// The transaction digest
    pub transaction_digest: TransactionDigest,

    /// ObjectReference and owner of new objects created.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub created: Vec<ObjectReferenceWithOwner>,
    /// ObjectReference and owner of mutated objects, including gas object.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub mutated: Vec<ObjectReferenceWithOwner>,
    /// ObjectReference and owner of objects that are unwrapped in this transaction.
    /// Unwrapped objects are objects that were wrapped into other objects in the past,
    /// and just got extracted out.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub unwrapped: Vec<ObjectReferenceWithOwner>,
    /// Object Refs of objects now deleted (the new refs).
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub deleted: Vec<ObjectReference>,
    /// Object refs of objects previously wrapped in other objects but now deleted.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub unwrapped_then_deleted: Vec<ObjectReference>,
    /// Object refs of objects now wrapped in other objects.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub wrapped: Vec<ObjectReference>,
    /// The updated gas object reference. Have a dedicated field for convenient access.
    /// It's also included in mutated.
    pub gas_object: ObjectReferenceWithOwner,
    /// The digest of the events emitted during execution,
    /// can be None if the transaction does not emit any event.
    pub events_digest: Option<TransactionEventsDigest>,
    /// The set of transaction digests this transaction depends on.
    #[cfg_attr(test, any(proptest::collection::size_range(0..=5).lift()))]
    pub dependencies: Vec<TransactionDigest>,
}

#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct ModifiedAtVersion {
    pub object_id: ObjectId,
    #[cfg_attr(feature = "serde", serde(with = "crate::_serde::ReadableDisplay"))]
    #[cfg_attr(feature = "schemars", schemars(with = "crate::_schemars::U64"))]
    pub version: Version,
}

#[derive(Eq, PartialEq, Clone, Debug)]
#[cfg_attr(
    feature = "serde",
    derive(serde_derive::Serialize, serde_derive::Deserialize)
)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(test, derive(test_strategy::Arbitrary))]
pub struct ObjectReferenceWithOwner {
    pub reference: ObjectReference,
    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 {
    use serde::Deserialize;
    use serde::Deserializer;
    use serde::Serialize;
    use serde::Serializer;

    use super::*;

    #[derive(serde_derive::Serialize)]
    struct ReadableTransactionEffectsV1Ref<'a> {
        #[serde(flatten)]
        status: &'a ExecutionStatus,
        #[serde(with = "crate::_serde::ReadableDisplay")]
        epoch: &'a EpochId,
        gas_used: &'a GasCostSummary,
        modified_at_versions: &'a Vec<ModifiedAtVersion>,
        shared_objects: &'a Vec<ObjectReference>,
        transaction_digest: &'a TransactionDigest,
        created: &'a Vec<ObjectReferenceWithOwner>,
        mutated: &'a Vec<ObjectReferenceWithOwner>,
        unwrapped: &'a Vec<ObjectReferenceWithOwner>,
        deleted: &'a Vec<ObjectReference>,
        unwrapped_then_deleted: &'a Vec<ObjectReference>,
        wrapped: &'a Vec<ObjectReference>,
        gas_object: &'a ObjectReferenceWithOwner,
        events_digest: &'a Option<TransactionEventsDigest>,
        dependencies: &'a Vec<TransactionDigest>,
    }

    #[derive(serde_derive::Deserialize)]
    struct ReadableTransactionEffectsV1 {
        #[serde(flatten)]
        status: ExecutionStatus,
        #[serde(with = "crate::_serde::ReadableDisplay")]
        epoch: EpochId,
        gas_used: GasCostSummary,
        modified_at_versions: Vec<ModifiedAtVersion>,
        shared_objects: Vec<ObjectReference>,
        transaction_digest: TransactionDigest,
        created: Vec<ObjectReferenceWithOwner>,
        mutated: Vec<ObjectReferenceWithOwner>,
        unwrapped: Vec<ObjectReferenceWithOwner>,
        deleted: Vec<ObjectReference>,
        unwrapped_then_deleted: Vec<ObjectReference>,
        wrapped: Vec<ObjectReference>,
        gas_object: ObjectReferenceWithOwner,
        events_digest: Option<TransactionEventsDigest>,
        dependencies: Vec<TransactionDigest>,
    }

    #[derive(serde_derive::Serialize)]
    struct BinaryTransactionEffectsV1Ref<'a> {
        status: &'a ExecutionStatus,
        epoch: &'a EpochId,
        gas_used: &'a GasCostSummary,
        modified_at_versions: &'a Vec<ModifiedAtVersion>,
        shared_objects: &'a Vec<ObjectReference>,
        transaction_digest: &'a TransactionDigest,
        created: &'a Vec<ObjectReferenceWithOwner>,
        mutated: &'a Vec<ObjectReferenceWithOwner>,
        unwrapped: &'a Vec<ObjectReferenceWithOwner>,
        deleted: &'a Vec<ObjectReference>,
        unwrapped_then_deleted: &'a Vec<ObjectReference>,
        wrapped: &'a Vec<ObjectReference>,
        gas_object: &'a ObjectReferenceWithOwner,
        events_digest: &'a Option<TransactionEventsDigest>,
        dependencies: &'a Vec<TransactionDigest>,
    }
    #[derive(serde_derive::Deserialize)]
    struct BinaryTransactionEffectsV1 {
        status: ExecutionStatus,
        epoch: EpochId,
        gas_used: GasCostSummary,
        modified_at_versions: Vec<ModifiedAtVersion>,
        shared_objects: Vec<ObjectReference>,
        transaction_digest: TransactionDigest,
        created: Vec<ObjectReferenceWithOwner>,
        mutated: Vec<ObjectReferenceWithOwner>,
        unwrapped: Vec<ObjectReferenceWithOwner>,
        deleted: Vec<ObjectReference>,
        unwrapped_then_deleted: Vec<ObjectReference>,
        wrapped: Vec<ObjectReference>,
        gas_object: ObjectReferenceWithOwner,
        events_digest: Option<TransactionEventsDigest>,
        dependencies: Vec<TransactionDigest>,
    }

    impl Serialize for TransactionEffectsV1 {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let Self {
                status,
                epoch,
                gas_used,
                modified_at_versions,
                shared_objects,
                transaction_digest,
                created,
                mutated,
                unwrapped,
                deleted,
                unwrapped_then_deleted,
                wrapped,
                gas_object,
                events_digest,
                dependencies,
            } = self;
            if serializer.is_human_readable() {
                let readable = ReadableTransactionEffectsV1Ref {
                    status,
                    epoch,
                    gas_used,
                    modified_at_versions,
                    shared_objects,
                    transaction_digest,
                    created,
                    mutated,
                    unwrapped,
                    deleted,
                    unwrapped_then_deleted,
                    wrapped,
                    gas_object,
                    events_digest,
                    dependencies,
                };
                readable.serialize(serializer)
            } else {
                let binary = BinaryTransactionEffectsV1Ref {
                    status,
                    epoch,
                    gas_used,
                    modified_at_versions,
                    shared_objects,
                    transaction_digest,
                    created,
                    mutated,
                    unwrapped,
                    deleted,
                    unwrapped_then_deleted,
                    wrapped,
                    gas_object,
                    events_digest,
                    dependencies,
                };
                binary.serialize(serializer)
            }
        }
    }

    impl<'de> Deserialize<'de> for TransactionEffectsV1 {
        fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where
            D: Deserializer<'de>,
        {
            if deserializer.is_human_readable() {
                let ReadableTransactionEffectsV1 {
                    status,
                    epoch,
                    gas_used,
                    modified_at_versions,
                    shared_objects,
                    transaction_digest,
                    created,
                    mutated,
                    unwrapped,
                    deleted,
                    unwrapped_then_deleted,
                    wrapped,
                    gas_object,
                    events_digest,
                    dependencies,
                } = Deserialize::deserialize(deserializer)?;
                Ok(Self {
                    status,
                    epoch,
                    gas_used,
                    modified_at_versions,
                    shared_objects,
                    transaction_digest,
                    created,
                    mutated,
                    unwrapped,
                    deleted,
                    unwrapped_then_deleted,
                    wrapped,
                    gas_object,
                    events_digest,
                    dependencies,
                })
            } else {
                let BinaryTransactionEffectsV1 {
                    status,
                    epoch,
                    gas_used,
                    modified_at_versions,
                    shared_objects,
                    transaction_digest,
                    created,
                    mutated,
                    unwrapped,
                    deleted,
                    unwrapped_then_deleted,
                    wrapped,
                    gas_object,
                    events_digest,
                    dependencies,
                } = Deserialize::deserialize(deserializer)?;
                Ok(Self {
                    status,
                    epoch,
                    gas_used,
                    modified_at_versions,
                    shared_objects,
                    transaction_digest,
                    created,
                    mutated,
                    unwrapped,
                    deleted,
                    unwrapped_then_deleted,
                    wrapped,
                    gas_object,
                    events_digest,
                    dependencies,
                })
            }
        }
    }
}