Skip to content

Commit

Permalink
Merge pull request #2098 from Azhan1431/main
Browse files Browse the repository at this point in the history
完成task4,task5
  • Loading branch information
Sifotd authored Dec 4, 2024
2 parents 39211da + 94021e3 commit c6e2680
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 7 deletions.
43 changes: 43 additions & 0 deletions mover/Azhan1431/code/task04/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 = "DEE621E1BD6E0EADA5C7442F797159F16C2260B0E4B53557F27EA8B53A2E4332"
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
dependencies = [
{ id = "Sui", name = "Sui" },
{ id = "my_coin_faucet", name = "my_coin_faucet" },
]

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

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

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

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x385e8128003d4c29e6a37e0f1b210ae956ae4c40b571f320ef93329fe8986783"
latest-published-id = "0x385e8128003d4c29e6a37e0f1b210ae956ae4c40b571f320ef93329fe8986783"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/Azhan1431/code/task04/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "game_coin"
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" }
my_coin_faucet = { local = "../my_coin_faucet" }
# 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_coin = "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"

83 changes: 83 additions & 0 deletions mover/Azhan1431/code/task04/sources/game_coin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#[allow(unused_use, lint(public_random))]
module game_coin::game_coin {

use sui::balance::Balance;
use sui::coin;
use sui::coin::Coin;
use sui::random;
use sui::random::{RandomGenerator, Random, new_generator};
use sui::transfer::{share_object, public_transfer, transfer};
use my_coin_faucet::yunfaucet::YUNFAUCET;
use sui::balance;

public struct Game has key, store {
id: UID,
// 合约存钱都要用Balance
amt: Balance<YUNFAUCET>
}

public struct ADMIN has key {
id: UID,
}

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

//所有权,共享
share_object(game);

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

// 存款
public entry fun Deposit(
game: &mut Game,
in: &mut Coin<YUNFAUCET>,
amount: u64
) {
// 0x1 means insufficient balance
assert!(coin::value(in) >= amount, 0x1);
let in_balance = balance::split(coin::balance_mut(in), amount);
balance::join(&mut game.amt, in_balance);
}

// 取款
public entry fun Withdraw(
_: &mut ADMIN,
game: &mut Game,
amount: u64,
ctx: &mut TxContext
) {
// 0x2 means persion pool insufficient balance
assert!(game.amt.value() >= amount, 0x2);
let out_balance = coin::take(&mut game.amt, amount, ctx);
public_transfer(out_balance, ctx.sender());
}

// game logic
public entry fun game(game: &mut Game,coin: &mut Coin<YUNFAUCET>,amount: u64,guess: bool,rand: &Random,ctx: &mut TxContext){
//判断用户余额是否大于押注余额
assert!(coin::value(coin) >= amount,0x3);

let mut generator = new_generator(rand, ctx);
let result = random::generate_u8_in_range(&mut generator, 0, 1);

let flag: bool = result == 1;
//如果猜测正确
if (flag == guess) {
let game = coin::take(&mut game.amt, amount, ctx);
coin::join(coin, game);
}
else{
Deposit(game, coin, amount);
}
}
}
18 changes: 18 additions & 0 deletions mover/Azhan1431/code/task04/tests/game_coin_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module game_coin::game_coin_tests;
// uncomment this line to import the module
// use game_coin::game_coin;
const ENotImplemented: u64 = 0;
#[test]
fun test_game_coin() {
// pass
}
#[test, expected_failure(abort_code = ::game_coin::game_coin_tests::ENotImplemented)]
fun test_game_coin_fail() {
abort ENotImplemented
}
*/
52 changes: 52 additions & 0 deletions mover/Azhan1431/code/task05/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
manifest_digest = "BEFD61DE00ABEC80694606E83F5FE2EF4377365F46AA178ED6B87861EE519702"
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3"
dependencies = [
{ id = "Sui", name = "Sui" },
{ id = "my_coin", name = "my_coin" },
{ id = "my_coin_faucet", name = "my_coin_faucet" },
]

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

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

[[move.package]]
id = "my_coin_faucet"
source = { local = "../my_coin_faucet" }

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

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

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x753eeb4f9a7971ee49bf16ed4f94986fbc7b234e7264140dbc1bf971d157a2a0"
latest-published-id = "0x753eeb4f9a7971ee49bf16ed4f94986fbc7b234e7264140dbc1bf971d157a2a0"
published-version = "1"
38 changes: 38 additions & 0 deletions mover/Azhan1431/code/task05/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "swap"
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" }
my_coin = {local = "../my_coin"}
my_coin_faucet = {local = "../my_coin_faucet"}
# 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]
swap = "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"

71 changes: 71 additions & 0 deletions mover/Azhan1431/code/task05/sources/swap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#[allow(duplicate_alias)]
module swap::swap {

use my_coin::yun::YUN;
use my_coin_faucet::yunfaucet::YUNFAUCET;
use sui::balance::{Self, Balance};
use sui::coin::{Self, Coin};
use sui::tx_context;

const ValueSmall: u64 = 100;

// 定义一个结构体 Pool,用于存储两种代币的余额
public struct Pool has key, store {
id: UID,
coinA: Balance<YUN>,
coinB: Balance<YUNFAUCET>,
}

// 初始化池子
fun init(ctx: &mut TxContext) {
let pool = Pool {
id: object::new(ctx),
coinA: balance::zero(),
coinB: balance::zero(),
};
transfer::share_object(pool);
}

// 向池子中存储代币A
public entry fun DepositA(pool: &mut Pool, coinA: &mut Coin<YUN>, amount: u64) {
assert!(coin::value(coinA) >= amount, ValueSmall);
let split_balance = balance::split(coin::balance_mut(coinA), amount);
balance::join(&mut pool.coinA, split_balance);
}

// 向池子中存储代币B
public entry fun DepositB(pool: &mut Pool, coinB: &mut Coin<YUNFAUCET>, amount: u64) {
assert!(coin::value(coinB) >= amount, ValueSmall);
let split_balance = balance::split(coin::balance_mut(coinB), amount);
balance::join(&mut pool.coinB, split_balance);
}

// 在池子中将代币A交换为代币B
public entry fun swap_A_to_B(pool: &mut Pool, coinA: &mut Coin<YUN>, amount: u64, ctx: &mut TxContext) {
let coinA_store_value = balance::value(&pool.coinA);
let coinB_store_value = balance::value(&pool.coinB);

assert!(amount > 0 && coinB_store_value > 0 && coinA_store_value > 0, ValueSmall);
//恒定乘积市场制造商(AMM)的核心机制利用,交换的越多,得到的比例就越小
let coinB_swap_value = (amount * coinB_store_value) / (coinA_store_value + amount);
assert!(coinB_swap_value > 0 && coinB_swap_value < coinB_store_value, ValueSmall);
let split_balance = balance::split(coin::balance_mut(coinA), amount);
balance::join(&mut pool.coinA, split_balance);
let coin_b_out = coin::take(&mut pool.coinB, coinB_swap_value, ctx);
transfer::public_transfer(coin_b_out, tx_context::sender(ctx));
}

// 在池子中将代币B交换为代币A
public entry fun swap_B_to_A(pool: &mut Pool, coinB: &mut Coin<YUNFAUCET>, amount: u64, ctx: &mut TxContext) {
let coinA_store_value = balance::value(&pool.coinA);
let coinB_store_value = balance::value(&pool.coinB);

assert!(amount > 0 && coinB_store_value > 0 && coinA_store_value > 0, ValueSmall);
let coinA_swap_value = (amount * coinA_store_value) / (coinB_store_value + amount);
assert!(coinA_swap_value > 0 && coinA_swap_value < coinA_store_value, ValueSmall);
let split_balance = balance::split(coin::balance_mut(coinB), amount);
balance::join(&mut pool.coinB, split_balance);
let coin_a_out = coin::take(&mut pool.coinA, coinA_swap_value, ctx);
transfer::public_transfer(coin_a_out, tx_context::sender(ctx));
}
}
18 changes: 18 additions & 0 deletions mover/Azhan1431/code/task05/tests/swap_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module swap::swap_tests;
// uncomment this line to import the module
// use swap::swap;
const ENotImplemented: u64 = 0;
#[test]
fun test_swap() {
// pass
}
#[test, expected_failure(abort_code = ::swap::swap_tests::ENotImplemented)]
fun test_swap_fail() {
abort ENotImplemented
}
*/
14 changes: 7 additions & 7 deletions mover/Azhan1431/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
- [x] scan上的NFT截图:![Scan截图](./image/nft.png)

## 04 Move Game
- [] game package id :
- [] deposit Coin hash:
- [] withdraw `Coin` hash:
- [] play game hash:
- [x] game package id : 0x385e8128003d4c29e6a37e0f1b210ae956ae4c40b571f320ef93329fe8986783
- [x] deposit Coin hash: D7W52ugPihfxC64qfaHeEeHqJBgFXoH4UjfQQ4VFk8KG
- [x] withdraw `Coin` hash: 3ogWgAE83rFzTjb2PpEGi1c8AGiPxrXKp1dbVGUY4m7A
- [x] play game hash: CVMN1eFLFyikicRyQusbeignkkri6oAd7haHi5dtrbku

## 05 Move Swap
- [] swap package id :
- [] call swap CoinA-> CoinB hash :
- [] call swap CoinB-> CoinA hash :
- [x] swap package id : 0x753eeb4f9a7971ee49bf16ed4f94986fbc7b234e7264140dbc1bf971d157a2a0
- [x] call swap CoinA-> CoinB hash : DwZi6Rt39t2NhPnnCcbcrmoi9iJoa8UyoDX1Ry9Po5rQ
- [x] call swap CoinB-> CoinA hash : 9ripiwMGxid2oUtdvQ1dbMGvX1TKRdrocEYMBfskGikY

## 06 Dapp-kit SDK PTB
- [] save hash :
Expand Down

0 comments on commit c6e2680

Please sign in to comment.