@@ -15,6 +15,182 @@ timezone: UTC+8
1515## Notes
1616
1717<!-- Content_START -->
18+ # 2025-08-15
19+
20+ ** 今天继续编写智能合约及其交互代码:**
21+
22+ * Transaction.sol*
23+ ~~~ rust
24+ // SPDX-License-Identifier: MIT
25+ pragma solidity ^ 0.8 . 0 ;
26+
27+ contract Transactions {
28+ uint256 transactionCount;
29+
30+ event Transfer (address from , address receiver , uint amount , string message , uint256 timestamp , string keyword );
31+
32+ struct TransferStruct {
33+ address sender ;
34+ address receiver ;
35+ uint amount ;
36+ string message ;
37+ uint256 timestamp ;
38+ string keyword ;
39+ }
40+
41+ TransferStruct [] transactions ;
42+
43+ function addToBlockchain (address payable receiver , uint amount , string memory message , string memory keyword ) public {
44+ transactionCount += 1 ;
45+ transactions . push (TransferStruct (msg . sender, receiver , amount , message , block . timestamp, keyword ));
46+
47+ emit Transfer (msg . sender, receiver , amount , message , block . timestamp, keyword );
48+ }
49+
50+ function getAllTransactions () public view returns (TransferStruct [] memory ) {
51+ return transactions ;
52+ }
53+
54+ function getTransactionCount () public view returns (uint256 ) {
55+ return transactionCount;
56+ }
57+
58+
59+ }
60+ ~~~
61+ * TransactionContext.tsx*
62+ ~~~ typescript
63+ import React , { useEffect , useState , type ReactNode } from " react" ;
64+ import { ethers } from " ethers" ;
65+
66+ import { contractAddress , contractABI } from " ../utils/constants" ;
67+
68+ export const TransactionContext = React .createContext <any >(null );
69+
70+ // 扩展Window接口
71+ declare global {
72+ interface Window {
73+ ethereum: any ;
74+ }
75+ }
76+
77+ const { ethereum } = window ;
78+
79+ const getEthereumContract = async () => {
80+ const provider = new ethers .BrowserProvider (ethereum );
81+ const signer = await provider .getSigner ();
82+ const transactionContract = new ethers .Contract (
83+ contractAddress ,
84+ contractABI ,
85+ signer
86+ );
87+
88+ return transactionContract ;
89+ };
90+
91+ interface TransactionProviderProps {
92+ children: ReactNode ;
93+ }
94+ export const TransactionProvider = ({ children }: TransactionProviderProps ) => {
95+ const [currentAccount, setCurrentAccount] = useState (" " );
96+ const [formData, setFormData] = useState ({
97+ addressTo: " " ,
98+ amount: " " ,
99+ keyword: " " ,
100+ message: " " ,
101+ });
102+ const [isLoading, setIsLoading] = useState (false );
103+ const [transactionCount, setTransactionCount] = useState (localStorage .getItem (" transactionCount" ));
104+
105+ const handleChange = (e : { target: { value: any ; }; }, name : any ) => {
106+ setFormData ((prevState ) => ({
107+ ... prevState ,
108+ [name ]: e .target .value ,
109+ }));
110+ };
111+ const checkIfWalletIsConnected = async () => {
112+ try {
113+ if (! ethereum ) {
114+ return alert (" Make sure you have metamask!" );
115+ }
116+ const accounts = await ethereum .request ({ method: " eth_accounts" });
117+ if (accounts .length ) {
118+ setCurrentAccount (accounts [0 ]);
119+ } else {
120+ console .log (" No accounts found" );
121+ }
122+ } catch (error ) {
123+ console .log (error );
124+ throw new Error (" No ethereum object" );
125+ }
126+
127+ };
128+
129+ const connectWallet = async () => {
130+ try {
131+ if (! ethereum ) return alert (" Please install metamask!" );
132+ const accounts = await ethereum .request ({ method: " eth_requestAccounts" });
133+ setCurrentAccount (accounts [0 ]);
134+
135+ } catch (error ) {
136+ console .log (error );
137+ throw new Error (" No ethereum object" );
138+ }
139+ };
140+
141+
142+ const sendTransaction = async () => {
143+ try {
144+ if (! ethereum ) return alert (" Please install metamask!" );
145+ const { addressTo, amount, keyword, message } = formData ;
146+ const transactionContract = await getEthereumContract ();
147+ const parsedAmount = ethers .parseEther (amount );
148+
149+ await ethereum .request ({
150+ method: " eth_sendTransaction" ,
151+ params: [
152+ {
153+ from: currentAccount ,
154+ to: addressTo ,
155+ gas: " 0x5208" ,
156+ value: parsedAmount ,
157+ },
158+ ],
159+ });
160+
161+ const transactionHash = await transactionContract .addToBlockchain (
162+ addressTo ,
163+ amount ,
164+ keyword ,
165+ message
166+ );
167+
168+ setIsLoading (true );
169+ console .log (` Loading - ${transactionHash .hash } ` );
170+ await transactionHash .wait ();
171+ setIsLoading (false );
172+ console .log (` Success ${transactionHash .hash } ` );
173+
174+ const transactionCount = await transactionContract .getTransactionCount ();
175+ setTransactionCount (transactionCount .toNumber ());
176+ } catch (error ) {
177+ console .log (error );
178+ throw new Error (" No ethereum object" );
179+ }
180+ };
181+
182+ useEffect (() => {
183+ checkIfWalletIsConnected ();
184+ }, []);
185+
186+ return (
187+ < TransactionContext .Provider value = {{ connectWallet , currentAccount , formData , setFormData , handleChange , sendTransaction }}>
188+ {children }
189+ < / TransactionContext .Provider >
190+ );
191+ };
192+ ~~~
193+
18194# 2025-08-14
19195
20196今日继续根据视频教程写实用Dapp的智能合约代码,并且看了肖臻老师的《区块链技术与应用》的“BTC-密码学原理”,“BTC-数据结构”和“BTC-协议”的讲解视频
0 commit comments