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

Closed
wants to merge 1 commit into from
Closed

task4 #2095

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
2 changes: 1 addition & 1 deletion mover/zerokk246/code/task2/faucet_coin/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-fram
# Override = { local = "../conflicting/version", override = true }

[addresses]
faucet_coin = "0x0"
faucet_coin = "0x5d0648b63188eec5a74c027f5bd781dce8a7653bdf64f086efb8af3213c7b214"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
Expand Down
43 changes: 43 additions & 0 deletions mover/zerokk246/code/task4/game/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 = 3
manifest_digest = "77A689AF8C059EF1786A22A5CF10A5FC344BCB225DBCEF5F29EACA2398A33591"
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.36.2"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x56a7257f9f296157d2bed41e7640e5075df2820d9b789c02c31eb2a57224f7f4"
latest-published-id = "0x56a7257f9f296157d2bed41e7640e5075df2820d9b789c02c31eb2a57224f7f4"
published-version = "1"
39 changes: 39 additions & 0 deletions mover/zerokk246/code/task4/game/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "game"
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]
game = "0x0"
faucet_coin = "0x5d0648b63188eec5a74c027f5bd781dce8a7653bdf64f086efb8af3213c7b214"

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

72 changes: 72 additions & 0 deletions mover/zerokk246/code/task4/game/sources/game.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/// Module: game
module game::game;

use sui::balance::Balance;
use sui::balance;
use sui::random;
use sui::coin::{Coin, from_balance, into_balance};
use faucet_coin::faucetcoin::FAUCETCOIN;

public struct Game has key {
id: UID,
balance: Balance<FAUCETCOIN>,
reward_rate: u64,
}

public struct GameCap has key {
id: UID,
}

fun init(ctx: &mut TxContext) {
let game = Game {
id: object::new(ctx),
balance: balance::zero(),
reward_rate: 1,
};

let cap = GameCap {
id: object::new(ctx),
};

transfer::share_object(game);
transfer::transfer(cap, ctx.sender());
}

#[allow(lint(public_random))]
public entry fun play(game: &mut Game, flip_value: bool, in: Coin<FAUCETCOIN>,
rand: &random::Random, ctx: &mut TxContext) {
let in_balance_v = in.value();
assert!(in_balance_v > 0 && game.balance.value() >= in_balance_v * game.reward_rate, 100);

let mut gen = random::new_generator(rand, ctx);
let b = gen.generate_bool();
if (b == flip_value) {
let reward = in_balance_v * game.reward_rate;
let b = game.balance.split(reward);
let coin = from_balance<FAUCETCOIN>(b, ctx);
transfer::public_transfer(coin, ctx.sender());
transfer::public_transfer(in, ctx.sender());
} else {
game.balance.join(into_balance(in));
}
}

public entry fun add_sui(game: &mut Game, in: Coin<FAUCETCOIN>, _ctx: &mut TxContext) {
game.balance.join(into_balance(in));
}

public entry fun withdraw(_: &GameCap, game: &mut Game, amount: u64, ctx: &mut TxContext) {
let b = game.balance.split(amount);
let coin = from_balance<FAUCETCOIN>(b, ctx);
transfer::public_transfer(coin, ctx.sender())
}

#[test_only]
public fun test_init(ctx: &mut TxContext) {
init(ctx);
}

#[test_only]
public fun balance(game: &Game): u64 {
game.balance.value()
}
59 changes: 59 additions & 0 deletions mover/zerokk246/code/task4/game/tests/game_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#[test_only]
module game::game_tests;
use game::game;
use game::game::{Game, balance};
use sui::test_scenario::{Self};
use sui::coin::{Coin, TreasuryCap};
use faucet_coin::faucetcoin::{Self, test_init_coin};
use sui::random;


const ENotImplemented: u64 = 0;

#[test]
fun test_game() {
let mut scenario_val = test_scenario::begin(@0x01);
let scenario = &mut scenario_val;
game::test_init(scenario.ctx());
test_init_coin(scenario.ctx());

scenario.next_tx(@0x1);
let mut cap = scenario.take_shared<TreasuryCap<faucetcoin::FAUCETCOIN>>();
faucetcoin::request_coin(&mut cap, scenario.ctx());
faucetcoin::request_coin(&mut cap, scenario.ctx());
faucetcoin::request_coin(&mut cap, scenario.ctx());

scenario.next_tx(@0x1);
let c = scenario.take_from_sender<Coin<faucetcoin::FAUCETCOIN>>();
assert!(c.balance().value() > 0, 1);

scenario.next_tx(@0x01);
let mut g = scenario.take_shared<Game>();

assert!(g.balance() == 0, 1);
game::add_sui(&mut g, c, scenario.ctx());
let old = g.balance();
assert!(old == 1000, 1);

scenario.next_tx(@0x0);
random::create_for_testing(scenario.ctx());
scenario.next_tx(@0x0);
let rand = scenario.take_shared<random::Random>();

scenario.next_tx(@0x01);
let c = scenario.take_from_sender<Coin<faucetcoin::FAUCETCOIN>>();
assert!(c.balance().value() > 0, 1);
game::play(&mut g, false, c, &rand, scenario.ctx());
assert!(old != g.balance(), 1);

test_scenario::return_shared(cap);
test_scenario::return_shared(rand);
test_scenario::return_shared(g);
scenario_val.end();
}

#[test, expected_failure]
fun test_game_fail() {
abort ENotImplemented
}

8 changes: 4 additions & 4 deletions mover/zerokk246/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
- [ x ] scan上的NFT截图:![Scan截图](./co-learn-2411/images/nft.png)

## 04 Move Game
- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [ x ] game package id : 0x56a7257f9f296157d2bed41e7640e5075df2820d9b789c02c31eb2a57224f7f4
- [ x ] deposit Coin hash: AZ6bcikwfSKYsMU2MRr2P38A9nujn9jLe87A2vehrKbR
- [ x ] withdraw `Coin` hash: 3XH1UxQFcMQSaV3jPCLViM6qLioHmyY8cVzNMn523stv
- [ x ] play game hash: EToDBaGfFFcUatwFNQTazYMedzVXY4qX9Lduq3nN1gZ6

## 05 Move Swap
- [] swap package id :
Expand Down
Loading