Skip to content

Commit

Permalink
Merge pull request #2077 from sahab328/main
Browse files Browse the repository at this point in the history
task5 + 第三周答疑 + 第四周学习 + task6 + task7 + task8
  • Loading branch information
Sifotd authored Dec 12, 2024
2 parents 31ed0d2 + c0bd1a6 commit eb149f5
Show file tree
Hide file tree
Showing 32 changed files with 6,096 additions and 12 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions mover/sahab328/coLearn2411/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
- [] 第一周:![学习记录截图](./images/你的图片地址)
- [] 第二周:![学习记录截图](./images/你的图片地址)
- [x] 第三周:![学习记录截图](./images/week3_learning.png)
- [] 第四周:![学习记录截图](./images/你的图片地址)

- [x] 第四周:![学习记录截图](./images/week4_learning.png)
## 参加直播答疑

- [] 第一周:![学习记录截图](./images/你的图片地址)
- [] 第二周:![学习记录截图](./images/你的图片地址)
- [] 第三周:![学习记录截图](./images/你的图片地址)
- [x] 第三周:![学习记录截图](./images/week3_officehour.png)
- [] 第四周:![学习记录截图](./images/你的图片地址)

## 群里分享学习笔记
Expand Down
40 changes: 40 additions & 0 deletions mover/sahab328/code/task5/sahab328/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 3
manifest_digest = "4F0B4221E13FB34E51D5B1BEC4E974DB3133DF055BB2AC4505191F3B45FD97EB"
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.36.2"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x280c8d9b3f86ef7999f71f201009731655379cf5991d499b1d24798e79c635f3"
latest-published-id = "0x280c8d9b3f86ef7999f71f201009731655379cf5991d499b1d24798e79c635f3"
published-version = "1"

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

116 changes: 116 additions & 0 deletions mover/sahab328/code/task5/sahab328/sources/sahab328.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
module sahab328::swap;

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

const EValueNotEqual:u64 = 0x1;

public struct Bank<phantom CoinA, phantom CoinB> has key {
id: UID,
a: Balance<CoinA>,
b: Balance<CoinB>,
lp:Supply<LPCoin<CoinA,CoinB>>,
fee: u64,
scaler: u64,
}

public struct LPCoin<phantom CoinA, phantom CoinB> has drop {}

fun init(ctx: &mut TxContext) {

}

public fun add_bank<CoinA, CoinB>(coin_a: Coin<CoinA>, coin_b: Coin<CoinB>, fee: u64, scaler:u64, ctx: &mut TxContext) {
let a_value = coin_a.value();
let b_value = coin_b.value();
let mut bank = Bank<CoinA, CoinB> {
id: object::new(ctx),
a: coin_a.into_balance(),
b: coin_b.into_balance(),
lp: balance::create_supply(LPCoin<CoinA,CoinB>{}),
fee: fee,
scaler: scaler,
};
let lp_value = std::u64::sqrt(a_value * b_value);
let lp_balance = bank.lp.increase_supply(lp_value);
transfer::public_transfer(lp_balance.into_coin(ctx), ctx.sender());
transfer::share_object(bank);
}

public fun get_bank_price<CoinA, CoinB>(bank: &Bank<CoinA, CoinB>): u64 {
let a_balance = bank.a.value();
let b_balance = bank.b.value();
bank.scaler * a_balance / b_balance
}

public fun get_price<CoinA, CoinB>(bank: &Bank<CoinA, CoinB>, a_value:u64, b_value:u64): u64 {
bank.scaler * a_value / b_value
}

public fun add<CoinA, CoinB>(bank: &mut Bank<CoinA, CoinB>, coin_a: Coin<CoinA>, coin_b: Coin<CoinB>, ctx: &mut TxContext) {
let bank_price = get_bank_price(bank);
let a_value = coin_a.value();
let b_value = coin_b.value();
let price = get_price(bank, a_value, b_value);
assert!(bank_price == price, EValueNotEqual);
bank.a.join(coin_a.into_balance());
bank.b.join(coin_b.into_balance());

let lp_value = std::u64::sqrt(a_value * b_value);
let lp_balance = bank.lp.increase_supply(lp_value);
transfer::public_transfer(lp_balance.into_coin(ctx), ctx.sender());
}

public fun remove<CoinA, CoinB>(bank: &mut Bank<CoinA, CoinB>, lp_coin: Coin<LPCoin<CoinA, CoinB>>, ctx: &mut TxContext) {
let lp_value = lp_coin.value();
let lp_balance = lp_coin.into_balance();
bank.lp.decrease_supply(lp_balance);
let lp_scale = bank.scaler * lp_value / bank.lp.supply_value();
let a_value = bank.a.value() * lp_scale / bank.scaler;
let b_value = bank.b.value() * lp_scale / bank.scaler;
let a_coin = bank.a.split(a_value).into_coin(ctx);
let b_coin = bank.b.split(b_value).into_coin(ctx);
transfer::public_transfer(a_coin, ctx.sender());
transfer::public_transfer(b_coin, ctx.sender());
}


// 1 a = x/y b
public fun a_to_b<CoinA, CoinB>(bank: &mut Bank<CoinA, CoinB>, coin: Coin<CoinA>, ctx: &mut TxContext) {
let a_before = bank.a.value();
let b_before = bank.b.value();
// 恒定乘积
let product = a_before * b_before;


// 手续费
let a_value = coin.value();
let a_fee_value = a_value * bank.fee / bank.scaler;

let a_after = a_before + a_value - a_fee_value;
let b_after = product / a_after;
let b_value = b_before - b_after;

bank.a.join(coin.into_balance());
let out_coin = bank.b.split(b_value).into_coin(ctx);
transfer::public_transfer(out_coin, ctx.sender());
}

// 1 b = y/x a
public fun b_to_a<CoinA, CoinB>(bank: &mut Bank<CoinA, CoinB>, coin: Coin<CoinB>, ctx: &mut TxContext) {
let a_before = bank.a.value();
let b_before = bank.b.value();
// 恒定乘积
let product = a_before * b_before;

// 手续费
let b_value = coin.value();
let b_fee_value = b_value * bank.fee / bank.scaler;

let b_after = b_before + b_value - b_fee_value;
let a_after = product / b_after;
let a_value = a_before - a_after;
bank.b.join(coin.into_balance());
let out_coin = bank.a.split(a_value).into_coin(ctx);
transfer::public_transfer(out_coin, ctx.sender());
}
18 changes: 18 additions & 0 deletions mover/sahab328/code/task5/sahab328/tests/sahab328_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
#[test_only]
module sahab328::sahab328_tests;
// uncomment this line to import the module
// use sahab328::sahab328;
const ENotImplemented: u64 = 0;
#[test]
fun test_sahab328() {
// pass
}
#[test, expected_failure(abort_code = ::sahab328::sahab328_tests::ENotImplemented)]
fun test_sahab328_fail() {
abort ENotImplemented
}
*/
35 changes: 35 additions & 0 deletions mover/sahab328/code/task6/learn-dapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sui dApp Starter Template

This dApp was created using `@mysten/create-dapp` that sets up a basic React
Client dApp using the following tools:

- [React](https://react.dev/) as the UI framework
- [TypeScript](https://www.typescriptlang.org/) for type checking
- [Vite](https://vitejs.dev/) for build tooling
- [Radix UI](https://www.radix-ui.com/) for pre-built UI components
- [ESLint](https://eslint.org/)
- [`@mysten/dapp-kit`](https://sdk.mystenlabs.com/dapp-kit) for connecting to
wallets and loading data
- [pnpm](https://pnpm.io/) for package management

## Starting your dApp

To install dependencies you can run

```bash
pnpm install
```

To start your dApp in development mode run

```bash
pnpm dev
```

## Building

To build your app for deployment you can run

```bash
pnpm build
```
59 changes: 59 additions & 0 deletions mover/sahab328/code/task6/learn-dapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!doctype html>
<html lang="en" class="dark-theme" style="color-scheme: dark">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Sui dApp Starter</title>

<style>
/*
Josh's Custom CSS Reset
https://www.joshwcomeau.com/css/custom-css-reset/
*/
*,
*::before,
*::after {
box-sizing: border-box;
}
* {
margin: 0;
}
body {
line-height: 1.5;
-webkit-font-smoothing: antialiased;
}
img,
picture,
video,
canvas,
svg {
display: block;
max-width: 100%;
}
input,
button,
textarea,
select {
font: inherit;
}
p,
h1,
h2,
h3,
h4,
h5,
h6 {
overflow-wrap: break-word;
}
#root,
#__next {
isolation: isolate;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
36 changes: 36 additions & 0 deletions mover/sahab328/code/task6/learn-dapp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "learn-dapp",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@mysten/dapp-kit": "0.14.40",
"@mysten/sui": "1.16.0",
"@radix-ui/colors": "^3.0.0",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/themes": "^3.1.1",
"@tanstack/react-query": "^5.50.1",
"navi-sdk": "^1.4.7",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.16.0",
"@typescript-eslint/parser": "^7.16.0",
"@vitejs/plugin-react-swc": "^3.7.0",
"eslint": "^9.6.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"prettier": "^3.3.2",
"typescript": "^5.5.3",
"vite": "^5.3.3"
}
}
Loading

0 comments on commit eb149f5

Please sign in to comment.