From 10c45ca7899637a1476d00bb4dc599932be9807a Mon Sep 17 00:00:00 2001 From: Konstantinos Chalkias Date: Wed, 18 May 2022 08:41:23 -0700 Subject: [PATCH] Chat smart contract - post and post_with_ref --- .../examples/nfts/sources/Chat.move | 51 +++++++++++++------ .../examples/nfts/tests/ChatTests.move | 3 +- .../framework/sources/Transfer.move | 2 +- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/sui_programmability/examples/nfts/sources/Chat.move b/sui_programmability/examples/nfts/sources/Chat.move index a40c2e0ab5672..cca426844f001 100644 --- a/sui_programmability/examples/nfts/sources/Chat.move +++ b/sui_programmability/examples/nfts/sources/Chat.move @@ -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; @@ -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, + // app-specific metadata. We do not enforce a metadata format and delegate this to app layer. metadata: vector, } @@ -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` for `text` instead of `String` for the same reason. - public(script) fun mint( - app_identifier: address, - text: vector, - ref_identifier: address, - metadata: vector, - ctx: &mut TxContext, - ) { + fun post_internal( + app_id: ID, + text: vector, + ref_id: Option, + metadata: vector, + 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, + metadata: vector, + 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` for `text` instead of `String` for the same reason. + public(script) fun post_with_ref( + app_identifier: address, + text: vector, + ref_identifier: address, + metadata: vector, + 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; diff --git a/sui_programmability/examples/nfts/tests/ChatTests.move b/sui_programmability/examples/nfts/tests/ChatTests.move index d66e518f510b4..18df57ab9255f 100644 --- a/sui_programmability/examples/nfts/tests/ChatTests.move +++ b/sui_programmability/examples/nfts/tests/ChatTests.move @@ -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) ); diff --git a/sui_programmability/framework/sources/Transfer.move b/sui_programmability/framework/sources/Transfer.move index 9d074551af40c..48e75c67f06e8 100644 --- a/sui_programmability/framework/sources/Transfer.move +++ b/sui_programmability/framework/sources/Transfer.move @@ -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.