
✦
作者: GG
/// Sui Chat NFT (i.e., a post, retweet, like, chat message etc).struct Chat has key, store {id: UID,// The ID of the chat app.app_id: address,// Post's text.text: String,// Set if referencing an another object (i.e., due to a Like, Retweet, Reply etc).// We allow referencing any object type, not ony Chat NFTs.ref_id: Option<address>,// app-specific metadata. We do not enforce a metadata format and delegate this to app layer.metadata: vector<u8>,}
发推文:初始化一个推文 Object,且挂载到发文用户存储上就行。
/// Mint (post) a Chat object.fun post_internal(app_id: address,text: vector<u8>,ref_id: Option<address>,metadata: vector<u8>,ctx: &mut TxContext,) {assert!(length(&text) <= MAX_TEXT_LENGTH, ETextOverflow);let chat = Chat {id: object::new(ctx),app_id,text: ascii::string(text),ref_id,metadata,};transfer::transfer(chat, tx_context::sender(ctx));}
转推:添加引用推文链接即可。
public entry fun post_with_ref(app_identifier: address,text: vector<u8>,ref_identifier: address,metadata: vector<u8>,ctx: &mut TxContext,) {post_internal(app_identifier, text, some(ref_identifier), metadata, ctx);}
删推: 实现一个删除推文的 object 的操作就行
/// Burn a Chat object.public entry fun burn(chat: Chat) {let Chat { id, app_id: _, text: _, ref_id: _, metadata: _ } = chat;object::delete(id);}
完整代码
module nfts::chat {use std::ascii::{Self, String};use std::option::{Self, Option, some};use sui::object::{Self, UID};use sui::transfer;use sui::tx_context::{Self, TxContext};use std::vector::length;/// Max text length.const MAX_TEXT_LENGTH: u64 = 512;/// Text size overflow.const ETextOverflow: u64 = 0;/// Sui Chat NFT (i.e., a post, retweet, like, chat message etc).struct Chat has key, store {id: UID,// The ID of the chat app.app_id: address,// Post's text.text: String,// Set if referencing an another object (i.e., due to a Like, Retweet, Reply etc).// We allow referencing any object type, not ony Chat NFTs.ref_id: Option<address>,// app-specific metadata. We do not enforce a metadata format and delegate this to app layer.metadata: vector<u8>,}/// Mint (post) a Chat object.fun post_internal(app_id: address,text: vector<u8>,ref_id: Option<address>,metadata: vector<u8>,ctx: &mut TxContext,) {assert!(length(&text) <= MAX_TEXT_LENGTH, ETextOverflow);let chat = Chat {id: object::new(ctx),app_id,text: ascii::string(text),ref_id,metadata,};transfer::transfer(chat, tx_context::sender(ctx));}/// Mint (post) a Chat object without referencing another object.public entry fun post(app_identifier: address,text: vector<u8>,metadata: vector<u8>,ctx: &mut TxContext,) {post_internal(app_identifier, text, option::none(), metadata, ctx);}/// Mint (post) a Chat object and reference another object (i.e., to simulate retweet, reply, like, attach)./// TODO: Using `address` as `app_identifier` & `ref_identifier` type, because we cannot pass `ID` to entry/// functions. Using `vector<u8>` for `text` instead of `String` for the same reason.public entry fun post_with_ref(app_identifier: address,text: vector<u8>,ref_identifier: address,metadata: vector<u8>,ctx: &mut TxContext,) {post_internal(app_identifier, text, some(ref_identifier), metadata, ctx);}/// Burn a Chat object.public entry fun burn(chat: Chat) {let Chat { id, app_id: _, text: _, ref_id: _, metadata: _ } = chat;object::delete(id);}}

MoveChina:
https://move-china.com/
【免责声明】市场有风险,投资需谨慎。本文不构成投资建议,用户应考虑本文中的任何意见、观点或结论是否符合其特定状况。据此投资,责任自负。
