From 265ea4edf5899131f366de218bc0084a163c0faa Mon Sep 17 00:00:00 2001 From: linqining Date: Mon, 25 Nov 2024 13:53:57 +0800 Subject: [PATCH 1/3] task4 --- mover/linqining/code/task4/linkgame/Move.lock | 35 +++++++++++++++++ mover/linqining/code/task4/linkgame/Move.toml | 38 +++++++++++++++++++ .../code/task4/linkgame/sources/linkgame.move | 18 +++++++++ .../task4/linkgame/tests/linkgame_tests.move | 18 +++++++++ 4 files changed, 109 insertions(+) create mode 100644 mover/linqining/code/task4/linkgame/Move.lock create mode 100644 mover/linqining/code/task4/linkgame/Move.toml create mode 100644 mover/linqining/code/task4/linkgame/sources/linkgame.move create mode 100644 mover/linqining/code/task4/linkgame/tests/linkgame_tests.move diff --git a/mover/linqining/code/task4/linkgame/Move.lock b/mover/linqining/code/task4/linkgame/Move.lock new file mode 100644 index 000000000..4f2dd75a7 --- /dev/null +++ b/mover/linqining/code/task4/linkgame/Move.lock @@ -0,0 +1,35 @@ +# @generated by Move, please check-in and do not edit manually. + +[move] +version = 3 +manifest_digest = "54DBE8503E0A4DB27477F65EBB674DD25B23C3DD30F292C24097C5E6E02E7AF9" +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/mainnet", subdir = "crates/sui-framework/packages/move-stdlib" } + +[[move.package]] +id = "Sui" +source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/sui-framework" } + +dependencies = [ + { id = "MoveStdlib", name = "MoveStdlib" }, +] + +[[move.package]] +id = "faucet_coin" +source = { git = "git@github.com:linqining/letsmove.git", rev = "main", subdir = "mover/linqining/code/task2/faucet_coin/" } + +dependencies = [ + { id = "Sui", name = "Sui" }, +] + +[move.toolchain-version] +compiler-version = "1.37.1" +edition = "2024.beta" +flavor = "sui" diff --git a/mover/linqining/code/task4/linkgame/Move.toml b/mover/linqining/code/task4/linkgame/Move.toml new file mode 100644 index 000000000..9b899fa7f --- /dev/null +++ b/mover/linqining/code/task4/linkgame/Move.toml @@ -0,0 +1,38 @@ +[package] +name = "linkgame" +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/mainnet" } +faucet_coin = { git = "git@github.com:linqining/letsmove.git", subdir = "mover/linqining/code/task2/faucet_coin/", rev = "main" } + +# 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] +linkgame = "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/linqining/code/task4/linkgame/sources/linkgame.move b/mover/linqining/code/task4/linkgame/sources/linkgame.move new file mode 100644 index 000000000..baff2905e --- /dev/null +++ b/mover/linqining/code/task4/linkgame/sources/linkgame.move @@ -0,0 +1,18 @@ +module linkgame::linkgame; +use sui::balance::Balance; +use faucet_coin::eig::EIG; +// +// struct AdminCap has key{ +// id:UID, +// } +// +// public struct DiceGame has key,store{ +// id: UID, +// // 存钱必须用这个结构体, +// // TODO 如何使用自己的代笔 +// amount: Balance, +// } + +fun init(ctx :&mut TxContext){ + +} \ No newline at end of file diff --git a/mover/linqining/code/task4/linkgame/tests/linkgame_tests.move b/mover/linqining/code/task4/linkgame/tests/linkgame_tests.move new file mode 100644 index 000000000..2593a79ee --- /dev/null +++ b/mover/linqining/code/task4/linkgame/tests/linkgame_tests.move @@ -0,0 +1,18 @@ +/* +#[test_only] +module linkgame::linkgame_tests; +// uncomment this line to import the module +// use linkgame::linkgame; + +const ENotImplemented: u64 = 0; + +#[test] +fun test_linkgame() { + // pass +} + +#[test, expected_failure(abort_code = ::linkgame::linkgame_tests::ENotImplemented)] +fun test_linkgame_fail() { + abort ENotImplemented +} +*/ From f5af805954f611296f3a92d84608e8fb27698bd5 Mon Sep 17 00:00:00 2001 From: linqining Date: Wed, 27 Nov 2024 12:58:15 +0800 Subject: [PATCH 2/3] feat: game impl --- .../code/task4/linkgame/sources/linkgame.move | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/mover/linqining/code/task4/linkgame/sources/linkgame.move b/mover/linqining/code/task4/linkgame/sources/linkgame.move index baff2905e..df5f27a99 100644 --- a/mover/linqining/code/task4/linkgame/sources/linkgame.move +++ b/mover/linqining/code/task4/linkgame/sources/linkgame.move @@ -1,18 +1,58 @@ module linkgame::linkgame; use sui::balance::Balance; -use faucet_coin::eig::EIG; -// -// struct AdminCap has key{ -// id:UID, -// } -// -// public struct DiceGame has key,store{ -// id: UID, -// // 存钱必须用这个结构体, -// // TODO 如何使用自己的代笔 -// amount: Balance, -// } +use faucet_coin::eig::{EIG}; +use sui::coin; +use sui::random::Random; +use sui::transfer::{share_object, public_transfer,transfer}; + +public struct AdminCap has key{ + id:UID, +} + +public struct CoinGame has key,store{ + id: UID, + // 存钱必须用这个结构体, + amount: Balance, +} fun init(ctx :&mut TxContext){ + let game = CoinGame { + id: object::new(ctx), + amount: sui::balance::zero(), + }; + share_object(game); + let admin = AdminCap{id:object::new(ctx)}; + transfer(admin,ctx.sender()); +} + +entry fun play(game: &mut CoinGame, rand:&Random, guess_val:bool, user_bet_coin: coin::Coin, ctx: &mut TxContext){ + let game_balance = game.amount.value(); + let user_bet_amount = user_bet_coin.value(); + + // 奖池大于用户奖池 + assert!(game_balance >= user_bet_amount * 10,0x1); + + // up 正面 !up 反面 + let mut generator = sui::random::new_generator(rand,ctx); + let gen_val = sui::random::generate_bool(&mut generator); + if (guess_val ==gen_val){ + let out_balance = game.amount.split(user_bet_amount); + let out_coin = coin::from_balance(out_balance,ctx); + public_transfer(out_coin,ctx.sender()); + public_transfer(user_bet_coin,ctx.sender()); + }else{ + let in_amt_balance = coin::into_balance(user_bet_coin); + game.amount.join(in_amt_balance); + } +} + +public entry fun deposit(game:&mut CoinGame,amount: coin::Coin,_:&mut TxContext){ + let in_amt = coin::into_balance(amount); + game.amount.join(in_amt); +} +public entry fun withdraw(_:&AdminCap,game:&mut CoinGame,amount: u64,ctx:&mut TxContext){ + let out_coin = game.amount.split(amount); + let withdraw_coins = coin::from_balance(out_coin,ctx); + public_transfer(withdraw_coins,ctx.sender()) } \ No newline at end of file From 22fea9850e483d79f02aeebf8cc6439d0face4f9 Mon Sep 17 00:00:00 2001 From: linqining Date: Wed, 27 Nov 2024 13:51:39 +0800 Subject: [PATCH 3/3] feat: task4 --- mover/linqining/code/task4/linkgame/Move.lock | 8 ++++++++ .../code/task4/linkgame/sources/linkgame.move | 18 +++++++++++------- mover/linqining/readme.md | 8 ++++---- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/mover/linqining/code/task4/linkgame/Move.lock b/mover/linqining/code/task4/linkgame/Move.lock index 4f2dd75a7..c77190656 100644 --- a/mover/linqining/code/task4/linkgame/Move.lock +++ b/mover/linqining/code/task4/linkgame/Move.lock @@ -33,3 +33,11 @@ dependencies = [ compiler-version = "1.37.1" edition = "2024.beta" flavor = "sui" + +[env] + +[env.mainnet] +chain-id = "35834a8a" +original-published-id = "0xe6e615d3803b8fc33f3bb3d0d474d0ba8c9847146419c8bedf5fa19c296bff50" +latest-published-id = "0xe6e615d3803b8fc33f3bb3d0d474d0ba8c9847146419c8bedf5fa19c296bff50" +published-version = "1" diff --git a/mover/linqining/code/task4/linkgame/sources/linkgame.move b/mover/linqining/code/task4/linkgame/sources/linkgame.move index df5f27a99..2cb734447 100644 --- a/mover/linqining/code/task4/linkgame/sources/linkgame.move +++ b/mover/linqining/code/task4/linkgame/sources/linkgame.move @@ -9,14 +9,14 @@ public struct AdminCap has key{ id:UID, } -public struct CoinGame has key,store{ +public struct LinkGame has key,store{ id: UID, // 存钱必须用这个结构体, amount: Balance, } fun init(ctx :&mut TxContext){ - let game = CoinGame { + let game = LinkGame { id: object::new(ctx), amount: sui::balance::zero(), }; @@ -24,8 +24,8 @@ fun init(ctx :&mut TxContext){ let admin = AdminCap{id:object::new(ctx)}; transfer(admin,ctx.sender()); } - -entry fun play(game: &mut CoinGame, rand:&Random, guess_val:bool, user_bet_coin: coin::Coin, ctx: &mut TxContext){ +// 0是反面1是正面 +entry fun play(game: &mut LinkGame, rand:&Random, guess_val:u8, user_bet_coin: coin::Coin, ctx: &mut TxContext){ let game_balance = game.amount.value(); let user_bet_amount = user_bet_coin.value(); @@ -35,7 +35,11 @@ entry fun play(game: &mut CoinGame, rand:&Random, guess_val:bool, user_bet_coin: // up 正面 !up 反面 let mut generator = sui::random::new_generator(rand,ctx); let gen_val = sui::random::generate_bool(&mut generator); - if (guess_val ==gen_val){ + let mut is_up = false; + if (guess_val ==1){ + is_up = true + }; + if (is_up ==gen_val){ let out_balance = game.amount.split(user_bet_amount); let out_coin = coin::from_balance(out_balance,ctx); public_transfer(out_coin,ctx.sender()); @@ -46,12 +50,12 @@ entry fun play(game: &mut CoinGame, rand:&Random, guess_val:bool, user_bet_coin: } } -public entry fun deposit(game:&mut CoinGame,amount: coin::Coin,_:&mut TxContext){ +public entry fun deposit(game:&mut LinkGame, amount: coin::Coin, _:&mut TxContext){ let in_amt = coin::into_balance(amount); game.amount.join(in_amt); } -public entry fun withdraw(_:&AdminCap,game:&mut CoinGame,amount: u64,ctx:&mut TxContext){ +public entry fun withdraw(_:&AdminCap, game:&mut LinkGame, amount: u64, ctx:&mut TxContext){ let out_coin = game.amount.split(amount); let withdraw_coins = coin::from_balance(out_coin,ctx); public_transfer(withdraw_coins,ctx.sender()) diff --git a/mover/linqining/readme.md b/mover/linqining/readme.md index 0e3b4c62d..b7345901b 100644 --- a/mover/linqining/readme.md +++ b/mover/linqining/readme.md @@ -36,10 +36,10 @@ ## 04 Move Game -- [] game package id : -- [] deposit Coin hash: -- [] withdraw `Coin` hash: -- [] play game hash: +- [x] game package id : 0xe6e615d3803b8fc33f3bb3d0d474d0ba8c9847146419c8bedf5fa19c296bff50 +- [x] deposit Coin hash: G8xWzdGqDbpbx94Vz1C6ePU6iJnrJ3E5KbLoGmGy15cx +- [x] withdraw `Coin` hash: 7wUYGYhkJbgiNRTjwA9ruJ3gAjMJeAAUht5e1rNxFrWM +- [x] play game hash: 6R8Gdu9zCuptC4y3BK9ZC543aZzDrNuE4xDB9mfd4ozT ## 05 Move Swap - [] swap package id :