Panic.sol 1.9 KB

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