-
Notifications
You must be signed in to change notification settings - Fork 896
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
1 parent
5ed1206
commit 5f8c001
Showing
5 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
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,49 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "331D7814D75E8C838EE91CC4DE51B96111370C5E9BFA0ABAAD4CED7DC13CD5A9" | ||
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/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_coin" | ||
source = { local = "..\\..\\task2\\faucet_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.37.1" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.testnet] | ||
chain-id = "4c78adac" | ||
original-published-id = "0xf5ae27970420e7712a8a1b6ba9e59fdaf46e6a16a74842759c096bea75fba910" | ||
latest-published-id = "0xf5ae27970420e7712a8a1b6ba9e59fdaf46e6a16a74842759c096bea75fba910" | ||
published-version = "1" | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60" | ||
latest-published-id = "0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60" | ||
published-version = "1" |
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,35 @@ | ||
[package] | ||
name = "flip_coin" | ||
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move | ||
# license = "" # e.g., "MIT", "GPL", "Apache 2.0" | ||
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"] | ||
|
||
[dependencies] | ||
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } | ||
faucet_coin = { local = "../../task2/faucet_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] | ||
flip_coin = "0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60" | ||
# 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 = "../code/task2/faucet_coin" } | ||
[dev-addresses] | ||
# The dev-addresses section allows overwriting named addresses for the `--test` | ||
# and `--dev` modes. | ||
# alice = "0xB0B" | ||
|
101 changes: 101 additions & 0 deletions
101
mover/wenchao13547/code/task4/flip_coin/sources/flip_coin.move
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,101 @@ | ||
module flip_coin::flip_coin { | ||
use std::ascii::{String, string}; | ||
use sui::balance; | ||
use sui::balance::Balance; | ||
use sui::coin::{Self, Coin, from_balance, into_balance, split}; | ||
use sui::random; | ||
use sui::random::Random; | ||
use sui::transfer::{share_object, transfer, public_transfer}; | ||
use sui::tx_context::sender; | ||
use faucet_coin::rr::RR; | ||
// use id::模块名::资源名 | ||
// 创建一个池子,并指定代币 | ||
public struct Game has key { | ||
id: UID, | ||
val: Balance<RR>, | ||
creator: String | ||
} | ||
|
||
// 设置管理员 | ||
public struct Admin has key { | ||
id: UID | ||
} | ||
|
||
fun init(ctx: &mut TxContext) { | ||
// 创建一个游戏对象,设置池子初始余额为0 | ||
let game = Game { | ||
id: object::new(ctx), | ||
val: balance::zero(), | ||
creator: string(b"wenchao13547") | ||
}; | ||
share_object(game); // 共享游戏对象 | ||
|
||
// 设置一个管理员对象 | ||
let admin = Admin { | ||
id: object::new(ctx) | ||
}; | ||
transfer(admin,sender(ctx)); // 将管理员权限转移给发送者 | ||
} | ||
|
||
|
||
public entry fun play( | ||
game: &mut Game, | ||
// 0x575393a3353c9678533de4bda5c797405bb04f20afdd8bd2c9be795aeb814514 0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60::flip_coin::Game | ||
user_value: bool, | ||
user_in: Coin<RR>, | ||
rand: &Random, | ||
ctx: &mut TxContext | ||
) { | ||
let user_in_coin = coin::value(&user_in); // 获取玩家下注硬币的值 | ||
let user_address = sender(ctx); // 获取玩家的地址 | ||
let game_val = balance::value(&game.val); // 获取游戏池子中的余额 | ||
|
||
// 如果池子的余额少于玩家下注的,则返回错误码 100 | ||
if (game_val < user_in_coin) { | ||
abort 100u64 | ||
}; | ||
|
||
// 创建一个随机数生成器,并转换为 bool 值 | ||
let mut generator = random::new_generator(rand,ctx); | ||
let mut flag = random::generate_bool(&mut generator); | ||
|
||
// 判断玩家是否猜对了 | ||
if (user_value == flag) { | ||
let win_balance = balance::split(&mut game.val,user_in_coin); // 从池子余额中分出与玩家下注的等值金额 | ||
let win_coin = from_balance(win_balance,ctx); // 将赢得的金额转为硬币 | ||
public_transfer(win_coin,user_address); // 将赢得的硬币转移给玩家 | ||
public_transfer(user_in,user_address); // 将玩家下注的硬币退还给玩家 | ||
} else { | ||
let in_balance = into_balance(user_in); // 将玩家的硬币转换为余额 | ||
balance::join(&mut game.val,in_balance); // 将余额加入到游戏的池子中 | ||
} | ||
} | ||
|
||
// 将代币添加到游戏中 | ||
public entry fun add_coin( | ||
game: &mut Game, | ||
add_coin: Coin<RR>, | ||
// 0xc195872db444df619c1310641cb501480eed42f0da6ddf89845a4ae5a8130bf3 0x2::coin::Coin<0xdcf3749d51e66858ee2443fcc0b92b33482986580aa2db686e5a0d1e5a07ffac::rr::RR> | ||
_ctx: &mut TxContext | ||
) { | ||
let add_coin_in_balance = into_balance(add_coin); // 将硬币转换为余额 | ||
balance::join(&mut game.val, add_coin_in_balance); // 将余额加入到游戏的总余额中 | ||
} | ||
|
||
// 分割代币 | ||
public fun split_coin<FAUCETCOIN>(coin: &mut Coin<FAUCETCOIN>, amt: u64, ctx: &mut TxContext): Coin<FAUCETCOIN> { | ||
split(coin, amt, ctx) | ||
} | ||
|
||
// 从游戏池子中取出 | ||
public entry fun remove_coin( | ||
_: &Admin, | ||
game: &mut Game, | ||
amt: u64, | ||
ctx: &mut TxContext | ||
) { | ||
let win_balance = balance::split(&mut game.val,amt); // 从游戏中分出指定金额 | ||
let win_coin = from_balance(win_balance,ctx); // 将分出金额转化为硬币 | ||
public_transfer(win_coin,sender(ctx)); // 将硬币转移给发送者 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
mover/wenchao13547/code/task4/flip_coin/tests/flip_coin_tests.move
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,18 @@ | ||
/* | ||
#[test_only] | ||
module flip_coin::flip_coin_tests; | ||
// uncomment this line to import the module | ||
// use flip_coin::flip_coin; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_flip_coin() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::flip_coin::flip_coin_tests::ENotImplemented)] | ||
fun test_flip_coin_fail() { | ||
abort ENotImplemented | ||
} | ||
*/ |
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