Skip to content

Commit

Permalink
Adding Chat smart contract
Browse files Browse the repository at this point in the history
  • Loading branch information
kchalkias committed May 18, 2022
1 parent 67c8893 commit a6ae2cc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sui_programmability/examples/games/sources/Hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Games::Hero {
experience: u64,
/// The hero's minimal inventory
sword: Option<Sword>,
// An ID of the game user is playing
/// An ID of the game user is playing
game_id: ID,
}

Expand Down
61 changes: 61 additions & 0 deletions sui_programmability/examples/nfts/sources/Chat.move
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);
}
}
37 changes: 37 additions & 0 deletions sui_programmability/examples/nfts/tests/ChatTests.move
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);
}
}
}
4 changes: 2 additions & 2 deletions sui_programmability/framework/sources/Coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ module Sui::Coin {
use Sui::TxContext::{Self, TxContext};
use Std::Vector;

/// A coin of type `T` worth `value`. Transferrable but not storable
/// A coin of type `T` worth `value`. Transferable but not storable
struct Coin<phantom T> has key {
id: VersionedID,
balance: Balance<T>
}

/// Capability allowing the bearer to mint and burn
/// coins of type `T`. Transferrable
/// coins of type `T`. Transferable
struct TreasuryCap<phantom T> has key, store {
id: VersionedID,
total_supply: u64
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 onwershp.
// doesn't match the ChildRef that represents the onwership.
const EChildIDMismatch: u64 = 0;

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

0 comments on commit a6ae2cc

Please sign in to comment.