From aa53051323774d9f339c8b20891c8cc1cc2be95f Mon Sep 17 00:00:00 2001 From: zhangmiancan Date: Tue, 7 Jan 2025 16:42:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20task=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mover/chinazmc/code/task5/swap/.gitignore | 1 + mover/chinazmc/code/task5/swap/Move.lock | 52 +++++++ mover/chinazmc/code/task5/swap/Move.toml | 38 +++++ mover/chinazmc/code/task5/swap/readme.md | 8 + .../code/task5/swap/sources/swap.move | 139 ++++++++++++++++++ .../code/task5/swap/tests/swap_tests.move | 18 +++ 6 files changed, 256 insertions(+) create mode 100644 mover/chinazmc/code/task5/swap/.gitignore create mode 100644 mover/chinazmc/code/task5/swap/Move.lock create mode 100644 mover/chinazmc/code/task5/swap/Move.toml create mode 100644 mover/chinazmc/code/task5/swap/readme.md create mode 100644 mover/chinazmc/code/task5/swap/sources/swap.move create mode 100644 mover/chinazmc/code/task5/swap/tests/swap_tests.move diff --git a/mover/chinazmc/code/task5/swap/.gitignore b/mover/chinazmc/code/task5/swap/.gitignore new file mode 100644 index 000000000..a007feab0 --- /dev/null +++ b/mover/chinazmc/code/task5/swap/.gitignore @@ -0,0 +1 @@ +build/* diff --git a/mover/chinazmc/code/task5/swap/Move.lock b/mover/chinazmc/code/task5/swap/Move.lock new file mode 100644 index 000000000..d11e4ac8c --- /dev/null +++ b/mover/chinazmc/code/task5/swap/Move.lock @@ -0,0 +1,52 @@ +# @generated by Move, please check-in and do not edit manually. + +[move] +version = 3 +manifest_digest = "D9330D6D66A2A2928A52F42E6CCFE947C71570DA19896E0C154B21570F8F3CEA" +deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3" +dependencies = [ + { id = "Sui", name = "Sui" }, + { id = "faucet_coin", name = "faucet_coin" }, + { id = "mycoin", name = "mycoin" }, +] + +[[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 = "faucet_coin" +source = { local = "..\\..\\task2\\faucet_coin" } + +dependencies = [ + { id = "Sui", name = "Sui" }, +] + +[[move.package]] +id = "mycoin" +source = { local = "..\\..\\task2\\mycoin" } + +dependencies = [ + { id = "Sui", name = "Sui" }, +] + +[move.toolchain-version] +compiler-version = "1.39.3" +edition = "2024.beta" +flavor = "sui" + +[env] + +[env.mainnet] +chain-id = "35834a8a" +original-published-id = "0xa6c85467bbeccb866123ff5ff524889fd70e13463ab2f155691d6163fb13a228" +latest-published-id = "0xa6c85467bbeccb866123ff5ff524889fd70e13463ab2f155691d6163fb13a228" +published-version = "1" diff --git a/mover/chinazmc/code/task5/swap/Move.toml b/mover/chinazmc/code/task5/swap/Move.toml new file mode 100644 index 000000000..5da60c11e --- /dev/null +++ b/mover/chinazmc/code/task5/swap/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" } +faucet_coin = { local = "../../task2/faucet_coin" } +mycoin = { local = "../../task2/mycoin" } +# 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/chinazmc/code/task5/swap/readme.md b/mover/chinazmc/code/task5/swap/readme.md new file mode 100644 index 000000000..8e6da0004 --- /dev/null +++ b/mover/chinazmc/code/task5/swap/readme.md @@ -0,0 +1,8 @@ +Transaction Digest: AJoqdHFTcJkQuyFom8RNCGRjckVu5PZJR6ogoVVRWqgo +PackageID: 0xa6c85467bbeccb866123ff5ff524889fd70e13463ab2f155691d6163fb13a228 + +pool object id : 0xd7de7087e486b41d9ad7cc7092950ec6eef4d58ee22ceb1f43785aa13914841f + + +add_liquidity Transaction Digest: GzQrbBmNes4fxUEMcECd9wodDg3tnrHiHeALTn4T2KEZ +swap_a_to_b Transaction Digest: ZKq53uXr39c8TQ13PNzSFAMbNocZMFP4hbtfciAFy4m \ No newline at end of file diff --git a/mover/chinazmc/code/task5/swap/sources/swap.move b/mover/chinazmc/code/task5/swap/sources/swap.move new file mode 100644 index 000000000..f040679d5 --- /dev/null +++ b/mover/chinazmc/code/task5/swap/sources/swap.move @@ -0,0 +1,139 @@ +/* +/// Module: swap +module swap::swap; +*/ + +// For Move coding conventions, see +// https://docs.sui.io/concepts/sui-move-concepts/conventions + + +module swap::swap { + use sui::coin::{Self, Coin}; + use sui::balance::{Self, Balance}; + use sui::event; + use std::string::{Self, String}; + + // 导入自定义的代币模块 + use mycoin::mycoin::MYCOIN; + use faucet_coin::faucet_coin::FAUCET_COIN; + const E_INSUFFICIENT_LIQUIDITY: u64 = 0; + const E_ZERO_AMOUNT: u64 = 1; + + //流动性池结构体 + public struct Pool has key { + id: UID, + coin_a: Balance, //代币A的余额 + coin_b: Balance //代币B的余额 + } + //添加流动性事件结构体 + public struct LiquidityEvent has copy,drop { + provider:address,//提供流动性的地址 + coin_a_amount:u64,//代币A的数量 + coin_b_amount:u64,//代币B的数量 + coin_a_type:String, + coin_b_type:String, + timestamp:u64,//时间戳 + } + //交换事件结构体 + public struct SwapEvent has copy,drop { + sender:address,//发起交换的地址 + coin_in_amount:u64,//输入代币的数量 + coin_out_amount:u64,//输出代币的数量 + coin_in_type:String,//输入代币的类型 + coin_out_type:String,//输出代币的类型 + timestamp:u64,//时间戳 + } + fun init(ctx: &mut TxContext) { + let pool = Pool{ + id: object::new(ctx), + coin_a:balance::zero(), + coin_b:balance::zero(), + }; + transfer::share_object(pool); + } + //添加流动性函数 + public entry fun add_liquidity( + pool: &mut Pool, + coin_a: Coin, + coin_b: Coin, + ctx: &mut TxContext + ){ + let a_amount = coin::value(&coin_a);//获取代币A的数量 + let b_amount = coin::value(&coin_b);//获取代币B的数量 + assert!(a_amount > 0 && b_amount > 0,E_ZERO_AMOUNT); + balance::join(&mut pool.coin_a,coin::into_balance(coin_a)); + balance::join(&mut pool.coin_b,coin::into_balance(coin_b)); + //记录添加流动性的事件 + event::emit(LiquidityEvent{ + provider: ctx.sender(), + coin_a_amount:a_amount, + coin_b_amount:b_amount, + coin_a_type:string::utf8(b"mycoin"), + coin_b_type:string::utf8(b"faucet_coin"), + timestamp: tx_context::epoch(ctx), + }); + } + //代币A换代币B的函数 + public entry fun swap_a_to_b( + pool: &mut Pool, + coin_a_in: Coin, + ctx: &mut TxContext + ) { + let a_amount = coin::value(&coin_a_in); // 获取输入的代币A的数量 + assert!(a_amount > 0, E_ZERO_AMOUNT); // 确保输入的代币数量大于零 + + let b_reserve = balance::value(&pool.coin_b); // 获取代币B的储备量 + assert!(b_reserve > 0, E_INSUFFICIENT_LIQUIDITY); // 确保代币B的储备量大于零 + + let a_reserve = balance::value(&pool.coin_a); // 获取代币A的储备量 + let b_out = (a_amount * b_reserve) / (a_reserve + a_amount); // 计算可以换取的代币B的数量 + assert!(b_out > 0 && b_out <= b_reserve, E_INSUFFICIENT_LIQUIDITY); // 确保可以换取的代币B数量有效 + + // 更新流动性池的余额 + balance::join(&mut pool.coin_a, coin::into_balance(coin_a_in)); // 将输入的代币A加入流动性池 + let coin_b_out = coin::take(&mut pool.coin_b, b_out, ctx); // 从流动性池中取出代币B + transfer::public_transfer(coin_b_out, tx_context::sender(ctx)); // 将代币B转给发起者 + + // 记录交换事件 + event::emit(SwapEvent { + sender: tx_context::sender(ctx), // 发起交换的地址 + coin_in_amount: a_amount, // 输入的代币A的数量 + coin_out_amount: b_out, // 输出的代币B的数量 + coin_in_type: string::utf8(b"MYCOIN"), // 输入的代币A的类型 + coin_out_type: string::utf8(b"faucet_coin"), // 输出的代币B的类型 + timestamp: tx_context::epoch(ctx), // 当前时间戳 + }); + } + //代币B换代币A的函数 + public entry fun swap_b_to_a( + pool: &mut Pool, + coin_b_in: Coin, + ctx: &mut TxContext + ) { + let b_amount = coin::value(&coin_b_in); // 获取输入的代币B的数量 + assert!(b_amount > 0, E_ZERO_AMOUNT); // 确保输入的代币数量大于零 + + let a_reserve = balance::value(&pool.coin_a); // 获取代币A的储备量 + assert!(a_reserve > 0, E_INSUFFICIENT_LIQUIDITY); // 确保代币A的储备量大于零 + + let b_reserve = balance::value(&pool.coin_b); // 获取代币B的储备量 + let a_out = (b_amount * a_reserve) / (b_reserve + b_amount); // 计算可以换取的代币A的数量 + assert!(a_out > 0 && a_out <= b_reserve, E_INSUFFICIENT_LIQUIDITY); // 确保可以换取的代币A数量有效 + + // 更新流动性池的余额 + balance::join(&mut pool.coin_b, coin::into_balance(coin_b_in)); // 将输入的代币B加入流动性池 + let coin_a_out = coin::take(&mut pool.coin_a, a_out, ctx); // 从流动性池中取出代币A + transfer::public_transfer(coin_a_out, tx_context::sender(ctx)); // 将代币A转给发起者 + + // 记录交换事件 + event::emit(SwapEvent { + sender: tx_context::sender(ctx), // 发起交换的地址 + coin_in_amount: b_amount, // 输入的代币的数量 + coin_out_amount: a_out, // 输出的代币的数量 + coin_in_type: string::utf8(b"FAUCET_COIN"), // 输入的代币的类型 + coin_out_type: string::utf8(b"MYCOIN"), // 输出的代币的类型 + timestamp: tx_context::epoch(ctx), // 当前时间戳 + }); + } + +} \ No newline at end of file diff --git a/mover/chinazmc/code/task5/swap/tests/swap_tests.move b/mover/chinazmc/code/task5/swap/tests/swap_tests.move new file mode 100644 index 000000000..81f52899c --- /dev/null +++ b/mover/chinazmc/code/task5/swap/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 +} +*/