How to Implement a Decentralized Twitter Using Dozens of Lines of Sui Version of the Move Language Code

ComingChat
3 min readNov 3, 2022

--

By: ChainX, ComingChat & OmniBTC CEO, GG

Define tweet object, abstract tweet structure, and each tweet is displayed in the form of NFT. Any external app or web page can be included to tweet using this protocol. Tweets are the only NFTs on-chain.

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

Post a tweet: Initialize a tweeting object and mount it on the tweeting user storage.

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

Retweet: Just add a link to quote the tweet.

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

Delete tweet: Just implement an operation to delete the object of the tweet.

/// Burn a Chat object.
public entry fun burn(chat: Chat) {
let Chat { id, app_id: _, text: _, ref_id: _, metadata: _ } = chat;
object::delete(id);
}

complete code:

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

Summarize

The entire code is the use case code on Sui Move.
Of course, this is just a simple column, but the basic function has been completed. We Coming.chat are going to build a more complete WeChat Moments space, that is, Daomoment in ComingChat. It will be online in about 2 months and meet our users.

--

--

ComingChat
ComingChat

Written by ComingChat

ComingChat is a lifestyle tool in the age of #web3 and #AI. It bridges #Web3( @SuiNetwork ) and ChatGPT( @OpenAI ), allowing users to leverage their advantages.

No responses yet