Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda