创建对象和 NFT - 编码俱乐部Sui 系列 #3

在这六个教学视频中的第三个视频中,您将学习如何在Sui网络上创建 NFT。

创建对象和 NFT - 编码俱乐部Sui 系列 #3

Encode ClubSui 系列的第三个视频中,我们回顾了Sui 上对象的性质,并介绍了创建不可篡改标记(NFT)的必要步骤。

Sui 基金会与编码俱乐部(Encode Club)合作,提供了六个以开发者为中心的系列视频。该系列视频从Sui 的基础知识到构建智能合约和在Sui 中处理对象的教程。

学习重点

Sui的数据模型使得创建对象成为网络编码的基本组成部分。在此,我们将提供代码片段,展示四种不同的对象模型、唯一标识符的组成部分以及如何创建 NFT。

对象审查

Sui 中对象是最基本的存储单元。对象可分为四类:

地址所属对象

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))
}

物体拥有的物体

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);
}

共享对象

struct ObjectC has key { id: UID }

public entry fun create_shared_object(ctx: &mut TxContext) {
	transfer::share_object(ObjectC { id: object::new(ctx) })
}

不可变对象

struct ObjectD has key { id: UID }

public entry fun create_immutable_object(ctx: &mut TxContext) {
	transfer::freeze_object(ObjectD { id: object::new(ctx) })
}

物体和 NFT

从技术上讲,在Sui 上,对象和 NFT 没有区别。让我们用下面的代码片段检查一下Sui 标准库中option.move对唯一标识符(UID)的定义。

/// 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,
}


由于Sui 在每次创建新对象时都会生成一个全局唯一的 ID,因此,即使两个对象的其他字段完全相同,也不会有两个对象是真正可互换的。

在Sui

由于Sui采用了以对象为中心的方法,创建 NFT 相对简单,如下面的代码片段所示。

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.)
}


不过,以上只是一个初级示例。SuiNFT 的独特编程模型允许无限的用例。如果您有兴趣就 NFT 标准或如何将其提升到更高水平献计献策,请访问我们的官方论坛Sui Developers

观看整个系列

  1. Sui 是什么?
  2. 智能合约
  3. 创建对象和 NFT
  4. 动态字段和集合
  5. RPG 构建基础
  6. 在区块链上部署游戏