-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yufei
committed
Jan 3, 2025
1 parent
45208c9
commit 39a5a7c
Showing
7 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters