-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mina
committed
Nov 26, 2024
1 parent
1b5554c
commit 2cde6c3
Showing
4 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
Submodule letsmove
added at
1b5554
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|