Skip to content

Commit

Permalink
Merge pull request #1875 from EarthChen/task4
Browse files Browse the repository at this point in the history
Task4: game
  • Loading branch information
Sifotd authored Nov 19, 2024
2 parents df57a88 + 2a590e6 commit ea57c27
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 11 deletions.
27 changes: 27 additions & 0 deletions mover/EarthChen/code/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 猜数游戏信息

## testnet

* packageId: 0x549616dbefd766ce256f00b4662eca840964dff08174f6206d141157e9b2bebd
* UpgradeCapID:0x3eea017ced2d484f0593b9b7a4bd7fd9da5504177cebccb7581ec18f39b99271
* guess_number::PrizePoolCap: 0xc362a9db9ecfbe3fa4043dd00be11c1c0e52158554ff457c83d472ae6d49acd6


### faucetcoin

* pacakgeId: 0x5d56e4b9ef9e4d7eed8ea64b337eb9220d9ca55c34e795f2db49f1b26fe248bf
* coin: 0x27b93ff04870aa99ce50ca35694fb66d5678629088cacefaa78c6d7d5f20763a


## mainnet

* publish hash: BJT1Dga7cHKK6B5KKPq2JQe1RBzwcaUH3nsRZddMxPmQ
* packageId:0x472b8f0ff90d472069fb57f33a9899243debb7c3201f1dc3bb2b49d15ec537ee
* UpgradeCapID:0x05d90f14cd51264d703c84c837e7febb4f556e16cd0306f8646f265f0352a4ea
* guess_number::PrizePoolCap:0xf88111d290581033164f4222c2cc8f827594b8431406bf1e378f64b138bd72d3
* inithash: HRaT2c1UsoXfAz5y9EhWLuhMuhqkWn3zzP8gZv5THatj
* poolId: 0x7dddecadd6f32cd78e5897991f12632ac02d9cbe9315835b5406e29c01b45b5f




12 changes: 6 additions & 6 deletions mover/EarthChen/code/task2/faucetcoin/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

[move]
version = 3
manifest_digest = "040FB3E59D8A1F2E682DCABB7B08CE36CD1FC73BDE7213AB39F548FED3A290C9"
manifest_digest = "36AAF1CDEC036D88764BAC3DCDE58C0D7D361D80DBC1ED0F543FFC7DC8AA1BA6"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ id = "Sui", name = "Sui" },
]

[[move.package]]
id = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/move-stdlib" }
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/mainnet", subdir = "crates/sui-framework/packages/sui-framework" }
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ id = "MoveStdlib", name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.37.1"
compiler-version = "1.37.3"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x586cd3af1a22322f864d4f97aeb94287918e4e8cfe5f82906419ecef0e2df04a"
latest-published-id = "0x586cd3af1a22322f864d4f97aeb94287918e4e8cfe5f82906419ecef0e2df04a"
original-published-id = "0x5d56e4b9ef9e4d7eed8ea64b337eb9220d9ca55c34e795f2db49f1b26fe248bf"
latest-published-id = "0x5d56e4b9ef9e4d7eed8ea64b337eb9220d9ca55c34e795f2db49f1b26fe248bf"
published-version = "1"

[env.mainnet]
Expand Down
2 changes: 1 addition & 1 deletion mover/EarthChen/code/task2/faucetcoin/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# 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" }
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.
Expand Down
49 changes: 49 additions & 0 deletions mover/EarthChen/code/task4/earthchen_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 = "3E7C99C33A2E20F63FB3D0534558440BAC3F2A5097CA4C5801FAED92F94005EE"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
dependencies = [
{ id = "Sui", name = "Sui" },
{ id = "faucetcoin", name = "faucetcoin" },
]

[[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 = "faucetcoin"
source = { local = "../../task2/faucetcoin" }

dependencies = [
{ id = "Sui", name = "Sui" },
]

[move.toolchain-version]
compiler-version = "1.37.3"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x549616dbefd766ce256f00b4662eca840964dff08174f6206d141157e9b2bebd"
latest-published-id = "0x549616dbefd766ce256f00b4662eca840964dff08174f6206d141157e9b2bebd"
published-version = "1"

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x472b8f0ff90d472069fb57f33a9899243debb7c3201f1dc3bb2b49d15ec537ee"
latest-published-id = "0x472b8f0ff90d472069fb57f33a9899243debb7c3201f1dc3bb2b49d15ec537ee"
published-version = "1"
38 changes: 38 additions & 0 deletions mover/EarthChen/code/task4/earthchen_game/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "guess_number"
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" }
faucetcoin = { local = "../../task2/faucetcoin" }

# 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]
guess_number = "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"

38 changes: 38 additions & 0 deletions mover/EarthChen/code/task4/earthchen_game/sources/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "guess_number"
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" }
faucetcoin = { local = "../faucetcoin" }

# 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]
guess_number = "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"

113 changes: 113 additions & 0 deletions mover/EarthChen/code/task4/earthchen_game/sources/earthchen_game.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/// Module: guess_number
module guess_number::guess_number {
use sui::object::{Self, UID};
use sui::balance::{Self, Balance};
use sui::coin::{Self, Coin};
use sui::transfer::{transfer, share_object, public_transfer};
use sui::tx_context::{Self, sender};
use sui::bcs;
use faucetcoin::faucetcoin::FAUCETCOIN;
use std::debug::print;
use std::string::utf8;

const ENotAllow: u64 = 0;
const EInvalidStake: u64 = 1;
const Rate: u64 = 2;
const MaxStake: u64 = 1000;
const MinStake: u64 = 10;

// otw
public struct GUESS_NUMBER has drop {}

// 奖金池,管理员为earthchen
public struct PrizePool has key {
id: UID,
prize: Balance<FAUCETCOIN>,
earthchen: address // my github id
}

// 用于创建、初始化奖金池,之后删除
public struct PrizePoolCap has key {
id: UID
}

// 创建Cap,并发送给earthchen
fun init(_otw: GUESS_NUMBER, ctx: &mut TxContext) {
let prize_pool_cap = PrizePoolCap {
id: object::new(ctx)
};
transfer(prize_pool_cap, tx_context::sender(ctx));
}

// earthchen用于创建奖金池,并注入初始奖金
public entry fun initialize_pool(cap: PrizePoolCap, coin: Coin<FAUCETCOIN>, ctx: &mut TxContext) {
let prize_pool = PrizePool {
id: object::new(ctx),
prize: coin.into_balance(),
earthchen: sender(ctx)
};
share_object(prize_pool);
let PrizePoolCap { id } = cap;
object::delete(id);
}



// 给奖金池注入更多奖金,任何人都有权限
public entry fun top_up(pool: &mut PrizePool, coin: Coin<FAUCETCOIN>) {
coin::put(&mut pool.prize, coin)
}

// 在奖金池取出指定数量金额
public entry fun withdraw(pool: &mut PrizePool, amount: u64, ctx: &mut TxContext) {
assert!(pool.earthchen == sender(ctx), ENotAllow);
let balance = pool.prize.split(amount);
let coin = coin::from_balance(balance, ctx);
public_transfer(coin, sender(ctx));
}

// 在奖金池取出所有奖金
public fun withdraw_all(pool: &mut PrizePool, ctx: &mut TxContext) {
assert!(pool.earthchen == sender(ctx), ENotAllow);
let balance = pool.prize.withdraw_all();
let coin = coin::from_balance(balance, ctx);
public_transfer(coin, sender(ctx));
}

// 查询奖金池中剩余的金额
public fun query_prize(pool: &mut PrizePool):u64 {
pool.prize.value()
}

// 通过交易hash的方式获取随机数
fun get_random_num(ctx: &mut TxContext): u8 {
let tx_digest = tx_context::digest(ctx);
let tx_digest_u64= bcs::peel_u8(&mut bcs::new(*tx_digest));
let random_index: u8 = tx_digest_u64 % 3;
random_index
}

// 开始游戏
public entry fun start_game(pool: &mut PrizePool, geuss_number: u8, stake: Coin<FAUCETCOIN>, ctx: &mut TxContext): bool {
let balance = stake.into_balance();
let value = balance.value();
let mut res = false;
assert!(value < MaxStake && value > MinStake, EInvalidStake);
pool.prize.join(balance);
let the_number = get_random_num(ctx);
print(&utf8(b"The right number is:"));
print(&the_number);
if (geuss_number == the_number) {
let prize_value = pool.prize.split(Rate * value);
let prize_coin = coin::from_balance(prize_value, ctx);
public_transfer(prize_coin, sender(ctx));
res = true;
};
res
}

#[test_only]
public fun init_for_testing(ctx: &mut TxContext) {
init(GUESS_NUMBER {}, ctx);
}
}
8 changes: 4 additions & 4 deletions mover/EarthChen/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
- [x] scan上的NFT截图:![Scan截图](./images/nft_scan.png)

## 04 Move Game
- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [x] game package id :0x472b8f0ff90d472069fb57f33a9899243debb7c3201f1dc3bb2b49d15ec537ee
- [x] deposit Coin hash: 8VXsjH2eVz1TWAc5DkAWrtPYVNoArEgVJZq8tCxnNscc
- [x] withdraw `Coin` hash: HizGTtY5FGAGuEQ3g9idAyFuP5BMh8dnH1hSKsTV9ZD8
- [x] play game hash: AjYKT5oKewnDWv9fnDvYRvoeRDBzfGuYGmPSMXGpv2cY

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

0 comments on commit ea57c27

Please sign in to comment.