SafeCast.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const assert = require('assert');
  2. const format = require('../format-lines');
  3. const { range } = require('../../helpers');
  4. const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
  5. // Returns the version of OpenZeppelin Contracts in which a particular function was introduced.
  6. // This is used in the docs for each function.
  7. const version = (selector, length) => {
  8. switch (selector) {
  9. case 'toUint(uint)': {
  10. switch (length) {
  11. case 8:
  12. case 16:
  13. case 32:
  14. case 64:
  15. case 128:
  16. return '2.5';
  17. case 96:
  18. case 224:
  19. return '4.2';
  20. default:
  21. assert(LENGTHS.includes(length));
  22. return '4.7';
  23. }
  24. }
  25. case 'toInt(int)': {
  26. switch (length) {
  27. case 8:
  28. case 16:
  29. case 32:
  30. case 64:
  31. case 128:
  32. return '3.1';
  33. default:
  34. assert(LENGTHS.includes(length));
  35. return '4.7';
  36. }
  37. }
  38. case 'toUint(int)': {
  39. switch (length) {
  40. case 256:
  41. return '3.0';
  42. default:
  43. assert(false);
  44. return;
  45. }
  46. }
  47. case 'toInt(uint)': {
  48. switch (length) {
  49. case 256:
  50. return '3.0';
  51. default:
  52. assert(false);
  53. return;
  54. }
  55. }
  56. default:
  57. assert(false);
  58. }
  59. };
  60. const header = `\
  61. pragma solidity ^0.8.0;
  62. /**
  63. * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
  64. * checks.
  65. *
  66. * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
  67. * easily result in undesired exploitation or bugs, since developers usually
  68. * assume that overflows raise errors. \`SafeCast\` restores this intuition by
  69. * reverting the transaction when such an operation overflows.
  70. *
  71. * Using this library instead of the unchecked operations eliminates an entire
  72. * class of bugs, so it's recommended to use it always.
  73. *
  74. * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
  75. * all math on \`uint256\` and \`int256\` and then downcasting.
  76. */
  77. `;
  78. const toUintDownCast = length => `\
  79. /**
  80. * @dev Returns the downcasted uint${length} from uint256, reverting on
  81. * overflow (when the input is greater than largest uint${length}).
  82. *
  83. * Counterpart to Solidity's \`uint${length}\` operator.
  84. *
  85. * Requirements:
  86. *
  87. * - input must fit into ${length} bits
  88. *
  89. * _Available since v${version('toUint(uint)', length)}._
  90. */
  91. function toUint${length}(uint256 value) internal pure returns (uint${length}) {
  92. require(value <= type(uint${length}).max, "SafeCast: value doesn't fit in ${length} bits");
  93. return uint${length}(value);
  94. }
  95. `;
  96. /* eslint-disable max-len */
  97. const toIntDownCast = length => `\
  98. /**
  99. * @dev Returns the downcasted int${length} from int256, reverting on
  100. * overflow (when the input is less than smallest int${length} or
  101. * greater than largest int${length}).
  102. *
  103. * Counterpart to Solidity's \`int${length}\` operator.
  104. *
  105. * Requirements:
  106. *
  107. * - input must fit into ${length} bits
  108. *
  109. * _Available since v${version('toInt(int)', length)}._
  110. */
  111. function toInt${length}(int256 value) internal pure returns (int${length} downcasted) {
  112. downcasted = int${length}(value);
  113. require(downcasted == value, "SafeCast: value doesn't fit in ${length} bits");
  114. }
  115. `;
  116. /* eslint-enable max-len */
  117. const toInt = length => `\
  118. /**
  119. * @dev Converts an unsigned uint${length} into a signed int${length}.
  120. *
  121. * Requirements:
  122. *
  123. * - input must be less than or equal to maxInt${length}.
  124. *
  125. * _Available since v${version('toInt(uint)', length)}._
  126. */
  127. function toInt${length}(uint${length} value) internal pure returns (int${length}) {
  128. // Note: Unsafe cast below is okay because \`type(int${length}).max\` is guaranteed to be positive
  129. require(value <= uint${length}(type(int${length}).max), "SafeCast: value doesn't fit in an int${length}");
  130. return int${length}(value);
  131. }
  132. `;
  133. const toUint = length => `\
  134. /**
  135. * @dev Converts a signed int${length} into an unsigned uint${length}.
  136. *
  137. * Requirements:
  138. *
  139. * - input must be greater than or equal to 0.
  140. *
  141. * _Available since v${version('toUint(int)', length)}._
  142. */
  143. function toUint${length}(int${length} value) internal pure returns (uint${length}) {
  144. require(value >= 0, "SafeCast: value must be positive");
  145. return uint${length}(value);
  146. }
  147. `;
  148. // GENERATE
  149. module.exports = format(
  150. header.trimEnd(),
  151. 'library SafeCast {',
  152. [...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256)],
  153. '}',
  154. );