Skip to content

Commit

Permalink
task4+task5
Browse files Browse the repository at this point in the history
  • Loading branch information
Mina committed Nov 26, 2024
1 parent 1b5554c commit 2cde6c3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 7 deletions.
1 change: 1 addition & 0 deletions letsmove
Submodule letsmove added at 1b5554
14 changes: 7 additions & 7 deletions mover/Ming-XX/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
65 changes: 65 additions & 0 deletions mover/Ming-XX/task4/my_game.move
Original file line number Diff line number Diff line change
@@ -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<MING_XX_FAUCET_COIN>,
}

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<MING_XX_FAUCET_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<MING_XX_FAUCET_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);
}
}


71 changes: 71 additions & 0 deletions mover/Ming-XX/task5/my_swap.move
Original file line number Diff line number Diff line change
@@ -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<MING_XX_COIN>,
faucet_coin_balance: Balance<MING_XX_FAUCET_COIN>,
}

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<MING_XX_COIN>(),
faucet_coin_balance: balance::zero<MING_XX_FAUCET_COIN>(),
};

transfer(admin_cap, ctx.sender());
share_object(pool);
}

public entry fun deposit_my_coin(pool: &mut Pool, coins: Coin<MING_XX_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<MING_XX_FAUCET_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<MING_XX_FAUCET_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<MING_XX_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);
}

0 comments on commit 2cde6c3

Please sign in to comment.