Set up Development Framework
Create the package
Running this command creates a directory with the name you provide (my_first_package
). The command populates the new directory with a skeleton Sui Move project that consists of a sources
directory and a Move.toml
manifest. Open the manifest with a text editor to review its contents:
Defining the package
Create a my_module.move
file under the sources folder, and populate with the below code:
The comments in the preceding code highlight different parts of a typical Sui Move source file.
Part 1: Imports
Code reuse is a necessity in modern programming. Sui Move supports this concept with imports that allow your module to use types and functions declared in other modules. In this example, the module imports from object
, transfer
, and tx_content
modules. These modules are available to the package because the Move.toml
file defines the Sui dependency (along with the sui
named address) where they are defined.
Part 2: Struct declarations
Structs define types that a module can create or destroy. Struct definitions can include abilities provided with the has
keyword. The structs in this example, for instance, have the key
ability, which indicates that these structs are Sui objects that you can transfer between addresses. The store
ability on the structs provide the ability to appear in other struct fields and be transferred freely.
Part 3: Module initializer
A special function that is invoked exactly once when the module publishes.
Part 4: Accessor functions
These functions allow the fields of the module's structs to be read from other modules.
Build the Package
Make sure your terminal or console is is in the directory that contains your package. Use the following command to build your package:
Test the Package
Sui includes support for the Move testing framework that enables you to write unit tests that analyzes Move code much like test frameworks for other languages.
An individual Move unit test is encapsulated in a public function that has no parameters, no return values, and has the #[test]
annotation. The testing framework executes such functions when you call the sui move test
command from the package root (my_move_package
directory as per our running example):
Sample Test
Sui-specific Testing
The previous testing example is largely pure Move and isn't specific to Sui beyond using some Sui packages, such as sui::tx_context
and sui::transfer
. While this style of testing is already useful for writing Move code for Sui, you might also want to test additional Sui-specific features. In particular, a Move call in Sui is encapsulated in a Sui transaction, and you might want to test interactions between different transactions within a single test (for example, one transaction creating an object and the other one transferring it).
Sui-specific testing is supported through the test_scenario module that provides Sui-related testing functionality otherwise unavailable in pure Move and its testing framework.
The test_scenario
module provides a scenario that emulates a series of Sui transactions, each with a potentially different user executing them. A test using this module typically starts the first transaction using the test_scenario::begin
function. This function takes an address of the user executing the transaction as its argument and returns an instance of the Scenario
struct representing a scenario.
An instance of the Scenario
struct contains a per-address object pool emulating Sui object storage, with helper functions provided to manipulate objects in the pool. After the first transaction finishes, subsequent test transactions start with the test_scenario::next_tx
function. This function takes an instance of the Scenario
struct representing the current scenario and an address of a user as arguments.
Update your my_module.move
file to include entry functions callable from Sui that implement sword creation and transfer. With these in place, you can then add a multi-transaction test that uses the test_scenario
module to test these new capabilities.
The first thing the code does is create some addresses that represent users participating in the testing scenario. The assumption is that there is one game administrator user and two regular users representing players. The test then creates a scenario by starting the first transaction on behalf of the administrator address.
The administrator executes the second transaction. The transaction creates a sword where the initial_owner
is the receiver.
The initial owner then executes the third transaction (passed as an argument to the test_scenario::next_tx
function), who then transfers the sword they now own to the final owner. In pure Move there is no notion of Sui storage; consequently, there is no easy way for the emulated Sui transaction to retrieve it from storage. This is where the test_scenario
module helps - its take_from_sender
function allows an object of a given type (Sword
) that is owned by an address executing the current transaction to be available for Move code manipulation. For now, assume that there is only one such object. In this case, the test transfers the object it retrieves from storage to another address.
Transaction effects, such as object creation and transfer become visible only after a given transaction completes. For example, if the second transaction in the running example created a sword and transferred it to the administrator's address, it would only become available for retrieval from the administrator's address (via test_scenario
, take_from_sender
, or take_from_address
functions) in the third transaction.
The final owner executes the fourth and final transaction that retrieves the sword object from storage and checks if it has the expected properties. Remember, as described in testing a package, in the pure Move testing scenario, after an object is available in Move code (after creation or retrieval from emulated storage), it cannot simply disappear.
In the pure Move testing function, the function transfers the sword object to the fake address to handle the diappearing problem. The test_scenario
package provides a more elegant solution, however, which is closer to what happens when Move code actually executes in the context of Sui - the package simply returns the sword to the object pool using the test_scenario::return_to_sender
function.
Last updated