EnumerableMapMock.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. const format = require('../format-lines');
  2. const TYPES = [
  3. { name: 'UintToAddressMap', keyType: 'uint256', valueType: 'address' },
  4. { name: 'AddressToUintMap', keyType: 'address', valueType: 'uint256' },
  5. { name: 'Bytes32ToBytes32Map', keyType: 'bytes32', valueType: 'bytes32' },
  6. { name: 'UintToUintMap', keyType: 'uint256', valueType: 'uint256' },
  7. { name: 'Bytes32ToUintMap', keyType: 'bytes32', valueType: 'uint256' },
  8. ];
  9. const header = `\
  10. pragma solidity ^0.8.0;
  11. import "../utils/structs/EnumerableMap.sol";
  12. `;
  13. const customSetMock = ({ name, keyType, valueType }) => `\
  14. // ${name}
  15. contract ${name}Mock {
  16. using EnumerableMap for EnumerableMap.${name};
  17. event OperationResult(bool result);
  18. EnumerableMap.${name} private _map;
  19. function contains(${keyType} key) public view returns (bool) {
  20. return _map.contains(key);
  21. }
  22. function set(${keyType} key, ${valueType} value) public {
  23. bool result = _map.set(key, value);
  24. emit OperationResult(result);
  25. }
  26. function remove(${keyType} key) public {
  27. bool result = _map.remove(key);
  28. emit OperationResult(result);
  29. }
  30. function length() public view returns (uint256) {
  31. return _map.length();
  32. }
  33. function at(uint256 index) public view returns (${keyType} key, ${valueType} value) {
  34. return _map.at(index);
  35. }
  36. function tryGet(${keyType} key) public view returns (bool, ${valueType}) {
  37. return _map.tryGet(key);
  38. }
  39. function get(${keyType} key) public view returns (${valueType}) {
  40. return _map.get(key);
  41. }
  42. function getWithMessage(${keyType} key, string calldata errorMessage) public view returns (${valueType}) {
  43. return _map.get(key, errorMessage);
  44. }
  45. }
  46. `;
  47. // GENERATE
  48. module.exports = format(
  49. header,
  50. ...TYPES.map(details => customSetMock(details)),
  51. );