-
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
1 parent
68b92aa
commit 939fcae
Showing
3 changed files
with
130 additions
and
0 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,37 @@ | ||
[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" } | ||
|
||
# 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" | ||
|
||
# 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: task4 | ||
module task4::task4; | ||
*/ | ||
|
||
/// Module: task4 | ||
module task4::task4; | ||
use sui::balance; | ||
use sui::balance::Balance; | ||
use sui::coin::{Coin, from_balance, into_balance}; | ||
use sui::random; | ||
use sui::random::Random; | ||
use sui::sui::SUI; | ||
use sui::transfer::{share_object, public_transfer, transfer}; | ||
use sui::tx_context::sender; | ||
|
||
public struct Game has key{ | ||
id : UID, | ||
amt: Balance<SUI> | ||
} | ||
|
||
public struct AdminCap has key{ | ||
id : UID | ||
} | ||
|
||
fun init(ctx:&mut TxContext){ | ||
let game = Game{ | ||
id : object::new(ctx), | ||
amt : balance::zero() | ||
|
||
}; | ||
share_object(game); | ||
|
||
let admin = AdminCap{ | ||
id : object::new(ctx) | ||
}; | ||
|
||
transfer(admin,ctx.sender()) | ||
|
||
} | ||
|
||
entry fun play(game : &mut Game , in:bool,in_coin:Coin<SUI>,rand:& Random,ctx:&mut TxContext){ | ||
|
||
let amt_value = in_coin.value(); | ||
let game_amt = game.amt.value(); | ||
assert!(game_amt >= amt_value*10,0x111); | ||
let mut gen = random::new_generator(rand,ctx); | ||
let mut flip_value = random::generate_bool(&mut gen); | ||
|
||
if (in == flip_value){ | ||
let out_balance = game.amt.split(amt_value); | ||
let out_coin = from_balance(out_balance,ctx); | ||
|
||
public_transfer(out_coin,ctx.sender()); | ||
public_transfer(in_coin,ctx.sender()); | ||
}else{ | ||
let in_amt_balance = into_balance(in_coin); | ||
game.amt.join(in_amt_balance); | ||
|
||
} | ||
} | ||
|
||
public entry fun add_sui(game:&mut Game,in:Coin<SUI>,_:&mut TxContext){ | ||
let in_balance = into_balance(in); | ||
game.amt.join(in_balance); | ||
} | ||
|
||
public entry fun remove_sui(_:&AdminCap,game: & mut Game,amt:u64,ctx : &mut TxContext){ | ||
|
||
let out_balance = game.amt.split(amt); | ||
let out_coin = from_balance(out_balance,ctx); | ||
public_transfer(out_coin,ctx.sender()); | ||
|
||
|
||
} |
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,18 @@ | ||
/* | ||
#[test_only] | ||
module task4::task4_tests; | ||
// uncomment this line to import the module | ||
// use task4::task4; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_task4() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::task4::task4_tests::ENotImplemented)] | ||
fun test_task4_fail() { | ||
abort ENotImplemented | ||
} | ||
*/ |