Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task4&5 #2096

Merged
merged 1 commit into from
Dec 4, 2024
Merged

task4&5 #2096

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module generate_coin::faucet_coin{

use sui::coin::{Self, TreasuryCap};
use sui::tx_context::TxContext;

Expand Down Expand Up @@ -35,5 +36,6 @@ module generate_coin::faucet_coin{
let treasury_cap = &mut treasury.treasury_cap;
let coin = coin::mint(treasury_cap, 10, ctx); // mint代币
transfer::public_transfer(coin, sender); // 转移至指定地址

}
}
40 changes: 40 additions & 0 deletions mover/looikaizhi/code/task4/game/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 = "09E96EFF150B65B8C2F33F9094E1DCF127B543DB3BF332FE094BF07CD9DAE41E"
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.36.2"
edition = "2024.beta"
flavor = "sui"

[env]

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

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x6b47edc9b36054885048cf077992f16b1c78b2fdf84a13c02c3b16aa8798c122"
latest-published-id = "0x6b47edc9b36054885048cf077992f16b1c78b2fdf84a13c02c3b16aa8798c122"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/looikaizhi/code/task4/game/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "game"
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" }

# 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]
game = "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"

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

use std::ascii::{string, String};
use std::option;
use sui::balance::{Balance, zero};
use sui::coin;
use sui::coin::Coin;
use sui::object;
use sui::object::UID;
use sui::random;
use sui::random::{Random};
use sui::transfer::{share_object, public_transfer, transfer};
use sui::tx_context::{TxContext, sender};
use sui::linked_table;
use game::gameFaucet::GAMEFAUCET;

public struct GAME has key{
id: UID,
name: String,
balance: Balance<GAMEFAUCET>,
provider: linked_table::LinkedTable<address, u64>,
}

public struct Admin has key{
id: UID,
}

fun init(ctx: &mut TxContext){
let game = GAME{
id: object::new(ctx),
name: string(b"looikaizhi"),
balance: zero(),
provider: linked_table::new(ctx),
};

share_object(game);

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

public fun game_rule(r: &Random,ctx: &mut TxContext):bool{
let mut gen = random::new_generator(r, ctx);
let value = random::generate_u8_in_range(&mut gen,1, 4);
if(value == 1) { return true }
else { return false }
}


entry fun play(game: &mut GAME, in_coin: Coin<GAMEFAUCET>, r: &Random, ctx: &mut TxContext){

let player_in = in_coin.value();
let pool_balance = game.balance.value();

assert!(player_in <= pool_balance/10);

let win_or_lose: bool = game_rule(r, ctx);
if(win_or_lose){
// win
let out_balance_pool = game.balance.split(player_in);
let out_coin = coin::from_balance(out_balance_pool, ctx);
public_transfer(out_coin, ctx.sender());
public_transfer(in_coin, ctx.sender());

}else{
// lose,
let in_balance = coin::into_balance(in_coin);
game.balance.join(in_balance);
};

provider_result(game, player_in, !win_or_lose);
}

public entry fun deposit_pool(game: &mut GAME, provider: Coin<GAMEFAUCET>, ctx: &mut TxContext){
let sender = ctx.sender();
let amount = provider.value();

if(!linked_table::contains(&mut game.provider, sender)){
linked_table::push_back(&mut game.provider, sender, 0);
};

// 凭证
let provider_balance = linked_table::borrow_mut(&mut game.provider, sender);
*provider_balance = *provider_balance + amount;

// 存入pool
let in_balance = coin::into_balance(provider);
game.balance.join(in_balance);

}

public entry fun withdraw_pool(game: &mut GAME, amount: u64, ctx: &mut TxContext){
let sender = ctx.sender();

// check
assert!(linked_table::contains(&game.provider, sender));
let provider_balance = linked_table::borrow_mut(&mut game.provider, sender);
assert!(*provider_balance >= amount);

*provider_balance = *provider_balance - amount;

let out_balance_pool = game.balance.split(amount);
let out_coin = coin::from_balance(out_balance_pool, ctx);
public_transfer(out_coin, sender);
}

public fun provider_result(game: &mut GAME, amount: u64, result: bool){
let pool_all_balance = game.balance.value();
assert!(pool_all_balance > 0);

let mut current_key: Option<address> = *linked_table::front(&mut game.provider);
while(option::is_some(&mut current_key)){
let key = option::extract(&mut current_key);
let provider_balance = linked_table::borrow_mut(&mut game.provider, key);

if(result){*provider_balance = *provider_balance + *provider_balance * amount / pool_all_balance}
else{ *provider_balance = *provider_balance - *provider_balance * amount / pool_all_balance};

current_key = *linked_table::next(&mut game.provider, key);
}
}

}
39 changes: 39 additions & 0 deletions mover/looikaizhi/code/task4/game/sources/gameFaucet.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module game::gameFaucet{
use sui::coin::{Self, TreasuryCap};
use sui::tx_context::TxContext;

public struct GAMEFAUCET has drop{}

public struct TreasuryCaoHolder has key, store{
id: UID,
treasury_cap: TreasuryCap<GAMEFAUCET>,
}

// otw = One-time witness,确保只能在init初始化调用一次,同时结构体只有drop能力
fun init(otw:GAMEFAUCET, ctx: &mut TxContext){
let (treasury_cap, metadata) = coin::create_currency(
otw,
6,
b"looikaizhi_Faucet",
b"LKZF",
b"Who can let me become a memecoin!!(This is faucet)",
option::none(),
ctx
);
transfer::public_freeze_object(metadata);

let treasury_cap_holder = TreasuryCaoHolder{
id: object::new(ctx),
treasury_cap,
};
transfer::share_object(treasury_cap_holder);

}

public entry fun mint(treasury: &mut TreasuryCaoHolder, amount: u64, ctx: &mut TxContext){
let sender = tx_context::sender(ctx);
let treasury_cap = &mut treasury.treasury_cap;
let coin = coin::mint(treasury_cap, amount, ctx); // mint代币
transfer::public_transfer(coin, sender); // 转移至指定地址
}
}
18 changes: 18 additions & 0 deletions mover/looikaizhi/code/task4/game/tests/game_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module game::game_tests;
// uncomment this line to import the module
// use game::game;

const ENotImplemented: u64 = 0;

#[test]
fun test_game() {
// pass
}

#[test, expected_failure(abort_code = ::game::game_tests::ENotImplemented)]
fun test_game_fail() {
abort ENotImplemented
}
*/
40 changes: 40 additions & 0 deletions mover/looikaizhi/code/task5/swapToken/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 = "350D36428AF0462D1E0B80838030324B93938CE450C6D56F72E8C77586BB3312"
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.36.2"
edition = "2024.beta"
flavor = "sui"

[env]

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

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x7dc5327fca61071f686cfe891049b92921f886ec698d6c86ccdba6fe9c350693"
latest-published-id = "0x7dc5327fca61071f686cfe891049b92921f886ec698d6c86ccdba6fe9c350693"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/looikaizhi/code/task5/swapToken/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "swapToken"
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" }

# 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]
swaptoken = "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"

39 changes: 39 additions & 0 deletions mover/looikaizhi/code/task5/swapToken/sources/swapFaucetA.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module swaptoken::swapFaucetA{
use sui::coin::{Self, TreasuryCap};
use sui::tx_context::TxContext;

public struct SWAPFAUCETA has drop{}

public struct TreasuryCaoHolder has key, store{
id: UID,
treasury_cap: TreasuryCap<SWAPFAUCETA>,
}

// otw = One-time witness,确保只能在init初始化调用一次,同时1结构体只有drop能力
fun init(otw:SWAPFAUCETA, ctx: &mut TxContext){
let (treasury_cap, metadata) = coin::create_currency(
otw,
6,
b"looikaizhi_Faucet",
b"LKZF_A",
b"Who can let me become a memecoin!!(This is faucet)",
option::none(),
ctx
);
transfer::public_freeze_object(metadata);

let treasury_cap_holder = TreasuryCaoHolder{
id: object::new(ctx),
treasury_cap,
};
transfer::share_object(treasury_cap_holder);

}

public entry fun mint(treasury: &mut TreasuryCaoHolder, amount: u64, ctx: &mut TxContext){
let sender = tx_context::sender(ctx);
let treasury_cap = &mut treasury.treasury_cap;
let coin = coin::mint(treasury_cap, amount, ctx); // mint代币
transfer::public_transfer(coin, sender); // 转移至指定地址
}
}
Loading
Loading