Skip to content

Instantly share code, notes, and snippets.

@damirka
Created November 26, 2022 13:59
Show Gist options
  • Save damirka/29147150dd35ab24226432b4712a2bab to your computer and use it in GitHub Desktop.
Save damirka/29147150dd35ab24226432b4712a2bab to your computer and use it in GitHub Desktop.
Resulting application; twitter!
module twitter::twitter {
use sui::object::{Self, ID, UID};
use sui::tx_context::{Self, TxContext};
use std::ascii::{Self, String};
use sui::balance::{Self, Balance};
use sui::coin::{Self, Coin};
use sui::sui::SUI;
use sui::transfer;
/// The max length of a tweet
const TWEET_SIZE: u64 = 255;
const BLUE_MARK_PRICE: u64 = 100_000_000; // 10^-9
use sui::dynamic_object_field as dof;
use sui::dynamic_field as df;
struct MoneyCollectorCap has key { id: UID }
struct MoneyCollector has key {
id: UID,
balance: Balance<SUI>,
}
struct BlueMark has key {
id: UID
}
struct Account has key {
id: UID,
name: String,
owner: address,
blue_mark: bool
}
struct Tweet has key, store {
id: UID,
text: String,
}
struct Retweet has store {
quote: ID
}
struct TweetedEvent has copy, drop { id: ID }
fun init(ctx: &mut TxContext) {
transfer::transfer(
MoneyCollectorCap { id: object::new(ctx) },
tx_context::sender(ctx)
);
transfer::share_object(MoneyCollector {
id: object::new(ctx),
balance: balance::zero()
})
}
public entry fun take_profits(
_: &MoneyCollectorCap,
musk: &mut MoneyCollector,
ctx: &mut TxContext
) {
let amount = balance::value(&musk.balance);
let profits = balance::split(&mut musk.balance, amount);
let coin = coin::from_balance(profits, ctx);
transfer::transfer(coin, tx_context::sender(ctx))
}
public entry fun buy_blue_mark(
musk: &mut MoneyCollector,
paid: Coin<SUI>,
ctx: &mut TxContext
) {
assert!(coin::value(&paid) >= BLUE_MARK_PRICE, 0);
coin::put(&mut musk.balance, paid);
transfer::transfer(
BlueMark { id: object::new(ctx) },
tx_context::sender(ctx)
)
}
public entry fun place_mark(
account: &mut Account,
mark: BlueMark,
ctx: &mut TxContext
) {
assert!(account.owner == tx_context::sender(ctx), 0);
let BlueMark { id } = mark;
object::delete(id);
account.blue_mark = true;
}
/// Creates a new account and shares it.
public entry fun create_account(name: String, ctx: &mut TxContext) {
transfer::share_object(Account {
id: object::new(ctx),
name,
owner: tx_context::sender(ctx),
blue_mark: false
})
}
/// Add a tweet to an account.
public entry fun tweet(
account: &mut Account,
text: String,
ctx: &mut TxContext
) {
assert!(ascii::length(&text) <= TWEET_SIZE, 0);
assert!(account.owner == tx_context::sender(ctx), 0);
let uid: UID = object::new(ctx);
let id: ID = object::uid_to_inner(&uid);
sui::event::emit(TweetedEvent { id: *&id });
dof::add(&mut account.id, id, Tweet {
id: uid,
text,
});
}
// public entry fun add_attachment(
// account: &mut Account,
// id: ID,
// ctx: &mut TxContext
// ) {
// }
public entry fun delete(
account: &mut Account,
id: ID,
ctx: &mut TxContext
) {
assert!(account.owner == tx_context::sender(ctx), 0);
let tweet = dof::remove(&mut account.id, id);
let Tweet { id, text: _ } = tweet;
object::delete(id)
}
public entry fun retweet(
account: &mut Account,
quoted_account: &Account,
quoted_tweet: ID,
ctx: &mut TxContext
) {
assert!(account.owner == tx_context::sender(ctx), 0);
assert!(dof::exists_(&quoted_account.id, quoted_tweet), 0);
df::add(&mut account.id, quoted_tweet, Retweet { quote: quoted_tweet })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment