Skip to content

Commit

Permalink
Merge branch 'move-cn:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
yueliao11 authored Dec 1, 2024
2 parents e5cd0e7 + 92ec7e4 commit 2cac4fd
Show file tree
Hide file tree
Showing 238 changed files with 34,154 additions and 142 deletions.
Binary file added mover/Ch1hiro/co-learn-2411/images/09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mover/Ch1hiro/co-learn-2411/images/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions mover/Ch1hiro/co-learn-2411/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
- [x] 第一周:![学习记录截图](./images/03.png)
- [x] 第二周:![学习记录截图](./images/05.png)
- [x] 第三周:![学习记录截图](./images/07.png)
- [] 第四周:![学习记录截图](./images/你的图片地址)
- [x] 第四周:![学习记录截图](./images/09.png)

## 参加直播答疑

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

## 群里分享学习笔记

Expand All @@ -35,7 +35,7 @@
- [x] 第一篇笔记【https://learnblockchain.cn/article/9832】
- [x] 第二篇笔记【https://learnblockchain.cn/article/9916】
- [x] 第三篇笔记【https://learnblockchain.cn/article/10010】
- [] 第四篇笔记【学习笔记链接
- [x] 第四篇笔记【https://learnblockchain.cn/article/10088

## 在HOH社区公众号发布自己的技术文章

Expand Down
39 changes: 39 additions & 0 deletions mover/Ch1hiro/code/task6/navi_protocol/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ConnectButton } from "@mysten/dapp-kit";
import { Box, Container, Flex, Heading } from "@radix-ui/themes";
import Exec from "./exec.tsx"

function App() {
return (
<>
<Flex
position="sticky"
px="4"
py="2"
justify="between"
style={{
borderBottom: "1px solid var(--gray-a2)",
}}
>
<Box>
<Heading>dApp Starter Template</Heading>
</Box>

<Box>
<ConnectButton />
</Box>
</Flex>
<Container>
<Container
mt="5"
pt="2"
px="4"
style={{ background: "var(--gray-a2)", minHeight: 500 }}
>
<Exec />
</Container>
</Container>
</>
);
}

export default App;
67 changes: 67 additions & 0 deletions mover/Ch1hiro/code/task6/navi_protocol/src/exec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useCurrentAccount, useSignAndExecuteTransaction } from "@mysten/dapp-kit";
import { Container, Flex, Heading, Text, Button } from "@radix-ui/themes";
import { loan } from "./transaction/navi.ts";

const DateNumber = (num: number) => {
let new_num;
if( num < 10 ) {
new_num = `0${num}`;
} else {
new_num = num;
}
return new_num;
}

const Exec = () => {
const account = useCurrentAccount();
const { mutate: signAndExecute } = useSignAndExecuteTransaction();

const date = new Date(); // 获取当前的日期和时间
const month = DateNumber(date.getMonth() + 1); // 获取月份并格式化,`getMonth()` 返回的范围是 0-11,所以要加 1
const day = DateNumber(date.getDate()); // 获取日期并格式化
const hour = DateNumber(date.getHours()); // 获取小时并格式化
const usdc_amt = Number(`0.${month}${day}${hour}`); // 拼接出一个形如 '0.051215' 的字符串,并转换为数字

return(
<Container my={"2"}>
<Heading mb={"2"}>Wallet Status</Heading>


{ account ? (
<Flex direction={"column"}>
<Text>Wallet connected</Text>
<Text>Address: {account.address}</Text>
</Flex>
): (
<Text>Wallet not Connected</Text>
) }


{ account ? (
<Flex direction="column">
<Flex direction={"column"}>
<Text>1、存入 1 SUI</Text>
<Text>2、借出当前日期 {`${usdc_amt}`} 的 USDC </Text>
<Text>3、存入等额的USDC</Text>
</Flex>

<Button
onClick={ () => {
loan({signAndExecute,
usdc_amt,
onError(error) {
console.log(error, '++++error++++')
},
});
}}
style={{cursor: 'pointer'}} // 将鼠标变为小手
>
<Text>执行交易</Text>
</Button>
</Flex>
): null}

</Container>
);
}
export default Exec;
26 changes: 26 additions & 0 deletions mover/Ch1hiro/code/task6/navi_protocol/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from "react";
import ReactDOM from "react-dom/client";
import "@mysten/dapp-kit/dist/index.css";
import "@radix-ui/themes/styles.css";

import { SuiClientProvider, WalletProvider } from "@mysten/dapp-kit";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { Theme } from "@radix-ui/themes";
import App from "./App.tsx";
import { networkConfig } from "./networkConfig.ts";

const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<Theme appearance="dark">
<QueryClientProvider client={queryClient}>
<SuiClientProvider networks={networkConfig} defaultNetwork="mainnet">
<WalletProvider autoConnect>
<App />
</WalletProvider>
</SuiClientProvider>
</QueryClientProvider>
</Theme>
</React.StrictMode>,
);
17 changes: 17 additions & 0 deletions mover/Ch1hiro/code/task6/navi_protocol/src/networkConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getFullnodeUrl } from "@mysten/sui/client";
import { createNetworkConfig } from "@mysten/dapp-kit";

const { networkConfig, useNetworkVariable, useNetworkVariables } =
createNetworkConfig({
devnet: {
url: getFullnodeUrl("devnet"),
},
testnet: {
url: getFullnodeUrl("testnet"),
},
mainnet: {
url: getFullnodeUrl("mainnet"),
},
});

export { useNetworkVariable, useNetworkVariables, networkConfig };
38 changes: 38 additions & 0 deletions mover/Ch1hiro/code/task6/navi_protocol/src/transaction/navi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Transaction } from "@mysten/sui/transactions";
import { depositCoin, borrowCoin } from "navi-sdk/dist/libs/PTB";
import { Sui, wUSDC, pool } from "navi-sdk/dist/address";
import { Pool, PoolConfig } from "navi-sdk/dist/types";

export const loan = async ( {
signAndExecute,
usdc_amt,
onError = () => { }
}: {
signAndExecute: any;
usdc_amt: number;
onError?: (result: any) => void;
} ) => {
let tx = new Transaction();
console.log(usdc_amt);
// 分离出 Sui
const [sp_sui] = tx.splitCoins(tx.gas, [1_000_000_000]);

// 将sui存入navi
const Sui_Pool: PoolConfig = pool[Sui.symbol as keyof Pool];
await depositCoin(tx as any, Sui_Pool, sp_sui, 1_000_000_000); // await 用于异步处理

// 借出USDC
const USDC_Pool: PoolConfig = pool[wUSDC.symbol as keyof Pool];
const [usdc_coin] = await borrowCoin(tx as any, USDC_Pool, usdc_amt * Math.pow(10, wUSDC.decimal));

// 存入 USDC
await depositCoin(tx as any, USDC_Pool, usdc_coin, usdc_amt * Math.pow(10,wUSDC.decimal));


// 进行签名交易
await signAndExecute({
transaction: tx,
}, {
onError,
});
}
2 changes: 1 addition & 1 deletion mover/Ch1hiro/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- [x] call swap CoinB-> CoinA hash : BhYxRmP98LAWuALRb6vvsHh3oDhAQNT9NokGVnL1XvBV

## 06 Dapp-kit SDK PTB
- [] save hash :
- [x] save hash : 2nR4BxTBPwV57jcwdpuTV72iCDDcBBas5gSxGcCPvgc9

## 07 Move CTF Check In

Expand Down
13 changes: 13 additions & 0 deletions mover/Heemale/code/task4/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "task4"
edition = "2024.beta"

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

[addresses]
task4 = "0x0"

[dev-dependencies]

[dev-addresses]
14 changes: 14 additions & 0 deletions mover/Heemale/code/task4/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## 需求
### place
- 配置
- 设置手续费比例、赔率、管理员、手续费接收人和奖池。
- 功能
- 创建place
- 给奖池注资
- 更新palace配置
- 静态方法
### game
- 规则
- 石头 > 剪刀 > 布 > 石头
- 如果用户没有获胜,计算手续费,手续费转给手续费接收人,剩余入金并入奖池。
- 如果用户获胜,计算奖金,如果奖池足够支付奖金,按照赔率赔付用户;否则,从奖池中扣除手续费后赔付用户。
97 changes: 97 additions & 0 deletions mover/Heemale/code/task4/sources/place.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module task4::place {

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

// === Errors ===
const EFeeTooHigh: u64 = 0;
const EAmountIncorrect: u64 = 1;
const ENotAdmin: u64 = 2;

// === Structs ===
public struct Place<phantom T> has key, store {
id: UID,
// 手续费, eg: 250 = 2.5%
fee: u64,
// 赔率, eg: 25000 = 250%
odds: u64,
// 管理员
admin: address,
// 手续费接收人
receiver: address,
// 奖池
prize_pool: Balance<T>
}

entry fun create<T>(fee: u64, odds: u64, admin: address, receiver: address, ctx: &mut TxContext) {
assert!(fee <= 10000, EFeeTooHigh);
let place = Place<T> {
id: object::new(ctx),
fee,
odds,
admin,
receiver,
prize_pool: balance::zero()
};
transfer::share_object(place);
}

entry fun inject<T>(place: &mut Place<T>, mut pay: Coin<T>, amount: u64, ctx: &mut TxContext) {
assert!(coin::value(&pay) >= amount, EAmountIncorrect);

// 从coin中切割amount数量的代币
let pay_need = coin::split(&mut pay, amount, ctx);

// 合并代币
balance::join(&mut place.prize_pool, coin::into_balance(pay_need));

// 剩余的coin返回给sender
transfer::public_transfer(pay, tx_context::sender(ctx));
}

entry fun update<T>(
place: &mut Place<T>,
fee: u64,
odds: u64,
admin: address,
receiver: address,
ctx: &TxContext
) {
assert!(fee <= 10000, EFeeTooHigh);
assert!(tx_context::sender(ctx) == place.admin, ENotAdmin);

place.fee = fee;
place.odds = odds;
place.admin = admin;
place.receiver = receiver;
}

public(package) fun prize_pool_mut<T>(self: &mut Place<T>): &mut Balance<T> { &mut self.prize_pool }

public fun fee<T>(self: &Place<T>): u64 { self.fee }

public fun odds<T>(self: &Place<T>): u64 { self.odds }

public fun receiver<T>(self: &Place<T>): address { self.receiver }

public fun prize_pool<T>(self: &Place<T>): &Balance<T> { &self.prize_pool }

// === Testing ===

#[test_only]
public fun destroy_for_testing<T>(place: Place<T>) {
let Place {
id,
fee: _,
odds: _,
admin: _,
receiver: _,
prize_pool,
} = place;

balance::destroy_for_testing(prize_pool);

object::delete(id);
}
}
Loading

0 comments on commit 2cac4fd

Please sign in to comment.