Skip to content

Instantly share code, notes, and snippets.

@MCarlomagno
Last active March 21, 2022 00:30
Show Gist options
  • Save MCarlomagno/22a5eded98fd637011c38700e686328d to your computer and use it in GitHub Desktop.
Save MCarlomagno/22a5eded98fd637011c38700e686328d to your computer and use it in GitHub Desktop.
pragma solidity >=0.7.0 <0.9.0;
contract Chat {
// ...
// We can use an autoincremental id for each new message.
uint lastMessageId;
function sendMessage(string _text) public {
// increments id and sends message.
lastMessageId++;
sendMessageEvent(lastMessageId, msg.sender, _text);
}
}
@ntourne
Copy link

ntourne commented Mar 20, 2022

I don't understand one thing:
When sendMessage() function is called then the lastMessageId is incremented which I suppose it's used by sendMessageEvent event. But instead a different value (messageIdCounter) is used.

BTW - I got this code from the Blockchain chat server application written in Solidity article. Thanks for sharing!

@MCarlomagno
Copy link
Author

Hi Nicolas! Yes, you are totally right, I refactored the variable name and I forgot to replace it in that line. Anyway, the full function code seems to be right. Thanks a lot for the correction :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment