-
Notifications
You must be signed in to change notification settings - Fork 905
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
Showing
5 changed files
with
285 additions
and
0 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 = "381785A9309D0F5B5963238F71D082304B7837A6A7BC1F8FE2873781B9015B6B" | ||
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3" | ||
dependencies = [ | ||
{ id = "M2887_coin", name = "M2887_coin" }, | ||
{ id = "M2887_faucet_coin", name = "M2887_faucet_coin" }, | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "M2887_coin" | ||
source = { local = "..\\..\\task2\\M2887_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "M2887_faucet_coin" | ||
source = { local = "..\\..\\task2\\M2887_faucet_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "MoveStdlib" | ||
source = { git = "https://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ id = "MoveStdlib", name = "MoveStdlib" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.38.3" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x1789eb26b84f8b1c645888a738f3a869bfccee0b62ef227a8d5a3e7c05cc7be3" | ||
latest-published-id = "0x1789eb26b84f8b1c645888a738f3a869bfccee0b62ef227a8d5a3e7c05cc7be3" | ||
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 = "m2887_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://gitee.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } | ||
M2887_faucet_coin = { local = "../../task2/M2887_faucet_coin" } | ||
M2887_coin = { local = "../../task2/M2887_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] | ||
m2887_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" | ||
|
164 changes: 164 additions & 0 deletions
164
mover/M2887/code/task5/move_swap/sources/move_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,164 @@ | ||
/* | ||
/// Module: m2887_swap | ||
module m2887_swap::m2887_swap; | ||
*/ | ||
|
||
module m2887_swap::m2887_swap; | ||
|
||
use m2887_faucet_coin::m2887_faucet_coin::M2887_FAUCET_COIN; | ||
use m2887_coin::m2887_coin::M2887_COIN; | ||
use sui::balance::{Self, Balance}; | ||
use sui::coin::{Self, Coin}; | ||
|
||
const EInputNotEnough: u64 = 0; | ||
const EPoolNotEnough: u64 = 1; | ||
|
||
public struct AdminCap has key { id: UID } | ||
|
||
public struct Pool has key { | ||
// 如果需要,可以加上两种代币分别的存量属性,这里先不添加 | ||
id: UID, | ||
faucet_coin: Balance<M2887_FAUCET_COIN>, | ||
//faucet_coin_balance: u64, | ||
my_coin: Balance<M2887_COIN>, | ||
//faucet_coin_balance: u64, | ||
} | ||
|
||
fun init(ctx: &mut TxContext) { | ||
let pool = Pool { | ||
id: object::new(ctx), | ||
faucet_coin: balance::zero<M2887_FAUCET_COIN>(), | ||
my_coin: balance::zero<M2887_COIN>(), | ||
}; | ||
transfer::share_object(pool); // pool需要公开,通过AdminCap给予自己额外的管理员权限 | ||
transfer::transfer(AdminCap { id: object::new(ctx) }, tx_context::sender(ctx)); | ||
} | ||
|
||
public entry fun deposit_my_coin( | ||
pool: &mut Pool, | ||
input: Coin<M2887_COIN>, | ||
amount: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
let caller = tx_context::sender(ctx); | ||
let input_value = coin::value(&input); | ||
assert!(input_value >= amount, EInputNotEnough); | ||
let mut input_balance = coin::into_balance(input); | ||
if (input_value > amount) { | ||
balance::join( | ||
&mut pool.my_coin, | ||
balance::split(&mut input_balance, amount), | ||
); | ||
let change = coin::from_balance(input_balance, ctx); | ||
transfer::public_transfer(change, caller); | ||
} else { | ||
balance::join(&mut pool.my_coin, input_balance); | ||
}; | ||
} | ||
|
||
public entry fun deposit__faucet_coin( | ||
pool: &mut Pool, | ||
input: Coin<M2887_FAUCET_COIN>, | ||
amount: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
let caller = tx_context::sender(ctx); | ||
let input_value = coin::value(&input); | ||
assert!(input_value >= amount, EInputNotEnough); | ||
let mut input_balance = coin::into_balance(input); | ||
if (input_value > amount) { | ||
balance::join( | ||
&mut pool.faucet_coin, | ||
balance::split(&mut input_balance, amount), | ||
); | ||
let change = coin::from_balance(input_balance, ctx); | ||
transfer::public_transfer(change, caller); | ||
} else { | ||
balance::join(&mut pool.faucet_coin, input_balance); | ||
}; | ||
} | ||
|
||
// 我们可以编写一个withdraw函数,并限制只有拥有管理员权限才能提取资金 | ||
public entry fun withdraw_zzf222_coin( | ||
_: &AdminCap, | ||
pool: &mut Pool, | ||
amount: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
let my_coin_balance = balance::split(&mut pool.my_coin, amount); | ||
let my_coin = coin::from_balance(my_coin_balance, ctx); | ||
transfer::public_transfer(my_coin, tx_context::sender(ctx)); | ||
} | ||
|
||
public entry fun withdraw_zzf222_faucet_coin( | ||
_: &AdminCap, | ||
pool: &mut Pool, | ||
amount: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
let faucet_coin_balance = balance::split(&mut pool.faucet_coin, amount); | ||
let faucet_coin = coin::from_balance(faucet_coin_balance, ctx); | ||
transfer::public_transfer(faucet_coin, tx_context::sender(ctx)); | ||
} | ||
|
||
// 在swap函数中,我们可以沿用部分上一节guess_game的代码 | ||
public entry fun swap_faucet_coin_to_my_coin( | ||
pool: &mut Pool, | ||
input: Coin<M2887_FAUCET_COIN>, | ||
amount: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
|
||
|
||
let caller = tx_context::sender(ctx); | ||
// get the input value and assert | ||
let input_value = coin::value(&input); | ||
let output_value = amount * 1000 / 2000; // amount千万不要写成input_value! | ||
assert!(input_value >= amount, EInputNotEnough); | ||
// transection the input value to Balance | ||
let mut input_balance = coin::into_balance(input); | ||
assert!(balance::value(&pool.my_coin) >= output_value, EPoolNotEnough); | ||
// if input value much than amount, change the excess | ||
if (input_value > amount) { | ||
balance::join( | ||
// join 函数用于接收代币 | ||
&mut pool.faucet_coin, | ||
balance::split(&mut input_balance, amount), | ||
); | ||
let change = coin::from_balance(input_balance, ctx); | ||
transfer::public_transfer(change, caller); | ||
} else { | ||
balance::join(&mut pool.faucet_coin, input_balance); | ||
}; | ||
let output_balance = balance::split(&mut pool.my_coin, output_value); | ||
let output = coin::from_balance(output_balance, ctx); | ||
transfer::public_transfer(output, caller); | ||
} | ||
|
||
public entry fun swap_my_coin_to_faucet_coin( | ||
pool: &mut Pool, | ||
input: Coin<M2887_COIN>, | ||
amount: u64, | ||
ctx: &mut TxContext, | ||
) { | ||
let caller = tx_context::sender(ctx); | ||
let input_value = coin::value(&input); | ||
let output_value = amount * 2000 / 1000; | ||
assert!(input_value >= amount, EInputNotEnough); | ||
let mut input_balance = coin::into_balance(input); | ||
assert!(balance::value(&pool.faucet_coin) >= output_value, EPoolNotEnough); | ||
if (input_value > amount) { | ||
balance::join( | ||
&mut pool.my_coin, | ||
balance::split(&mut input_balance, amount), | ||
); | ||
let change = coin::from_balance(input_balance, ctx); | ||
transfer::public_transfer(change, caller); | ||
} else { | ||
balance::join(&mut pool.my_coin, input_balance); | ||
}; | ||
let output_balance = balance::split(&mut pool.faucet_coin, output_value); | ||
let output = coin::from_balance(output_balance, ctx); | ||
transfer::public_transfer(output, caller); | ||
} | ||
|
18 changes: 18 additions & 0 deletions
18
mover/M2887/code/task5/move_swap/tests/move_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 move_swap::move_swap_tests; | ||
// uncomment this line to import the module | ||
// use move_swap::move_swap; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_move_swap() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::move_swap::move_swap_tests::ENotImplemented)] | ||
fun test_move_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