DEV Community

truong
truong

Posted on

Sui basic - Object

Introduction

Sui Move is a fully object-centric language. Transactions on Sui are expressed as operations where the inputs and outputs are both objects. Sui objects are the basic unit of storage in Sui. It all starts from the struct keyword.

Let's first start with an example that represents a transcript recording a student's grades:

struct Transcript {
    history: u8,
    math: u8,
    literature: u8,
}
Enter fullscreen mode Exit fullscreen mode

The above definition is a regular Move struct, but it is not a Sui object. In order to make a custom Move type instantiate a Sui object in global storage, we need to add the key ability, and a globally unique id: UID field inside the struct definition.

use sui::object::{UID};

struct TranscriptObject has key {
    id: UID,
    history: u8,
    math: u8,
    literature: u8,
}
Enter fullscreen mode Exit fullscreen mode

Create a Sui Object

Creating a Sui object requires a unique ID. We use the sui::object::new function to create a new ID passing in the current TxContext.

In Sui, every object must have an owner, which can be either an address, another object, or "shared". In our examples, we decided to make our new transcriptObject owned by the transaction sender. It is done using the transfer function of the Sui framework and using tx_context::sender function to get the current entry call's sender's address.

We will discuss object ownership more in-depth in the next section.

use sui::object::{Self};
use sui::tx_context::{Self, TxContext};
use sui::transfer;

public fun create_transcript_object(history: u8, math: u8, literature: u8, ctx: &mut TxContext) {
  let transcriptObject = TranscriptObject {
    id: object::new(ctx),
    history,
    math,
    literature,
  };
  transfer::transfer(transcriptObject, tx_context::sender(ctx))
}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘Note: Move supports field punning, which allows us to skip the field values if the field name happens to be the same as the name of the value variable it is bound to.

πŸ’‘Note: You can find some example in here.

Referent

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (1)

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli β€’

Welcome to dev.to

Image of Stellar post

πŸš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

If this post resonated with you, feel free to hit ❀️ or leave a quick comment to share your thoughts!

Okay