Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成 task4 5 6 #1247

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions mover/Zimknn/code/task4/Move.lock
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"
39 changes: 39 additions & 0 deletions mover/Zimknn/code/task4/Move.toml
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"

92 changes: 92 additions & 0 deletions mover/Zimknn/code/task4/sources/task4.move
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);
}
}
34 changes: 34 additions & 0 deletions mover/Zimknn/code/task5/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 2
manifest_digest = "39149E9191F5A7299278C257F4DBF8CA774C7A7EC549B5951C87448D6A311A49"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.28.4"
edition = "2024.beta"
flavor = "sui"

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x60f45a1dcbb8fee154af0f8c0e0dc41a171f3050ead824aad23037b9c8448c07"
latest-published-id = "0x60f45a1dcbb8fee154af0f8c0e0dc41a171f3050ead824aad23037b9c8448c07"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/Zimknn/code/task5/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "task5"
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/mainnet" }

# 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]
task5 = "0x0"

# 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"

Loading