Possible reentrancy vulnerabilities. Avoid state changes after transfer.
I think you can do this:
function tipPost(uint _id) public payable {
//Ensure the id is not invalid
require(_id > 0 && _id <= postCount, "Valid id required");
//fetch post
Post memory _post = posts[_id];
address payable _author = _post.author; //fetch the author
address(_author).transfer(msg.value); //transfer Ether to the author//We increment the tip amount
_post.tipAmount += msg.value;
posts[_id] = _post;
// let's trigger a post tipped event
emit PostTipped(_id, _post.content, _post.tipAmount, _author );
}
to
function tipPost(uint256 _id) public payable {
require(_id > 0 && _id <= postCount, "Valid id required");
posts[_id].tipAmount += msg.value;
posts[_id].author.transfer(msg.value);
emit PostTipped(_id, posts[_id].content, posts[_id].tipAmount, posts[_id].author);
}
I am grateful for you corrections so far. I like the refactored tipPost method, it is clean and direct.
My next post on web3 project will sure be better with your corrections. Thanks man.
Let me be a bore a little more ;)
solidity ^0.8.10posts[postCount] = Post(postCount, _content, 0, msg.sender);to
posts[postCount] = Post(postCount, _content, 0, payable(msg.sender));emit PostCreated(postCount, _content, 0, msg.sender);to
emit PostCreated(postCount, _content, 0, payable(msg.sender));address(_author).transfer(msg.value);to
_author.transfer(msg.value);Possible reentrancy vulnerabilities. Avoid state changes after transfer. I think you can do this:
function tipPost(uint _id) public payable { //Ensure the id is not invalid require(_id > 0 && _id <= postCount, "Valid id required"); //fetch post Post memory _post = posts[_id]; address payable _author = _post.author; //fetch the author address(_author).transfer(msg.value); //transfer Ether to the author //We increment the tip amount _post.tipAmount += msg.value; posts[_id] = _post; // let's trigger a post tipped event emit PostTipped(_id, _post.content, _post.tipAmount, _author ); }to
function tipPost(uint256 _id) public payable { require(_id > 0 && _id <= postCount, "Valid id required"); posts[_id].tipAmount += msg.value; posts[_id].author.transfer(msg.value); emit PostTipped(_id, posts[_id].content, posts[_id].tipAmount, posts[_id].author); }