forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
module NFTs::Chat { | ||
use Sui::ID::{Self, ID, VersionedID}; | ||
use Std::ASCII::{Self, String}; | ||
use Std::Option::Option; | ||
use Sui::Transfer; | ||
use Sui::TxContext::{Self, TxContext}; | ||
|
||
/// 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: VersionedID, | ||
// The ID of the chat app. | ||
app_id: ID, | ||
// 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<ID>, | ||
// app-specific metadata. | ||
metadata: vector<u8>, | ||
} | ||
|
||
/// Simple Chat.text getter. | ||
public fun text(chat: &Chat): String { | ||
chat.text | ||
} | ||
|
||
/// Mint (post) a Chat object. | ||
public(script) fun mint( | ||
app_id: ID, | ||
text: String, | ||
ref_id: Option<ID>, | ||
metadata: vector<u8>, | ||
ctx: &mut TxContext, | ||
) { | ||
assert!(ASCII::length(&text) <= MAX_TEXT_LENGTH, ETextOverflow); | ||
let sender = TxContext::sender(ctx); | ||
let chat = Chat { | ||
id: TxContext::new_id(ctx), | ||
app_id, | ||
text, | ||
ref_id, | ||
metadata, | ||
}; | ||
Transfer::transfer(chat, sender); | ||
} | ||
|
||
/// Burn a Chat object. | ||
public(script) fun burn(chat: Chat, _ctx: &mut TxContext) { | ||
let Chat { id, app_id: _, text: _, ref_id: _, metadata: _ } = chat; | ||
ID::delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#[test_only] | ||
module NFTs::ChatTests { | ||
use NFTs::Chat::{Self, Chat}; | ||
use Std::ASCII::Self; | ||
use Sui::ID::Self; | ||
use Std::Option::Self; | ||
use Sui::TestScenario::Self; | ||
|
||
const USER1_ADDRESS: address = @0xA001; | ||
const METADATA: vector<u8> = vector[0u8]; | ||
const HELLO: vector<u8> = vector[72, 101, 108, 108, 111]; // "Hello" in ASCII. | ||
|
||
#[test] | ||
public(script) fun test_chat() { | ||
let scenario = &mut TestScenario::begin(&USER1_ADDRESS); | ||
{ | ||
Chat::mint( | ||
ID::new(@0xC001), // This should be an application object ID. | ||
ASCII::string(HELLO), | ||
Option::none(), // We're not referencing another Object (it's not a retweet or reply). | ||
METADATA, // Some metadata (it could be empty). | ||
TestScenario::ctx(scenario) | ||
); | ||
}; | ||
|
||
TestScenario::next_tx(scenario, &USER1_ADDRESS); | ||
{ | ||
assert!(TestScenario::can_take_owned<Chat>(scenario), 0); | ||
let chat = TestScenario::take_owned<Chat>(scenario); // if can remove, object exists | ||
assert!(Chat::text(&chat) == ASCII::string(HELLO), 0); | ||
TestScenario::return_owned(scenario, chat); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters