SafeCast.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. `;
  75. const toUintDownCast = length => `\
  76. /**
  77. * @dev Returns the downcasted uint${length} from uint256, reverting on
  78. * overflow (when the input is greater than largest uint${length}).
  79. *
  80. * Counterpart to Solidity's \`uint${length}\` operator.
  81. *
  82. * Requirements:
  83. *
  84. * - input must fit into ${length} bits
  85. *
  86. * _Available since v${version('toUint(uint)', length)}._
  87. */
  88. function toUint${length}(uint256 value) internal pure returns (uint${length}) {
  89. require(value <= type(uint${length}).max, "SafeCast: value doesn't fit in ${length} bits");
  90. return uint${length}(value);
  91. }
  92. `;
  93. /* eslint-disable max-len */
  94. const toIntDownCast = length => `\
  95. /**
  96. * @dev Returns the downcasted int${length} from int256, reverting on
  97. * overflow (when the input is less than smallest int${length} or
  98. * greater than largest int${length}).
  99. *
  100. * Counterpart to Solidity's \`int${length}\` operator.
  101. *
  102. * Requirements:
  103. *
  104. * - input must fit into ${length} bits
  105. *
  106. * _Available since v${version('toInt(int)', length)}._
  107. */
  108. function toInt${length}(int256 value) internal pure returns (int${length} downcasted) {
  109. downcasted = int${length}(value);
  110. require(downcasted == value, "SafeCast: value doesn't fit in ${length} bits");
  111. }
  112. `;
  113. /* eslint-enable max-len */
  114. const toInt = length => `\
  115. /**
  116. * @dev Converts an unsigned uint${length} into a signed int${length}.
  117. *
  118. * Requirements:
  119. *
  120. * - input must be less than or equal to maxInt${length}.
  121. *
  122. * _Available since v${version('toInt(uint)', length)}._
  123. */
  124. function toInt${length}(uint${length} value) internal pure returns (int${length}) {
  125. // Note: Unsafe cast below is okay because \`type(int${length}).max\` is guaranteed to be positive
  126. require(value <= uint${length}(type(int${length}).max), "SafeCast: value doesn't fit in an int${length}");
  127. return int${length}(value);
  128. }
  129. `;
  130. const toUint = length => `\
  131. /**
  132. * @dev Converts a signed int${length} into an unsigned uint${length}.
  133. *
  134. * Requirements:
  135. *
  136. * - input must be greater than or equal to 0.
  137. *
  138. * _Available since v${version('toUint(int)', length)}._
  139. */
  140. function toUint${length}(int${length} value) internal pure returns (uint${length}) {
  141. require(value >= 0, "SafeCast: value must be positive");
  142. return uint${length}(value);
  143. }
  144. `;
  145. // GENERATE
  146. module.exports = format(
  147. header.trimEnd(),
  148. 'library SafeCast {',
  149. [...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256)],
  150. '}',
  151. );