Creating Objects and NFTs - Encode Club Sui Series #3
In this third of six educational videos, you will learn how to create an NFT on the Sui network.
In the third video of Encode Club’s Sui series, we review the nature of objects on Sui, and walk through the steps necessary to create a non-fungible token (NFT).
The Sui Foundation partnered with Encode Club to offer a series of six developer-focused videos. This series will range from the basics of Sui to tutorials about building smart contracts and working with objects in Sui.
Learning Highlights
Sui’s data model makes creating objects a fundamental part of coding on the network. Here we offer code snippets showing the four different object models, the components of a unique identifier, and how to create an NFT.
Object Review
Briefly summarizing what we went over in the previous session, objects in Sui are the most basic unit of storage. Objects fall into one of four categories:
Address-owned object
struct ObjectA has key { id: UID }
public entry fun create_object_owned_by_an_address(ctx: &mut TxContext) {
transfer::transfer({
ObjectA { id: object::new(ctx) }
}, tx_context::sender(ctx))
}
Object-owned object
struct ObjectB has key, store { id: UID }
public entry fun create_object_owned_by_an_object(parent: &mut ObjectA, ctx: &mut TxContext) {
let child = ObjectB { id: object::new(ctx) };
ofield::add(&mut parent.id, b"child", child);
}
Shared object
struct ObjectC has key { id: UID }
public entry fun create_shared_object(ctx: &mut TxContext) {
transfer::share_object(ObjectC { id: object::new(ctx) })
}
Immutable object
struct ObjectD has key { id: UID }
public entry fun create_immutable_object(ctx: &mut TxContext) {
transfer::freeze_object(ObjectD { id: object::new(ctx) })
}
Objects and NFTs
Technically, there are no differences between objects and NFTs on Sui. Let’s examine the Sui standard library’s definition of unique identifier (UID) from option.move using the code snippet below.
/// Globally unique IDs that define an object's ID in storage. Any Sui object, that is a struct
/// with the `key` ability, must have `id: UID` as its first field.
/// These are globally unique in the sense that no two values of type `UID` are ever equal, in
/// other words for any two values `id1: UID` and `id2: UID`, `id1` != `id2`.
/// This is a privileged type that can only be derived from a `TxContext`.
/// `UID` doesn't have the `drop` ability, so deleting a `UID` requires a call to `delete`.
struct UID has store {
id: ID,
}
Because Sui generates a globally unique ID every time a new object is created, no two objects will ever be truly fungible, even if the rest of their fields are identical.
Creating an NFT in Sui
Due to Sui’s object-centric approach, creating an NFT is relatively simple, as shown in the code snippet below.
struct NFT has key, store {
id: UID,
name: String,
description: String,
url: Url,
// ... Additional attributes for various use cases (i.e. game, social profile, etc.)
}
However, the above displays a rudimentary example. Sui’s unique programming model allows practically limitless use cases. Anyone interested in contributing ideas on NFT standards or how they could be supercharged to the next level should visit our official Sui Developers forum!