-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'move-cn:main' into main
- Loading branch information
Showing
238 changed files
with
34,154 additions
and
142 deletions.
There are no files selected for viewing
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
mover/Ch1hiro/code/task6/navi_protocol/src/networkConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
mover/Ch1hiro/code/task6/navi_protocol/src/transaction/navi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
## 需求 | ||
### place | ||
- 配置 | ||
- 设置手续费比例、赔率、管理员、手续费接收人和奖池。 | ||
- 功能 | ||
- 创建place | ||
- 给奖池注资 | ||
- 更新palace配置 | ||
- 静态方法 | ||
### game | ||
- 规则 | ||
- 石头 > 剪刀 > 布 > 石头 | ||
- 如果用户没有获胜,计算手续费,手续费转给手续费接收人,剩余入金并入奖池。 | ||
- 如果用户获胜,计算奖金,如果奖池足够支付奖金,按照赔率赔付用户;否则,从奖池中扣除手续费后赔付用户。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.