# Write Smart Contracts with Sui Move

**URL:** <https://forums.sui.io/t/write-smart-contracts-with-sui-move/835>\
**Category:** Move\
**Created:** [December 18, 2022, 8:41am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835 "2022-12-18T08:41:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kushagara\_sui](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/kushagara_sui/32/140_2.png) [@kushagara\_sui](https://forums.sui.io/u/kushagara_sui)\
**Post date:** [December 18, 2022, 8:41am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/1 "2022-12-18T08:41:39Z")

</div>

Welcome to the Sui tutorial for building smart contracts with the [Move](https://github.com/MystenLabs/awesome-move) language. This tutorial provides a brief explanation of the Move language and includes concrete examples to demonstrate how Move can be used in Sui.

## Quick links[#](https://docs.sui.io/build/move#quick-links)

- [Why Move?](https://docs.sui.io/learn/why-move)- Quick links to external Move resources and a comparison with Solidity
- [How Sui Move differs from Core Move](https://docs.sui.io/learn/sui-move-diffs)- Highlights the differences between the core Move language and the Move we use in Sui
- [Programming Objects Tutorial Series](https://docs.sui.io/build/programming-with-objects)- Tutorial series that walks through all the powerful ways to interact with objects in Sui Move.

## Move[#](https://docs.sui.io/build/move#move)

Move is an open source language for writing safe smart contracts. It was originally developed at Facebook to power the [Diem](https://github.com/diem/diem) blockchain. However, Move was designed as a platform-agnostic language to enable common libraries, tooling, and developer communities across blockchains with vastly different data and execution models. [Sui](https://github.com/MystenLabs/sui/blob/main/README.md), [0L](https://github.com/OLSF/libra), and [Starcoin](https://github.com/starcoinorg/starcoin) are using Move, and there are also plans to integrate the language in several upcoming and existing platforms (e.g., [Celo](https://www.businesswire.com/news/home/20210921006104/en/Celo-Sets-Sights-On-Becoming-Fastest-EVM-Chain-Through-Collaboration-With-Mysten-Labs)).

The Move language documentation is available in the [Move GitHub](https://github.com/move-language/move) repository and includes a [tutorial](https://github.com/move-language/move/blob/main/language/documentation/tutorial/README.md) and a [book](https://github.com/move-language/move/blob/main/language/documentation/book/src/SUMMARY.md) describing language features in detail. These are invaluable resources to deepen your understanding of the Move language but not strict prerequisites to following the Sui tutorial, which we strived to make self-contained. Further, Sui does differ in some ways from Move, which we explore here.

In Sui, Move is used to define, create and manage programmable Sui [objects](https://docs.sui.io/learn/objects) representing user-level assets. Sui imposes additional restrictions on the code that can be written in Move, effectively using a subset of Move (a.k.a. _Sui Move_), which makes certain parts of the original Move documentation not applicable to smart contract development in Sui. Consequently, it’s best to simply follow this tutorial and relevant Move documentation links provided in the tutorial.

Before we look at the Move code included with Sui, let’s talk briefly about Move code organization, which applies both to code included with Sui and the custom code written by the developers.

## Move code organization[#](https://docs.sui.io/build/move#move-code-organization)

The main unit of Move code organization (and distribution) is a _package_. A package consists of a set of _modules_ defined in separate files with the `.move` extension. These files include Move functions and type definitions. A package must include the `Move.toml` manifest file describing package configuration, for example package metadata or package dependencies. See [Move.toml](https://github.com/move-language/move/blob/main/language/documentation/book/src/packages.md#movetoml) for more information about package manifest files.

The minimal package source directory structure looks as follows and contains the manifest file and the `sources` subdirectory where one or more module files are located:

```auto
my_move_package
├── Move.toml
├── sources
    ├── my_module.move

```

- [COPY](javascript:void(0))

See [Package Layout and Manifest Syntax](https://github.com/move-language/move/blob/main/language/documentation/book/src/packages.md#package-layout-and-manifest-syntax) for more information on package layout.

We are now ready to look at some Move code! You can either keep reading for an introductory description of the main Move language constructs or you can jump straight into the code by [writing a simple Move package](https://docs.sui.io/build/move/write-package), and checking out additional code [examples](https://docs.sui.io/explore/examples).

## First look at Move source code[#](https://docs.sui.io/build/move#first-look-at-move-source-code)

The Sui platform includes _framework_ Move code that is needed to bootstrap Sui operations. In particular, Sui supports multiple user-defined coin types, which are custom assets defined in the Move language. Sui framework code contains the `Coin` module supporting creation and management of custom coins. The `Coin` module is located in the [coin.move](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/sources/coin.move) file. As you would expect, the manifest file describing how to build the package containing the `Coin` module is located in the corresponding [Move.toml](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/Move.toml) file.

Let’s see how module definition appears in the `Coin` module file:

```auto
module sui::coin {
...
}

```

- [COPY](javascript:void(0))

(Let’s not worry about the rest of the module contents for now; you can read more about [modules](https://github.com/move-language/move/blob/main/language/documentation/book/src/modules-and-scripts.md#modules) in the Move book later.)

> **Important:** In Sui Move, package names are always in CamelCase, while the address alias is lowercase, for examples `sui = 0x2` and `std = 0x1`. So: `Sui` = name of the imported package (Sui = sui framework), `sui` = address alias of 0x2, `sui::sui` = module sui under the address 0x2, and `sui::sui::SUI` = type in the module above.

As we can see, when defining a module we specify the module name (`Coin`), preceded by the name of the package where this module resides (`Sui`). The combination of the package name and the module name is used to uniquely identify a module in Move source code (e.g., to be able to use if from other modules). The package name is globally unique, but different packages can contain modules with the same name. Module names are not unique, but combined with unique package name renders a unique combination.

For example, if you have package “P” that has been published, you cannot publish another package named “P”. At the same time you can have module “P1::M1”, “P2::M1”, and “P1::M2” but not another, say, “P1::M1” in the system at the same time.

In addition to having a presence at the source code level, as we discussed in [Move code organization](https://docs.sui.io/build/move#move-code-organization), a package in Sui is also a Sui object and must have a unique numeric ID in addition to a unique name, which is assigned in the manifest file:

```auto
[addresses]
sui = "0x2"

```

- [COPY](javascript:void(0))

### Move structs[#](https://docs.sui.io/build/move#move-structs)

The `Coin` module defines the `Coin` struct type that can be used to represent different types of user-defined coins as Sui objects:

```auto
struct Coin<phantom T> has key, store {
    id: UID,
    value: u64
}

```

- [COPY](javascript:void(0))

Move’s struct type is similar to struct types defined in other programming languages, such as C or C++, and contains a name and a set of typed fields. In particular, struct fields can be of a primitive type, such as an integer type, or of a struct type.

You can read more about Move [primitive types](https://github.com/move-language/move/blob/main/language/documentation/book/src/SUMMARY.md#primitive-types) and [structs](https://github.com/move-language/move/blob/main/language/documentation/book/src/structs-and-resources.md) in the Move book.

In order for a Move struct type to define a Sui object type such as `Coin`, its first field must be `id: UID`, which is a struct type defined in the [object module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/sources/object.move). The Move struct type must also have the `key` ability, which allows the object to be persisted in Sui’s global storage. Abilities of a Move struct are listed after the `has` keyword in the struct definition, and their existence (or lack thereof) helps enforcing various properties on a definition or on instances of a given struct.

You can read more about struct [abilities](https://github.com/move-language/move/blob/main/language/documentation/book/src/abilities.md) in the Move book.

The reason that the `Coin` struct can represent different types of coin is that the struct definition is parameterized with a type parameter. When an instance of the `Coin` struct is created, it can be passed an arbitrary concrete Move type (e.g. another struct type) to distinguish different types of coins from one another.

Learn about Move type parameters known as [generics](https://github.com/move-language/move/blob/main/language/documentation/book/src/generics.md) and also about the optional [phantom keyword](https://github.com/move-language/move/blob/main/language/documentation/book/src/generics.md#phantom-type-parameters)) at your leisure.

In particular, one type of custom coin already defined in Sui is `Coin<SUI>`, which represents a token used to pay for Sui computations (more generally known as _gas_) - in this case, the concrete type used to parameterize the `Coin` struct is the `SUI` struct in the [SUI module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/sources/sui.move):

```auto
struct SUI has drop {}

```

- [COPY](javascript:void(0))

We will show how to define and instantiate custom structs in the section describing how to [write a simple Move package](https://docs.sui.io/build/move/write-package).

### Move functions[#](https://docs.sui.io/build/move#move-functions)

Similarly to other popular programming languages, the main unit of computation in Move is a function. Let us look at one of the simplest functions defined in the [Coin module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/sources/coin.move), that is the `value` function.

```auto
public fun value<T>(self: &Coin<T>): u64 {
    self.value
}

```

- [COPY](javascript:void(0))

This _public_ function can be called by functions in other modules to return the unsigned integer value currently stored in a given instance of the `Coin` struct. Direct access to fields of a struct is allowed only within the module defining a given struct as described in [Privileged Struct Operations](https://github.com/move-language/move/blob/main/language/documentation/book/src/structs-and-resources.md#privileged-struct-operations). The body of the function simply retrieves the `value` field from the `Coin` struct instance parameter and returns it. Note that the coin parameter is a read-only reference to the `Coin` struct instance, indicated by the `&` preceding the parameter type. Move’s type system enforces an invariant that struct instance arguments passed by read-only references (as opposed to mutable references) cannot be modified in the body of a function.

You can read more about Move [references](https://github.com/move-language/move/blob/main/language/documentation/book/src/references.md#references) in the Move book.

We will show how to call Move functions from other functions and how to define the new ones in the section describing how to [write a simple Move package](https://docs.sui.io/build/move/write-package).

In addition to functions callable from other functions, however, the Sui flavor of the Move language also defines so called _entry functions_ that can be called directly from Sui (e.g., from a Sui application that can be written in a different language) and must satisfy a certain set of properties.

#### Entry functions[#](https://docs.sui.io/build/move#entry-functions)

One of the basic operations in Sui is transfer of gas objects between [addresses](https://github.com/move-language/move/blob/main/language/documentation/book/src/address.md) representing individual users. And one of the simplest entry functions is defined in the [SUI module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/sources/sui.move) to implement gas object transfer:

```auto
public entry fun transfer(c: coin::Coin<SUI>, recipient: address, _ctx: &mut TxContext) {
    ...
}

```

- [COPY](javascript:void(0))

(Let’s not worry about the function body for now - since the function is part of Sui framework, you can trust that it will do what it is intended to do.)

In general, an entry function, must satisfy the following properties:

- have the `entry` modifier
  - Note: The visibility does not matter. The function can be `public`, `public(friend)`, or internal.

- have no return value
- (optional) have a mutable reference to an instance of the `TxContext` struct defined in the [TxContext module](https://github.com/MystenLabs/sui/blob/main/crates/sui-framework/sources/tx_context.move) as the last parameter

More concretely, the `transfer` function is public, has no return value, and has three parameters:

- `c` - represents a gas object whose ownership is to be transferred
- `recipient` - the [address](https://github.com/move-language/move/blob/main/language/documentation/book/src/address.md) of the intended recipient
- `_ctx` - a mutable reference to an instance of the `TxContext` struct (in this particular case, this parameter is not actually used in the function’s body as indicated by its name starting with `_`)
  - Note that since it is unused, the parameter could be removed. The mutable reference to the `TxContext` is optional for entry functions.

You can see how the `transfer` function is called from a Sui CLI client in [Calling Move code](https://docs.sui.io/build/cli-client#calling-move-code).

[Sui Local Network](https://docs.sui.io/build/sui-local-network)[Write Move Packages](https://docs.sui.io/build/move/write-package)

---

<div class="post-metadata">

**Author:** ![Aubrey](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@Aubrey](https://forums.sui.io/u/Aubrey)\
**Post date:** [December 18, 2022, 10:08am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/2 "2022-12-18T10:08:38Z")

</div>

Thank you, I will try that now

More thanks 😍

---

<div class="post-metadata">

**Author:** ![cronextbnb](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/cronextbnb/32/437_2.png) [@cronextbnb](https://forums.sui.io/u/cronextbnb)\
**Post date:** [December 18, 2022, 10:37am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/3 "2022-12-18T10:37:54Z")

</div>

this is the best article I’ve seen in a while. thanks

---

<div class="post-metadata">

**Author:** ![kushagara\_sui](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/kushagara_sui/32/140_2.png) [@kushagara\_sui](https://forums.sui.io/u/kushagara_sui)\
**Post date:** [December 18, 2022, 3:39pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/4 "2022-12-18T15:39:06Z")

</div>

you are welcome brother

---

<div class="post-metadata">

**Author:** ![PaulNP](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/paulnp/32/234_2.png) [@PaulNP](https://forums.sui.io/u/PaulNP)\
**Post date:** [December 18, 2022, 5:14pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/5 "2022-12-18T17:14:47Z")

</div>

Wow, you are the best. Thank you so much!

---

<div class="post-metadata">

**Author:** ![Shourya175](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/shourya175/32/141_2.png) [@Shourya175](https://forums.sui.io/u/Shourya175)\
**Post date:** [December 19, 2022, 12:25am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/6 "2022-12-19T00:25:54Z")

</div>

Thanks brother you helped a lot

---

<div class="post-metadata">

**Author:** ![Overlordrichy](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/overlordrichy/32/348_2.png) [@Overlordrichy](https://forums.sui.io/u/Overlordrichy)\
**Post date:** [December 19, 2022, 8:53am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/7 "2022-12-19T08:53:07Z")

</div>

great thread,thanks for explaining this more

---

<div class="post-metadata">

**Author:** ![randall\_Mysten](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@randall\_Mysten](https://forums.sui.io/u/randall_Mysten)\
**Post date:** [December 19, 2022, 8:38pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/8 "2022-12-19T20:38:17Z")

</div>

You are all welcome to contribute to the docs (this thread is just a copy of [Write Smart Contracts with Sui Move | Sui Docs](https://docs.sui.io/devnet/build/move)).

---

<div class="post-metadata">

**Author:** ![been](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/been/32/969_2.png) [@been](https://forums.sui.io/u/been)\
**Post date:** [December 29, 2022, 7:53am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/9 "2022-12-29T07:53:51Z")

</div>

thanks your informations. it is great

---

<div class="post-metadata">

**Author:** ![Amarena](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/amarena/32/3831_2.png) [@Amarena](https://forums.sui.io/u/Amarena)\
**Post date:** [February 19, 2023, 1:35am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/10 "2023-02-19T01:35:11Z")

</div>

thanks for the information

---

<div class="post-metadata">

**Author:** ![maghoya](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/maghoya/32/3838_2.png) [@maghoya](https://forums.sui.io/u/maghoya)\
**Post date:** [February 19, 2023, 1:46am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/11 "2023-02-19T01:46:30Z")

</div>

Glad to read some of these details, at least I got additional references⭐ , and you’re great👏

---

<div class="post-metadata">

**Author:** ![the\_metapuffers](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/the_metapuffers/32/4115_2.png) [@the\_metapuffers](https://forums.sui.io/u/the_metapuffers)\
**Post date:** [February 20, 2023, 4:15am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/12 "2023-02-20T04:15:15Z")

</div>

> [@kushagara\_sui](#):
>
> - [Why Move?](https://docs.sui.io/learn/why-move)- Quick links to external Move resources and a comparison with Solidity
> - [How Sui Move differs from Core Move](https://docs.sui.io/learn/sui-move-diffs)- Highlights the differences between the core Move language and the Move we use in Sui
> - [Programming Objects Tutorial Series](https://docs.sui.io/build/programming-with-objects)- Tutorial series that walks through all the powerful ways to interact with objects in Sui Move.

Thanks for this links)

---

<div class="post-metadata">

**Author:** ![kushagara\_sui](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/kushagara_sui/32/140_2.png) [@kushagara\_sui](https://forums.sui.io/u/kushagara_sui)\
**Post date:** [February 20, 2023, 9:36am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/13 "2023-02-20T09:36:16Z")

</div>

youre welcome @the_metapuffers

---

<div class="post-metadata">

**Author:** ![vadzo](https://avatars.discourse-cdn.com/v4/letter/v/0ea827/32.png) [@vadzo](https://forums.sui.io/u/vadzo)\
**Post date:** [February 22, 2023, 7:54am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/14 "2023-02-22T07:54:46Z")

</div>

😀 😀 😀 😀 😀 😀  
cool article

---

<div class="post-metadata">

**Author:** ![Khok](https://avatars.discourse-cdn.com/v4/letter/k/dec6dc/32.png) [@Khok](https://forums.sui.io/u/Khok)\
**Post date:** [February 22, 2023, 12:18pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/15 "2023-02-22T12:18:13Z")

</div>

Very helpful article, thank you so much 😉

---

<div class="post-metadata">

**Author:** ![dream](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/dream/32/8059_2.png) [@dream](https://forums.sui.io/u/dream)\
**Post date:** [February 22, 2023, 12:36pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/16 "2023-02-22T12:36:44Z")

</div>

Wow, you are the best. Thank you so much! 😚

---

<div class="post-metadata">

**Author:** ![Kulik](https://avatars.discourse-cdn.com/v4/letter/k/ac91a4/32.png) [@Kulik](https://forums.sui.io/u/Kulik)\
**Post date:** [February 22, 2023, 1:43pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/17 "2023-02-22T13:43:31Z")

</div>

I will try it now, thank you 🤩 😜

---

<div class="post-metadata">

**Author:** ![Alexgordon](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/alexgordon/32/10918_2.png) [@Alexgordon](https://forums.sui.io/u/Alexgordon)\
**Post date:** [February 22, 2023, 2:43pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/18 "2023-02-22T14:43:56Z")

</div>

unfortunately I haven’t found anything like Playground SDK yet there

---

<div class="post-metadata">

**Author:** ![Egich](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/egich/32/7244_2.png) [@Egich](https://forums.sui.io/u/Egich)\
**Post date:** [February 22, 2023, 3:48pm UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/19 "2023-02-22T15:48:11Z")

</div>

thank youuuu 🎉  
i will try

---

<div class="post-metadata">

**Author:** ![Bliss](https://dub1.discourse-cdn.com/sui/user_avatar/forums.sui.io/bliss/32/9818_2.png) [@Bliss](https://forums.sui.io/u/Bliss)\
**Post date:** [February 25, 2023, 7:16am UTC](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835/20 "2023-02-25T07:16:52Z")

</div>

**thanks a lot for the topic!** 😍

[Next page](https://forums.sui.io/t/write-smart-contracts-with-sui-move/835.md?page=2)
