ReentrancyGuard.sol 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev Contract module that helps prevent reentrant calls to a function.
  6. *
  7. * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
  8. * available, which can be applied to functions to make sure there are no nested
  9. * (reentrant) calls to them.
  10. *
  11. * Note that because there is a single `nonReentrant` guard, functions marked as
  12. * `nonReentrant` may not call one another. This can be worked around by making
  13. * those functions `private`, and then adding `external` `nonReentrant` entry
  14. * points to them.
  15. *
  16. * TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
  17. * consider using {ReentrancyGuardTransient} instead.
  18. *
  19. * TIP: If you would like to learn more about reentrancy and alternative ways
  20. * to protect against it, check out our blog post
  21. * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
  22. */
  23. abstract contract ReentrancyGuard {
  24. // Booleans are more expensive than uint256 or any type that takes up a full
  25. // word because each write operation emits an extra SLOAD to first read the
  26. // slot's contents, replace the bits taken up by the boolean, and then write
  27. // back. This is the compiler's defense against contract upgrades and
  28. // pointer aliasing, and it cannot be disabled.
  29. // The values being non-zero value makes deployment a bit more expensive,
  30. // but in exchange the refund on every call to nonReentrant will be lower in
  31. // amount. Since refunds are capped to a percentage of the total
  32. // transaction's gas, it is best to keep them low in cases like this one, to
  33. // increase the likelihood of the full refund coming into effect.
  34. uint256 private constant NOT_ENTERED = 1;
  35. uint256 private constant ENTERED = 2;
  36. uint256 private _status;
  37. /**
  38. * @dev Unauthorized reentrant call.
  39. */
  40. error ReentrancyGuardReentrantCall();
  41. constructor() {
  42. _status = NOT_ENTERED;
  43. }
  44. /**
  45. * @dev Prevents a contract from calling itself, directly or indirectly.
  46. * Calling a `nonReentrant` function from another `nonReentrant`
  47. * function is not supported. It is possible to prevent this from happening
  48. * by making the `nonReentrant` function external, and making it call a
  49. * `private` function that does the actual work.
  50. */
  51. modifier nonReentrant() {
  52. _nonReentrantBefore();
  53. _;
  54. _nonReentrantAfter();
  55. }
  56. function _nonReentrantBefore() private {
  57. // On the first call to nonReentrant, _status will be NOT_ENTERED
  58. if (_status == ENTERED) {
  59. revert ReentrancyGuardReentrantCall();
  60. }
  61. // Any calls to nonReentrant after this point will fail
  62. _status = ENTERED;
  63. }
  64. function _nonReentrantAfter() private {
  65. // By storing the original value once again, a refund is triggered (see
  66. // https://eips.ethereum.org/EIPS/eip-2200)
  67. _status = NOT_ENTERED;
  68. }
  69. /**
  70. * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
  71. * `nonReentrant` function in the call stack.
  72. */
  73. function _reentrancyGuardEntered() internal view returns (bool) {
  74. return _status == ENTERED;
  75. }
  76. }