Skip to content

Commit

Permalink
task05
Browse files Browse the repository at this point in the history
完成task05
  • Loading branch information
Azhan1431 committed Dec 3, 2024
1 parent 912302a commit 94021e3
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 3 deletions.
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
}
*/
6 changes: 3 additions & 3 deletions mover/Azhan1431/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
- [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 94021e3

Please sign in to comment.