Skip to content

Commit

Permalink
complete task4
Browse files Browse the repository at this point in the history
  • Loading branch information
yufei committed Jan 3, 2025
1 parent 45208c9 commit 39a5a7c
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 4 deletions.
1 change: 1 addition & 0 deletions mover/yufin/code/task4/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/*
40 changes: 40 additions & 0 deletions mover/yufin/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 = 3
manifest_digest = "ED18027FE1E04C9C17AFCADBD07CCA64749BB3234A62A2FDA2C1172E7092CD17"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ id = "Sui", name = "Sui" },
]

[[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.toolchain-version]
compiler-version = "1.39.3"
edition = "2024.beta"
flavor = "sui"

[env]

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

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x6c38fa662e97fc659e1e9181b074342fe398f5dca6b64e428e927df0bf2883a4"
latest-published-id = "0x6c38fa662e97fc659e1e9181b074342fe398f5dca6b64e428e927df0bf2883a4"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/yufin/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 (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"

147 changes: 147 additions & 0 deletions mover/yufin/code/task4/sources/flip.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
module task4::flip_coin{
use std::string;
use sui::balance::Balance;
use sui::coin;
use sui::coin::Coin;
use sui::transfer::public_transfer;
use std::string::{String, from_ascii};
use std::type_name;
use sui::event;
use sui::random;

const EOverBalance: u64 = 0x1;
const EVaultIsEmpty: u64 = 0x2;


public struct Vault<phantom T> has key {
id: UID,
amount: Balance<T>,
}

public struct AdminCap has key {
id: UID,
}

public struct TransEvent has copy, drop {
caller: address,
amount_change: u64,
direction: String,
pool_balance: u64,
coin_type: String,
}

fun init(ctx: &mut TxContext) {
let admin = AdminCap{id: object::new(ctx)};
transfer::transfer(admin, ctx.sender());
}

public entry fun new_vault<T>(coin_in: Coin<T>, ctx: &mut TxContext) {
let balance = coin::into_balance(coin_in);

let vault = Vault {
id: object::new(ctx),
amount: balance,
};

transfer::share_object(vault);
}

public entry fun deposit<T>(vault: &mut Vault<T>, mut coin_in: Coin<T>, amount: u64, ctx: &mut TxContext) {
let delta_amount: u64;
if (coin_in.value() <= amount) {
delta_amount = coin_in.value();
vault.amount.join(coin::into_balance(coin_in));
} else {
let split_coin = coin_in.split(amount, ctx);
vault.amount.join(coin::into_balance(split_coin));
public_transfer(coin_in, ctx.sender());
delta_amount = amount;
};

event::emit(TransEvent{
caller: ctx.sender(),
amount_change: delta_amount,
direction: string::utf8(b"in"),
pool_balance: vault.amount.value(),
coin_type: from_ascii(type_name::get<T>().into_string()),
})
}

public entry fun batch_deposit<T>(vault: &mut Vault<T>, amount: u64, mut chips: vector<Coin<T>>, ctx:&mut TxContext) {
let mut merged_coin = vector::pop_back(&mut chips);
while (!vector::is_empty(&chips)) {
let coin_to_merge = vector::pop_back(&mut chips);
merged_coin.join(coin_to_merge);
};
vector::destroy_empty(chips);
let split_coin = merged_coin.split(amount, ctx);
vault.amount.join(coin::into_balance(split_coin));
if (merged_coin.value() > 0) {
transfer::public_transfer(merged_coin, tx_context::sender(ctx));
} else {
coin::destroy_zero(merged_coin);
};

event::emit(TransEvent{
caller: ctx.sender(),
amount_change: amount,
direction: string::utf8(b"in"),
pool_balance: vault.amount.value(),
coin_type: from_ascii(type_name::get<T>().into_string()),
});
}

public entry fun withdraw<T>(_: &AdminCap, vault: &mut Vault<T>, amount: u64, ctx: &mut TxContext) {
assert!(vault.amount.value() > amount, EOverBalance);
let withdraw_balance = vault.amount.split(amount);
let withdraw_coin = coin::from_balance(withdraw_balance, ctx);
public_transfer(withdraw_coin, ctx.sender());
event::emit(TransEvent{
caller: ctx.sender(),
amount_change: amount,
direction: string::utf8(b"out"),
pool_balance: vault.amount.value(),
coin_type: from_ascii(type_name::get<T>().into_string()),
});
}

entry fun play<T>(rand: &random::Random, vault: &mut Vault<T>, res:bool, mut coin_in: Coin<T>, amount: u64, ctx: &mut TxContext) {
let chip: Coin<T>;
if (coin_in.value() > amount) {
// need split
chip = coin_in.split(amount, ctx);
transfer::public_transfer(coin_in, ctx.sender());
} else {
chip = coin_in;
};

let mut gen = random::new_generator(rand, ctx);
let fliped_res:bool = random::generate_bool(&mut gen);
let bet_amount_val = chip.value();
assert!(vault.amount.value() >= bet_amount_val * 10, EOverBalance);
let direction: vector<u8>;

if (res == fliped_res) {
// double
let reward_balance = vault.amount.split(bet_amount_val);
let reward_coin = coin::from_balance(reward_balance, ctx);
public_transfer(reward_coin, ctx.sender());
public_transfer(chip, ctx.sender());
direction = b"out";
} else {
// broken
let bet_balance = coin::into_balance(chip);
vault.amount.join(bet_balance);
direction = b"in";
};

event::emit(TransEvent{
caller: ctx.sender(),
amount_change: bet_amount_val,
direction: string::utf8(direction),
pool_balance: vault.amount.value(),
coin_type: from_ascii(type_name::get<T>().into_string()),
});
}

}
30 changes: 30 additions & 0 deletions mover/yufin/code/task4/tests/call.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# testnet

#vault<FAUCET> pkgID: 0x60bc1b8e7f9219a10fb3886b666bb4a68556c3992d37697da5b880043de413fb
#FAUCET typeId: 0x115adc3cce5ec094078e6a17c65336a2780a0ec579042f11aeda3f5a8d3b8d3d::faucet::FAUCET

# flipV2 pkgid: 0xab9394bb569f178b6b711bc6b46d951c0307e516d57f138e527246ddb84a9513
# vault pkgid: 0x59553951c763b904caeb9efbcfa77b9af6a099b1d2546ea64dfd67adad8fae0d

sui client call --package 0xab9394bb569f178b6b711bc6b46d951c0307e516d57f138e527246ddb84a9513 \
--module flip_coin \
--function play \
--type-args 0x115adc3cce5ec094078e6a17c65336a2780a0ec579042f11aeda3f5a8d3b8d3d::faucet::FAUCET \
--args 0x8 \
0x59553951c763b904caeb9efbcfa77b9af6a099b1d2546ea64dfd67adad8fae0d \
true \
0x771f0656ed44300dca07c49fc5e9dcc22f800efce1fd6da514c0903cc3791cc3 \
50000000


# mainnet

sui client call --package 0x6c38fa662e97fc659e1e9181b074342fe398f5dca6b64e428e927df0bf2883a4 \
--module flip_coin \
--function play \
--type-args 0xe31a18ace543a8d317824b15718a54cf0477e5c93bcedb2f4c5d877dedc16a18::faucet::FAUCET \
--args 0x8 \
0xe1684af2aeb4659196f57ae47ad09878d59e4e69faf258e52febd121202f921e \
true \
0x68ac633bd609f7dc74cb60e63e4b9d44b2658ee67d4745347fae4c19e7eb727a \
50000000
18 changes: 18 additions & 0 deletions mover/yufin/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
}
*/
8 changes: 4 additions & 4 deletions mover/yufin/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
- [] scan上的NFT截图:![Scan截图](./images/task3nftscan.png)

## 04 Move Game
- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [] game package id : 0x6c38fa662e97fc659e1e9181b074342fe398f5dca6b64e428e927df0bf2883a4
- [] deposit Coin hash: Ga9KvaR3fcqSeRVjBtzzYk4Zc83vmRBeHS1ZJMsV5VHc
- [] withdraw `Coin` hash: Aa6QFbRbtRUaoMjDYSPLptjBJ4wut5mr8QKpNbAHnMc5
- [] play game hash: Byg6qCLoRW7yraE5v65MqcUH3hW4wFUr4mTTaahukYmw

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

0 comments on commit 39a5a7c

Please sign in to comment.