Skip to content

Commit

Permalink
Merge pull request #1581 from billaGitHub2016/main
Browse files Browse the repository at this point in the history
完成task4
  • Loading branch information
uvd authored Oct 15, 2024
2 parents db11970 + 4fb0996 commit 5f2742e
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 4 deletions.
40 changes: 40 additions & 0 deletions mover/billaGitHub2016/code/task4/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 2
manifest_digest = "DF9F928D650FE3C031917CEB7CC124C1360CAF1A3C7BA8B03E5E539F951A74A0"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }

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

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

[env]

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

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047"
latest-published-id = "0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047"
published-version = "1"
39 changes: 39 additions & 0 deletions mover/billaGitHub2016/code/task4/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "task4"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
published-at = "0x9a38fce43e3f33e78b2d92b5e91b15b19ade8d2f8d64a9575cb7900adf652ab0" #testnet

# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]

[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"

46 changes: 46 additions & 0 deletions mover/billaGitHub2016/code/task4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# testnet
packageID: 0x9a38fce43e3f33e78b2d92b5e91b15b19ade8d2f8d64a9575cb7900adf652ab0
upgradeCap: 0xe2ce951cceb4acab4f6ad71b9acd5455f7038fab4997cc75c5d4d1b5a636e0b9
wallet: 0x4c016d80dbd4c4715a877fe274fe33bdd33b1557e38f888db23be790564fcfd6

## 铸币
sui client call --function mint --module billa_github2016_faucet_coin --package 0x635055d3c136d15d7f39bb610f1bcebbd35b58cc76c8ecd4f715a0ef7ab7ffe1 --args 0x9a5a5e5d1ddcea82226594fe8c4e38f1dddeb6089ca48f95cd73eb7e9f4bb16f 2000000000 0x4c016d80dbd4c4715a877fe274fe33bdd33b1557e38f888db23be790564fcfd6 --gas-budget 20000000

## create_game
sui client call --package 0x9a38fce43e3f33e78b2d92b5e91b15b19ade8d2f8d64a9575cb7900adf652ab0 --module coin_toss_game_billa_github2016 --function create_game --args 0x2d138b99e6e00be0db0cec4052b2507b35d1f10db13a1b21ae8aa633ce5dca06 --type-args 0x635055d3c136d15d7f39bb610f1bcebbd35b58cc76c8ecd4f715a0ef7ab7ffe1::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 20000000

adminCap: 0x3bdfdee2e561adfc5824f9aca9b6d25da0ccd825659b56ab61c7bfd2307235a4
game: 0x0fd658b9397e9e4e26bd19c1d187f8a847f5bd61e909616e97262a2e43e9c6f7

## play
sui client call --package 0x9a38fce43e3f33e78b2d92b5e91b15b19ade8d2f8d64a9575cb7900adf652ab0 --module coin_toss_game_billa_github2016 --function play --args 0x0fd658b9397e9e4e26bd19c1d187f8a847f5bd61e909616e97262a2e43e9c6f7 0x0080dc94a661df944f3c073432759c2309c391cf0e845dbd60b99a478331117b 1 0x8 --type-args 0x635055d3c136d15d7f39bb610f1bcebbd35b58cc76c8ecd4f715a0ef7ab7ffe1::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 20000000

## withdraw
sui client call --package 0x9a38fce43e3f33e78b2d92b5e91b15b19ade8d2f8d64a9575cb7900adf652ab0 --module coin_toss_game_billa_github2016 --function withdraw --args 0x3bdfdee2e561adfc5824f9aca9b6d25da0ccd825659b56ab61c7bfd2307235a4 0x0fd658b9397e9e4e26bd19c1d187f8a847f5bd61e909616e97262a2e43e9c6f7 2110000000 --type-args 0x635055d3c136d15d7f39bb610f1bcebbd35b58cc76c8ecd4f715a0ef7ab7ffe1::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 20000000

## increase
sui client call --package 0x9a38fce43e3f33e78b2d92b5e91b15b19ade8d2f8d64a9575cb7900adf652ab0 --module coin_toss_game_billa_github2016 --function increase_game_pool --args 0x3bdfdee2e561adfc5824f9aca9b6d25da0ccd825659b56ab61c7bfd2307235a4 0x0fd658b9397e9e4e26bd19c1d187f8a847f5bd61e909616e97262a2e43e9c6f7 0xd4f013e5efd22c0d4d23c9c07adc5ae27033a3f11f0879f7c308483d98ca07e5 --type-args 0x635055d3c136d15d7f39bb610f1bcebbd35b58cc76c8ecd4f715a0ef7ab7ffe1::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 20000000

# mainnet
PackageID: 0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047
UpgradeCap: 0x947489324162d5b037f514738085c963eda3b7f6b1d75d5d209a29a27ee89416

## 铸币
sui client call --function mint --module billa_github2016_faucet_coin --package 0xb09a7d960e811b57f91e1222eba51a206b16b93ca20750c9a0950e85292be0b2 --args 0xcdb4d669e9527a0dd7f35ad3e57ecb76f79d75d77d4406d0974d74fc11f80c1d 1000000000 0x4c016d80dbd4c4715a877fe274fe33bdd33b1557e38f888db23be790564fcfd6 --gas-budget 70000000

## create game
sui client call --function mint --module billa_github2016_faucet_coin --package 0xb09a7d960e811b57f91e1222eba51a206b16b93ca20750c9a0950e85292be0b2 --args 0xcdb4d669e9527a0dd7f35ad3e57ecb76f79d75d77d4406d0974d74fc11f80c1d 1000000000 0x4c016d80dbd4c4715a877fe274fe33bdd33b1557e38f888db23be790564fcfd6 --gas-budget 70000000
adminCap: 0x681b0ab08bc6d9b932037dfdd7ab1f0a15355b602f8a41ac0d78c71dbdb1e94b
game: 0xe7fe2757fd5a1d1bc74191b88be4d7905c96c0a8d41c5c56d8a3ba72eb620e0d

## play
sui client call --package 0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047 --module coin_toss_game_billa_github2016 --function play --args 0xe7fe2757fd5a1d1bc74191b88be4d7905c96c0a8d41c5c56d8a3ba72eb620e0d 0x6987dc3e58996228589691505380402e7853667ceaac4e72ddf6cf0525953dbf 1 0x8 --type-args 0xb09a7d960e811b57f91e1222eba51a206b16b93ca20750c9a0950e85292be0b2::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 70000000

## increase
sui client call --package 0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047 --module coin_toss_game_billa_github2016 --function increase_game_pool --args 0x681b0ab08bc6d9b932037dfdd7ab1f0a15355b602f8a41ac0d78c71dbdb1e94b 0xe7fe2757fd5a1d1bc74191b88be4d7905c96c0a8d41c5c56d8a3ba72eb620e0d 0x794f633098792dadd2d8d5a7c9f26194ba26add2bf664a47f5117d4d6a1e7439 --type-args 0xb09a7d960e811b57f91e1222eba51a206b16b93ca20750c9a0950e85292be0b2::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 70000000

## withdraw
sui client call --package 0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047 --module coin_toss_game_billa_github2016 --function withdraw --args 0x681b0ab08bc6d9b932037dfdd7ab1f0a15355b602f8a41ac0d78c71dbdb1e94b 0xe7fe2757fd5a1d1bc74191b88be4d7905c96c0a8d41c5c56d8a3ba72eb620e0d 1000000000 --type-args 0xb09a7d960e811b57f91e1222eba51a206b16b93ca20750c9a0950e85292be0b2::billa_github2016_faucet_coin::BILLA_GITHUB2016_FAUCET_COIN --gas-budget 70000000



Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

/// 猜硬币游戏
module task4::coin_toss_game_billa_github2016 {
use sui::balance;
use sui::balance::{ Balance };
use sui::coin::{ Self, Coin };
use sui::random::{Random, new_generator};
use sui::event;
use std::string::{ String, utf8 };

public struct Game<phantom T> has key, store {
id: UID,
pool: Balance<T>,
ticket: u64
}

public struct AdminCap has key {
id: UID,
}

public struct GAME_RESULT_EVENT has copy, drop {
game_id: ID,
player_address: address,
result: u8,
coin_result: String,
is_win: bool
}

const EPoolPlayNotEnough: u64 = 1;
const ECoinBalanceNotEnough: u64 = 2;
const EPoolWithdrawNotEnough: u64 = 3;

fun init(_ctx: &TxContext) {

}

public fun create_game<T>(pool: Coin<T>, ctx: &mut TxContext) {
let mut game = Game<T> {
id: object::new(ctx),
pool: balance::zero<T>(),
ticket: 100_000_000 // 0.1
};
coin::put(&mut game.pool, pool);

transfer::share_object(game);
let admin_cap = AdminCap { id: object::new(ctx) };
transfer::transfer(admin_cap, ctx.sender());
}

#[allow(lint(self_transfer))]
public entry fun play<T>(game: &mut Game<T>, bet_coin: Coin<T>, toss_result: u8, r: &Random, ctx: &mut TxContext) {
let coin_value = bet_coin.value();
let pool_value: u64 = game.pool.value();
assert!(coin_value >= game.ticket, ECoinBalanceNotEnough);
assert!(pool_value >= game.ticket, EPoolPlayNotEnough);

let mut generator = new_generator(r, ctx);
let v = generator.generate_u8_in_range(1, 2);
let mut coin_result = utf8(b"front");
if (v == 2) {
coin_result = utf8(b"reverse");
};

if (v == toss_result) {
let reward = balance::split(&mut game.pool, game.ticket);
let reward_coin = coin::from_balance(reward, ctx);
transfer::public_transfer(reward_coin, ctx.sender());
transfer::public_transfer(bet_coin, ctx.sender());
} else {
let mut coin_balance = coin::into_balance(bet_coin);
if (coin_value > game.ticket) {
game.pool.join(coin_balance.split(game.ticket));
let change = coin::from_balance(coin_balance, ctx);
transfer::public_transfer(change, ctx.sender());
} else {
game.pool.join(coin_balance);
};
};

event::emit(GAME_RESULT_EVENT {
game_id: object::uid_to_inner(&game.id),
player_address: ctx.sender(),
result: v,
is_win: v == toss_result,
coin_result
});
}

public entry fun increase_game_pool<T>(_: &AdminCap, game: &mut Game<T>, increase_coin: Coin<T>, _ctx: &mut TxContext) {
let increase_coin_balance = coin::into_balance(increase_coin);
game.pool.join(increase_coin_balance);
}

#[allow(lint(self_transfer))]
public entry fun withdraw<T>(_: &AdminCap, game: &mut Game<T>, withdraw_balance: u64, ctx: &mut TxContext) {
let game_pool_balance = game.pool.value();
assert!(withdraw_balance <= game_pool_balance, EPoolWithdrawNotEnough);
let withdraw_coin_balance = game.pool.split(withdraw_balance);
let withdraw_coin = coin::from_balance(withdraw_coin_balance, ctx);
transfer::public_transfer(withdraw_coin, ctx.sender());
}
}

19 changes: 19 additions & 0 deletions mover/billaGitHub2016/code/task4/tests/coin_toss_game_test.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
#[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
}
}
*/
10 changes: 6 additions & 4 deletions mover/billaGitHub2016/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
- [x] nft package id : 0xa3407929538dd603fa7ab7f1b156de82440026e99ff566ec32c8716175021b05
- [x] nft object id : 0x21f671bb724158b6c9ad0a621a36c33d1c68f6ea8e79d4b97ab92f0bf9d9ffa5
- [x] 转账 nft hash: AgaeNhrMEMsZUAKgBMhFpGQrFCRSDSohTeWv9syzCWQJ
- [x] scan上的nft截图: ![Scan截图](./images/nft.jpg)
- [x] scna上的nft截图: ![Scan截图](./images/nft.jpg)

## 04 Move Game
- [] game package id :
- [] call game hash:
- [x] game package id : 0xf40f121899689d2b314d0f17fc5ff9e163e31eee40a92a37e43e34baaa36a047
- [x] deposit Coin hash: Gc6jhESX8E7HDagqZRCj1QrzZNT5nd9PxBW4e8xVmtGT
- [x] withdraw `Coin` hash: HpzTBEfq8qhCUGARqBwksFrP6Jxedhv4GN58aj8X7fh9
- [x] play game hash: 74XhGT3JpBeaKo3CUS4C2JDEn9dh1RrazVoNn3j2LmT8

## 05 Move Swap
- [] swap package id :
- [] call swap hash:

## 06 SDK PTB
- [] save hash :
- [] save hash :

0 comments on commit 5f2742e

Please sign in to comment.