Security

PullPayment

  • A pattern that can be used to avoid reentrancy attacks

ReentrancyGuard

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

contract FundMe is ReentrancyGuard {
...

    function withdraw() external payable onlyOwner nonReentrant {
...

Pausable

  • A common emergency response mechanism that can pause functionality while a remediation is pending

Constructor

  • Older versions of Solidity didn't have the constructor keyword

  • It took the function with EXACTLY the same name as the contract and used that as the constructor

  • So... if you got the name wrong even a little bit, the constructor wouldn't run and become accessible to anyone

Self Destruct

  • If you send funds then immediately call selfdestruct() then the contract you call could try to revert and send the funds back, but it can't since you selfdestructed, so it keeps the funds, but doesn't continue with the code past the revert point

  • If you code your contract badly, this could brick the function (e.g. setting a winner after checking the current balance)

  • Therefore, use a counter that is part of the function, instead of address(this).balance so that the code is executed as expected during revert

  • So instead, use a global balance variable to keep track of the funds, not just the contract balance

Last updated