Panic.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.1) (utils/Panic.sol)
  3. pragma solidity ^0.8.20;
  4. /**
  5. * @dev Helper library for emitting standardized panic codes.
  6. *
  7. * ```solidity
  8. * contract Example {
  9. * using Panic for uint256;
  10. *
  11. * // Use any of the declared internal constants
  12. * function foo() { Panic.GENERIC.panic(); }
  13. *
  14. * // Alternatively
  15. * function foo() { Panic.panic(Panic.GENERIC); }
  16. * }
  17. * ```
  18. *
  19. * Follows the list from https://github.com/ethereum/solidity/blob/v0.8.24/libsolutil/ErrorCodes.h[libsolutil].
  20. *
  21. * _Available since v5.1._
  22. */
  23. // slither-disable-next-line unused-state
  24. library Panic {
  25. /// @dev generic / unspecified error
  26. uint256 internal constant GENERIC = 0x00;
  27. /// @dev used by the assert() builtin
  28. uint256 internal constant ASSERT = 0x01;
  29. /// @dev arithmetic underflow or overflow
  30. uint256 internal constant UNDER_OVERFLOW = 0x11;
  31. /// @dev division or modulo by zero
  32. uint256 internal constant DIVISION_BY_ZERO = 0x12;
  33. /// @dev enum conversion error
  34. uint256 internal constant ENUM_CONVERSION_ERROR = 0x21;
  35. /// @dev invalid encoding in storage
  36. uint256 internal constant STORAGE_ENCODING_ERROR = 0x22;
  37. /// @dev empty array pop
  38. uint256 internal constant EMPTY_ARRAY_POP = 0x31;
  39. /// @dev array out of bounds access
  40. uint256 internal constant ARRAY_OUT_OF_BOUNDS = 0x32;
  41. /// @dev resource error (too large allocation or too large array)
  42. uint256 internal constant RESOURCE_ERROR = 0x41;
  43. /// @dev calling invalid internal function
  44. uint256 internal constant INVALID_INTERNAL_FUNCTION = 0x51;
  45. /// @dev Reverts with a panic code. Recommended to use with
  46. /// the internal constants with predefined codes.
  47. function panic(uint256 code) internal pure {
  48. assembly ("memory-safe") {
  49. mstore(0x00, 0x4e487b71)
  50. mstore(0x20, code)
  51. revert(0x1c, 0x24)
  52. }
  53. }
  54. }