Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_task5 #2005

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions mover/z0yuan/code/task5/z0yuan/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 = "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"
39 changes: 39 additions & 0 deletions mover/z0yuan/code/task5/z0yuan/Move.toml
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"

81 changes: 81 additions & 0 deletions mover/z0yuan/code/task5/z0yuan/sources/my_swap.move
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));
}
}
18 changes: 18 additions & 0 deletions mover/z0yuan/code/task5/z0yuan/tests/z0yuan_tests.move
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
}
*/
6 changes: 3 additions & 3 deletions mover/z0yuan/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
- [x] play game hash:GtKW22GhaheeArUcYEY7bQVBYrSJRqb7LWUmqTFq1qPB

## 05 Move Swap
- [] swap package id :
- [] call swap CoinA-> CoinB hash :
- [] call swap CoinB-> CoinA hash :
- [x] swap package id :0x876bde98af774c6e5df5a0e0f484b8a2c9e3abadf596581ebce0fc62eeda2e85
- [x] call swap CoinA-> CoinB hash :BmcdN6yKbgUYLiJQEg2sXKv4CYxMoccfFRbtwFGkZCpw
- [x] call swap CoinB-> CoinA hash :DsDNLY7q1q8GnPpWVDaCVGyQH7WCGzxE3M3RLnZs7ApX

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