11module movy ::cheats ;
22
33use sui::tx_context::TxContext ;
4+ use sui::vec_map::VecMap ;
45
56const TX_HASH_LENGTH : u64 = 32 ;
67
8+
9+ /// Attempted to return an object to the inventory that was not previously removed from the
10+ /// inventory during the current transaction. Can happen if the user attempts to call
11+ /// `return_to_address` on a locally constructed object rather than one returned from a
12+ /// `test_scenario` function such as `take_from_address`.
13+ const ECantReturnObject : u64 = 2 ;
14+
15+ /// Attempted to retrieve an object of a particular type from the inventory, but it is empty.
16+ /// Can happen if the user already transferred the object or a previous transaction failed to
17+ /// transfer the object to the user.
18+ const EEmptyInventory : u64 = 3 ;
19+
20+ /// The effects of a transaction
21+ public struct TransactionEffects has drop {
22+ /// The objects created this transaction
23+ created: vector <ID >,
24+ /// The objects written/modified this transaction
25+ written: vector <ID >,
26+ /// The objects deleted this transaction
27+ deleted: vector <ID >,
28+ /// The objects transferred to an account this transaction
29+ transferred_to_account: VecMap <ID , /* owner */ address >,
30+ /// The objects transferred to an object this transaction
31+ transferred_to_object: VecMap <ID , /* owner */ ID >,
32+ /// The objects shared this transaction
33+ shared: vector <ID >,
34+ /// The objects frozen this transaction
35+ frozen: vector <ID >,
36+ /// The number of user events emitted this transaction
37+ num_user_events: u64 ,
38+ }
39+
740/// A cheat scenario for mocking a multi-transaction Sui execution
841public struct CheatScenario {
942 txn_number: u64 ,
@@ -18,11 +51,15 @@ public fun begin(sender: address): CheatScenario {
1851 }
1952}
2053
54+ public fun ctx (scenario: &mut CheatScenario ): &mut TxContext {
55+ &mut scenario.ctx
56+ }
57+
2158/// Ends the test scenario
2259/// Returns the results from the final transaction
2360/// Will abort if shared or immutable objects were deleted, transferred, or wrapped.
2461/// Will abort if TransactionEffects cannot be generated
25- public fun end (scenario: CheatScenario ) {
62+ public fun end (scenario: CheatScenario ): TransactionEffects {
2663 let CheatScenario { txn_number: _, ctx: _ } = scenario;
2764 end_transaction ()
2865}
@@ -36,7 +73,7 @@ public fun end(scenario: CheatScenario) {
3673/// Returns the results from the previous transaction
3774/// Will abort if shared or immutable objects were deleted, transferred, or wrapped.
3875/// Will abort if TransactionEffects cannot be generated
39- public fun next_tx (scenario: &mut CheatScenario , sender: address ) {
76+ public fun next_tx (scenario: &mut CheatScenario , sender: address ): TransactionEffects {
4077 // create a seed for new transaction digest to ensure that this tx has a different
4178 // digest (and consequently, different object ID's) than the previous tx
4279 scenario.txn_number = scenario.txn_number + 1 ;
@@ -105,16 +142,34 @@ native fun new_tx_context(
105142 ids_created: u64
106143): TxContext ;
107144
145+ /// Helper combining `take_shared_by_id` and `most_recent_id_shared`
146+ /// Aborts if there is no shared object of type `T` in the global inventory
147+ public fun take_shared <T : key >(): T {
148+ let id_opt = most_recent_id_shared <T >();
149+ assert ! (id_opt.is_some (), EEmptyInventory );
150+ take_by_id (id_opt.destroy_some ())
151+ }
152+
153+ /// Return `t` to the global inventory
154+ public fun return_shared <T : key >(t: T ) {
155+ let id = object::id (&t);
156+ assert ! (was_taken_shared (id), ECantReturnObject );
157+ share_object_impl (t)
158+ }
159+
160+
161+ native fun was_taken_shared (id: ID ): bool ;
162+ native fun most_recent_id_shared <T : key >(): Option <ID >;
163+
108164// native fun objects_by_type<T: key>(): vector<ID>;
109165
110166native fun take_by_id <T : key >(id: ID ): T ;
111167
112168// Forward to sui std native call
113- // native fun share_object_impl<T: key>(obj: T);
114-
169+ native fun share_object_impl <T : key >(obj: T );
115170// native fun freeze_object_impl<T: key>(obj: T);
116171
117- native fun end_transaction ();
172+ native fun end_transaction (): TransactionEffects ;
118173
119174// public fun return_shared<T: key>(t: T) {
120175// share_object_impl(t)
0 commit comments