From 94021e3fe37de359bbfaef323fc748687264d9ce Mon Sep 17 00:00:00 2001 From: Azhan1431 <870400783@qq.com> Date: Tue, 3 Dec 2024 23:55:26 +0800 Subject: [PATCH] task05 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完成task05 --- mover/Azhan1431/code/task05/Move.lock | 52 ++++++++++++++ mover/Azhan1431/code/task05/Move.toml | 38 ++++++++++ mover/Azhan1431/code/task05/sources/swap.move | 71 +++++++++++++++++++ .../code/task05/tests/swap_tests.move | 18 +++++ mover/Azhan1431/readme.md | 6 +- 5 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 mover/Azhan1431/code/task05/Move.lock create mode 100644 mover/Azhan1431/code/task05/Move.toml create mode 100644 mover/Azhan1431/code/task05/sources/swap.move create mode 100644 mover/Azhan1431/code/task05/tests/swap_tests.move diff --git a/mover/Azhan1431/code/task05/Move.lock b/mover/Azhan1431/code/task05/Move.lock new file mode 100644 index 000000000..578ed019d --- /dev/null +++ b/mover/Azhan1431/code/task05/Move.lock @@ -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" diff --git a/mover/Azhan1431/code/task05/Move.toml b/mover/Azhan1431/code/task05/Move.toml new file mode 100644 index 000000000..072363dc6 --- /dev/null +++ b/mover/Azhan1431/code/task05/Move.toml @@ -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 (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" } +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" + diff --git a/mover/Azhan1431/code/task05/sources/swap.move b/mover/Azhan1431/code/task05/sources/swap.move new file mode 100644 index 000000000..080de1082 --- /dev/null +++ b/mover/Azhan1431/code/task05/sources/swap.move @@ -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, + coinB: Balance, + } + + // 初始化池子 + 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, 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, 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, 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, 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)); + } +} \ No newline at end of file diff --git a/mover/Azhan1431/code/task05/tests/swap_tests.move b/mover/Azhan1431/code/task05/tests/swap_tests.move new file mode 100644 index 000000000..81f52899c --- /dev/null +++ b/mover/Azhan1431/code/task05/tests/swap_tests.move @@ -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 +} +*/ diff --git a/mover/Azhan1431/readme.md b/mover/Azhan1431/readme.md index e2a3674c0..f2555abfc 100644 --- a/mover/Azhan1431/readme.md +++ b/mover/Azhan1431/readme.md @@ -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 :