Skip to content

Commit

Permalink
任务4
Browse files Browse the repository at this point in the history
  • Loading branch information
AlwenXX authored Oct 19, 2024
1 parent 9c38e2b commit 62bf973
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 4 deletions.
43 changes: 43 additions & 0 deletions mover/AlwenXX/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 = 2
manifest_digest = "36998FC1B588C4D41A4FFF3B30EF441F5E1F4671E93C5FE278D0F3CAA3997208"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
dependencies = [
{ name = "Sui" },
{ name = "task2" },
]

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

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

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

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0xb5abc8171e489495f68e18312b6867a1875340fdd03ef4ce905c08f49e5d37de"
latest-published-id = "0xb5abc8171e489495f68e18312b6867a1875340fdd03ef4ce905c08f49e5d37de"
published-version = "1"
39 changes: 39 additions & 0 deletions mover/AlwenXX/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
# 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" }
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 = "0x072b9943e22a65053ebbc1b20d4bb857e1c710f55e54069a5f9d1739d9eb44af"

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

91 changes: 91 additions & 0 deletions mover/AlwenXX/code/task4/sources/task4.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module task4::flip_coin {

use sui::balance;
use sui::balance::{Balance, zero};
use sui::coin;
use sui::coin::{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::alwenxx_faucet_coin::{ALWENXX_FAUCET_COIN};

public struct Game has key {
id: UID,
val: Balance<ALWENXX_FAUCET_COIN>
}

public struct AdaminCap has key {
id: UID
}


fun init(ctx: &mut TxContext) {
let game = Game {
id: object::new(ctx),
val: zero()
};

share_object(game);

let admin = AdaminCap {
id: object::new(ctx)
};

transfer(admin, sender(ctx));
}
#[allow(lint(public_random))]
public entry fun play(
game: &mut Game,
flip_value: bool,
in: coin::Coin<ALWENXX_FAUCET_COIN>,
rand: &Random,
ctx: &mut TxContext
) {
let coin_value = coin::value(&in);

let game_val = balance::value(&game.val);

if (game_val < coin_value) {
abort 0
};

if (game_val < coin_value * 10) {
abort 1
};

let mut gen = random::new_generator(rand, ctx);
let flag = random::generate_bool(&mut gen);

let play_address = sender(ctx);
if (flip_value == flag) {
withdraw(game, coin_value, play_address, ctx);
public_transfer(in, play_address);
} else {
deposit(game, in, ctx);
}
}

fun deposit(game: &mut Game, in: coin::Coin<ALWENXX_FAUCET_COIN>, _ctx: &mut TxContext) {
let in_balance = into_balance(in);
balance::join(&mut game.val, in_balance);
}

public entry fun public_deposit(
game: &mut Game,
in: coin::Coin<ALWENXX_FAUCET_COIN>,
ctx: &mut TxContext
) {
deposit(game, in, ctx);
}

fun withdraw(game: &mut Game, amt: u64, to: address, ctx: &mut TxContext) {
let win_balance = balance::split(&mut game.val, amt);
let win_coin = from_balance(win_balance, ctx);
public_transfer(win_coin, to);
}

public entry fun public_withdraw(_: &AdaminCap, game: &mut Game, amt: u64, ctx: &mut TxContext) {
withdraw(game, amt, sender(ctx), ctx);
}
}
19 changes: 19 additions & 0 deletions mover/AlwenXX/code/task4/tests/task4_tests.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
}
}
*/
8 changes: 4 additions & 4 deletions mover/AlwenXX/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
- [x] scan上的NFT截图:![Scan截图](./scn.png)

## 04 Move Game
- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [x] game package id : 0xb5abc8171e489495f68e18312b6867a1875340fdd03ef4ce905c08f49e5d37de
- [x] deposit Coin hash: 2CYaQ3JtLKMvMdhKVdERFCWouqt8NTMyRL1bs8ri9hpP
- [x] withdraw `Coin` hash: 3ZttwkxDn5oQKuC8W3c8gjaNP579ZokD6NupgcAAGADx
- [x] play game hash: Ca6jMT5umfAw53P1fdM1gE1bDK7LgownMpAcsbNaKtZ6

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

0 comments on commit 62bf973

Please sign in to comment.