如何使用几十行 Sui 版本 Move 语言代码实现个去中心化的 Twitter
2022-11-03 11:22
Move-China 中文社区
2022-11-03 11:22
Move-China 中文社区
2022-11-03 11:22
订阅此专栏
收藏此文章


者: GG



定义推文 object,抽象推文结构体,每条推文以 NFT 的形式展现。可以包含任意外部 App 或者网页使用该协议来发推文。推文是链上的唯一 NFT。

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


总结

整个代码是 Sui Move 上的用例代码。当然,这只是个简单用列,但已经完成了基本功能。我们 Coming.chat 准备做一个更完善的微信朋友圈空间,也就是 ComingChat 里面的 DAOMoment。大概 2 个月左右会和我们的用户见面。




MoveChina:

https://move-china.com/

相关Wiki

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

Move-China 中文社区
数据请求中
查看更多

推荐专栏

数据请求中
在 App 打开