From 0fed03be95f9d139b10e858634bcee76f32def9e Mon Sep 17 00:00:00 2001 From: janrone Date: Mon, 18 Nov 2024 18:03:57 +0800 Subject: [PATCH] finish task#4 --- mover/janrone/code/move_game/Move.lock | 43 ++++++++ mover/janrone/code/move_game/Move.toml | 38 +++++++ .../code/move_game/sources/move_game.move | 100 ++++++++++++++++++ .../code/move_game/tests/move_game_tests.move | 18 ++++ mover/janrone/readme.md | 8 +- 5 files changed, 203 insertions(+), 4 deletions(-) create mode 100644 mover/janrone/code/move_game/Move.lock create mode 100644 mover/janrone/code/move_game/Move.toml create mode 100644 mover/janrone/code/move_game/sources/move_game.move create mode 100644 mover/janrone/code/move_game/tests/move_game_tests.move diff --git a/mover/janrone/code/move_game/Move.lock b/mover/janrone/code/move_game/Move.lock new file mode 100644 index 000000000..f07e5ae00 --- /dev/null +++ b/mover/janrone/code/move_game/Move.lock @@ -0,0 +1,43 @@ +# @generated by Move, please check-in and do not edit manually. + +[move] +version = 3 +manifest_digest = "8DDBC927B5F7856D296FDABA5B37C37289D26E381F8A6BD06625BB0F9FFA9A76" +deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600" +dependencies = [ + { id = "Sui", name = "Sui" }, + { id = "faucet_j_coin", name = "faucet_j_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_j_coin" +source = { local = "../move_coin/faucet_j_coin" } + +dependencies = [ + { id = "Sui", name = "Sui" }, +] + +[move.toolchain-version] +compiler-version = "1.37.1" +edition = "2024.beta" +flavor = "sui" + +[env] + +[env.mainnet] +chain-id = "35834a8a" +original-published-id = "0xef7757243da29bc7efe22a38a27200c9f826cfd045739ee3c79e13b6b93ae78a" +latest-published-id = "0xef7757243da29bc7efe22a38a27200c9f826cfd045739ee3c79e13b6b93ae78a" +published-version = "1" diff --git a/mover/janrone/code/move_game/Move.toml b/mover/janrone/code/move_game/Move.toml new file mode 100644 index 000000000..4c926aba8 --- /dev/null +++ b/mover/janrone/code/move_game/Move.toml @@ -0,0 +1,38 @@ +[package] +name = "move_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_j_coin = { local = "../move_coin/faucet_j_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] +move_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" + diff --git a/mover/janrone/code/move_game/sources/move_game.move b/mover/janrone/code/move_game/sources/move_game.move new file mode 100644 index 000000000..407ce1599 --- /dev/null +++ b/mover/janrone/code/move_game/sources/move_game.move @@ -0,0 +1,100 @@ +/* +/// Module: move_game +module move_game::move_game; +*/ + +module move_game::game { + use sui::coin::{Self, Coin}; + use faucet_j_coin::faucet_j_coin::FAUCET_J_COIN; + use sui::balance::{Self, Balance}; + use sui::random::{Self, Random}; + + const EUserInsufficientBalance: u64 = 1000; + const EGameInsufficientBalance: u64 = 1001; + + public struct Game has key { + id: UID, + balance: Balance, + } + + public struct Admin has key { + id: UID, + } + + fun init(ctx: &mut TxContext) { + let game = Game { + id: object::new(ctx), + balance: balance::zero(), + }; + transfer::share_object(game); + + let admin = Admin {id: object::new(ctx)}; + transfer::transfer(admin, ctx.sender()); + } + + public entry fun Deposit( + game: &mut Game, + coin: &mut Coin, + amount: u64, + ) { + assert!( + coin::value(coin) >= amount, + EUserInsufficientBalance + ); + let split_balance = balance::split(coin::balance_mut(coin), amount); + balance::join(&mut game.balance, split_balance); + } + + // 只有管理员能够提现 + public entry fun Withdraw( + game: &mut Game, + _: &Admin, + amount: u64, + ctx: &mut TxContext + ) { + assert!( + game.balance.value() >= amount, + EGameInsufficientBalance + ); + let cash = coin::take(&mut game.balance, amount, ctx); + transfer::public_transfer(cash, ctx.sender()); + } + + // 如果用户猜对了,获得双倍奖励。如果用户猜错了,损失投入的代币 + + public entry fun Play( + game: &mut Game, + rnd: &Random, + guess: bool, + coin: &mut Coin, + amount: u64, + ctx: &mut TxContext + ) { + // 检查合约余额是否充足,确保用户获胜时有足够金额奖励 + assert!( + game.balance.value() >= amount, + EGameInsufficientBalance + ); + // 检查用户的余额是否充足 + assert!( + coin::value(coin) >= amount, + EUserInsufficientBalance + ); + + // 生成随机数 + let mut gen = random::new_generator(rnd, ctx); + let flag = random::generate_bool(&mut gen); + + // 如果获胜 + if (flag == guess) { + // 投入的代币不变,另外奖励等额的代币 + let reward = coin::take(&mut game.balance, amount, ctx); + coin::join(coin, reward); + } + // 猜错了就损失投入的代币 + else { + Self::Deposit(game, coin, amount) + } + } +} + diff --git a/mover/janrone/code/move_game/tests/move_game_tests.move b/mover/janrone/code/move_game/tests/move_game_tests.move new file mode 100644 index 000000000..c5ca6e5e8 --- /dev/null +++ b/mover/janrone/code/move_game/tests/move_game_tests.move @@ -0,0 +1,18 @@ +/* +#[test_only] +module move_game::move_game_tests; +// uncomment this line to import the module +// use move_game::move_game; + +const ENotImplemented: u64 = 0; + +#[test] +fun test_move_game() { + // pass +} + +#[test, expected_failure(abort_code = ::move_game::move_game_tests::ENotImplemented)] +fun test_move_game_fail() { + abort ENotImplemented +} +*/ diff --git a/mover/janrone/readme.md b/mover/janrone/readme.md index b72755e1b..30ee5acac 100644 --- a/mover/janrone/readme.md +++ b/mover/janrone/readme.md @@ -42,10 +42,10 @@ - [x] scan上的NFT截图:![Scan截图](./co-learn-2411/images//nft_move_img_1.jpg) ## 04 Move Game -- [] game package id : -- [] deposit Coin hash: -- [] withdraw `Coin` hash: -- [] play game hash: +- [x] game package id : 0xef7757243da29bc7efe22a38a27200c9f826cfd045739ee3c79e13b6b93ae78a +- [x] deposit Coin hash: AteMkxCoTTNtvQD1DQgr39911vkQCxNZsGWj4uFxGJw +- [x] withdraw `Coin` hash: ALLscCgww3Yo5yekaucbm2TfzZjuudkusvEfZ2X3kkyS +- [x] play game hash: CW5woz1EzzX7FhMtiBKZwqXkXxF8W7yjT6UDkGB1piwZ ## 05 Move Swap - [] swap package id :