CheckpointsMock.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 getAtRecentBlock(uint256 blockNumber) public view returns (uint256) {
  21. return _totalCheckpoints.getAtRecentBlock(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 upperLookupRecent(uint${256 - length} key) public view returns (uint224) {
  45. return _totalCheckpoints.upperLookupRecent(key);
  46. }
  47. function length() public view returns (uint256) {
  48. return _totalCheckpoints._checkpoints.length;
  49. }
  50. }
  51. `;
  52. // GENERATE
  53. module.exports = format(
  54. header,
  55. legacy(),
  56. ...VALUE_SIZES.map(checkpoint),
  57. );