CheckpointsMock.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 latestCheckpoint() public view returns (bool, uint256, uint256) {
  15. return _totalCheckpoints.latestCheckpoint();
  16. }
  17. function length() public view returns (uint256) {
  18. return _totalCheckpoints.length();
  19. }
  20. function push(uint256 value) public returns (uint256, uint256) {
  21. return _totalCheckpoints.push(value);
  22. }
  23. function getAtBlock(uint256 blockNumber) public view returns (uint256) {
  24. return _totalCheckpoints.getAtBlock(blockNumber);
  25. }
  26. function getAtProbablyRecentBlock(uint256 blockNumber) public view returns (uint256) {
  27. return _totalCheckpoints.getAtProbablyRecentBlock(blockNumber);
  28. }
  29. }
  30. `;
  31. const checkpoint = length => `\
  32. contract Checkpoints${length}Mock {
  33. using Checkpoints for Checkpoints.Trace${length};
  34. Checkpoints.Trace${length} private _totalCheckpoints;
  35. function latest() public view returns (uint${length}) {
  36. return _totalCheckpoints.latest();
  37. }
  38. function latestCheckpoint() public view returns (bool, uint${256 - length}, uint${length}) {
  39. return _totalCheckpoints.latestCheckpoint();
  40. }
  41. function length() public view returns (uint256) {
  42. return _totalCheckpoints.length();
  43. }
  44. function push(uint${256 - length} key, uint${length} value) public returns (uint${length}, uint${length}) {
  45. return _totalCheckpoints.push(key, value);
  46. }
  47. function lowerLookup(uint${256 - length} key) public view returns (uint${length}) {
  48. return _totalCheckpoints.lowerLookup(key);
  49. }
  50. function upperLookup(uint${256 - length} key) public view returns (uint${length}) {
  51. return _totalCheckpoints.upperLookup(key);
  52. }
  53. }
  54. `;
  55. // GENERATE
  56. module.exports = format(
  57. header,
  58. legacy(),
  59. ...VALUE_SIZES.map(checkpoint),
  60. );