Skip to content

Commit

Permalink
task5 code
Browse files Browse the repository at this point in the history
  • Loading branch information
M2887 committed Dec 12, 2024
1 parent 61d1a4d commit 571a715
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 0 deletions.
52 changes: 52 additions & 0 deletions mover/M2887/code/task5/move_swap/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 = "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"
39 changes: 39 additions & 0 deletions mover/M2887/code/task5/move_swap/Move.toml
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 mover/M2887/code/task5/move_swap/sources/move_swap.move
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 mover/M2887/code/task5/move_swap/tests/move_swap_tests.move
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
}
*/
12 changes: 12 additions & 0 deletions mover/M2887/notes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,16 @@
sui client call --package 0x7e1792a1a41a30206da3198784b9285dfa1e66bf227d834eb5d2ac9495554a64 --module flip_coin --function play --args 0x62118895a58dbb94b3e27acb3b5d64e11bf95ba5a2f6edcc8df718a2cc3e60de true 0x05e1ef276631b526d3fd3429e8e900aa3bf758656557452167bf683e5175492b 0x8 --gas-budget 1000000

sui client call --package 0x7e1792a1a41a30206da3198784b9285dfa1e66bf227d834eb5d2ac9495554a64 --module flip_coin --function remove_sui --args 0x5a800a73fc12ab69963c4c2f8175ac99e89576c95c64b969ef8de56c1bc5674d 0x62118895a58dbb94b3e27acb3b5d64e11bf95ba5a2f6edcc8df718a2cc3e60de 100 --gas-budget 10000000
```

##### task5 bash
```bash
coin mint
packageID:0x5a3a556c1b4cb0d9e20595fc0819bef617de4a6b16c258e58f49e7d1c27d4cf9
TreasuryCap:0x2b50049a4b87eab90cd48c5d04d261fa179c5ea1026bb7fb2b74506b6b05d5d2

fauect_coin mint
packageID:0x8526f8f3d7e23ca6e2c68c67855357745f03a9097d93a3dd3274a44290fc3faa
TreasuryCap:0xff5d5b9c397db5e2c9fd8655140473ddc87f67970b7f460bf2c05a80c1a4c4e5

```

0 comments on commit 571a715

Please sign in to comment.