ReentrancyGuardUpgradeable.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
  3. pragma solidity ^0.8.0;
  4. import "../proxy/utils/Initializable.sol";
  5. /**
  6. * @dev Contract module that helps prevent reentrant calls to a function.
  7. *
  8. * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
  9. * available, which can be applied to functions to make sure there are no nested
  10. * (reentrant) calls to them.
  11. *
  12. * Note that because there is a single `nonReentrant` guard, functions marked as
  13. * `nonReentrant` may not call one another. This can be worked around by making
  14. * those functions `private`, and then adding `external` `nonReentrant` entry
  15. * points to them.
  16. *
  17. * TIP: If you would like to learn more about reentrancy and alternative ways
  18. * to protect against it, check out our blog post
  19. * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
  20. */
  21. abstract contract ReentrancyGuardUpgradeable is Initializable {
  22. // Booleans are more expensive than uint256 or any type that takes up a full
  23. // word because each write operation emits an extra SLOAD to first read the
  24. // slot's contents, replace the bits taken up by the boolean, and then write
  25. // back. This is the compiler's defense against contract upgrades and
  26. // pointer aliasing, and it cannot be disabled.
  27. // The values being non-zero value makes deployment a bit more expensive,
  28. // but in exchange the refund on every call to nonReentrant will be lower in
  29. // amount. Since refunds are capped to a percentage of the total
  30. // transaction's gas, it is best to keep them low in cases like this one, to
  31. // increase the likelihood of the full refund coming into effect.
  32. uint256 private constant _NOT_ENTERED = 1;
  33. uint256 private constant _ENTERED = 2;
  34. uint256 private _status;
  35. function __ReentrancyGuard_init() internal onlyInitializing {
  36. __ReentrancyGuard_init_unchained();
  37. }
  38. function __ReentrancyGuard_init_unchained() internal onlyInitializing {
  39. _status = _NOT_ENTERED;
  40. }
  41. /**
  42. * @dev Prevents a contract from calling itself, directly or indirectly.
  43. * Calling a `nonReentrant` function from another `nonReentrant`
  44. * function is not supported. It is possible to prevent this from happening
  45. * by making the `nonReentrant` function external, and making it call a
  46. * `private` function that does the actual work.
  47. */
  48. modifier nonReentrant() {
  49. // On the first call to nonReentrant, _notEntered will be true
  50. require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
  51. // Any calls to nonReentrant after this point will fail
  52. _status = _ENTERED;
  53. _;
  54. // By storing the original value once again, a refund is triggered (see
  55. // https://eips.ethereum.org/EIPS/eip-2200)
  56. _status = _NOT_ENTERED;
  57. }
  58. /**
  59. * This empty reserved space is put in place to allow future versions to add new
  60. * variables without shifting down storage in the inheritance chain.
  61. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  62. */
  63. uint256[49] private __gap;
  64. }