Skip to content

Instantly share code, notes, and snippets.

@bartubozkurt
Last active February 1, 2023 17:25
Show Gist options
  • Save bartubozkurt/f72df64bc342058391bc718a705d6b86 to your computer and use it in GitHub Desktop.
Save bartubozkurt/f72df64bc342058391bc718a705d6b86 to your computer and use it in GitHub Desktop.
/* Bad */
function badWithdraw(uint amounts) public {
require(amounts <= balances[msg.sender]); // 1.π—–π—›π—˜π—–π—ž
(bool success, bytes memory data)
= msg.sender.call{value: amounts}(""); // 3.π—œπ—‘π—§π—˜π—₯π—”π—–π—§π—œπ—’π—‘
require(success);
balances[msg.sender] - amounts; // 2.π—˜π—™π—™π—˜π—–π—§π—¦
}
/* Better */
function goodWithdraw(uint amounts) public {
require(amounts <= balances[msg.sender]); // 1.π—–π—›π—˜π—–π—ž
balances[msg.sender] - amounts; // 2.π—˜π—™π—™π—˜π—–π—§π—¦
(bool success, bytes memory data)
= msg.sender.call{value: amounts}(""); // 3.π—œπ—‘π—§π—˜π—₯π—”π—–π—§π—œπ—’π—‘
require(success);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment