Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished task 2 3 4 #1238

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions mover/Erikaibble/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/mainnet" }

# 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/Erikaibble/code/task2/sources/coin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module task2::erikaibble_coin {
use sui::coin;
use sui::coin::{TreasuryCap};
use sui::transfer::{public_transfer, public_freeze_object};

public struct ERIKAIBBLE_COIN has drop{}
fun init(witness: ERIKAIBBLE_COIN, ctx: &mut TxContext){
let (treasuryCap, denyCap ,metadata) = coin::create_regulated_currency(
witness,
8,
b"ERIKAIBBLE",
b"ERIKAIBBLE 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<ERIKAIBBLE_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/Erikaibble/code/task2/sources/faucet.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module task2::erikaibble_faucet_coin {
use sui::coin;
use sui::coin::TreasuryCap;
use sui::transfer::{ public_share_object, public_freeze_object};

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

public entry fun mint(cap: &mut TreasuryCap<ERIKAIBBLE_FAUCET_COIN>, value: u64, recipient: address, ctx: &mut TxContext){
coin::mint_and_transfer(cap, value, recipient, ctx);
}
}
37 changes: 37 additions & 0 deletions mover/Erikaibble/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/mainnet" }

# 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/Erikaibble/code/task3/sources/task3.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module task3::erikaibble_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 ERIKAIBBLE_NFT has drop {}

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

fun init(otw: ERIKAIBBLE_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/169317715?v=4"),
];

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/Erikaibble/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/mainnet" }

# 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"

170 changes: 170 additions & 0 deletions mover/Erikaibble/code/task4/sources/task4.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
module task4::erikaibble_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 struct SpinResult has copy, drop, store {
spin1: u8,
spin2: u8,
spin3: u8,
}

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) : SpinResult {
let spin1 = (rand_u64_range(0, 5, ctx) as u8);
let spin2 = (rand_u64_range(0, 5, ctx) as u8);
let spin3 = (rand_u64_range(0, 5, ctx) as u8);
SpinResult { spin1, spin2, spin3 }
}

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_spins = get_spin_result(ctx);
let winning_combination = SpinResult { spin1: 2, spin2: 2, spin3: 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_spins.spin1 == winning_combination.spin1 && player_spins.spin2 == winning_combination.spin2 && player_spins.spin3 == winning_combination.spin3) {
( 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/Erikaibble/img/scan3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading