SafeCastMock.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. const format = require('../format-lines');
  2. const { range } = require('../../helpers');
  3. const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
  4. const header = `\
  5. pragma solidity ^0.8.0;
  6. import "../utils/math/SafeCast.sol";
  7. `;
  8. const toInt = length => `\
  9. function toInt${length}(uint${length} a) public pure returns (int${length}) {
  10. return a.toInt${length}();
  11. }
  12. `;
  13. const toUint = length => `\
  14. function toUint${length}(int${length} a) public pure returns (uint${length}) {
  15. return a.toUint${length}();
  16. }
  17. `;
  18. const toIntDownCast = length => `\
  19. function toInt${length}(int256 a) public pure returns (int${length}) {
  20. return a.toInt${length}();
  21. }
  22. `;
  23. const toUintDownCast = length => `\
  24. function toUint${length}(uint256 a) public pure returns (uint${length}) {
  25. return a.toUint${length}();
  26. }
  27. `;
  28. // GENERATE
  29. module.exports = format(
  30. header,
  31. 'contract SafeCastMock {',
  32. [
  33. 'using SafeCast for uint256;',
  34. 'using SafeCast for int256;',
  35. '',
  36. toUint(256),
  37. ...LENGTHS.map(toUintDownCast),
  38. toInt(256),
  39. ...LENGTHS.map(toIntDownCast),
  40. ].flatMap(fn => fn.split('\n')).slice(0, -1),
  41. '}',
  42. );