Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task3 4 5 update #1983

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mover/brainsk/code/task2/faucetcoin/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ flavor = "sui"

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

[env.mainnet]
Expand Down
5 changes: 5 additions & 0 deletions mover/brainsk/code/task4/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

query:
sui client call --package 0xd593563f21c3598ab5109488d634de8cbfe59b3463856b4dacf1cd90357a65b6 --module task4 --function query_prize --args 0x1439204a54be461586c088ced490ecdb74874d20efdcc808130b5d1bea304ecd
start_game:
sui client call --package 0xd593563f21c3598ab5109488d634de8cbfe59b3463856b4dacf1cd90357a65b6 --module task4 --function start_game --args 0x1439204a54be461586c088ced490ecdb74874d20efdcc808130b5d1bea304ecd 8 0x9a5671fd2a300c700e410d2b37823b943efee8a470fadf40c14cc1bdd484369c
49 changes: 49 additions & 0 deletions mover/brainsk/code/task4/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 = "AE9016CD7391A4C668A0331A1F53A001C32DD709924ABFBB6F22843DAE89A682"
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.38.0"
edition = "2024.beta"
flavor = "sui"

[env]

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

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0xd593563f21c3598ab5109488d634de8cbfe59b3463856b4dacf1cd90357a65b6"
latest-published-id = "0xd593563f21c3598ab5109488d634de8cbfe59b3463856b4dacf1cd90357a65b6"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/brainsk/code/task4/Move.toml
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" }
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]
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"

108 changes: 108 additions & 0 deletions mover/brainsk/code/task4/sources/task4.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/// Module: task4
module task4::task4 {
use sui::object::{Self, UID};
use sui::balance::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 = 100000000000000000;
const MinStake: u64 = 10;

// otw
public struct TASK4 has drop {}

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

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

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

// 用于创建奖金池,并注入初始奖金
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(),
brainsk: sender(ctx)
};
share_object(prize_pool);
let PrizePoolCap { id } = cap;
object::delete(id);
}

// 给奖金池注入更多奖金,任何人都有权限
public entry fun deposit(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.brainsk == 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.brainsk == 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 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 = random_num(ctx);
print(&utf8(b"The 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
}
}
18 changes: 18 additions & 0 deletions mover/brainsk/code/task4/tests/task4_tests.move
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
}
*/
9 changes: 9 additions & 0 deletions mover/brainsk/code/task5/brainsk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

deposit_mycoin:
sui client call --package 0x8fce92f5a9044de1b3b19a622c2f2beb74c080321612ca546a8ddfbae089c264 --module brainsk --function deposit_mycoin --args 0x38c79f4b37e56ec98cfdd56c72faea011d4dab9eae0775c399ff975ac003a1c8 0x05f29334ed951b1748ddd65e502056175a83582b077949ad4299670449f3a430
deposit_faucetcoin:
sui client call --package 0x8fce92f5a9044de1b3b19a622c2f2beb74c080321612ca546a8ddfbae089c264 --module brainsk --function deposit_faucetcoin --args 0x38c79f4b37e56ec98cfdd56c72faea011d4dab9eae0775c399ff975ac003a1c8 0xa404c780507a31981dc02c3ed95cf63965f78a66c3963c6065d432ca7b71971f
faucte_swap_my:
sui client call --package 0x8fce92f5a9044de1b3b19a622c2f2beb74c080321612ca546a8ddfbae089c264 --module brainsk --function faucte_swap_my --args 0x38c79f4b37e56ec98cfdd56c72faea011d4dab9eae0775c399ff975ac003a1c8 0xc0b59caa882ecc24c77bb0848c893b0fbfdf419bc52b592329fdb634564a30cb
my_swap_faucet:
sui client call --package 0x8fce92f5a9044de1b3b19a622c2f2beb74c080321612ca546a8ddfbae089c264 --module brainsk --function my_swap_faucet --args 0x38c79f4b37e56ec98cfdd56c72faea011d4dab9eae0775c399ff975ac003a1c8 0xc77d969fdc7b17f7e4fcb609277772c58191ddb53208ce8ded44fab3626d28c5
52 changes: 52 additions & 0 deletions mover/brainsk/code/task5/brainsk/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
manifest_digest = "7E391F177F35AAA09364D249E82FC27924488069285497FF7FBD9FA8534BC6A6"
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3"
dependencies = [
{ id = "Sui", name = "Sui" },
{ id = "faucetcoin", name = "faucetcoin" },
{ id = "mycoin", name = "mycoin" },
]

[[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.package]]
id = "mycoin"
source = { local = "../../task2/mycoin" }

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

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

[env]

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

Loading