From 2cde6c3e388adf1d13d6ad9201caaea663db0d34 Mon Sep 17 00:00:00 2001 From: Mina Date: Tue, 26 Nov 2024 04:21:02 -0300 Subject: [PATCH] task4+task5 --- letsmove | 1 + mover/Ming-XX/readme.md | 14 +++---- mover/Ming-XX/task4/my_game.move | 65 +++++++++++++++++++++++++++++ mover/Ming-XX/task5/my_swap.move | 71 ++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 7 deletions(-) create mode 160000 letsmove create mode 100644 mover/Ming-XX/task4/my_game.move create mode 100644 mover/Ming-XX/task5/my_swap.move diff --git a/letsmove b/letsmove new file mode 160000 index 000000000..1b5554cfc --- /dev/null +++ b/letsmove @@ -0,0 +1 @@ +Subproject commit 1b5554cfc3fd1f07a423e8f2fe53a80a352d54fa diff --git a/mover/Ming-XX/readme.md b/mover/Ming-XX/readme.md index 55741a2c3..55417e898 100644 --- a/mover/Ming-XX/readme.md +++ b/mover/Ming-XX/readme.md @@ -33,15 +33,15 @@ - [✓] scan上的NFT截图:![Scan截图](./images/nft.jpg) ## 04 Move Game -- [] game package id : -- [] deposit Coin hash: -- [] withdraw `Coin` hash: -- [] play game hash: +- [✓] game package id :0xc1157171a3c69d7fc44233c5df1a91b95eef4dcf1ff0c01a95aee83ae398df86 +- [✓] deposit Coin hash:D6Lk3Xe9G2C6YRnHTUPnPqYH33ZQnkPRz139QN9PyZ3s +- [✓] withdraw `Coin` hash:6Sgxwo519NPZvvyCXuzpGwgLanm7wZk9XEN75Wy894dB +- [✓] play game hash:FqrxjL6wz2rY8UrvX2WU4ihgUW9HvbJEwuz85rLMxma9 ## 05 Move Swap -- [] swap package id : -- [] call swap CoinA-> CoinB hash : -- [] call swap CoinB-> CoinA hash : +- [✓] swap package id :0x97bd347ac2fa70f0bb11bf19e6ae442c6ea1ae98c1d5ac8b6471ebdb655dc829 +- [✓] call swap CoinA-> CoinB hash :x7Pt6JzPvE7Lz59KAqbdTEF69anfNBgiYckG6sEfqgy +- [✓] call swap CoinB-> CoinA hash :4tZdG2gVm5zxCF8mJ4bKjVzFE4BWoUkR3F9HMQqKe7rC ## 06 Dapp-kit SDK PTB - [] save hash : diff --git a/mover/Ming-XX/task4/my_game.move b/mover/Ming-XX/task4/my_game.move new file mode 100644 index 000000000..7b022f20a --- /dev/null +++ b/mover/Ming-XX/task4/my_game.move @@ -0,0 +1,65 @@ +module my_game::my_game; +use my_coin::ming_xx_faucet_coin::MING_XX_FAUCET_COIN; +use sui::balance::{Self, Balance}; +use sui::coin::{Self, Coin}; +use sui::random::{Self, Random}; +use sui::transfer::{share_object, public_transfer, transfer}; + +const EExceedBalance: u64 = 0x0; + +public struct Ming_XX_Game has key { + id: UID, + val: Balance, +} + +public struct AdminCap has key { + id: UID, +} + +fun init(ctx: &mut TxContext) { + let game = Ming_XX_Game { + id: object::new(ctx), + val: balance::zero(), + }; + share_object(game); + + let admin = AdminCap { + id: object::new(ctx), + }; + + transfer(admin, ctx.sender()); +} + +public entry fun DepositCoin(game: &mut Ming_XX_Game, coin: Coin,_ctx:&mut TxContext) { + game.val.join(coin::into_balance(coin)); +} + +public entry fun WithdrawCoin(_: &AdminCap, game: &mut Ming_XX_Game, value: u64, ctx: &mut TxContext) { + let out_balance = game.val.split(value); + let out_coin = coin::from_balance(out_balance, ctx); + public_transfer(out_coin, ctx.sender()); +} + +entry fun play( + game: &mut Ming_XX_Game, + rnd: &Random, + guess: bool, + in_coin: Coin, + ctx: &mut TxContext +) { + let mut gen = random::new_generator(rnd, ctx); + let flip_value = random::generate_bool(&mut gen); + let val_value = in_coin.value(); + let game_val = game.val.value(); + assert!(game_val >= val_value * 10, EExceedBalance); + if (guess == flip_value) { + let out_balance = game.val.split(val_value); + let out_coin = coin::from_balance(out_balance, ctx); + public_transfer(out_coin, ctx.sender()); + public_transfer(in_coin, ctx.sender()); + }else { + DepositCoin(game,in_coin,ctx); + } +} + + diff --git a/mover/Ming-XX/task5/my_swap.move b/mover/Ming-XX/task5/my_swap.move new file mode 100644 index 000000000..f0fa0a94f --- /dev/null +++ b/mover/Ming-XX/task5/my_swap.move @@ -0,0 +1,71 @@ +module ming_xx::my_swap; + +use my_coin::ming_xx_coin::MING_XX_COIN; +use my_coin::ming_xx_faucet_coin::MING_XX_FAUCET_COIN; + +use sui::balance::{Self, Balance, split}; +use sui::coin::{Coin, into_balance, from_balance}; +use sui::transfer::{share_object, transfer, public_transfer}; + +const EExceedBalance: u64 = 0x0; + +public struct Pool has key { + id: UID, + my_coin_balance: Balance, + faucet_coin_balance: Balance, +} + +public struct AdminCap has key { + id: UID, +} + +fun init(ctx: &mut TxContext) { + let admin_cap = AdminCap { + id: object::new(ctx), + }; + let pool = Pool { + id: object::new(ctx), + my_coin_balance: balance::zero(), + faucet_coin_balance: balance::zero(), + }; + + transfer(admin_cap, ctx.sender()); + share_object(pool); +} + +public entry fun deposit_my_coin(pool: &mut Pool, coins: Coin, _ctx: &mut TxContext) { + balance::join(&mut pool.my_coin_balance, into_balance(coins)); +} + +public entry fun deposit_faucet_coin(pool: &mut Pool, coins: Coin, _ctx: &mut TxContext) { + balance::join(&mut pool.faucet_coin_balance, into_balance(coins)); +} + + +fun withdraw_my_coin(pool: &mut Pool, amount: u64, receiver: address, ctx: &mut TxContext) { + let coins = from_balance(split(&mut pool.my_coin_balance, amount), ctx); + public_transfer(coins, receiver); +} + +fun withdraw_faucet_coin(pool: &mut Pool, amount: u64, receiver: address, ctx: &mut TxContext) { + let coins = from_balance(split(&mut pool.faucet_coin_balance, amount), ctx); + public_transfer(coins, receiver); +} + +public entry fun swap_my_coin(pool: &mut Pool, coins: Coin, ctx: &mut TxContext) { + let swap_address = ctx.sender(); + let swap_value = coins.value() / 10; + assert!(swap_value <= pool.my_coin_balance.value(), EExceedBalance); + deposit_faucet_coin(pool, coins, ctx); + withdraw_my_coin(pool, swap_value, swap_address, ctx); +} + + +public entry fun swap_faucet_coin(pool: &mut Pool, coins: Coin, ctx: &mut TxContext) { + let swap_address = ctx.sender(); + let swap_value = coins.value() * 10; + assert!(swap_value <= pool.faucet_coin_balance.value(), EExceedBalance); + deposit_my_coin(pool, coins, ctx); + withdraw_faucet_coin(pool, swap_value, swap_address, ctx); +} +