EnumerableSetMock.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const format = require('../format-lines');
  2. const TYPES = [
  3. { name: 'Bytes32Set', type: 'bytes32' },
  4. { name: 'AddressSet', type: 'address' },
  5. { name: 'UintSet', type: 'uint256' },
  6. ];
  7. const header = `\
  8. pragma solidity ^0.8.0;
  9. import "../utils/structs/EnumerableSet.sol";
  10. `;
  11. const customSetMock = ({ name, type }) => `\
  12. // ${name}
  13. contract Enumerable${name}Mock {
  14. using EnumerableSet for EnumerableSet.${name};
  15. event OperationResult(bool result);
  16. EnumerableSet.${name} private _set;
  17. function contains(${type} value) public view returns (bool) {
  18. return _set.contains(value);
  19. }
  20. function add(${type} value) public {
  21. bool result = _set.add(value);
  22. emit OperationResult(result);
  23. }
  24. function remove(${type} value) public {
  25. bool result = _set.remove(value);
  26. emit OperationResult(result);
  27. }
  28. function length() public view returns (uint256) {
  29. return _set.length();
  30. }
  31. function at(uint256 index) public view returns (${type}) {
  32. return _set.at(index);
  33. }
  34. function values() public view returns (${type}[] memory) {
  35. return _set.values();
  36. }
  37. }
  38. `;
  39. // GENERATE
  40. module.exports = format(
  41. header,
  42. ...TYPES.map(details => customSetMock(details)),
  43. );