CheckpointsMock.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const format = require('../format-lines');
  2. const VALUE_SIZES = [ 224, 160 ];
  3. const header = `\
  4. pragma solidity ^0.8.0;
  5. import "../utils/Checkpoints.sol";
  6. `;
  7. const legacy = () => `\
  8. contract CheckpointsMock {
  9. using Checkpoints for Checkpoints.History;
  10. Checkpoints.History private _totalCheckpoints;
  11. function latest() public view returns (uint256) {
  12. return _totalCheckpoints.latest();
  13. }
  14. function push(uint256 value) public returns (uint256, uint256) {
  15. return _totalCheckpoints.push(value);
  16. }
  17. function getAtBlock(uint256 blockNumber) public view returns (uint256) {
  18. return _totalCheckpoints.getAtBlock(blockNumber);
  19. }
  20. function getAtProbablyRecentBlock(uint256 blockNumber) public view returns (uint256) {
  21. return _totalCheckpoints.getAtProbablyRecentBlock(blockNumber);
  22. }
  23. function length() public view returns (uint256) {
  24. return _totalCheckpoints._checkpoints.length;
  25. }
  26. }
  27. `;
  28. const checkpoint = length => `\
  29. contract Checkpoints${length}Mock {
  30. using Checkpoints for Checkpoints.Trace${length};
  31. Checkpoints.Trace${length} private _totalCheckpoints;
  32. function latest() public view returns (uint${length}) {
  33. return _totalCheckpoints.latest();
  34. }
  35. function push(uint${256 - length} key, uint${length} value) public returns (uint${length}, uint${length}) {
  36. return _totalCheckpoints.push(key, value);
  37. }
  38. function lowerLookup(uint${256 - length} key) public view returns (uint${length}) {
  39. return _totalCheckpoints.lowerLookup(key);
  40. }
  41. function upperLookup(uint${256 - length} key) public view returns (uint${length}) {
  42. return _totalCheckpoints.upperLookup(key);
  43. }
  44. function length() public view returns (uint256) {
  45. return _totalCheckpoints._checkpoints.length;
  46. }
  47. }
  48. `;
  49. // GENERATE
  50. module.exports = format(
  51. header,
  52. legacy(),
  53. ...VALUE_SIZES.map(checkpoint),
  54. );