-
Notifications
You must be signed in to change notification settings - Fork 890
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
Alan
committed
Nov 30, 2024
1 parent
5e21e50
commit e68466d
Showing
5 changed files
with
250 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 = "7DD52D3940BE2C7C1DD8A7B7384CD813A12FAD6C5D5E0906912375825A2AED30" | ||
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600" | ||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
{ id = "task2", name = "task2" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "MoveStdlib" | ||
source = { git = "https://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ id = "MoveStdlib", name = "MoveStdlib" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "task2" | ||
source = { local = "C:\\Users\\HP\\Desktop\\HOH_SUI\\Move-bootcamp\\move-bootcamp\\07_generics\\task2" } | ||
|
||
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 = "0xc1e5ad7464b4816ce73ffec8752eeaa068306836909081736ce5d6dff0b98748" | ||
latest-published-id = "0xc1e5ad7464b4816ce73ffec8752eeaa068306836909081736ce5d6dff0b98748" | ||
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,40 @@ | ||
[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://gitee.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" } | ||
task2 = { local = "C:/Users/HP/Desktop/HOH_SUI/Move-bootcamp/move-bootcamp/07_generics/task2" } | ||
|
||
# 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,144 @@ | ||
module task4::game { | ||
use sui::coin::{Self, Coin}; | ||
use task2::iusd_Faucet::IUSD_FAUCET; | ||
use sui::balance::{Self, Balance}; | ||
use sui::random::{Self, Random}; | ||
use std::debug; | ||
use std::string; | ||
const EUserInsufficientBalance: u64 = 1000; | ||
const EGameInsufficientBalance: u64 = 1001; | ||
|
||
public struct Game has key { | ||
id: UID, | ||
balance: Balance<IUSD_FAUCET>, | ||
} | ||
|
||
public struct Admin has key { | ||
id: UID, | ||
} | ||
|
||
fun init(ctx: &mut TxContext) { | ||
let game = Game { | ||
id: object::new(ctx), | ||
balance: balance::zero(), | ||
}; | ||
transfer::share_object(game); | ||
|
||
let admin = Admin {id: object::new(ctx)}; | ||
transfer::transfer(admin, ctx.sender()); | ||
} | ||
|
||
public entry fun Deposit( | ||
game: &mut Game, | ||
coin: &mut Coin<IUSD_FAUCET>, | ||
amount: u64, | ||
) { | ||
assert!( | ||
coin::value(coin) >= amount, | ||
EUserInsufficientBalance | ||
); | ||
let split_balance = balance::split(coin::balance_mut(coin), amount); | ||
balance::join(&mut game.balance, split_balance); | ||
} | ||
|
||
// 只有管理员能够提现 | ||
public entry fun Withdraw( | ||
game: &mut Game, | ||
_: &Admin, | ||
amount: u64, | ||
ctx: &mut TxContext | ||
) { | ||
assert!( | ||
game.balance.value() >= amount, | ||
EGameInsufficientBalance | ||
); | ||
let cash = coin::take(&mut game.balance, amount, ctx); | ||
transfer::public_transfer(cash, ctx.sender()); | ||
} | ||
|
||
// 如果用户猜对了,获得双倍奖励。如果用户猜错了,损失投入的代币 | ||
|
||
public entry fun Play( | ||
game: &mut Game, | ||
rnd: &Random, | ||
guess: bool, | ||
coin: &mut Coin<IUSD_FAUCET>, | ||
amount: u64, | ||
ctx: &mut TxContext | ||
) { | ||
// 检查合约余额是否充足,确保用户获胜时有足够金额奖励 | ||
assert!( | ||
game.balance.value() >= 2 * amount, | ||
EGameInsufficientBalance | ||
); | ||
// 检查用户的余额是否充足 | ||
assert!( | ||
coin::value(coin) >= amount, | ||
EUserInsufficientBalance | ||
); | ||
|
||
// 生成随机数 | ||
let mut gen = random::new_generator(rnd, ctx); | ||
let flag = random::generate_bool(&mut gen); | ||
|
||
// 如果获胜 | ||
if (flag == guess) { | ||
// 投入的代币不变,另外奖励等额的代币 | ||
let reward = coin::take(&mut game.balance, 2 * amount, ctx); | ||
coin::join(coin, reward); | ||
// 输出github id | ||
string::utf8(b"Veincealan is sad because you won the game and took the prize money."); | ||
} | ||
// 猜错了就损失投入的代币 | ||
else { | ||
Self::Deposit(game, coin, amount); | ||
string::utf8(b"Veincealan is happy because you lost your principal."); | ||
} | ||
} | ||
} | ||
|
||
//Play-mainnet | ||
/* | ||
sui client call --package 0xc1e5ad7464b4816ce73ffec8752eeaa068306836909081736ce5d6dff0b98748 --module game --function Play --args 0x2090934e5c863eba3123005a374a5456ea4e1080d594afaebe3d4e844caa4a5c 0x8 true 0xcc1b3d887bd59599565e296493acfb632767259b2fe4e8361735060f7b04ab9f 8000000000 | ||
--gas-budget 1000000 | ||
*/ | ||
|
||
//Deposit-mainnet | ||
/* | ||
sui client call | ||
--package 0xc1e5ad7464b4816ce73ffec8752eeaa068306836909081736ce5d6dff0b98748 | ||
--module game --function Deposit | ||
--args 0x2090934e5c863eba3123005a374a5456ea4e1080d594afaebe3d4e844caa4a5c 0xcc1b3d887bd59599565e296493acfb632767259b2fe4e8361735060f7b04ab9f 488000000000 | ||
--gas-budget 1000000 | ||
*/ | ||
|
||
//Withdraw-mainnet | ||
/* | ||
sui client call --package 0xc1e5ad7464b4816ce73ffec8752eeaa068306836909081736ce5d6dff0b98748 --module game --function Withdraw --args 0x2090934e5c863eba3123005a374a5456ea4e1080d594afaebe3d4e844caa4a5c 0x2900c761604ec24792faf75a9b288af4f942a52c49b4b05e49d46f1f5ba36535 472000000000 | ||
--gas-budget 1000000 | ||
*/ | ||
|
||
//Play-testnet | ||
/* | ||
sui client call | ||
--package 0xe6f28e7bd5782928c59f3b85ab6c243de3c9be8182d6feab34948c85bfaa9858 | ||
--module game | ||
--function Play | ||
--args 0xe4bd680ffa42484f65b1c2b2c40c177fadf47b9efd642353b1c6e4fb28940d6f 0x8 true 0x88c9520c30bc48beac7eb388d79213be32d0ae66bdd918398a8180c144b3df2e 8000000000 | ||
--gas-budget 1000000 | ||
*/ | ||
|
||
//Deposit-testnet | ||
/* | ||
sui client call | ||
--package 0xe6f28e7bd5782928c59f3b85ab6c243de3c9be8182d6feab34948c85bfaa9858 | ||
--module game --function Deposit | ||
--args 0xe4bd680ffa42484f65b1c2b2c40c177fadf47b9efd642353b1c6e4fb28940d6f 0x21ab8fe8ce147d335496db0cac2c6ba16a07f3bff47d1ae86d5ab72967b1fdb7 488000000000 | ||
--gas-budget 1000000 | ||
*/ | ||
|
||
//Withdraw-testnet | ||
/* | ||
sui client call --package 0xe6f28e7bd5782928c59f3b85ab6c243de3c9be8182d6feab34948c85bfaa9858 --module game --function Withdraw --args 0xe4bd680ffa42484f65b1c2b2c40c177fadf47b9efd642353b1c6e4fb28940d6f 0x7d4ae89956552ae48931c0d144c79b36ed1522c978706ae5e39ec685e12d92a8 496000000000 | ||
--gas-budget 1000000 | ||
*/ |
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,19 @@ | ||
/* | ||
#[test_only] | ||
module flip_coin::flip_coin_tests { | ||
// uncomment this line to import the module | ||
// use flip_coin::flip_coin; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_flip_coin() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::flip_coin::flip_coin_tests::ENotImplemented)] | ||
fun test_flip_coin_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