-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2005 from z0yuan/z0yuan
update_task5
- Loading branch information
Showing
5 changed files
with
193 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "413988F34385F497DF4F294D04908DB515D4FE3C98A705EB5EB196DDC6549706" | ||
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3" | ||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
{ id = "coin", name = "coin" }, | ||
{ id = "faucet", name = "faucet" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "MoveStdlib" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/mainnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ id = "MoveStdlib", name = "MoveStdlib" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "coin" | ||
source = { local = "../coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "faucet" | ||
source = { local = "../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 = "0x876bde98af774c6e5df5a0e0f484b8a2c9e3abadf596581ebce0fc62eeda2e85" | ||
latest-published-id = "0x876bde98af774c6e5df5a0e0f484b8a2c9e3abadf596581ebce0fc62eeda2e85" | ||
published-version = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
[package] | ||
name = "z0yuan" | ||
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/mainnet" } | ||
faucet = { local = "../faucet" } | ||
coin = { local = "../coin" } | ||
|
||
# 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] | ||
z0yuan = "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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#[allow(unused_use, duplicate_alias)] | ||
module z0yuan::my_swap{ | ||
use sui::coin::{Self, Coin, TreasuryCap}; | ||
use sui::balance::{Self, Balance}; | ||
use sui::transfer; | ||
use sui::tx_context::{Self, TxContext}; | ||
|
||
use coin::my_coin::{MY_COIN}; | ||
use faucet::mycoin::{MYCOIN}; | ||
|
||
const ValueSmall: u64 = 100; | ||
|
||
public struct Pool has key,store { | ||
id: UID, | ||
coinA: Balance<MY_COIN>, | ||
coinB: Balance<MYCOIN>, | ||
} | ||
//初始化函数 | ||
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<MY_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<MYCOIN>,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<MY_COIN>,amount: u64,ctx: &mut TxContext){ | ||
//池子中储存的的A和B代币的量 | ||
let coinB_store_value = balance::value(&pool.coinB); | ||
let coinA_store_value = balance::value(&pool.coinA); | ||
|
||
assert!(amount > 0 && coinB_store_value > 0 && coinA_store_value > 0, ValueSmall); | ||
|
||
//计算A能转换多少B | ||
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<MYCOIN>,amount: u64,ctx: &mut TxContext){ | ||
//池子中储存的的A和B代币的量 | ||
let coinB_store_value = balance::value(&pool.coinB); | ||
let coinA_store_value = balance::value(&pool.coinA); | ||
|
||
assert!(amount > 0 && coinB_store_value > 0 && coinA_store_value > 0, ValueSmall); | ||
|
||
//计算B能转换多少A | ||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
#[test_only] | ||
module z0yuan::z0yuan_tests; | ||
// uncomment this line to import the module | ||
// use z0yuan::z0yuan; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_z0yuan() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::z0yuan::z0yuan_tests::ENotImplemented)] | ||
fun test_z0yuan_fail() { | ||
abort ENotImplemented | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters