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

task5 #2132

Merged
merged 1 commit into from
Dec 12, 2024
Merged

task5 #2132

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
54 changes: 54 additions & 0 deletions mover/Helen2022a/code/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# ################### task5 ###################

Transaction Digest: C3GTuHvewEZBdZg6k7DztgosRPYS4Th9rMVjudHuuaqy
swap package id : 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035

# 创建bank
Transaction Digest: 2KsUVg3T1aRSxUHXGYdS41KHY5r3UtZ36guMNWGauDoh

sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function createBank --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"

# mint my coin 100
sui client call --package 0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e --module my_coin --function mint_and_send --args 0x2e8acd5b90955411516907a58fe2f16317230449829bc71ac99d3c35215aa578 10000000000 0x221ce69b6e0be6bb3d38f5fb4ce8eb5ee78a3ff8ae58b2be0f2263812c244ad8

Transaction Digest: 5yqA9j3uB4SA5uLAQT7ALFZ49wzhcSyyhUJQUXJWkPWJ
my coin object id: 0xd4d744d11a5776524188adb3d467b911c541f63d31202e50d59a2c0b5a8bf78d

# mint faucet coin 100
sui client call --package 0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b --module faucet_coin --function mint_and_send --args 0xe6c8a1222e5143b21a4e762d5441d575a27471efde1c320628c139991748c6d4 10000000000 0x221ce69b6e0be6bb3d38f5fb4ce8eb5ee78a3ff8ae58b2be0f2263812c244ad8

Transaction Digest: 9YW1k97CGNqiCJWc5cbtCZbKfsvRspDJo5Xd9VGoMwJi
faucet object id: 0xd93fe86c9929cac14127470bebb875a70fced2ae81fdd2ab7c1b38aa6f560cf3

# 存银行
Transaction Digest: CpRCYQoStpwKVX372bPV171rCaqaBybaRdG66V6vzPTX
sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function deposit_coin_A --args 0x804542cd5dce0abeceaff84e26896a661df365763ac60d4780c5c9c411635084 0xd4d744d11a5776524188adb3d467b911c541f63d31202e50d59a2c0b5a8bf78d --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"

Transaction Digest: BvEp76zQfC1w7MeR4ob5wEsDbPmqeX3v85vgLepBmgLm
sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function deposit_coin_B --args 0x804542cd5dce0abeceaff84e26896a661df365763ac60d4780c5c9c411635084 0xd93fe86c9929cac14127470bebb875a70fced2ae81fdd2ab7c1b38aa6f560cf3 --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"

# 1 my coin 换 faucet coin

mint 1 my coin
sui client call --package 0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e --module my_coin --function mint_and_send --args 0x2e8acd5b90955411516907a58fe2f16317230449829bc71ac99d3c35215aa578 100000000 0x221ce69b6e0be6bb3d38f5fb4ce8eb5ee78a3ff8ae58b2be0f2263812c244ad8
my coin object id: 0x9f23b13403ed0299fe1c4b8f94f455b4186ffc7b09fbacb5d827fc8f84e71edb

Transaction Digest: Hqvwr3MH6pw6tUaWizYvRtsDYfCGVCh9gra74uQrg9hd
sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function swap_A_to_B --args 0x804542cd5dce0abeceaff84e26896a661df365763ac60d4780c5c9c411635084 0x9f23b13403ed0299fe1c4b8f94f455b4186ffc7b09fbacb5d827fc8f84e71edb --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"

# 20 faucet coin 换 my coin

mint 20 faucet coin
sui client call --package 0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b --module faucet_coin --function mint_and_send --args 0xe6c8a1222e5143b21a4e762d5441d575a27471efde1c320628c139991748c6d4 2000000000 0x221ce69b6e0be6bb3d38f5fb4ce8eb5ee78a3ff8ae58b2be0f2263812c244ad8
faucet coin object id: 0x6028e524dedc49e2f42ed88ff64d61203241f807f1ec6880faf7d4a6fdd786c0

Transaction Digest: 63E8Tr1ffEqDAq1PGdhTz7JGK2iCBJdoQaAHaFN5zxvc
sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function swap_B_to_A --args 0x804542cd5dce0abeceaff84e26896a661df365763ac60d4780c5c9c411635084 0x6028e524dedc49e2f42ed88ff64d61203241f807f1ec6880faf7d4a6fdd786c0 --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"

# 从银行取20 my coin
Transaction Digest: D3dnZar8fSSLamRGhBYEYZ7mdy5JkRgnHUqhvoxQ4oQX
sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function withdraw_coin_A --args 0x00fcdd927ff0684811f0871582f92aaafbafc2ad128308ebfff8964dca2eb923 0x804542cd5dce0abeceaff84e26896a661df365763ac60d4780c5c9c411635084 2000000000 --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"

# 从银行取20 faucet coin
Transaction Digest: 9vH7cpNZ1uqso3HxDijF62K7uGma3iDi5VWVqhfM1GjN
sui client call --package 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035 --module coin_swap --function withdraw_coin_B --args 0x00fcdd927ff0684811f0871582f92aaafbafc2ad128308ebfff8964dca2eb923 0x804542cd5dce0abeceaff84e26896a661df365763ac60d4780c5c9c411635084 2000000000 --type-args "0x5c3a6b4a55686eb982ce65be5cb4b0d7935765c8be1dc1902de031efd945866e::my_coin::MY_COIN" "0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e87395ba3059b::faucet_coin::FAUCET_COIN"
34 changes: 34 additions & 0 deletions mover/Helen2022a/code/task5/coin_swap/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
manifest_digest = "630A4912AD57C99278826430269B24156D2E3D494AE516E00DEECD65CAC4D2BE"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ id = "Sui", name = "Sui" },
]

[[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.toolchain-version]
compiler-version = "1.37.1"
edition = "2024.beta"
flavor = "sui"

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035"
latest-published-id = "0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035"
published-version = "1"
37 changes: 37 additions & 0 deletions mover/Helen2022a/code/task5/coin_swap/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "helen2022a"
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" }

# 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]
helen2022a = "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"

101 changes: 101 additions & 0 deletions mover/Helen2022a/code/task5/coin_swap/sources/coin_swap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module helen2022a::coin_swap {

use sui::balance::{Self, zero, Balance};
use sui::coin::{Self, Coin};
use sui::transfer::{public_transfer, transfer, share_object};
use std::debug;
use std::ascii::string;

// 1 coinA = 5 coinB
const SWAP_RATE_A_2_B: u64 = 5;

//银行金额不足
const EBankInsufficientBalance: u64 = 1001;

//取款权限
public struct AdminCap has key {
id: UID
}

// 简单版
public struct Bank<phantom COIN_A, phantom COIN_B> has key {
id: UID,
coin_a: Balance<COIN_A>,
coin_b: Balance<COIN_B>
}

fun init(ctx: &mut TxContext) {
let adminCap = AdminCap { id: object::new(ctx) };
transfer(adminCap, tx_context::sender(ctx));
}

public entry fun createBank<COIN_A, COIN_B>(ctx: &mut TxContext) {
let bank = Bank {
id: object::new(ctx),
coin_a: zero<COIN_A>(),
coin_b: zero<COIN_B>()
};
share_object(bank);
}

// 存 coin A
public entry fun deposit_coin_A<COIN_A, COIN_B>(bank: &mut Bank<COIN_A, COIN_B>, coin_a: Coin<COIN_A>) {
let balance = coin::into_balance<COIN_A>(coin_a);
balance::join<COIN_A>(&mut bank.coin_a, balance);
}

// 取 coin A
public entry fun withdraw_coin_A<COIN_A, COIN_B>(_: &AdminCap, bank: &mut Bank<COIN_A, COIN_B>, amount_a : u64, ctx: &mut TxContext) {

assert!(bank.coin_a.value() >= amount_a, EBankInsufficientBalance );
let a_balance = balance::split<COIN_A>(&mut bank.coin_a, amount_a);
let coinA = coin::from_balance<COIN_A>(a_balance, ctx);
public_transfer(coinA, tx_context::sender(ctx));
}


// 存 coin B
public entry fun deposit_coin_B<COIN_A, COIN_B>(bank: &mut Bank<COIN_A, COIN_B>, coin_b: Coin<COIN_B>) {
let balance = coin::into_balance<COIN_B>(coin_b);
balance::join<COIN_B>(&mut bank.coin_b, balance);
}

// 取 coin B
public entry fun withdraw_coin_B<COIN_A, COIN_B>(_: &AdminCap, bank: &mut Bank<COIN_A, COIN_B>, amount_b: u64, ctx: &mut TxContext) {
assert!(bank.coin_b.value() >= amount_b, EBankInsufficientBalance );
let b_balance = balance::split<COIN_B>(&mut bank.coin_b, amount_b);
let coinB = coin::from_balance<COIN_B>(b_balance, ctx);
public_transfer(coinB, tx_context::sender(ctx));
}

// coin A 兑换 coinB
public entry fun swap_A_to_B<COIN_A, COIN_B>(bank: &mut Bank<COIN_A, COIN_B>, coinA: Coin<COIN_A>, ctx: &mut TxContext) {
let amount = coin::value<COIN_A>(&coinA);
let amount_b = amount * SWAP_RATE_A_2_B;
assert!(bank.coin_b.value() >= amount_b, EBankInsufficientBalance );
// 减少 b, 发送给兑换人
let withdraw_b_balance = balance::split<COIN_B>(&mut bank.coin_b, amount_b);
let coin_b = coin::from_balance<COIN_B>(withdraw_b_balance, ctx);
public_transfer(coin_b, tx_context::sender(ctx));
// 增加 a
let a_balance = coin::into_balance<COIN_A>(coinA);
balance::join<COIN_A>(&mut bank.coin_a, a_balance);
}

public entry fun swap_B_to_A<COIN_A, COIN_B>(bank: &mut Bank<COIN_A, COIN_B>, coinB: Coin<COIN_B>, ctx: &mut TxContext) {
let amount = coin::value<COIN_B>(&coinB);
let amount_a = amount / SWAP_RATE_A_2_B;
assert!(bank.coin_a.value() >= amount_a, EBankInsufficientBalance );
// 减少 a, 发送给兑换人
let withdraw_a_balance = balance::split<COIN_A>(&mut bank.coin_a, amount_a);
let coin_a = coin::from_balance<COIN_A>(withdraw_a_balance, ctx);
public_transfer(coin_a, tx_context::sender(ctx));
// 增加 b
let b_balance = coin::into_balance<COIN_B>(coinB);
balance::join<COIN_B>(&mut bank.coin_b, b_balance);
debug::print(&string(b"coinA"));
debug::print(&balance::value(&bank.coin_a));
debug::print(&string(b"coinB"));
debug::print(&balance::value(&bank.coin_b));
}
}
18 changes: 18 additions & 0 deletions mover/Helen2022a/code/task5/coin_swap/tests/coin_swap_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module coin_swap::coin_swap_tests;
// uncomment this line to import the module
// use coin_swap::coin_swap;

const ENotImplemented: u64 = 0;

#[test]
fun test_coin_swap() {
// pass
}

#[test, expected_failure(abort_code = ::coin_swap::coin_swap_tests::ENotImplemented)]
fun test_coin_swap_fail() {
abort ENotImplemented
}
*/
6 changes: 3 additions & 3 deletions mover/Helen2022a/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ sui client call --package 0x5d2aad9075f6d13961d0d08545eaf9e3194b466f9775bc45492e


## 05 Move Swap
- [] swap package id :
- [] call swap CoinA-> CoinB hash :
- [] call swap CoinB-> CoinA hash :
- [] swap package id : 0xf4fb98c8c3fc3a792aa2a66104a3f64efafc099007fd99004c3dfe5aa0dd7035
- [] call swap CoinA-> CoinB hash : Hqvwr3MH6pw6tUaWizYvRtsDYfCGVCh9gra74uQrg9hd
- [] call swap CoinB-> CoinA hash : 63E8Tr1ffEqDAq1PGdhTz7JGK2iCBJdoQaAHaFN5zxvc

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