Skip to content

Commit

Permalink
feat: finish task5,6,7 and add follow images.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heemale committed Dec 7, 2024
1 parent 2bc596f commit 422436a
Show file tree
Hide file tree
Showing 43 changed files with 6,728 additions and 3 deletions.
37 changes: 37 additions & 0 deletions mover/Heemale/code/task5/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "task5"
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]
task5 = "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"

49 changes: 49 additions & 0 deletions mover/Heemale/code/task5/sources/faucet_coin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module task5::faucet_coin {

use std::string;
use sui::coin::{Self, TreasuryCap};
use sui::url;

public struct FAUCET_COIN has drop {}

fun init(witness: FAUCET_COIN, ctx: &mut TxContext) {
let (treasury, meta_data) = coin::create_currency<FAUCET_COIN>(
witness,
6,
b"Heemale Faucet",
b"Heemale Faucet Coin",
b"相信的心是我们的魔法",
option::some(
url::new_unsafe(
string::to_ascii(
string::utf8(
b"https://avatars.githubusercontent.com/u/57651639?s=400&u=25e8d8a5c8eed5d1408617994c5d8ea8ec0ac5c2&v=4"
)
)
)
),
ctx
);

transfer::public_share_object(treasury);
transfer::public_freeze_object(meta_data);
}

entry fun mint(
treasury_cap: &mut TreasuryCap<FAUCET_COIN>,
amount: u64,
recipient: address,
ctx: &mut TxContext
) {
let coin = coin::mint(treasury_cap, amount, ctx);
transfer::public_transfer(coin, recipient);
}

// === Testing ===

#[test_only]
public fun init_for_testing(ctx: &mut TxContext){
let witness = FAUCET_COIN {};
init(witness, ctx);
}
}
49 changes: 49 additions & 0 deletions mover/Heemale/code/task5/sources/my_coin.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module task5::my_coin {

use std::string;
use sui::coin::{Self, TreasuryCap};
use sui::url;

public struct MY_COIN has drop {}

fun init(witness: MY_COIN, ctx: &mut TxContext) {
let (treasury, meta_data) = coin::create_currency<MY_COIN>(
witness,
6,
b"Heemale",
b"Heemale Coin",
b"相信的心是我们的魔法",
option::some(
url::new_unsafe(
string::to_ascii(
string::utf8(
b"https://avatars.githubusercontent.com/u/57651639?s=400&u=25e8d8a5c8eed5d1408617994c5d8ea8ec0ac5c2&v=4"
)
)
)
),
ctx
);

transfer::public_transfer(treasury, tx_context::sender(ctx));
transfer::public_freeze_object(meta_data);
}

entry fun mint(
treasury_cap: &mut TreasuryCap<MY_COIN>,
amount: u64,
recipient: address,
ctx: &mut TxContext
) {
let coin = coin::mint(treasury_cap, amount, ctx);
transfer::public_transfer(coin, recipient);
}

// === Testing ===

#[test_only]
public fun init_for_testing(ctx: &mut TxContext){
let witness = MY_COIN {};
init(witness, ctx);
}
}
66 changes: 66 additions & 0 deletions mover/Heemale/code/task5/sources/swap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module task5::swap {

use sui::coin::{Self, Coin};
use sui::balance::{Self, Balance};

const ETreasuryNotEnough: u64 = 0;

public struct Treasury<phantom T, phantom V> has key {
id: UID,
coin_a: Balance<T>,
coin_b: Balance<V>,
}

public fun new<T, V>(ctx: &mut TxContext) {
let treasury = Treasury {
id: object::new(ctx),
coin_a: balance::zero<T>(),
coin_b: balance::zero<V>(),
};
transfer::share_object(treasury);
}

public fun add_liquidity<T, V>(treasury: &mut Treasury<T, V>, coin_a: Coin<T>, coin_b: Coin<V>) {
let (treasury_coin_a, treasury_coin_b) = get_coin_mut<T, V>(treasury);
balance::join(treasury_coin_a, coin::into_balance(coin_a));
balance::join(treasury_coin_b, coin::into_balance(coin_b));
}

public fun swap_a_to_b<T, V>(treasury: &mut Treasury<T, V>, coin_a: Coin<T>, ctx: &mut TxContext): Coin<V> {
let (treasury_coin_a, treasury_coin_b) = get_coin_mut<T, V>(treasury);

let coin_value = coin::value(&coin_a);
let treasury_coin_b_value = balance::value(treasury_coin_b);
assert!(treasury_coin_b_value >= coin_value, ETreasuryNotEnough);

balance::join(treasury_coin_a, coin::into_balance(coin_a));

let balance_out = balance::split(treasury_coin_b, coin_value);
let coin_out = coin::from_balance(balance_out, ctx);

coin_out
}

public fun swap_b_to_a<T, V>(treasury: &mut Treasury<T, V>, coin_b: Coin<V>, ctx: &mut TxContext): Coin<T> {
let (treasury_coin_a, treasury_coin_b) = get_coin_mut<T, V>(treasury);

let coin_value = coin::value(&coin_b);
let treasury_coin_a_value = balance::value(treasury_coin_a);
assert!(treasury_coin_a_value >= coin_value, ETreasuryNotEnough);

balance::join(treasury_coin_b, coin::into_balance(coin_b));

let balance_out = balance::split(treasury_coin_a, coin_value);
let coin_out = coin::from_balance(balance_out, ctx);

coin_out
}

public(package) fun get_coin_mut<T, V>(treasury: &mut Treasury<T, V>): (&mut Balance<T>, &mut Balance<V>) {
(&mut treasury.coin_a, &mut treasury.coin_b)
}

public(package) fun get_coin<T, V>(treasury: &Treasury<T, V>): (&Balance<T>, &Balance<V>) {
(&treasury.coin_a, &treasury.coin_b)
}
}
107 changes: 107 additions & 0 deletions mover/Heemale/code/task5/tests/test_swap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#[test_only]
module task5::test_swap {
use sui::test_scenario::{Self, ctx};
use sui::coin::{TreasuryCap, Coin};
use sui::balance::{Self};

use task5::faucet_coin::{Self, FAUCET_COIN};
use task5::my_coin::{Self, MY_COIN};
use task5::swap::{Self, Treasury};

const ADMIN: address = @0xA;

#[test]
fun process_for_testing() {
let mut scenario = test_scenario::begin(ADMIN);

// 初始化代币a合约和代币b合约
{
faucet_coin::init_for_testing(ctx(&mut scenario));
my_coin::init_for_testing(ctx(&mut scenario));
};
test_scenario::next_tx(&mut scenario, ADMIN);

// 获取代币a铸币权
let mut treasury_cap_a = test_scenario::take_shared<TreasuryCap<FAUCET_COIN>>(&scenario);
// 获取代币b铸币权
let mut treasury_cap_b = test_scenario::take_from_address<TreasuryCap<MY_COIN>>(&scenario, ADMIN);

// 创建库存
swap::new<FAUCET_COIN, MY_COIN>(ctx(&mut scenario));
test_scenario::next_tx(&mut scenario, ADMIN);
// 铸造代币a
faucet_coin::mint(&mut treasury_cap_a, 100_000_000_000, ADMIN, ctx(&mut scenario));
test_scenario::next_tx(&mut scenario, ADMIN);
// 铸造代币b
my_coin::mint(&mut treasury_cap_b, 100_000_000_000, ADMIN, ctx(&mut scenario));
test_scenario::next_tx(&mut scenario, ADMIN);

// 获取库存对象
let mut treasury = test_scenario::take_shared<Treasury<FAUCET_COIN, MY_COIN>>(&scenario);
// 获取代币a
let despoit_coin_a = test_scenario::take_from_address<Coin<FAUCET_COIN>>(&scenario, ADMIN);
// 获取代币b
let despoit_coin_b = test_scenario::take_from_address<Coin<MY_COIN>>(&scenario, ADMIN);

// === add_liquidity ===

// 添加流动性
swap::add_liquidity<FAUCET_COIN, MY_COIN>(
&mut treasury,
despoit_coin_a,
despoit_coin_b,
);
test_scenario::next_tx(&mut scenario, ADMIN);
// 查看资金
{
let (treasury_coin_a, treasury_coin_b) = swap::get_coin_mut<FAUCET_COIN, MY_COIN>(&mut treasury);
assert!(balance::value(treasury_coin_a) == 100_000_000_000, 0);
assert!(balance::value(treasury_coin_b) == 100_000_000_000, 0);
};

// === swap_a_to_b ===

// 获取代币a
faucet_coin::mint(&mut treasury_cap_a, 100, ADMIN, ctx(&mut scenario));
test_scenario::next_tx(&mut scenario, ADMIN);
// 获取代币a
let input_coin_a = test_scenario::take_from_address<Coin<FAUCET_COIN>>(&scenario, ADMIN);
// 代币a兑换代币b
{
let out_coin = swap::swap_a_to_b<FAUCET_COIN, MY_COIN>(&mut treasury, input_coin_a, ctx(&mut scenario));
transfer::public_transfer(out_coin, ADMIN);
};
test_scenario::next_tx(&mut scenario, ADMIN);
// 查看资金
{
let (treasury_coin_a, treasury_coin_b) = swap::get_coin_mut<FAUCET_COIN, MY_COIN>(&mut treasury);
assert!(balance::value(treasury_coin_a) == 100_000_000_000 + 100, 0);
assert!(balance::value(treasury_coin_b) == 100_000_000_000 - 100, 0);
};

// === swap_b_to_a ===

// 铸造代币b
my_coin::mint(&mut treasury_cap_b, 100, ADMIN, ctx(&mut scenario));
test_scenario::next_tx(&mut scenario, ADMIN);
// 获取代币b
let input_coin_b = test_scenario::take_from_address<Coin<MY_COIN>>(&scenario, ADMIN);
// 代币b兑换代币a
{
let out_coin = swap::swap_b_to_a<FAUCET_COIN, MY_COIN>(&mut treasury, input_coin_b, ctx(&mut scenario));
transfer::public_transfer(out_coin, ADMIN);
};
test_scenario::next_tx(&mut scenario, ADMIN);
// 查看资金
{
let (treasury_coin_a, treasury_coin_b) = swap::get_coin_mut<FAUCET_COIN, MY_COIN>(&mut treasury);
assert!(balance::value(treasury_coin_a) == 100_000_000_000 + 100 - 100, 0);
assert!(balance::value(treasury_coin_b) == 100_000_000_000 - 100 + 100, 0);
};

test_scenario::return_shared(treasury_cap_a);
transfer::public_transfer(treasury_cap_b, ADMIN);
test_scenario::return_shared(treasury);
test_scenario::end(scenario);
}
}
1 change: 1 addition & 0 deletions mover/Heemale/code/task6/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_NETWORK=mainnet
21 changes: 21 additions & 0 deletions mover/Heemale/code/task6/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Mindfrog

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions mover/Heemale/code/task6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# task6

This is a Next.js Sui dApp project...
Binary file added mover/Heemale/code/task6/app/favicon.ico
Binary file not shown.
13 changes: 13 additions & 0 deletions mover/Heemale/code/task6/app/fonts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import localFont from "next/font/local";

export const geistSans = localFont({
src: "./fonts/GeistVF.woff",
variable: "--font-geist-sans",
weight: "100 900",
});

export const geistMono = localFont({
src: "./fonts/GeistMonoVF.woff",
variable: "--font-geist-mono",
weight: "100 900",
});
Binary file not shown.
Binary file added mover/Heemale/code/task6/app/fonts/GeistVF.woff
Binary file not shown.
7 changes: 7 additions & 0 deletions mover/Heemale/code/task6/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
font-family: Arial, Helvetica, sans-serif;
}
23 changes: 23 additions & 0 deletions mover/Heemale/code/task6/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Metadata } from "next";
import "./globals.css";
import { geistSans, geistMono } from "./fonts";
import { Providers } from "./providers";

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={`${geistSans.variable} ${geistMono.variable} antialiased`}>
<Providers>{children}</Providers>
</body>
</html>
);
}
Loading

0 comments on commit 422436a

Please sign in to comment.