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

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (1)

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

Welcome to dev.to

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay