Skip to content

Commit

Permalink
add task4
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongchenyu6 committed Nov 20, 2024
1 parent c1ab016 commit f4e21f0
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 4 deletions.
49 changes: 49 additions & 0 deletions mover/xiongchenyu6/code/task4/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
manifest_digest = "84A42D3EFFDD8DE8ECCDBC0E1150EB1E5BD8EC2C4AE04DBD4A44BD598011BC8A"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
dependencies = [
{ id = "Sui", name = "Sui" },
{ id = "task2", name = "task2" },
]

[[move.package]]
id = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" }

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

dependencies = [
{ id = "MoveStdlib", name = "MoveStdlib" },
]

[[move.package]]
id = "task2"
source = { local = "../task2" }

dependencies = [
{ id = "Sui", name = "Sui" },
]

[move.toolchain-version]
compiler-version = "1.38.0"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x5dbfcbe125b8f5abff72b4ed6aac324e6045788befc3d8246992628e0e4c93de"
latest-published-id = "0x5dbfcbe125b8f5abff72b4ed6aac324e6045788befc3d8246992628e0e4c93de"
published-version = "1"

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x3fd9c2ff46850b9bdf4695b0da6e79e912095c9cb34976c16d8e8221895c439a"
latest-published-id = "0x3fd9c2ff46850b9bdf4695b0da6e79e912095c9cb34976c16d8e8221895c439a"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/xiongchenyu6/code/task4/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "my_game"
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" }
task2 = { local = "../task2" }

# 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]
my_game = "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"
88 changes: 88 additions & 0 deletions mover/xiongchenyu6/code/task4/sources/my_game.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#[allow(unused_use, duplicate_alias, lint(public_random))]
module my_game::my_game {
// Use necessary modules and types
use sui::coin::{Self, Coin, TreasuryCap};
use sui::balance::{Self, Balance};
use sui::random::{Self, Random, new_generator};
use task2::faucet_coin::{FAUCET_COIN};
use 0x1::bcs::{};

// Constants in ALL_CAPS with underscores
const BALANCE_VALUE_SMALL: u64 = 1000;

/// Struct representing a reward pool with a unique ID and balance
public struct RewardPool has key, store {
id: UID,
balance: Balance<FAUCET_COIN>,
}

/// Struct representing the owner with a unique ID
public struct Owner has key {
id: UID,
}

/// Initializes the reward pool and assigns ownership
fun init(ctx: &mut TxContext) {
// Create a new reward pool with zero balance
let reward_pool = RewardPool { id: object::new(ctx), balance: balance::zero(), };
// Share the reward pool object
transfer::share_object(reward_pool);

// Create a new owner object
let owner = Owner { id: object::new(ctx) };
// Transfer ownership to the sender
transfer::transfer(owner, tx_context::sender(ctx));
}

/// Deposits a specified amount into the reward pool from the given coin
public entry fun deposit(reward: &mut RewardPool, coin: &mut Coin<FAUCET_COIN>, amount: u64) {
// Ensure the coin has enough value for the deposit amount
assert!(coin::value(coin) >= amount, BALANCE_VALUE_SMALL);

// Split the coin balance to get the deposit amount
let split_balance = balance::split(coin::balance_mut(coin), amount);
// Join the split balance into the reward pool's balance
balance::join(&mut reward.balance, split_balance);
}

/// Withdraws a specified amount from the reward pool to the owner
entry fun withdraw(reward: &mut RewardPool, amount: u64, _: &mut Owner, ctx: &mut TxContext) {
// Ensure the reward pool has enough balance for the withdrawal
assert!(reward.balance.value() >= amount, BALANCE_VALUE_SMALL);

// Take the amount from the reward pool's balance
let recv_balance = coin::take(&mut reward.balance, amount, ctx);
// Transfer the withdrawn amount to the sender
transfer::public_transfer(recv_balance, tx_context::sender(ctx));
}

/// Plays a game where the user guesses a boolean value
public entry fun game(
reward_pool: &mut RewardPool,
coin: &mut Coin<FAUCET_COIN>,
amount: u64,
guess: bool,
rand: &Random,
ctx: &mut TxContext
) {
// Ensure the coin has enough value to place the bet
assert!(coin::value(coin) >= amount, BALANCE_VALUE_SMALL);

// Create a new random number generator
let mut generator = new_generator(rand, ctx);
// Generate a random number between 0 and 1
let result = random::generate_u8_in_range(&mut generator, 0, 1);

// Convert the random result to a boolean
let flag: bool = result == 1;
if (flag == guess) {
// If the guess is correct, reward the player
let reward = coin::take(&mut reward_pool.balance, amount, ctx);
coin::join(coin, reward);
} else {
// If the guess is incorrect, deposit the amount into the reward pool
deposit(reward_pool, coin, amount);
}
}
}

18 changes: 18 additions & 0 deletions mover/xiongchenyu6/code/task4/tests/my_game_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module my_game::my_game_tests;
// uncomment this line to import the module
// use my_game::my_game;
const ENotImplemented: u64 = 0;
#[test]
fun test_my_game() {
// pass
}
#[test, expected_failure(abort_code = ::my_game::my_game_tests::ENotImplemented)]
fun test_my_game_fail() {
abort ENotImplemented
}
*/
8 changes: 4 additions & 4 deletions mover/xiongchenyu6/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@

## 04 Move Game

- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [x] game package id : 0x3fd9c2ff46850b9bdf4695b0da6e79e912095c9cb34976c16d8e8221895c439a
- [x] deposit Coin hash: 6SQrfU3LGLrhmXZNbMfb71jAfkPwksjAPGU7K9hMLUXL
- [x] withdraw `Coin` hash: DSHjboMZdQYdVbt2d6FVa4mYM7QBSERHecsB2KUJXUVH
- [x] play game hash: 8nr8rLEVPatAvYdRDsfkq1kbqdYYYES4mWJEvMX2qDnK

## 05 Move Swap

Expand Down

0 comments on commit f4e21f0

Please sign in to comment.