x/nft 已迁移至 ./contrib/x/nft,不再作为核心 Cosmos SDK 的一部分进行积极维护。它仍可继续使用,但不包含在 SDK Bug Bounty 计划中。之所以迁移,是因为它从未被广泛采用。

目录

摘要

x/nft 是一个 Cosmos SDK 模块实现,依据 ADR 43,允许你创建 NFT 类别、创建 NFT、转移 NFT、更新 NFT,并在集成该模块后支持多种查询。它与 ERC721 规范完全兼容。

概念

类别

x/nft 模块定义了结构体 Class,用于描述某一类 NFT 的共性特征。在该类别下,你可以创建多种 NFT,这相当于以太坊中的一个 ERC721 合约。其设计定义见 ADR 043。

NFT

NFT 的全称是 Non-Fungible Token。由于 NFT 具有不可替代性,因此它可以用来表示独一无二的事物。该模块实现的 NFT 与以太坊 ERC721 标准完全兼容。

状态

类别

Class 主要由 id、name、symbol、description、uri、uri_hash 和 data 组成,其中 id 是该类别的唯一标识符,类似于以太坊 ERC721 合约地址,其余字段均为可选。
  • Class: 0x01 | classID | -> ProtocolBuffer(Class)

NFT

NFT 主要由 class_id、id、uri、uri_hash 和 data 组成。其中,class_id 与 id 构成一个用于唯一标识 NFT 的二元组;uri 和 uri_hash 为可选字段,用于标识 NFT 的链下存储位置;data 是 Any 类型。使用 x/nft 模块的链可以通过扩展该字段进行自定义。
  • NFT: 0x02 | classID | 0x00 | nftID |-> ProtocolBuffer(NFT)

按所有者查询类别中的 NFT

NFTOfClassByOwner 主要用于实现通过 classID 和 owner 查询全部 NFT 的功能,不承担其他冗余功能。
  • NFTOfClassByOwner: 0x03 | owner | 0x00 | classID | 0x00 | nftID |-> 0x01

所有者

由于 NFT 中没有额外字段用于标识 NFT 的所有者,因此使用额外的键值对来保存 NFT 的所有权。随着 NFT 的转移,该键值对也会同步更新。
  • OwnerKey: 0x04 | classID | 0x00 | nftID |-> owner

总供应量

TotalSupply 负责跟踪某一类别下全部 NFT 的数量。在该类别下执行 mint 操作时,供应量加一;执行 burn 操作时,供应量减一。
  • OwnerKey: 0x05 | classID |-> totalSupply

消息

本节介绍 NFT 模块中消息的处理方式。
ClassID 和 NftID 的校验由应用开发者自行负责。
SDK 不会为这些字段提供任何校验。

MsgSend

你可以使用 MsgSend 消息来转移 NFT 的所有权。这是 x/nft 模块提供的功能。当然,你也可以使用 Transfer 方法来实现自己的转移逻辑,但你需要额外注意转移权限。 在以下情况下,消息处理应当失败:
  • 提供的 ClassID 不存在。
  • 提供的 Id 不存在。
  • 提供的 Sender 不是该 NFT 的所有者。

事件

NFT 模块会发出定义于 Protobuf 参考 中的 proto 事件。
x/nft has been moved to ./contrib/x/nft and is no longer actively maintained as part of the core Cosmos SDK. It is still available for use but is not included in the SDK Bug Bounty program. It was moved because it was never widely adopted.

Contents

Abstract

x/nft is an implementation of a Cosmos SDK module, per ADR 43, that allows you to create nft classification, create nft, transfer nft, update nft, and support various queries by integrating the module. It is fully compatible with the ERC721 specification.

Concepts

Class

x/nft module defines a struct Class to describe the common characteristics of a class of nft, under this class, you can create a variety of nft, which is equivalent to an erc721 contract for Ethereum. The design is defined in the ADR 043.

NFT

The full name of NFT is Non-Fungible Tokens. Because of the irreplaceable nature of NFT, it means that it can be used to represent unique things. The nft implemented by this module is fully compatible with Ethereum ERC721 standard.

State

Class

Class is mainly composed of id, name, symbol, description, uri, uri_hash,data where id is the unique identifier of the class, similar to the Ethereum ERC721 contract address, the others are optional.
  • Class: 0x01 | classID | -> ProtocolBuffer(Class)

NFT

NFT is mainly composed of class_id, id, uri, uri_hash and data. Among them, class_id and id are two-tuples that identify the uniqueness of nft, uri and uri_hash is optional, which identifies the off-chain storage location of the nft, and data is an Any type. Use Any chain of x/nft modules can be customized by extending this field
  • NFT: 0x02 | classID | 0x00 | nftID |-> ProtocolBuffer(NFT)

NFTOfClassByOwner

NFTOfClassByOwner is mainly to realize the function of querying all nfts using classID and owner, without other redundant functions.
  • NFTOfClassByOwner: 0x03 | owner | 0x00 | classID | 0x00 | nftID |-> 0x01

Owner

Since there is no extra field in NFT to indicate the owner of nft, an additional key-value pair is used to save the ownership of nft. With the transfer of nft, the key-value pair is updated synchronously.
  • OwnerKey: 0x04 | classID | 0x00 | nftID |-> owner

TotalSupply

TotalSupply is responsible for tracking the number of all nfts under a certain class. Mint operation is performed under the changed class, supply increases by one, burn operation, and supply decreases by one.
  • OwnerKey: 0x05 | classID |-> totalSupply

Messages

In this section we describe the processing of messages for the NFT module.
The validation of ClassID and NftID is left to the app developer.
The SDK does not provide any validation for these fields.

MsgSend

You can use the MsgSend message to transfer the ownership of nft. This is a function provided by the x/nft module. Of course, you can use the Transfer method to implement your own transfer logic, but you need to pay extra attention to the transfer permissions. The message handling should fail if:
  • provided ClassID does not exist.
  • provided Id does not exist.
  • provided Sender does not the owner of nft.

Events

The nft module emits proto events defined in the Protobuf reference.