Skip to content

Commit

Permalink
task 4 by cryptoctl
Browse files Browse the repository at this point in the history
chin xyz
  • Loading branch information
cryptoctl committed Nov 20, 2024
1 parent 64463bd commit 7f35f89
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANtiPY0Dt07pjl+uL+59aXoFoHmBk2ASftqtyx+AwakX
10 changes: 4 additions & 6 deletions mover/cryptoctl/code/task2/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "task2"
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])"]
published-at = "0x595755e12a700673fd174e05eac9a6e22cfb5d202de027d5830c256bd20d5b3a"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }
Expand All @@ -19,7 +20,10 @@ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-fram
# Override = { local = "../conflicting/version", override = true }

[addresses]
# mainnet
task2 = "0x595755e12a700673fd174e05eac9a6e22cfb5d202de027d5830c256bd20d5b3a"
# testnet
# task2 = "0x260cf370e2f907c25e21cdf2b285224a88b3b839f74fb3b25fa3e0fb063f14bd"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
Expand All @@ -29,9 +33,3 @@ task2 = "0x595755e12a700673fd174e05eac9a6e22cfb5d202de027d5830c256bd20d5b3a"
# 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"

43 changes: 43 additions & 0 deletions mover/cryptoctl/code/task4/Move.lock
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 = "DE039AFB413BCB1F8EB657E4505F2237C9429034C55BA5EA4E9698CF9FA36946"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
dependencies = [
{ id = "Sui", name = "Sui" },
{ id = "task2", name = "task2" },
]

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

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

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

[env]

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

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

106 changes: 106 additions & 0 deletions mover/cryptoctl/code/task4/sources/task4.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
/// Module: task4*/
module task4::game;

use sui::balance::{Balance};
use sui::balance;
use sui::coin::{Coin, into_balance};
use sui::coin;
// use sui::{from_balance,into_balance};
use sui::random;
use sui::random::Random;
use sui::transfer::{share_object,transfer,public_transfer};
use sui::tx_context::sender;
use task2::faucet_coin::FAUCET_COIN;
const EGameInsufficientBalance: u64 = 1001;
const EUserInsufficientBalance: u64 = 1002;

public struct AdminCap has key{
id:UID
}

public struct CryptoCtl_Game has key, store{
id:UID,
balance: Balance<FAUCET_COIN>,
}

public struct GAME has drop {}

fun init(_otw:GAME, ctx: &mut TxContext) {
let game = CryptoCtl_Game {
id: object::new(ctx),
balance: balance::zero<FAUCET_COIN>()
};
share_object(game);

let admin = AdminCap {id: object::new(ctx)};
transfer(admin, sender(ctx));
}

public entry fun deposit(
game: &mut CryptoCtl_Game,
coin:&mut Coin<FAUCET_COIN>,
amount: u64,
ctx:&mut TxContext
){
assert!(
coin.value() >= amount,
EUserInsufficientBalance
);
let split_coin = coin.split(amount, ctx);

game.balance.join(into_balance(split_coin));

//let into_balance = into_balance(coin);
//game.balance.join(into_balance);
}

public entry fun withdraw(_admin:&AdminCap, game:&mut CryptoCtl_Game, amount:u64, to:address, ctx:&mut TxContext){

assert!(
game.balance.value() >= amount,
EGameInsufficientBalance
);

let cash = coin::take(&mut game.balance, amount, ctx);
public_transfer(cash, to);
}

// 如果用户猜对了,获得双倍奖励。如果用户猜错了,损失投入的代币

public entry fun play(
game: &mut CryptoCtl_Game,
rnd: &Random,
guess: bool,
coin: &mut Coin<FAUCET_COIN>,
amount: u64,
ctx: &mut TxContext
) {
// 检查合约余额是否充足,确保用户获胜时有足够金额奖励
assert!(
game.balance.value() >= amount,
EGameInsufficientBalance
);
// 检查用户的余额是否充足
assert!(
coin.value() >= 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, amount, ctx);
coin.join(reward);
}
// 猜错了就损失投入的代币
else {
Self::deposit(game, coin, amount, ctx)
}
}

18 changes: 18 additions & 0 deletions mover/cryptoctl/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
}
*/
13 changes: 12 additions & 1 deletion mover/cryptoctl/notes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ sui client call --package 0x595755e12a700673fd174e05eac9a6e22cfb5d202de027d5830c

https://avatars.githubusercontent.com/u/8326809?v=4

sui client publish --skip-dependency-verification
sui client publish --skip-dependency-verification

testnet
sui client call --package 0xdc96c5f9ebaeb964cb66569ffa1d9177c032e3900c291ed4edcc005da8809b19 --module game --function play --args 0x451444889ef2880302e10dcd3f39819aefa06590825e9feadf18f7a427bbb953 0x0000000000000000000000000000000000000000000000000000000000000008 true 0x75bdccae7ed097f5112d4a1a8ad142b5e0d4b3280ea24f09618698263164a385 1 --dry-run

game_id
0x605c2e65b5bb0c5cc0ff8b72868276d59201a6bea19e75b91a682abfac61536a

coin_id
0x6a10d03353adf8fb24e28f17d626b7e7825dc92ee2ae0e19e067f1641dd4b3b7

sui client call --package 0x76c5be08481759db8a418253eb5fba7c08b2615a94427dc801b375e667442c87 --module game --function play --args 0x605c2e65b5bb0c5cc0ff8b72868276d59201a6bea19e75b91a682abfac61536a 0x0000000000000000000000000000000000000000000000000000000000000008 true 0x6a10d03353adf8fb24e28f17d626b7e7825dc92ee2ae0e19e067f1641dd4b3b7 1 --dry-run
8 changes: 4 additions & 4 deletions mover/cryptoctl/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
- [x] scan上的NFT截图:![Scan截图](./images/nft_suiscan.png)

## 04 Move Game
- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [x] game package id :0x76c5be08481759db8a418253eb5fba7c08b2615a94427dc801b375e667442c87
- [x] deposit Coin hash:HLqCXABdnyyB2W7EYK8PoAKjj8MFT64vpXBTdfBvNeGg
- [x] withdraw `Coin` hash:3rKw6dneqLvt2WqjTjvARmwZmFFFdrJPTivMLimWpntn
- [x] play game hash:B8kV437qWqbxq2RDpRaTwfaJnGVWdQmpBiPKrSsK9k4q

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

0 comments on commit 7f35f89

Please sign in to comment.