@@ -15,6 +15,106 @@ timezone: UTC+8
1515## Notes
1616
1717<!-- Content_START -->
18+ # 2025-08-19
19+
20+ # My First Dapp
21+ ## 建立專案
22+ 打開 VS Code 的終端機,輸入:
23+ ``` bash
24+ # 建立 Next.js 專案
25+ npx create-next-app@latest message-board-dapp
26+ cd message-board-dapp
27+
28+ # 安裝 web3 工具
29+ npm install ethers
30+ ```
31+
32+ ## 準備 ABI(合約介面) & 合約地址
33+ 新增一個 lib 資料夾,在 lib/ 裡新建
34+ 1 . contract.js:
35+ ``` js
36+ // lib/contract.js
37+ import abi from " ./MessageBoardABI.json" ;
38+
39+ // 你部署到 Sepolia 的合約地址
40+ export const CONTRACT_ADDRESS = " 0x你的合約地址" ;
41+
42+ // 合約 ABI
43+ export const CONTRACT_ABI = abi;
44+ ```
45+
46+ 2 . MessageBoardABI.json:
47+ 可以從 Remix 編譯後的 ABI 複製下來。
48+ ``` json
49+ [
50+ {
51+ "inputs" : [],
52+ "stateMutability" : " nonpayable" ,
53+ "type" : " constructor"
54+ },
55+ {
56+ "anonymous" : false ,
57+ "inputs" : [
58+ {
59+ "indexed" : true ,
60+ "internalType" : " address" ,
61+ "name" : " sender" ,
62+ "type" : " address"
63+ },
64+ {
65+ "indexed" : false ,
66+ "internalType" : " string" ,
67+ "name" : " message" ,
68+ "type" : " string"
69+ }
70+ ],
71+ "name" : " NewMessage" ,
72+ "type" : " event"
73+ },
74+ ...
75+ ]
76+ ```
77+
78+ ## 連接 MetaMask(Wallet Connection)
79+ ``` solidity=
80+ "use client";
81+
82+ import { useState } from "react";
83+
84+ export default function Home() {
85+ const [account, setAccount] = useState<string | null>(null);
86+
87+ const connectWallet = async () => {
88+ if (!window.ethereum) {
89+ alert("請先安裝 MetaMask");
90+ return;
91+ }
92+
93+ try {
94+ const accounts: string[] = await window.ethereum.request({
95+ method: "eth_requestAccounts",
96+ });
97+ setAccount(accounts[0]);
98+ console.log("Connected account:", accounts[0]);
99+ } catch (err) {
100+ console.error(err);
101+ }
102+ };
103+
104+ return (
105+ <div style={{ padding: "2rem" }}>
106+ <h1>Message Board DApp</h1>
107+ {account ? (
108+ <p>已連接錢包: {account}</p>
109+ ) : (
110+ <button onClick={connectWallet}>Connect Wallet</button>
111+ )}
112+ </div>
113+ );
114+ }
115+ ```
116+ 待補
117+
18118# 2025-08-18
19119
20120### 整數溢出 (Integer Overflow / Underflow)
0 commit comments