Deploy a Decentralized To-Do List on Conflux eSpace Testnet (Step-by-Step)

Building a Decentralized To-Do List with Solidity on Conflux eSpace Testnet

In the world of blockchain, decentralization opens up exciting possibilities for everyday applications. One such innovation is a decentralized to-do list, where tasks are stored immutably on the blockchain, accessible only to their creators. In this blog, we’ll explore how to create and deploy a DecentralizedTodoList smart contract using Solidity, deploy it on the Conflux eSpace testnet using Remix, and interact with it step-by-step.


What is a Decentralized To-Do List?

A decentralized to-do list leverages blockchain technology to ensure that tasks are stored securely and transparently. Unlike traditional apps, where data is centralized on a server, this smart contract allows users to add, complete, and delete tasks using their Ethereum-compatible wallet addresses. The contract we’ll build is deployed on the Conflux eSpace testnet, a fast and scalable blockchain network, making it an ideal playground for developers.


The Smart Contract Code

The DecentralizedTodoList contract is written in Solidity (version 0.8.0) under the MIT license. It uses a Task struct to define each task with a description and a completion status. Tasks are mapped to the user’s address, ensuring privacy and ownership.

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

contract DecentralizedTodoList {
    // Define a struct for each task
    struct Task {
        string description;
        bool isCompleted;
    }

    // Mapping from user address to their list of tasks
    mapping(address => Task[]) private userTasks;

    // Event emitted when a task is added
    event TaskAdded(address indexed user, uint256 taskId, string description);

    // Event emitted when a task is completed
    event TaskCompleted(address indexed user, uint256 taskId);

    // Event emitted when a task is deleted
    event TaskDeleted(address indexed user, uint256 taskId);

    // Function to add a new task
    function addTask(string memory _description) public {
        uint256 taskId = userTasks[msg.sender].length;
        userTasks[msg.sender].push(Task({description: _description, isCompleted: false}));
        emit TaskAdded(msg.sender, taskId, _description);
    }

    // Function to get the number of tasks for the caller
    function getTaskCount() public view returns (uint256) {
        return userTasks[msg.sender].length;
    }

    // Function to get details of a specific task by index
    function getTask(uint256 _index) public view returns (string memory description, bool isCompleted) {
        require(_index < userTasks[msg.sender].length, "Task index out of bounds");
        Task memory task = userTasks[msg.sender][_index];
        return (task.description, task.isCompleted);
    }

    // Function to mark a task as completed
    function completeTask(uint256 _index) public {
        require(_index < userTasks[msg.sender].length, "Task index out of bounds");
        userTasks[msg.sender][_index].isCompleted = true;
        emit TaskCompleted(msg.sender, _index);
    }

    // Function to delete a task (swap with last and pop to avoid gaps)
    function deleteTask(uint256 _index) public {
        require(_index < userTasks[msg.sender].length, "Task index out of bounds");
        uint256 lastIndex = userTasks[msg.sender].length - 1;
        if (_index != lastIndex) {
            userTasks[msg.sender][_index] = userTasks[msg.sender][lastIndex];
        }
        userTasks[msg.sender].pop();
        emit TaskDeleted(msg.sender, _index);
    }
} 

Key Features

  • Adding a Task: The addTask function lets users create a new task by providing a description. It assigns a unique ID based on the task array length and emits a TaskAdded event.
  • Task Management: Functions like getTaskCount, getTask, completeTask, and deleteTask allow users to check their task total, view details, mark tasks as done, and remove them efficiently by swapping with the last task.
  • Safety Features: Bounds checks prevent out-of-bounds errors, and events (TaskCompleted, TaskDeleted) track all actions for transparency.

This design ensures a secure and user-friendly task management system on the blockchain.


Deploying on Conflux eSpace Testnet with Remix

For this project, I used Remix, a popular online IDE for Ethereum smart contract development, to compile and deploy the contract. Here’s the process:

  1. Connect MetaMask: Connect your MetaMask wallet to the Conflux eSpace testnet and acquire CFX test tokens from the faucet.
  2. Open Remix: Go to remix.ethereum.org, paste the contract code, and compile it.
  3. Deploy: Configure the environment to Conflux eSpace testnet, deploy the contract, and confirm the transaction. Once deployed, you’ll receive the contract address.

Interacting with the Contract

Let’s walk through a demo using Remix:

  • Add a Task: Submit a transaction to add a task, like “Conflux Hackathon Project.” The task is now stored on the blockchain.
  • Check Task Count: After adding one task, the count is 1, confirming the addition.
  • Retrieve Task Details: Use index 0 to fetch the description (“Conflux Hackathon Project”) and initial status (false).
  • Mark as Completed: Call completeTask with index 0, and the status updates to true.
  • Delete Task: Use deleteTask to remove the task, and the count drops to 0.

This hands-on process showcases the contract’s functionality in real-time.


Why Use Conflux eSpace Testnet?

The Conflux eSpace testnet offers a robust environment for testing smart contracts with low costs and high performance. It’s an excellent choice for experimenting with decentralized applications before moving to mainnet.


Getting Started

To replicate this project:

  1. Install MetaMask and set up access to the Conflux eSpace testnet.
  2. Use Remix (remix.ethereum.org) to compile and deploy the contract.
  3. Follow the demo steps above to interact with your deployed contract.

Conclusion

Building a decentralized to-do list is a fantastic way to dive into blockchain development. Using Remix for deployment and the Conflux eSpace testnet’s capabilities, this project offers a practical example of decentralized app development. Whether you’re a beginner or an experienced developer, this tutorial provides a solid foundation to explore more complex dApps.

Video tutorial: https://youtu.be/QLxsumrcCuM?si=wBqgnY_czBu57NNt

GitHub repo: https://github.com/Vikash-8090-Yadav/Conflux-Tutorial/tree/main/TodoList

Stay tuned for more blockchain tutorials, and feel free to share your own decentralized projects in the comments!

Happy coding!

1 Like