File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,6 +15,45 @@ INFJ,珠海,数据科学在读,目前在学习ai agent,ai应用开发,
1515## Notes
1616
1717<!-- Content_START -->
18+ # 2025-08-18
19+
20+ ## 前端交互通过按钮连接钱包
21+ 1、一般是先申请账户授权,再使用MetaMask的弹窗进行登陆
22+ 另外每一个chainId都是不一样的,比如sepolia的chainId是0xaa36a7,所以需要检测一下当前的网络是否是sepolia
23+ 用户端的cilent中index.js的连接钱包的代码
24+ ``` javascript
25+ const connectWallet = async () => {
26+ try {
27+ const {ethereum } = window
28+ if (! ethereum) {
29+ console .error (" MetaMask not detected" )
30+ return
31+ }
32+
33+ // 1. 先请求账户授权,MetaMask 会弹窗
34+ const accounts = await ethereum .request ({method: ' eth_requestAccounts' })
35+ setIsUserLoggedIn (true )
36+ setCurrentAccount (accounts[0 ])
37+
38+ // 2. 再检测网络
39+ let chainId = await ethereum .request ({method: ' eth_chainId' })
40+ console .log (" Connected to chain:" , chainId)
41+ const sepoliaChainId = ' 0xaa36a7'
42+ if (chainId !== sepoliaChainId) {
43+ alert (" you are not connected to the Sepolia testNet!" )
44+ setCorrectNetwork (false )
45+ return
46+ } else {
47+ setCorrectNetwork (true )
48+ }
49+
50+ console .log (" Found account:" , accounts[0 ])
51+ } catch (error) {
52+ console .error (" Error connecting wallet:" , error)
53+ }
54+ }
55+ ```
56+
1857# 2025-08-17
1958
2059## 关于truffle部署测试网(sepolia)
You can’t perform that action at this time.
0 commit comments