Skip to content

Commit

Permalink
Merge pull request #2113 from zerokk246/main
Browse files Browse the repository at this point in the history
Task4
  • Loading branch information
Sifotd authored Dec 9, 2024
2 parents bf1e38b + 16e6aa6 commit 1387140
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 5 deletions.
6 changes: 6 additions & 0 deletions mover/zerokk246/code/task2/faucet_coin/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ chain-id = "4c78adac"
original-published-id = "0x5d0648b63188eec5a74c027f5bd781dce8a7653bdf64f086efb8af3213c7b214"
latest-published-id = "0x5d0648b63188eec5a74c027f5bd781dce8a7653bdf64f086efb8af3213c7b214"
published-version = "1"

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0xa03f42f57279ce7534ba0e07c4311c073751b6f40d25c2596c3a7ce41ddbc9da"
latest-published-id = "0xa03f42f57279ce7534ba0e07c4311c073751b6f40d25c2596c3a7ce41ddbc9da"
published-version = "1"
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 = "0xa03f42f57279ce7534ba0e07c4311c073751b6f40d25c2596c3a7ce41ddbc9da"

# 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
49 changes: 49 additions & 0 deletions mover/zerokk246/code/task4/game/Move.lock
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 = "ABFF912E02E5E1D0C70ACDE9D6579AADDEBB71A7EB0011105336DDCD0604A203"
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"

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x49b7e2f1587b5150f3ec99349f6f78c831521ccea0824d0c543a77aa909c8f03"
latest-published-id = "0x49b7e2f1587b5150f3ec99349f6f78c831521ccea0824d0c543a77aa909c8f03"
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 = "0xa03f42f57279ce7534ba0e07c4311c073751b6f40d25c2596c3a7ce41ddbc9da"

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

75 changes: 75 additions & 0 deletions mover/zerokk246/code/task4/game/sources/game.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/// 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;
use std::string::String;

public struct Game has key {
id: UID,
balance: Balance<FAUCETCOIN>,
name: String,
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(),
name: b"zerokk246's Game".to_string(),
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 : 0x49b7e2f1587b5150f3ec99349f6f78c831521ccea0824d0c543a77aa909c8f03
- [ x ] deposit Coin hash: EBrw6crTaLd7dojUuv4dNDwsjAFdYSXRHWYLwZCi7ymV
- [ x ] withdraw `Coin` hash: 52Qkmm34YM8Ln3BcMVVkLdWMjRWWy53PKbtv1wahuD6d
- [ x ] play game hash: AAa6mgLYhTDQXch5V5LQPTGn1dLgwE8ZfoqetgJBDwwx

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

0 comments on commit 1387140

Please sign in to comment.