Use hardhat-exposed to reduce the need for mocks (#3666)
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
@ -16,18 +16,10 @@ function getVersion (path) {
|
||||
}
|
||||
|
||||
for (const [ file, template ] of Object.entries({
|
||||
// SafeCast
|
||||
'utils/math/SafeCast.sol': './templates/SafeCast.js',
|
||||
'mocks/SafeCastMock.sol': './templates/SafeCastMock.js',
|
||||
// EnumerableSet
|
||||
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
|
||||
'mocks/EnumerableSetMock.sol': './templates/EnumerableSetMock.js',
|
||||
// EnumerableMap
|
||||
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
|
||||
'mocks/EnumerableMapMock.sol': './templates/EnumerableMapMock.js',
|
||||
// Checkpoints
|
||||
'utils/Checkpoints.sol': './templates/Checkpoints.js',
|
||||
'mocks/CheckpointsMock.sol': './templates/CheckpointsMock.js',
|
||||
})) {
|
||||
const script = path.relative(path.join(__dirname, '../..'), __filename);
|
||||
const input = path.join(path.dirname(script), template);
|
||||
|
||||
@ -1,80 +0,0 @@
|
||||
const format = require('../format-lines');
|
||||
|
||||
const VALUE_SIZES = [ 224, 160 ];
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../utils/Checkpoints.sol";
|
||||
`;
|
||||
|
||||
const legacy = () => `\
|
||||
contract CheckpointsMock {
|
||||
using Checkpoints for Checkpoints.History;
|
||||
|
||||
Checkpoints.History private _totalCheckpoints;
|
||||
|
||||
function latest() public view returns (uint256) {
|
||||
return _totalCheckpoints.latest();
|
||||
}
|
||||
|
||||
function latestCheckpoint() public view returns (bool, uint256, uint256) {
|
||||
return _totalCheckpoints.latestCheckpoint();
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _totalCheckpoints.length();
|
||||
}
|
||||
|
||||
function push(uint256 value) public returns (uint256, uint256) {
|
||||
return _totalCheckpoints.push(value);
|
||||
}
|
||||
|
||||
function getAtBlock(uint256 blockNumber) public view returns (uint256) {
|
||||
return _totalCheckpoints.getAtBlock(blockNumber);
|
||||
}
|
||||
|
||||
function getAtProbablyRecentBlock(uint256 blockNumber) public view returns (uint256) {
|
||||
return _totalCheckpoints.getAtProbablyRecentBlock(blockNumber);
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const checkpoint = length => `\
|
||||
contract Checkpoints${length}Mock {
|
||||
using Checkpoints for Checkpoints.Trace${length};
|
||||
|
||||
Checkpoints.Trace${length} private _totalCheckpoints;
|
||||
|
||||
function latest() public view returns (uint${length}) {
|
||||
return _totalCheckpoints.latest();
|
||||
}
|
||||
|
||||
function latestCheckpoint() public view returns (bool, uint${256 - length}, uint${length}) {
|
||||
return _totalCheckpoints.latestCheckpoint();
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _totalCheckpoints.length();
|
||||
}
|
||||
|
||||
function push(uint${256 - length} key, uint${length} value) public returns (uint${length}, uint${length}) {
|
||||
return _totalCheckpoints.push(key, value);
|
||||
}
|
||||
|
||||
function lowerLookup(uint${256 - length} key) public view returns (uint${length}) {
|
||||
return _totalCheckpoints.lowerLookup(key);
|
||||
}
|
||||
|
||||
function upperLookup(uint${256 - length} key) public view returns (uint${length}) {
|
||||
return _totalCheckpoints.upperLookup(key);
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
legacy(),
|
||||
...VALUE_SIZES.map(checkpoint),
|
||||
);
|
||||
@ -1,66 +0,0 @@
|
||||
const format = require('../format-lines');
|
||||
|
||||
const TYPES = [
|
||||
{ name: 'UintToAddressMap', keyType: 'uint256', valueType: 'address' },
|
||||
{ name: 'AddressToUintMap', keyType: 'address', valueType: 'uint256' },
|
||||
{ name: 'Bytes32ToBytes32Map', keyType: 'bytes32', valueType: 'bytes32' },
|
||||
{ name: 'UintToUintMap', keyType: 'uint256', valueType: 'uint256' },
|
||||
{ name: 'Bytes32ToUintMap', keyType: 'bytes32', valueType: 'uint256' },
|
||||
];
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../utils/structs/EnumerableMap.sol";
|
||||
`;
|
||||
|
||||
const customSetMock = ({ name, keyType, valueType }) => `\
|
||||
// ${name}
|
||||
contract ${name}Mock {
|
||||
using EnumerableMap for EnumerableMap.${name};
|
||||
|
||||
event OperationResult(bool result);
|
||||
|
||||
EnumerableMap.${name} private _map;
|
||||
|
||||
function contains(${keyType} key) public view returns (bool) {
|
||||
return _map.contains(key);
|
||||
}
|
||||
|
||||
function set(${keyType} key, ${valueType} value) public {
|
||||
bool result = _map.set(key, value);
|
||||
emit OperationResult(result);
|
||||
}
|
||||
|
||||
function remove(${keyType} key) public {
|
||||
bool result = _map.remove(key);
|
||||
emit OperationResult(result);
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _map.length();
|
||||
}
|
||||
|
||||
function at(uint256 index) public view returns (${keyType} key, ${valueType} value) {
|
||||
return _map.at(index);
|
||||
}
|
||||
|
||||
function tryGet(${keyType} key) public view returns (bool, ${valueType}) {
|
||||
return _map.tryGet(key);
|
||||
}
|
||||
|
||||
function get(${keyType} key) public view returns (${valueType}) {
|
||||
return _map.get(key);
|
||||
}
|
||||
|
||||
function getWithMessage(${keyType} key, string calldata errorMessage) public view returns (${valueType}) {
|
||||
return _map.get(key, errorMessage);
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
...TYPES.map(details => customSetMock(details)),
|
||||
);
|
||||
@ -1,56 +0,0 @@
|
||||
const format = require('../format-lines');
|
||||
|
||||
const TYPES = [
|
||||
{ name: 'Bytes32Set', type: 'bytes32' },
|
||||
{ name: 'AddressSet', type: 'address' },
|
||||
{ name: 'UintSet', type: 'uint256' },
|
||||
];
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../utils/structs/EnumerableSet.sol";
|
||||
`;
|
||||
|
||||
const customSetMock = ({ name, type }) => `\
|
||||
// ${name}
|
||||
contract Enumerable${name}Mock {
|
||||
using EnumerableSet for EnumerableSet.${name};
|
||||
|
||||
event OperationResult(bool result);
|
||||
|
||||
EnumerableSet.${name} private _set;
|
||||
|
||||
function contains(${type} value) public view returns (bool) {
|
||||
return _set.contains(value);
|
||||
}
|
||||
|
||||
function add(${type} value) public {
|
||||
bool result = _set.add(value);
|
||||
emit OperationResult(result);
|
||||
}
|
||||
|
||||
function remove(${type} value) public {
|
||||
bool result = _set.remove(value);
|
||||
emit OperationResult(result);
|
||||
}
|
||||
|
||||
function length() public view returns (uint256) {
|
||||
return _set.length();
|
||||
}
|
||||
|
||||
function at(uint256 index) public view returns (${type}) {
|
||||
return _set.at(index);
|
||||
}
|
||||
|
||||
function values() public view returns (${type}[] memory) {
|
||||
return _set.values();
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
...TYPES.map(details => customSetMock(details)),
|
||||
);
|
||||
0
scripts/generate/templates/SafeCast.js
Executable file → Normal file
0
scripts/generate/templates/SafeCast.js
Executable file → Normal file
@ -1,50 +0,0 @@
|
||||
const format = require('../format-lines');
|
||||
const { range } = require('../../helpers');
|
||||
|
||||
const LENGTHS = range(8, 256, 8).reverse(); // 248 → 8 (in steps of 8)
|
||||
|
||||
const header = `\
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "../utils/math/SafeCast.sol";
|
||||
`;
|
||||
|
||||
const toInt = length => `\
|
||||
function toInt${length}(uint${length} a) public pure returns (int${length}) {
|
||||
return a.toInt${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
const toUint = length => `\
|
||||
function toUint${length}(int${length} a) public pure returns (uint${length}) {
|
||||
return a.toUint${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
const toIntDownCast = length => `\
|
||||
function toInt${length}(int256 a) public pure returns (int${length}) {
|
||||
return a.toInt${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
const toUintDownCast = length => `\
|
||||
function toUint${length}(uint256 a) public pure returns (uint${length}) {
|
||||
return a.toUint${length}();
|
||||
}
|
||||
`;
|
||||
|
||||
// GENERATE
|
||||
module.exports = format(
|
||||
header,
|
||||
'contract SafeCastMock {',
|
||||
[
|
||||
'using SafeCast for uint256;',
|
||||
'using SafeCast for int256;',
|
||||
'',
|
||||
toUint(256),
|
||||
...LENGTHS.map(toUintDownCast),
|
||||
toInt(256),
|
||||
...LENGTHS.map(toIntDownCast),
|
||||
].flatMap(fn => fn.split('\n')).slice(0, -1),
|
||||
'}',
|
||||
);
|
||||
Reference in New Issue
Block a user