-
Notifications
You must be signed in to change notification settings - Fork 940
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 #2033 from linqining/task/task4
task4
- Loading branch information
Showing
5 changed files
with
165 additions
and
4 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,43 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "54DBE8503E0A4DB27477F65EBB674DD25B23C3DD30F292C24097C5E6E02E7AF9" | ||
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/mainnet", subdir = "crates/sui-framework/packages/move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ id = "MoveStdlib", name = "MoveStdlib" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "faucet_coin" | ||
source = { git = "[email protected]:linqining/letsmove.git", rev = "main", subdir = "mover/linqining/code/task2/faucet_coin/" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.37.1" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0xe6e615d3803b8fc33f3bb3d0d474d0ba8c9847146419c8bedf5fa19c296bff50" | ||
latest-published-id = "0xe6e615d3803b8fc33f3bb3d0d474d0ba8c9847146419c8bedf5fa19c296bff50" | ||
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,38 @@ | ||
[package] | ||
name = "linkgame" | ||
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" } | ||
faucet_coin = { git = "[email protected]:linqining/letsmove.git", subdir = "mover/linqining/code/task2/faucet_coin/", rev = "main" } | ||
|
||
# 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] | ||
linkgame = "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,62 @@ | ||
module linkgame::linkgame; | ||
use sui::balance::Balance; | ||
use faucet_coin::eig::{EIG}; | ||
use sui::coin; | ||
use sui::random::Random; | ||
use sui::transfer::{share_object, public_transfer,transfer}; | ||
|
||
public struct AdminCap has key{ | ||
id:UID, | ||
} | ||
|
||
public struct LinkGame has key,store{ | ||
id: UID, | ||
// 存钱必须用这个结构体, | ||
amount: Balance<EIG>, | ||
} | ||
|
||
fun init(ctx :&mut TxContext){ | ||
let game = LinkGame { | ||
id: object::new(ctx), | ||
amount: sui::balance::zero(), | ||
}; | ||
share_object(game); | ||
let admin = AdminCap{id:object::new(ctx)}; | ||
transfer(admin,ctx.sender()); | ||
} | ||
// 0是反面1是正面 | ||
entry fun play(game: &mut LinkGame, rand:&Random, guess_val:u8, user_bet_coin: coin::Coin<EIG>, ctx: &mut TxContext){ | ||
let game_balance = game.amount.value(); | ||
let user_bet_amount = user_bet_coin.value(); | ||
|
||
// 奖池大于用户奖池 | ||
assert!(game_balance >= user_bet_amount * 10,0x1); | ||
|
||
// up 正面 !up 反面 | ||
let mut generator = sui::random::new_generator(rand,ctx); | ||
let gen_val = sui::random::generate_bool(&mut generator); | ||
let mut is_up = false; | ||
if (guess_val ==1){ | ||
is_up = true | ||
}; | ||
if (is_up ==gen_val){ | ||
let out_balance = game.amount.split(user_bet_amount); | ||
let out_coin = coin::from_balance(out_balance,ctx); | ||
public_transfer(out_coin,ctx.sender()); | ||
public_transfer(user_bet_coin,ctx.sender()); | ||
}else{ | ||
let in_amt_balance = coin::into_balance(user_bet_coin); | ||
game.amount.join(in_amt_balance); | ||
} | ||
} | ||
|
||
public entry fun deposit(game:&mut LinkGame, amount: coin::Coin<EIG>, _:&mut TxContext){ | ||
let in_amt = coin::into_balance(amount); | ||
game.amount.join(in_amt); | ||
} | ||
|
||
public entry fun withdraw(_:&AdminCap, game:&mut LinkGame, amount: u64, ctx:&mut TxContext){ | ||
let out_coin = game.amount.split(amount); | ||
let withdraw_coins = coin::from_balance(out_coin,ctx); | ||
public_transfer(withdraw_coins,ctx.sender()) | ||
} |
18 changes: 18 additions & 0 deletions
18
mover/linqining/code/task4/linkgame/tests/linkgame_tests.move
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 linkgame::linkgame_tests; | ||
// uncomment this line to import the module | ||
// use linkgame::linkgame; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_linkgame() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::linkgame::linkgame_tests::ENotImplemented)] | ||
fun test_linkgame_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