Skip to content

Commit

Permalink
task 2 3 4
Browse files Browse the repository at this point in the history
  • Loading branch information
JianhaWang authored Jul 30, 2024
1 parent 7d0e5f1 commit e732448
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 13 deletions.
37 changes: 37 additions & 0 deletions mover/JianhaWang/code/task2/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "task2"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
task2 = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

26 changes: 26 additions & 0 deletions mover/JianhaWang/code/task2/sources/coin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module task2::jianhawang_coin {
use sui::coin;
use sui::coin::{TreasuryCap};
use sui::transfer::{public_transfer, public_freeze_object};

public struct JIANHAWANG_COIN has drop{}
fun init(witness: JIANHAWANG_COIN, ctx: &mut TxContext){
let (treasuryCap, denyCap ,metadata) = coin::create_regulated_currency(
witness,
8,
b"JIANHAWANG",
b"JIANHAWANG Coin",
b"move coin",
option::none(),
ctx
);
public_transfer(treasuryCap, tx_context::sender(ctx));
public_transfer(denyCap, tx_context::sender(ctx));
public_freeze_object(metadata);
}

public entry fun mint(cap: &mut TreasuryCap<JIANHAWANG_COIN>, amount: u64, recipient: address, ctx: &mut TxContext){
let coin = coin::mint(cap, amount, ctx);
public_transfer(coin, recipient);
}
}
24 changes: 24 additions & 0 deletions mover/JianhaWang/code/task2/sources/faucet.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module task2::jianhawang_faucet_coin {
use sui::coin;
use sui::coin::TreasuryCap;
use sui::transfer::{ public_share_object, public_freeze_object};

public struct JIANHAWANG_FAUCET_COIN has drop{}
fun init(witness: JIANHAWANG_FAUCET_COIN, ctx: &mut TxContext){
let (treasury, metadata) = coin::create_currency(
witness,
8,
b"JIANHAWANG_PUBLIC",
b"jianhawang Faucet coin",
b"test faucet coin",
option::none(),
ctx
);
public_share_object(treasury);
public_freeze_object(metadata);
}

public entry fun mint(cap: &mut TreasuryCap<JIANHAWANG_FAUCET_COIN>, value: u64, recipient: address, ctx: &mut TxContext){
coin::mint_and_transfer(cap, value, recipient, ctx);
}
}
37 changes: 37 additions & 0 deletions mover/JianhaWang/code/task3/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "task3"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
task3 = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

47 changes: 47 additions & 0 deletions mover/JianhaWang/code/task3/sources/task3.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module task3::jianhawang_nft {
use std::ascii::String;
use std::string::utf8;
use sui::display;
use sui::object;
use sui::object::{UID};
use sui::package;
use sui::transfer;
use sui::transfer::public_transfer;
use sui::tx_context;
use sui::tx_context::{TxContext};

public struct JIANHAWANG_NFT has drop {}

public struct NFT has key,store {
id: UID,
name: String,
}

fun init(otw: JIANHAWANG_NFT, ctx: &mut sui::tx_context::TxContext) {
let keys = vector[
utf8(b"name"),
utf8(b"image_url"),
];

let values = vector[
utf8(b"{name}"),
utf8(b"https://avatars.githubusercontent.com/u/168746492"),
];

let publisher = package::claim(otw, ctx);
let mut display = display::new_with_fields<NFT>(&publisher, keys, values,ctx);

display::update_version(&mut display);

transfer::public_transfer(publisher, tx_context::sender(ctx));
transfer::public_transfer(display, tx_context::sender(ctx));
}

public entry fun mint_to(name: String, recipient: address,ctx: &mut TxContext) {
let nft = NFT{
id: object::new(ctx),
name,
};
public_transfer(nft, recipient)
}
}
37 changes: 37 additions & 0 deletions mover/JianhaWang/code/task4/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "task4"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
task4 = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

162 changes: 162 additions & 0 deletions mover/JianhaWang/code/task4/sources/task4.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
module task4::jianhawang_game {
use std::string::{Self, String};
use sui::event;
use sui::balance::{Self, Balance};
use sui::coin::{Self, Coin};
use sui::tx_context::sender;
use std::hash;
use sui::bcs;

const EPoolNotEnough: u64 = 1;
const EInputNotEnough: u64 = 2;
const ERR_HIGH_ARG_GREATER_THAN_LOW_ARG: u64 = 101;

public struct GameResult has copy, drop {
result: String,
is_winner: bool,
}

public struct Game<phantom T> has key {
id: UID,
pool: Balance<T>,
ticket: u64,
reward: u64,
}

public fun game_pool<T>(game: &Game<T>): u64 {
balance::value(&game.pool)
}

public fun game_ticket<T>(game: &Game<T>): u64 {
game.ticket
}

public fun game_reward<T>(game: &Game<T>): u64 {
game.reward
}

public struct AdminCap has key {
id: UID,
}

fun init(_ctx: &mut TxContext) {
}

entry fun creat_game<T>(ctx: &mut TxContext) {
let game = Game<T> {
id: object::new(ctx),
pool: balance::zero<T>(),
ticket: 1000,
reward: 2000,
};
transfer::share_object(game);

let admin_cap = AdminCap { id: object::new(ctx) };
transfer::transfer(admin_cap, sender(ctx));
}

fun get_spin_result(ctx: &mut TxContext) : u8 {
let spin = (rand_u64_range(0, 5, ctx) as u8);
spin
}

public entry fun play<T>(game: &mut Game<T>, input: Coin<T>, ctx: &mut TxContext) {
assert!(balance::value(&game.pool) >= game.reward - game.ticket, EPoolNotEnough);

let player_spin = get_spin_result(ctx);
let winning_combination = 2;

let input_value = coin::value(&input);
assert!(input_value >= game.ticket, EInputNotEnough);

let mut input_balance = coin::into_balance(input);
if (input_value > game.ticket) {
balance::join(
&mut game.pool,
balance::split(&mut input_balance, game.ticket)
);
let change = coin::from_balance(input_balance, ctx);
transfer::public_transfer(change, sender(ctx));
} else {
balance::join(&mut game.pool, input_balance);
};

let (result, is_winner) = if (player_spin == winning_combination) {
( string::utf8(b"Congratulations, you hit the jackpot! Collect your coins"), true)
} else {
( string::utf8(b"Try again, better luck next time!"), false)
};

if (is_winner) {
let reward_balance = balance::split(&mut game.pool, game.reward);
let reward = coin::from_balance(reward_balance, ctx);
transfer::public_transfer(reward, sender(ctx));
};

event::emit(GameResult {
result,
is_winner,
});
}

public entry fun deposit<T>(game: &mut Game<T>, input: Coin<T>, amount: u64, ctx: &mut TxContext) {
let input_value = coin::value(&input);
assert!(input_value >= amount, EInputNotEnough);

let mut input_balance = coin::into_balance(input);
if (input_value > amount) {
balance::join(
&mut game.pool,
balance::split(&mut input_balance, amount)
);
let change = coin::from_balance(input_balance, ctx);
transfer::public_transfer(change, sender(ctx));
} else {
balance::join(&mut game.pool, input_balance);
}
}

public entry fun withdraw<T>(_: &AdminCap, game: &mut Game<T>, amount: u64, ctx: &mut TxContext) {
let output_balance = balance::split(&mut game.pool, amount);
let output = coin::from_balance(output_balance, ctx);
transfer::public_transfer(output, sender(ctx));
}

fun seed(ctx: &mut TxContext): vector<u8> {
let ctx_bytes = bcs::to_bytes(ctx);
let uid = object::new(ctx);
let uid_bytes: vector<u8> = object::uid_to_bytes(&uid);
object::delete(uid);

let mut info: vector<u8> = vector::empty<u8>();
vector::append<u8>(&mut info, ctx_bytes);
vector::append<u8>(&mut info, uid_bytes);

let hash: vector<u8> = hash::sha3_256(info);
hash
}

fun bytes_to_u64(bytes: vector<u8>): u64 {
let mut value = 0u64;
let mut i = 0u64;
while (i < 8) {
value = value | ((*vector::borrow(&bytes, i) as u64) << ((8 * (7 - i)) as u8));
i = i + 1;
};
return value
}

fun rand_u64_with_seed(_seed: vector<u8>): u64 {
bytes_to_u64(_seed)
}

fun rand_u64_range_with_seed(_seed: vector<u8>, low: u64, high: u64): u64 {
assert!(high > low, ERR_HIGH_ARG_GREATER_THAN_LOW_ARG);
let value = rand_u64_with_seed(_seed);
(value % (high - low)) + low
}

public fun rand_u64_range(low: u64, high: u64, ctx: &mut TxContext): u64 {
rand_u64_range_with_seed(seed(ctx), low, high)
}
}
Binary file added mover/JianhaWang/notes/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e732448

Please sign in to comment.