-
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.
Merge pull request #2113 from zerokk246/main
Task4
- Loading branch information
Showing
7 changed files
with
233 additions
and
5 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
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
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 = "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" |
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 = "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" | ||
|
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,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() | ||
} |
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,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 | ||
} | ||
|
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