Site icon 2k1

Xây dựng todo app với smartcontract

Todo app web3

Cài đặt môi trường

node 14.19.2

$ node -v
v14.19.2

ganache desktop
https://trufflesuite.com/ganache/

Hardhat

Biên dịch contract

yarn compile

Deploy contract local

yarn deploy:local

Tạo typechain cho web3

yarn compile:web3

Contract

Todo.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TodoList {
    uint256 public taskCount = 0;
    struct Task {
        uint256 id;
        string content;
        bool completed;
    }

    mapping(uint256 => Task) public tasks;
    event TaskAdded(uint256 id, string content);

    constructor() {
        createTask("Learn smartcontract");
    }

    // function for create task
    function createTask(string memory _content) public {
        taskCount++;
        tasks[taskCount] = Task(taskCount, _content, false);
        emit TaskAdded(taskCount, _content);
    }

    function getTask(uint256 _id) public view returns (Task memory) {
        return tasks[_id];
    }

    function completeTask(uint256 _id) public {
        tasks[_id].completed = true;
    }
}

Đây là một smart contract đơn giản viết trên nền tảng Ethereum bằng ngôn ngữ Solidity. Smart contract này định nghĩa một ứng dụng Todo List đơn giản, cho phép người dùng tạo mới một task, xem nội dung của task, và đánh dấu task đã hoàn thành.

Các thành phần chính của smart contract:

Sourcecode

npv2k1/web3-todo-app: Learn and develop a simple web3 todo app (github.com)

Exit mobile version