Skip to content

Commit

Permalink
Merge pull request #2103 from qiaopengjun5162/main
Browse files Browse the repository at this point in the history
finished task5
  • Loading branch information
Sifotd authored Dec 4, 2024
2 parents 5d024c1 + 20bf7b4 commit 77cfb39
Show file tree
Hide file tree
Showing 6 changed files with 2,061 additions and 3 deletions.
52 changes: 52 additions & 0 deletions mover/qiaopengjun5162/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 = 2
manifest_digest = "80568E4332C0FB4FE6FCEF26FA0863F2675F91209E2AB8BE704EE7313266F400"
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3"
dependencies = [
{ name = "Sui" },
{ name = "faucet_coin" },
{ name = "mycoin" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
]

[[move.package]]
name = "faucet_coin"
source = { local = "../../task2/faucet_coin" }

dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "mycoin"
source = { local = "../../task2/mycoin" }

dependencies = [
{ name = "Sui" },
]

[move.toolchain-version]
compiler-version = "1.32.0"
edition = "2024.beta"
flavor = "sui"

[env]

[env.mainnet]
chain-id = "35834a8a"
original-published-id = "0x4d7b1f70c0457133d29a8b468719cb2506962da8123c04f0de4413eb32040445"
latest-published-id = "0x4d7b1f70c0457133d29a8b468719cb2506962da8123c04f0de4413eb32040445"
published-version = "1"
39 changes: 39 additions & 0 deletions mover/qiaopengjun5162/code/task5/move_swap/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "move_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" }
mycoin = {local = "../../task2/mycoin"}
faucet_coin = {local = "../../task2/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]
move_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"

75 changes: 75 additions & 0 deletions mover/qiaopengjun5162/code/task5/move_swap/sources/move_swap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/// Module: move_swap
module move_swap::move_swap {
use sui::balance::{Self, Balance};
use sui::coin::{Self, Coin};
use sui::transfer::{public_transfer, share_object, transfer};
use mycoin::mycoin::MYCOIN;
use faucet_coin::faucet_coin::FAUCET_COIN;

public struct AdminCap has key {
id: UID
}

public struct Pool has key {
id: UID,
mycoin: Balance<MYCOIN>,
faucet_coin: Balance<FAUCET_COIN>
}

fun init(ctx: &mut TxContext) {
let pool = Pool {
id: object::new(ctx),
mycoin: balance::zero<MYCOIN>(),
faucet_coin: balance::zero<FAUCET_COIN>()
};
share_object(pool);

let adminCap = AdminCap {
id: object::new(ctx)
};

transfer(adminCap, ctx.sender());
}

// deposit
public entry fun deposit_mycoin(pool: &mut Pool, coin: Coin<MYCOIN>, _: &mut TxContext) {
balance::join(&mut pool.mycoin, coin::into_balance(coin));
}

public entry fun deposit_faucet_coin(pool: &mut Pool, coin: Coin<FAUCET_COIN>, _: &mut TxContext) {
pool.faucet_coin.join(coin::into_balance(coin));
}

// withdraw
public entry fun withdraw_mycoin(_: &AdminCap, pool: &mut Pool, amount: u64, ctx: &mut TxContext) {
let withdrawBalance = pool.mycoin.split(amount);
public_transfer(coin::from_balance(withdrawBalance, ctx), ctx.sender());
}

public entry fun withdraw_faucet_coin(_: &AdminCap, pool: &mut Pool, amount: u64, ctx: &mut TxContext) {
let withdrawBalance = pool.faucet_coin.split(amount);
public_transfer(coin::from_balance(withdrawBalance, ctx), ctx.sender());
}

// swap
// 1 mycoin -> 2 faucet_coin
public entry fun mycoin_to_faucet_coin(pool: &mut Pool, coin: Coin<MYCOIN>, ctx: &mut TxContext) {
let mycoin_amt = coin.value();
let faucet_coin_amt = mycoin_amt * 20000 / 10000;

pool.mycoin.join(coin::into_balance(coin));
let faucet_coin_b = pool.faucet_coin.split(faucet_coin_amt);
public_transfer(coin::from_balance(faucet_coin_b, ctx), ctx.sender());
}

// 2 faucet_coin -> 1 mycoin
public entry fun faucet_coin_to_mycoin(pool: &mut Pool, coin: Coin<FAUCET_COIN>, ctx: &mut TxContext) {
let faucet_coin_amt = coin.value();
let mycoin_amt = faucet_coin_amt * 10000 / 20000;

pool.faucet_coin.join(coin::into_balance(coin));
let mycoin_b = pool.mycoin.split(mycoin_amt);
public_transfer(coin::from_balance(mycoin_b, ctx), ctx.sender());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
#[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
}
}
*/
Loading

0 comments on commit 77cfb39

Please sign in to comment.