-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0742b4
commit 9df37d8
Showing
5 changed files
with
239 additions
and
8 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,58 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "30FA6C9D98F6B608CA698A5643BAFA93A1FDAB773E878CD03639FABADC4B98C5" | ||
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3" | ||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
{ id = "hwen_coin", name = "hwen_coin" }, | ||
{ id = "hwen_faucet_coin", name = "hwen_faucet_coin" }, | ||
] | ||
|
||
[[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 = "hwen_coin" | ||
source = { local = "..\\..\\task2\\hwen_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "hwen_faucet_coin" | ||
source = { local = "..\\..\\task2\\hwen_faucet_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.36.2" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.testnet] | ||
chain-id = "4c78adac" | ||
original-published-id = "0xcde791168671576e4ad49da511f31aca896aa87540ee6020fad114fa1c00744e" | ||
latest-published-id = "0xcde791168671576e4ad49da511f31aca896aa87540ee6020fad114fa1c00744e" | ||
published-version = "1" | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x3fb5d5b05557c3fe832c720dec96c6f6f622879fa0586b5c8b5eb8252e3f2dbc" | ||
latest-published-id = "0x3fb5d5b05557c3fe832c720dec96c6f6f622879fa0586b5c8b5eb8252e3f2dbc" | ||
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,42 @@ | ||
[package] | ||
name = "hwen_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" } | ||
hwen_coin = { local = "../../task2/hwen_coin" } | ||
hwen_faucet_coin = { local = "../../task2/hwen_faucet_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] | ||
hwen_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" |
113 changes: 113 additions & 0 deletions
113
mover/hwen227/code/task5/hwen_swap/sources/hwen_swap.move
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,113 @@ | ||
/// Module: hwen_swap | ||
module hwen_swap::hwen_swap{ | ||
use hwen_faucet_coin::hwen_faucet_coin::HWEN_FAUCET_COIN; | ||
use hwen_coin::hwen_coin::HWEN_COIN; | ||
use sui::coin::{Self, Coin}; | ||
use sui::transfer::{transfer, share_object, public_transfer}; | ||
use sui::balance::{Self, Balance}; | ||
|
||
const EXCHANGE_RATE: u64 = 2 ; | ||
|
||
public struct AdminCap has key { | ||
id: UID | ||
} | ||
|
||
public struct MySwap has key { | ||
id: UID, | ||
hwen_coin:Balance<HWEN_COIN>, | ||
|
||
faucet_coin:Balance<HWEN_FAUCET_COIN> | ||
} | ||
|
||
/// 初始化函数,创建并分享一个新的 MySwap 对象和 AdminCap 对象 | ||
fun init(ctx:&mut TxContext){ | ||
let admin_cap = AdminCap{id: object::new(ctx)}; | ||
let swap = MySwap{ | ||
id: object::new(ctx), | ||
hwen_coin: balance::zero<HWEN_COIN>(), | ||
faucet_coin: balance::zero<HWEN_FAUCET_COIN>() | ||
}; | ||
|
||
share_object(swap); | ||
transfer(admin_cap, ctx.sender()); | ||
} | ||
|
||
/// 存入 HWEN_COIN 到 MySwap 中 | ||
entry fun deposit_hwen_coin( | ||
swap: &mut MySwap, | ||
hwen: Coin<HWEN_COIN>) | ||
{ | ||
let hwen_balance = coin::into_balance(hwen); | ||
swap.hwen_coin.join(hwen_balance); | ||
//balance::join(&mut swap.hwen_coin, hwen_balance); | ||
} | ||
|
||
/// 存入 HWEN_FAUCET_COIN 到 MySwap 中 | ||
public entry fun deposit_faucet_coin( | ||
swap: &mut MySwap, | ||
faucet: Coin<HWEN_FAUCET_COIN>) | ||
{ | ||
let faucet_balance = coin::into_balance(faucet); | ||
balance::join(&mut swap.faucet_coin, faucet_balance); | ||
} | ||
|
||
/// 从 MySwap 中提取指定数量的 HWEN_COIN | ||
public entry fun withdraw_hwen_coin( | ||
_unused: &AdminCap, | ||
swap: &mut MySwap, | ||
amount: u64, | ||
ctx : &mut TxContext) | ||
{ | ||
let hwen_balance = balance::split(&mut swap.hwen_coin, amount); | ||
let hwen_coin = coin::from_balance(hwen_balance, ctx); | ||
public_transfer(hwen_coin, ctx.sender()) | ||
} | ||
|
||
/// 从 MySwap 中提取指定数量的 HWEN_FAUCET_COIN | ||
entry fun withdraw_faucet_coin( | ||
_unused: &AdminCap, | ||
swap: &mut MySwap, | ||
amount: u64, | ||
ctx : &mut TxContext) | ||
{ | ||
let faucet_balance = balance::split(&mut swap.faucet_coin, amount); | ||
let faucet_coin = coin::from_balance(faucet_balance, ctx); | ||
public_transfer(faucet_coin, ctx.sender()) | ||
} | ||
|
||
/// 将 HWEN_COIN 交换为 HWEN_FAUCET_COIN | ||
entry fun swap_hwen_to_faucet( | ||
swap: &mut MySwap, | ||
input: Coin<HWEN_COIN>, | ||
ctx : &mut TxContext | ||
){ | ||
|
||
let hwen_amount = coin::value(&input); | ||
let hwen_balance = coin::into_balance(input); | ||
balance::join(&mut swap.hwen_coin, hwen_balance); | ||
|
||
let faucet_amount = hwen_amount * EXCHANGE_RATE; | ||
|
||
let faucet_balance = balance::split(&mut swap.faucet_coin,faucet_amount); | ||
let faucet_coin = coin::from_balance(faucet_balance,ctx); | ||
public_transfer(faucet_coin,ctx.sender()) | ||
} | ||
|
||
/// 将 HWEN_FAUCET_COIN 交换为 HWEN_COIN | ||
entry fun swap_faucet_to_hwen( | ||
swap: &mut MySwap, | ||
input: Coin<HWEN_FAUCET_COIN>, | ||
ctx : &mut TxContext) | ||
{ | ||
let faucet_amount = coin::value(&input); | ||
let faucet_balance = coin::into_balance(input); | ||
balance::join(&mut swap.faucet_coin, faucet_balance,); | ||
|
||
let hwen_amount = faucet_amount / EXCHANGE_RATE; | ||
|
||
let hwen_balance = balance::split(&mut swap.hwen_coin, hwen_amount); | ||
let hwen_coin = coin::from_balance(hwen_balance, ctx); | ||
public_transfer(hwen_coin, ctx.sender()) | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
mover/hwen227/code/task5/hwen_swap/tests/hwen_swap_tests.move
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 hwen_swap::hwen_swap_tests; | ||
// uncomment this line to import the module | ||
// use hwen_swap::hwen_swap; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_hwen_swap() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::hwen_swap::hwen_swap_tests::ENotImplemented)] | ||
fun test_hwen_swap_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