From e777176536a6878eaf9e65d0d5d8fe2e1c5e3e75 Mon Sep 17 00:00:00 2001 From: zerokk246 Date: Tue, 3 Dec 2024 20:52:48 +0800 Subject: [PATCH] task4 --- .../code/task2/faucet_coin/Move.toml | 2 +- mover/zerokk246/code/task4/game/Move.lock | 43 +++++++++++ mover/zerokk246/code/task4/game/Move.toml | 39 ++++++++++ .../code/task4/game/sources/game.move | 72 +++++++++++++++++++ .../code/task4/game/tests/game_tests.move | 59 +++++++++++++++ mover/zerokk246/readme.md | 8 +-- 6 files changed, 218 insertions(+), 5 deletions(-) create mode 100644 mover/zerokk246/code/task4/game/Move.lock create mode 100644 mover/zerokk246/code/task4/game/Move.toml create mode 100644 mover/zerokk246/code/task4/game/sources/game.move create mode 100644 mover/zerokk246/code/task4/game/tests/game_tests.move diff --git a/mover/zerokk246/code/task2/faucet_coin/Move.toml b/mover/zerokk246/code/task2/faucet_coin/Move.toml index 1b517a2be..bdc46c6d1 100644 --- a/mover/zerokk246/code/task2/faucet_coin/Move.toml +++ b/mover/zerokk246/code/task2/faucet_coin/Move.toml @@ -19,7 +19,7 @@ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-fram # Override = { local = "../conflicting/version", override = true } [addresses] -faucet_coin = "0x0" +faucet_coin = "0x5d0648b63188eec5a74c027f5bd781dce8a7653bdf64f086efb8af3213c7b214" # Named addresses will be accessible in Move as `@name`. They're also exported: # for example, `std = "0x1"` is exported by the Standard Library. diff --git a/mover/zerokk246/code/task4/game/Move.lock b/mover/zerokk246/code/task4/game/Move.lock new file mode 100644 index 000000000..a152961bf --- /dev/null +++ b/mover/zerokk246/code/task4/game/Move.lock @@ -0,0 +1,43 @@ +# @generated by Move, please check-in and do not edit manually. + +[move] +version = 3 +manifest_digest = "77A689AF8C059EF1786A22A5CF10A5FC344BCB225DBCEF5F29EACA2398A33591" +deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600" +dependencies = [ + { id = "Sui", name = "Sui" }, + { id = "faucet_coin", name = "faucet_coin" }, +] + +[[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 = "faucet_coin" +source = { local = "../../task2/faucet_coin" } + +dependencies = [ + { id = "Sui", name = "Sui" }, +] + +[move.toolchain-version] +compiler-version = "1.36.2" +edition = "2024.beta" +flavor = "sui" + +[env] + +[env.testnet] +chain-id = "4c78adac" +original-published-id = "0x56a7257f9f296157d2bed41e7640e5075df2820d9b789c02c31eb2a57224f7f4" +latest-published-id = "0x56a7257f9f296157d2bed41e7640e5075df2820d9b789c02c31eb2a57224f7f4" +published-version = "1" diff --git a/mover/zerokk246/code/task4/game/Move.toml b/mover/zerokk246/code/task4/game/Move.toml new file mode 100644 index 000000000..f6ea2e43e --- /dev/null +++ b/mover/zerokk246/code/task4/game/Move.toml @@ -0,0 +1,39 @@ +[package] +name = "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 (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"] + +[dependencies] +Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } +faucet_coin = { local = "../../task2/faucet_coin" } + +# 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] +game = "0x0" +faucet_coin = "0x5d0648b63188eec5a74c027f5bd781dce8a7653bdf64f086efb8af3213c7b214" + +# 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" + diff --git a/mover/zerokk246/code/task4/game/sources/game.move b/mover/zerokk246/code/task4/game/sources/game.move new file mode 100644 index 000000000..bff157629 --- /dev/null +++ b/mover/zerokk246/code/task4/game/sources/game.move @@ -0,0 +1,72 @@ +/// Module: game +module game::game; + +use sui::balance::Balance; +use sui::balance; +use sui::random; +use sui::coin::{Coin, from_balance, into_balance}; +use faucet_coin::faucetcoin::FAUCETCOIN; + +public struct Game has key { + id: UID, + balance: Balance, + reward_rate: u64, +} + +public struct GameCap has key { + id: UID, +} + +fun init(ctx: &mut TxContext) { + let game = Game { + id: object::new(ctx), + balance: balance::zero(), + reward_rate: 1, + }; + + let cap = GameCap { + id: object::new(ctx), + }; + + transfer::share_object(game); + transfer::transfer(cap, ctx.sender()); +} + +#[allow(lint(public_random))] +public entry fun play(game: &mut Game, flip_value: bool, in: Coin, + rand: &random::Random, ctx: &mut TxContext) { + let in_balance_v = in.value(); + assert!(in_balance_v > 0 && game.balance.value() >= in_balance_v * game.reward_rate, 100); + + let mut gen = random::new_generator(rand, ctx); + let b = gen.generate_bool(); + if (b == flip_value) { + let reward = in_balance_v * game.reward_rate; + let b = game.balance.split(reward); + let coin = from_balance(b, ctx); + transfer::public_transfer(coin, ctx.sender()); + transfer::public_transfer(in, ctx.sender()); + } else { + game.balance.join(into_balance(in)); + } +} + +public entry fun add_sui(game: &mut Game, in: Coin, _ctx: &mut TxContext) { + game.balance.join(into_balance(in)); +} + +public entry fun withdraw(_: &GameCap, game: &mut Game, amount: u64, ctx: &mut TxContext) { + let b = game.balance.split(amount); + let coin = from_balance(b, ctx); + transfer::public_transfer(coin, ctx.sender()) +} + +#[test_only] +public fun test_init(ctx: &mut TxContext) { + init(ctx); +} + +#[test_only] +public fun balance(game: &Game): u64 { + game.balance.value() +} \ No newline at end of file diff --git a/mover/zerokk246/code/task4/game/tests/game_tests.move b/mover/zerokk246/code/task4/game/tests/game_tests.move new file mode 100644 index 000000000..9c943f255 --- /dev/null +++ b/mover/zerokk246/code/task4/game/tests/game_tests.move @@ -0,0 +1,59 @@ +#[test_only] +module game::game_tests; +use game::game; +use game::game::{Game, balance}; +use sui::test_scenario::{Self}; +use sui::coin::{Coin, TreasuryCap}; +use faucet_coin::faucetcoin::{Self, test_init_coin}; +use sui::random; + + +const ENotImplemented: u64 = 0; + +#[test] +fun test_game() { + let mut scenario_val = test_scenario::begin(@0x01); + let scenario = &mut scenario_val; + game::test_init(scenario.ctx()); + test_init_coin(scenario.ctx()); + + scenario.next_tx(@0x1); + let mut cap = scenario.take_shared>(); + faucetcoin::request_coin(&mut cap, scenario.ctx()); + faucetcoin::request_coin(&mut cap, scenario.ctx()); + faucetcoin::request_coin(&mut cap, scenario.ctx()); + + scenario.next_tx(@0x1); + let c = scenario.take_from_sender>(); + assert!(c.balance().value() > 0, 1); + + scenario.next_tx(@0x01); + let mut g = scenario.take_shared(); + + assert!(g.balance() == 0, 1); + game::add_sui(&mut g, c, scenario.ctx()); + let old = g.balance(); + assert!(old == 1000, 1); + + scenario.next_tx(@0x0); + random::create_for_testing(scenario.ctx()); + scenario.next_tx(@0x0); + let rand = scenario.take_shared(); + + scenario.next_tx(@0x01); + let c = scenario.take_from_sender>(); + assert!(c.balance().value() > 0, 1); + game::play(&mut g, false, c, &rand, scenario.ctx()); + assert!(old != g.balance(), 1); + + test_scenario::return_shared(cap); + test_scenario::return_shared(rand); + test_scenario::return_shared(g); + scenario_val.end(); +} + +#[test, expected_failure] +fun test_game_fail() { + abort ENotImplemented +} + diff --git a/mover/zerokk246/readme.md b/mover/zerokk246/readme.md index 756315291..47391e902 100644 --- a/mover/zerokk246/readme.md +++ b/mover/zerokk246/readme.md @@ -32,10 +32,10 @@ - [ x ] scan上的NFT截图:![Scan截图](./co-learn-2411/images/nft.png) ## 04 Move Game -- [] game package id : -- [] deposit Coin hash: -- [] withdraw `Coin` hash: -- [] play game hash: +- [ x ] game package id : 0x56a7257f9f296157d2bed41e7640e5075df2820d9b789c02c31eb2a57224f7f4 +- [ x ] deposit Coin hash: AZ6bcikwfSKYsMU2MRr2P38A9nujn9jLe87A2vehrKbR +- [ x ] withdraw `Coin` hash: 3XH1UxQFcMQSaV3jPCLViM6qLioHmyY8cVzNMn523stv +- [ x ] play game hash: EToDBaGfFFcUatwFNQTazYMedzVXY4qX9Lduq3nN1gZ6 ## 05 Move Swap - [] swap package id :