From 3879daf8b5480a71ce0710b7a084c4c6ba9d63fe Mon Sep 17 00:00:00 2001 From: Geek-zlk Date: Fri, 12 Jul 2024 09:31:55 +0800 Subject: [PATCH] task6 --- mover/Geek-zlk/code/task6_sdk/index.html | 12 ++ mover/Geek-zlk/code/task6_sdk/package.json | 24 ++++ mover/Geek-zlk/code/task6_sdk/src/App.css | 43 +++++++ mover/Geek-zlk/code/task6_sdk/src/App.tsx | 124 ++++++++++++++++++++ mover/Geek-zlk/code/task6_sdk/src/index.css | 64 ++++++++++ mover/Geek-zlk/code/task6_sdk/src/main.tsx | 16 +++ mover/Geek-zlk/readme.md | 2 +- 7 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 mover/Geek-zlk/code/task6_sdk/index.html create mode 100644 mover/Geek-zlk/code/task6_sdk/package.json create mode 100644 mover/Geek-zlk/code/task6_sdk/src/App.css create mode 100644 mover/Geek-zlk/code/task6_sdk/src/App.tsx create mode 100644 mover/Geek-zlk/code/task6_sdk/src/index.css create mode 100644 mover/Geek-zlk/code/task6_sdk/src/main.tsx diff --git a/mover/Geek-zlk/code/task6_sdk/index.html b/mover/Geek-zlk/code/task6_sdk/index.html new file mode 100644 index 000000000..251e0801f --- /dev/null +++ b/mover/Geek-zlk/code/task6_sdk/index.html @@ -0,0 +1,12 @@ + + + + + + SDK Help + + +
+ + + diff --git a/mover/Geek-zlk/code/task6_sdk/package.json b/mover/Geek-zlk/code/task6_sdk/package.json new file mode 100644 index 000000000..076a96228 --- /dev/null +++ b/mover/Geek-zlk/code/task6_sdk/package.json @@ -0,0 +1,24 @@ +{ + "name": "with-vite", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite --port 3000" + }, + "dependencies": { + "@mysten/sui.js": "0.51.2", + "@suiet/wallet-kit": "0.2.24", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "tweetnacl": "^1.0.3", + "navi-sdk": "^1.1.21" + }, + "devDependencies": { + "@types/react": "^18.0.22", + "@types/react-dom": "^18.0.7", + "@vitejs/plugin-react": "^2.2.0", + "typescript": "^5.4.3", + "vite": "^3.2.7" + } +} diff --git a/mover/Geek-zlk/code/task6_sdk/src/App.css b/mover/Geek-zlk/code/task6_sdk/src/App.css new file mode 100644 index 000000000..ac87691e7 --- /dev/null +++ b/mover/Geek-zlk/code/task6_sdk/src/App.css @@ -0,0 +1,43 @@ +#root { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +.logo { + height: 6em; + padding: 1.5em; + will-change: filter; +} +.logo:hover { + filter: drop-shadow(0 0 2em #646cffaa); +} +.logo.react:hover { + filter: drop-shadow(0 0 2em #61dafbaa); +} + +@keyframes logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} + +.card { + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + padding: 2em; +} + +.read-the-docs { + color: #888; +} + +.btn-group button+button{ + margin-left: 12px; +} \ No newline at end of file diff --git a/mover/Geek-zlk/code/task6_sdk/src/App.tsx b/mover/Geek-zlk/code/task6_sdk/src/App.tsx new file mode 100644 index 000000000..2ef205dbc --- /dev/null +++ b/mover/Geek-zlk/code/task6_sdk/src/App.tsx @@ -0,0 +1,124 @@ +import "./App.css"; +import { + ConnectButton, + useAccountBalance, + useWallet, + ErrorCode, + formatSUI, +} from "@suiet/wallet-kit"; +import "@suiet/wallet-kit/style.css"; +import { TransactionBlock } from "@mysten/sui.js/transactions"; +import {depositCoin, borrowCoin} from 'navi-sdk/dist/libs/PTB' +import { Pool, PoolConfig } from "navi-sdk/dist/types"; +import { pool } from 'navi-sdk/dist/address' +import {Sui, USDC} from 'navi-sdk/dist/address'; + +function App() { + const wallet = useWallet(); + const { balance } = useAccountBalance(); + + function uint8arrayToHex(value: Uint8Array | undefined) { + if (!value) return ""; + // @ts-ignore + return value.toString("hex"); + } + + + async function handleNAVISDK() { + if (!wallet.account) return; + const account = wallet.account; + const sender = account.address; + try { + let txb = new TransactionBlock(); + txb.setSender(sender); + + let getCoinInfo = balance; + if (!getCoinInfo) { + throw new Error("Insufficient balance"); + } + + let deposit_amount = 1e9; + let sui_symbol = Sui.symbol; + const pool_sui: PoolConfig = pool[sui_symbol as keyof Pool]; + const [to_deposit] = txb.splitCoins(txb.gas, [deposit_amount]); + await depositCoin(txb, pool_sui, to_deposit, deposit_amount); + + const formatDateNumber = (num: number) => { + return num < 10 ? `0${num}` : num; + } + const date = new Date(); + const month = formatDateNumber(date.getMonth() + 1); + const day = formatDateNumber(date.getDate()); + const hour = formatDateNumber(date.getHours()); + const usdcAmt = Number(`0.${month}${day}${hour}`); + + let borrow_amount = usdcAmt * 1e6; + let usdc_symbol = USDC.symbol; + const pool_usdc: PoolConfig = pool[usdc_symbol as keyof Pool]; + const [borrow_coin] = await borrowCoin(txb, pool_usdc, borrow_amount); + + await depositCoin(txb, pool_usdc, borrow_coin, borrow_amount); + + const resData = await wallet.signAndExecuteTransactionBlock({ + transactionBlock: txb, + }); + console.log("transaction digest: " + JSON.stringify(resData)); + } catch (e) { + console.error("failed", e); + alert("failed (see response in the console)"); + } + } + + return ( +
+
+ { + if (error.code === ErrorCode.WALLET__CONNECT_ERROR__USER_REJECTED) { + console.warn( + "user rejected the connection to " + error.details?.wallet + ); + } else { + console.warn("unknown connect error: ", error); + } + }} + /> + + {!wallet.connected ? ( +

Connect wallet from now!

+ ) : ( +
+
+

current wallet: {wallet.adapter?.name}

+

+ wallet status:{" "} + {wallet.connecting + ? "connecting" + : wallet.connected + ? "connected" + : "disconnected"} +

+

wallet address: {wallet.account?.address}

+

current network: {wallet.chain?.name}

+

+ wallet balance:{" "} + {formatSUI(balance ?? 0, { + withAbbr: false, + })}{" "} + SUI +

+

+ wallet publicKey: {uint8arrayToHex(wallet.account?.publicKey)} +

+
+
+ +
+
+ )} +
+
+ ); +} + +export default App; diff --git a/mover/Geek-zlk/code/task6_sdk/src/index.css b/mover/Geek-zlk/code/task6_sdk/src/index.css new file mode 100644 index 000000000..41bc8b138 --- /dev/null +++ b/mover/Geek-zlk/code/task6_sdk/src/index.css @@ -0,0 +1,64 @@ +:root { + font-family: Inter, Avenir, Helvetica, Arial, sans-serif; + font-size: 16px; + line-height: 24px; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} + + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/mover/Geek-zlk/code/task6_sdk/src/main.tsx b/mover/Geek-zlk/code/task6_sdk/src/main.tsx new file mode 100644 index 000000000..b27eb00c3 --- /dev/null +++ b/mover/Geek-zlk/code/task6_sdk/src/main.tsx @@ -0,0 +1,16 @@ +import React from 'react' +import ReactDOM from 'react-dom/client' +import App from './App' +import './index.css' + +import { + WalletProvider, +} from '@suiet/wallet-kit'; + +ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( + + + + + +) diff --git a/mover/Geek-zlk/readme.md b/mover/Geek-zlk/readme.md index dea7d16de..7ea526f31 100644 --- a/mover/Geek-zlk/readme.md +++ b/mover/Geek-zlk/readme.md @@ -44,7 +44,7 @@ - [x] call swap CoinB-> CoinA hash : 7mCeLNCEtrrp1kihrmnaoBqcRhHDa9HbbZhnf9BqATcf ## 06 Dapp-kit SDK PTB -- [] save hash : +- [x] save hash : 84JWFrKa1woNASYskNgysb6woRXs8WCYkRcgbJ3JWVRj ## 07 Move CTF Check In - [] CLI call 截图 : ![截图](./images/你的图片地址)