Setup

Khởi tạo Truffle project:

truffle init

Tạo contract TodoList.sol và compile:

truffle compile

Viết file migration:

var TodoList = artifacts.require("../contracts/TodoList.sol")
 
module.exports = function(deployer) {
	deployer.deploy(TodoList)
}

Cấu hình network (bật Ganache lên trước):

development: {
	host: "127.0.0.1",     // Localhost (default: none)
	port: 7545,            // Standard Ethereum port (default: none)
	network_id: "*",       // Any network (default: none)
},

Deploy:

truffle deploy

Create Task

Khởi tạo contract:

contract TodoList {
	uint public taskCount = 0;
	
	struct Task {
		uint id;
		string content;
		bool completed;
	}
	
	Task[] public tasks;
}

Khởi tạo hàm tạo task:

function createTask(string memory _content) public {
	Task memory newTask = Task(taskCount, _content, false);
 
	tasks.push(newTask);
 
	emit TaskCreated(taskCount, _content, false);
 
	taskCount++;
}

Event:

event TaskCreated(uint id, string content, bool completed);

Constructor:

constructor() {
	createTask("Do exercises!");
}

Deploy lại.

Mark as Completed

Hàm đánh dấu task đã hoàn thành:

 function toggleCompleted(uint _id) public {
	Task memory task = tasks[_id];
 
	task.completed = !task.completed;
 
	tasks[_id] = task;
 
	emit TaskCompleted(_id, task.completed);
}

Event:

event TaskCompleted(uint id, bool completed);

Deploy

Interaction

Viết code để tương tác với contract thông qua Ethers.

Tương tác thông qua giao diện và sửa lỗi nếu có.

Sepolia

Cấu hình để deploy lên Sepolia testnet.

Tương tác thông qua giao diện và sửa lỗi nếu có.