Skip to content

Instantly share code, notes, and snippets.

@jongan69
Created January 30, 2023 05:22
Show Gist options
  • Save jongan69/544c0a143340b9b513af7acdcb3e5b52 to your computer and use it in GitHub Desktop.
Save jongan69/544c0a143340b9b513af7acdcb3e5b52 to your computer and use it in GitHub Desktop.
A smart contract for managing todo tasks

TaskContract

A smart contract for a Todo App on the Ethereum blockchain.

Overview

TaskContract is a smart contract for a Todo App on the Ethereum blockchain. It allows users to add tasks and delete tasks. It also allows users to view their own tasks.

Functions

TaskContract has the following functions:

  • addTask(string memory taskText, bool isDeleted) - adds a new task to the contract.
  • getMyTasks() - returns an array of tasks that belong to the user.
  • deleteTasks(uint256 taskId, bool isDeleted) - deletes a task from the contract.

Events

TaskContract emits the following events:

  • AddTask(address recipient, uint256 taskId) - emitted when a task is added.
  • DeleteTask(uint256 taskId, bool isDeleted) - emitted when a task is deleted.
// Todo App Task Contract
pragma solidity ^0.8.17;
contract TaskContract {
event AddTask(address recipient, uint256 taskId);
event DeleteTask(uint256 taskId, bool isDeleted);
struct Task {
uint256 id;
string taskText;
bool isDeleted;
}
Task[] private tasks;
mapping(uint256 => address) taskToOwner;
function addTask(string memory taskText, bool isDeleted) external {
uint256 taskId = tasks.length;
tasks.push(Task(taskId, taskText, isDeleted));
taskToOwner[taskId] = msg.sender;
emit AddTask(msg.sender, taskId);
}
function getMyTasks() external view returns (Task[] memory) {
Task[] memory temporary = new Task[](tasks.length);
uint256 counter = 0;
for (uint256 i = 0; i < tasks.length; i++) {
if (taskToOwner[i] == msg.sender && tasks[i].isDeleted == false) {
temporary[counter] = tasks[i];
counter++;
}
}
Task[] memory result = new Task[](counter);
for (uint256 i = 0; i < counter; i++) {
result[i] = temporary[i];
}
return result;
}
function deleteTasks(uint256 taskId, bool isDeleted) external {
if (taskToOwner[taskId] == msg.sender) {
tasks[taskId].isDeleted = isDeleted;
emit DeleteTask(taskId, isDeleted);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment