Skip to content

Commit

Permalink
Chat smart contract - post and post_with_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
kchalkias committed May 18, 2022
1 parent e72e274 commit 10c45ca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
51 changes: 36 additions & 15 deletions sui_programmability/examples/nfts/sources/Chat.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

module NFTs::Chat {
use Sui::ID::{Self, ID, VersionedID};
use Std::ASCII::{Self, String};
use Std::Option::{Self, Option, some};
use Sui::ID::{Self, ID, VersionedID};
use Sui::Transfer;
use Sui::TxContext::{Self, TxContext};
use Std::Vector::length;
Expand All @@ -23,9 +24,8 @@ module NFTs::Chat {
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.
// By convention applications can use the all zeros address, when no reference is required.
ref_id: ID,
// app-specific metadata.
ref_id: Option<ID>,
// app-specific metadata. We do not enforce a metadata format and delegate this to app layer.
metadata: vector<u8>,
}

Expand All @@ -35,26 +35,47 @@ module NFTs::Chat {
}

/// Mint (post) a Chat object.
/// 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(script) fun mint(
app_identifier: address,
text: vector<u8>,
ref_identifier: address,
metadata: vector<u8>,
ctx: &mut TxContext,
) {
fun post_internal(
app_id: ID,
text: vector<u8>,
ref_id: Option<ID>,
metadata: vector<u8>,
ctx: &mut TxContext,
) {
assert!(length(&text) <= MAX_TEXT_LENGTH, ETextOverflow);
let chat = Chat {
id: TxContext::new_id(ctx),
app_id: ID::new(app_identifier),
app_id,
text: ASCII::string(text),
ref_id: ID::new(ref_identifier),
ref_id,
metadata,
};
Transfer::transfer(chat, TxContext::sender(ctx));
}

/// Mint (post) a Chat object without referencing another object.
public(script) fun post(
app_identifier: address,
text: vector<u8>,
metadata: vector<u8>,
ctx: &mut TxContext,
) {
post_internal(ID::new(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(script) fun post_with_ref(
app_identifier: address,
text: vector<u8>,
ref_identifier: address,
metadata: vector<u8>,
ctx: &mut TxContext,
) {
post_internal(ID::new(app_identifier), text, some(ID::new(ref_identifier)), metadata, ctx);
}

/// Burn a Chat object.
public(script) fun burn(chat: Chat, _ctx: &mut TxContext) {
let Chat { id, app_id: _, text: _, ref_id: _, metadata: _ } = chat;
Expand Down
3 changes: 1 addition & 2 deletions sui_programmability/examples/nfts/tests/ChatTests.move
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ module NFTs::ChatTests {
public(script) fun test_chat() {
let scenario = &mut TestScenario::begin(&USER1_ADDRESS);
{
Chat::mint(
Chat::post(
@0xC001, // This should be an application object ID.
HELLO,
@0x0000, // We're referencing the all-zero bytes object (i.e., it's not a retweet or reply).
METADATA, // Some metadata (it could be empty).
TestScenario::ctx(scenario)
);
Expand Down
2 changes: 1 addition & 1 deletion sui_programmability/framework/sources/Transfer.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Sui::Transfer {
friend Sui::Collection;

// When transferring a child object, this error is thrown if the child object
// doesn't match the ChildRef that represents the onwership.
// doesn't match the ChildRef that represents the ownership.
const EChildIDMismatch: u64 = 0;

/// Represents a reference to a child object, whose type is T.
Expand Down

0 comments on commit 10c45ca

Please sign in to comment.