-
Notifications
You must be signed in to change notification settings - Fork 886
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
Showing
4 changed files
with
178 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,43 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 2 | ||
manifest_digest = "3ABF32818B72E6D04438DED6A1EE7108F878BD790FA71CF7256257AEA16B0E43" | ||
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600" | ||
dependencies = [ | ||
{ name = "Sui" }, | ||
{ name = "task2" }, | ||
] | ||
|
||
[[move.package]] | ||
name = "MoveStdlib" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" } | ||
|
||
[[move.package]] | ||
name = "Sui" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ name = "MoveStdlib" }, | ||
] | ||
|
||
[[move.package]] | ||
name = "task2" | ||
source = { local = "../task2" } | ||
|
||
dependencies = [ | ||
{ name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.29.2" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.testnet] | ||
chain-id = "4c78adac" | ||
original-published-id = "0x3f1f464e51c7f96bc4f707d74d0ec132dbf4a2bc097828001724e4c4a0151baf" | ||
latest-published-id = "0x3f1f464e51c7f96bc4f707d74d0ec132dbf4a2bc097828001724e4c4a0151baf" | ||
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,39 @@ | ||
[package] | ||
name = "task4" | ||
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" } | ||
task2 = { local = "../task2" } | ||
|
||
# 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] | ||
task4 = "0x0" | ||
task2 = "0x2f980ae2b2a73f93cf6b8a0d8744627dab914b5270c3dfbd4ca63758ac2d21fe" | ||
|
||
# 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" | ||
|
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,92 @@ | ||
module task4::flip_coin { | ||
|
||
use sui::balance; | ||
use sui::balance::{Balance, zero}; | ||
use sui::coin; | ||
use sui::coin::{from_balance, into_balance}; | ||
use sui::random; | ||
use sui::random::Random; | ||
use sui::transfer::{share_object, transfer, public_transfer}; | ||
use sui::tx_context::sender; | ||
use task2::zimknn_faucet_coin::{ZIMKNN_FAUCET_COIN}; | ||
|
||
public struct Game has key { | ||
id: UID, | ||
val: Balance<ZIMKNN_FAUCET_COIN> | ||
} | ||
|
||
public struct AdaminCap has key { | ||
id: UID | ||
} | ||
|
||
|
||
fun init(ctx: &mut TxContext) { | ||
let game = Game { | ||
id: object::new(ctx), | ||
val: zero() | ||
}; | ||
|
||
share_object(game); | ||
|
||
let admin = AdaminCap { | ||
id: object::new(ctx) | ||
}; | ||
|
||
transfer(admin, sender(ctx)); | ||
} | ||
#[allow(lint(public_random))] | ||
public entry fun play( | ||
game: &mut Game, | ||
flip_value: bool, | ||
in: coin::Coin<ZIMKNN_FAUCET_COIN>, | ||
rand: &Random, | ||
ctx: &mut TxContext | ||
) { | ||
let coin_value = coin::value(&in); | ||
|
||
let game_val = balance::value(&game.val); | ||
|
||
// 池子总量大于投注数量,防止给用户返还资金不够 | ||
if (game_val < coin_value) { | ||
abort 0 | ||
}; | ||
// 池子总量大于投注数量10倍,防止all in漏洞 | ||
if (game_val < coin_value * 10) { | ||
abort 1 | ||
}; | ||
|
||
let mut gen = random::new_generator(rand, ctx); | ||
let flag = random::generate_bool(&mut gen); | ||
|
||
let play_address = sender(ctx); | ||
if (flip_value == flag) { | ||
withdraw(game, coin_value, play_address, ctx); | ||
public_transfer(in, play_address); | ||
} else { | ||
deposit(game, in, ctx); | ||
} | ||
} | ||
|
||
fun deposit(game: &mut Game, in: coin::Coin<ZIMKNN_FAUCET_COIN>, _ctx: &mut TxContext) { | ||
let in_balance = into_balance(in); | ||
balance::join(&mut game.val, in_balance); | ||
} | ||
|
||
public entry fun public_deposit( | ||
game: &mut Game, | ||
in: coin::Coin<ZIMKNN_FAUCET_COIN>, | ||
ctx: &mut TxContext | ||
) { | ||
deposit(game, in, ctx); | ||
} | ||
|
||
fun withdraw(game: &mut Game, amt: u64, to: address, ctx: &mut TxContext) { | ||
let win_balance = balance::split(&mut game.val, amt); | ||
let win_coin = from_balance(win_balance, ctx); | ||
public_transfer(win_coin, to); | ||
} | ||
|
||
public entry fun public_withdraw(_: &AdaminCap, game: &mut Game, amt: u64, ctx: &mut TxContext) { | ||
withdraw(game, amt, sender(ctx), 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