@@ -15,6 +15,105 @@ INFJ,珠海,数据科学在读,目前在学习ai agent,ai应用开发,
1515## Notes
1616
1717<!-- Content_START -->
18+ # 2025-08-16
19+
20+ ### 简单Dapp开发
21+ 参考课程地址:https://www.youtube.com/watch?v=awQTDVvYyjI
22+ 课程名称:build a Todo App with Solidity, nextjs, Truffle
23+ 该项目主要采用Truffle,考虑后面为适应市场改用为hardhat
24+
25+ 前端已简单生成主要是写后端的智能合约
26+ ``` solidity
27+ // SPDX-License-Identifier: MIT
28+ pragma solidity ^0.8.15;
29+
30+ // task:{ id: 0, taskText: 'clean', isDeleted: false },
31+ // DEPLOYED Contract Address
32+ // ABI
33+
34+
35+ contract TaskContract {
36+ event AddTask(address recipient, uint taskId);
37+ event DeleteTask(uint taskId, bool isDeleted);
38+
39+ struct Task {
40+ uint id;
41+ string taskText;
42+ bool isDeleted;
43+ }
44+
45+ Task[] private tasks;
46+ mapping(uint256 => address) taskToOwner;
47+ // {0: '0x123124124...'}
48+ // {1: '0xabc124124...'}
49+
50+ function addTask(string memory taskText, bool isDeleted) external {
51+ uint taskId = tasks.length;
52+ tasks.push(Task(taskId, taskText, isDeleted));
53+ taskToOwner[taskId] = msg.sender;
54+ emit AddTask(msg.sender, taskId);
55+ }
56+
57+ // get tasks that are mine and not deleted
58+ function getMyTasks() external view returns (Task[] memory) {
59+ Task[] memory temporary = new Task[](tasks.length);
60+ uint counter = 0;
61+
62+ // temporary [{taskText:"hello", isDeleted:false}, empty]
63+ for (uint i=0; i < tasks.length; i++) {
64+ if (taskToOwner[i] == msg.sender && tasks[i].isDeleted == false) {
65+ temporary[counter] = tasks[i];
66+ counter++;
67+ }
68+ }
69+
70+ // Resize the temporary array to fit the actual number of tasks
71+ Task[] memory result = new Task[](counter);
72+ for (uint i = 0; i < counter; i++) {
73+ result[i] = temporary[i];
74+ }
75+ return result;
76+ }
77+
78+ function deleteTask(uint taskId, bool isDeleted) external {
79+ if(taskToOwner[taskId] == msg.sender){
80+ tasks[taskId].isDeleted = isDeleted;
81+ emit DeleteTask(taskId, isDeleted);
82+ }
83+ }
84+
85+ }
86+
87+ ```
88+ 此课程为2022年,truffle配置
89+ 需要在先安装truffle
90+ ``` bash
91+ npm install truffle -g
92+ ```
93+ 安装后,在项目根目录下执行
94+ ``` bash
95+ truffle init
96+ ```
97+ 在contract文件夹下创建TaskContract.sol文件,将合约代码复制进去
98+ 在migrations文件夹下创建1_initial_migration.js文件,内容如下
99+ ``` javascript
100+ const TaskContract = artifacts .require (" TaskContract" );
101+
102+ module .exports = function (deployer ) {
103+ deployer .deploy (TaskContract);
104+ };
105+ ```
106+ 再在命令行中输入
107+ ``` bash
108+ truffle dashboard
109+ ```
110+ 在其中进行钱包连接
111+ 然后用命令
112+ ``` bash
113+ truffle migrate -- network
114+ ```
115+ 通过钱包确认部署,但是需要ETH的gas费,在想可不可以用测试币?
116+
18117# 2025-08-15
19118
20119## 运营向分享会
0 commit comments