Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
How to be aware of mapping updates so then i can update my client app?

Here is my smart contract for a todo list.

Everything works, i've made a front-end to test it.

But, i'm trying to get real time updates when a task is removed, added or updated.

I know there are events for that, but how can i use them with a mapping data type?

Anyone could help me ?

Thanks !

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
pragma experimental ABIEncoderV2;

contract ToDoList {
    constructor() public {
        addTask("Tache initiale");
    }

    struct Task {
        uint256 id;
        string description;
        bool completed;
    }

    mapping(address => Task[]) public tasks;

    function addTask(string memory _description) public {
        uint256 id = uint256(
            keccak256(abi.encodePacked(msg.sender, _description))
        );

        tasks[msg.sender].push(
            Task({id: id, description: _description, completed: false})
        );
    }

    function changeTaskStatus(uint256 _id, bool _status) public {
        for (uint256 i = 0; i < tasks[msg.sender].length; i++) {
            if (tasks[msg.sender][i].id == _id) {
                tasks[msg.sender][i].completed = _status;
            }
        }
    }

    function removeTask(uint256 _id) public {
        for (uint256 i = 0; i < tasks[msg.sender].length; i++) {
            if (tasks[msg.sender][i].id == _id) {
                delete tasks[msg.sender][i];
            }
        }
    }

    function getTasks() public view returns (Task[] memory) {
        return tasks[msg.sender];
    }

    function tasksCount() public view returns (uint256) {
        return tasks[msg.sender].length;
    }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are not emitting any events.You should create appropiate events and emit them properly. For example:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
pragma experimental ABIEncoderV2;

contract ToDoList {
    ...
   mapping(address => Task[]) public tasks;

    event AddTask(address indexed creator, uint256 indexed id, string description);

    function addTask(string memory _description) public {
        uint256 id = uint256(
            keccak256(abi.encodePacked(msg.sender, _description))
        );

        tasks[msg.sender].push(
            Task({id: id, description: _description, completed: false})
        );

        emit AddTask(msg.sender,id,_description);
    }

...
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!