Replace error strings with custom errors (#4261)
This commit is contained in:
@ -19,6 +19,13 @@ import "../math/SafeCast.sol";
|
||||
*/
|
||||
`;
|
||||
|
||||
const errors = `\
|
||||
/**
|
||||
* @dev A value was attempted to be inserted on a past checkpoint.
|
||||
*/
|
||||
error CheckpointUnorderedInsertion();
|
||||
`;
|
||||
|
||||
const template = opts => `\
|
||||
struct ${opts.historyTypeName} {
|
||||
${opts.checkpointTypeName}[] ${opts.checkpointFieldName};
|
||||
@ -145,7 +152,9 @@ function _insert(
|
||||
${opts.checkpointTypeName} memory last = _unsafeAccess(self, pos - 1);
|
||||
|
||||
// Checkpoint keys must be non-decreasing.
|
||||
require(last.${opts.keyFieldName} <= key, "Checkpoint: decreasing keys");
|
||||
if(last.${opts.keyFieldName} > key) {
|
||||
revert CheckpointUnorderedInsertion();
|
||||
}
|
||||
|
||||
// Update or push new checkpoint
|
||||
if (last.${opts.keyFieldName} == key) {
|
||||
@ -226,6 +235,7 @@ function _unsafeAccess(${opts.checkpointTypeName}[] storage self, uint256 pos)
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
'library Checkpoints {',
|
||||
errors,
|
||||
OPTS.flatMap(opts => template(opts)),
|
||||
'}',
|
||||
);
|
||||
|
||||
@ -66,6 +66,11 @@ const defaultMap = () => `\
|
||||
// This means that we can only create new EnumerableMaps for types that fit
|
||||
// in bytes32.
|
||||
|
||||
/**
|
||||
* @dev Query for an nonexistent map key.
|
||||
*/
|
||||
error EnumerableMapNonexistentKey(bytes32 key);
|
||||
|
||||
struct Bytes32ToBytes32Map {
|
||||
// Storage of keys
|
||||
EnumerableSet.Bytes32Set _keys;
|
||||
@ -149,7 +154,9 @@ function tryGet(Bytes32ToBytes32Map storage map, bytes32 key) internal view retu
|
||||
*/
|
||||
function get(Bytes32ToBytes32Map storage map, bytes32 key) internal view returns (bytes32) {
|
||||
bytes32 value = map._values[key];
|
||||
require(value != 0 || contains(map, key), "EnumerableMap: nonexistent key");
|
||||
if(value == 0 && !contains(map, key)) {
|
||||
revert EnumerableMapNonexistentKey(key);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@ -77,6 +77,32 @@ pragma solidity ^0.8.19;
|
||||
*/
|
||||
`;
|
||||
|
||||
const errors = `\
|
||||
/**
|
||||
* @dev Value doesn't fit in an uint of \`bits\` size.
|
||||
*/
|
||||
error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);
|
||||
|
||||
/**
|
||||
* @dev An int value doesn't fit in an uint of \`bits\` size.
|
||||
*
|
||||
* NOTE: The \`bits\` argument is \`min(bits, 255)\`.
|
||||
*/
|
||||
error SafeCastOverflowedIntToUint(uint8 bits, int256 value);
|
||||
|
||||
/**
|
||||
* @dev Value doesn't fit in an int of \`bits\` size.
|
||||
*/
|
||||
error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);
|
||||
|
||||
/**
|
||||
* @dev An uint value doesn't fit in an int of \`bits\` size.
|
||||
*
|
||||
* NOTE: The \`bits\` argument is \`min(bits, 255)\`.
|
||||
*/
|
||||
error SafeCastOverflowedUintToInt(uint8 bits, uint256 value);
|
||||
`;
|
||||
|
||||
const toUintDownCast = length => `\
|
||||
/**
|
||||
* @dev Returns the downcasted uint${length} from uint256, reverting on
|
||||
@ -91,7 +117,9 @@ const toUintDownCast = length => `\
|
||||
* _Available since v${version('toUint(uint)', length)}._
|
||||
*/
|
||||
function toUint${length}(uint256 value) internal pure returns (uint${length}) {
|
||||
require(value <= type(uint${length}).max, "SafeCast: value doesn't fit in ${length} bits");
|
||||
if (value > type(uint${length}).max) {
|
||||
revert SafeCastOverflowedUintDowncast(${length}, value);
|
||||
}
|
||||
return uint${length}(value);
|
||||
}
|
||||
`;
|
||||
@ -113,7 +141,9 @@ const toIntDownCast = length => `\
|
||||
*/
|
||||
function toInt${length}(int256 value) internal pure returns (int${length} downcasted) {
|
||||
downcasted = int${length}(value);
|
||||
require(downcasted == value, "SafeCast: value doesn't fit in ${length} bits");
|
||||
if (downcasted != value) {
|
||||
revert SafeCastOverflowedIntDowncast(${length}, value);
|
||||
}
|
||||
}
|
||||
`;
|
||||
/* eslint-enable max-len */
|
||||
@ -130,7 +160,9 @@ const toInt = length => `\
|
||||
*/
|
||||
function toInt${length}(uint${length} value) internal pure returns (int${length}) {
|
||||
// Note: Unsafe cast below is okay because \`type(int${length}).max\` is guaranteed to be positive
|
||||
require(value <= uint${length}(type(int${length}).max), "SafeCast: value doesn't fit in an int${length}");
|
||||
if (value > uint${length}(type(int${length}).max)) {
|
||||
revert SafeCastOverflowedUintToInt(${Math.min(length, 255)}, value);
|
||||
}
|
||||
return int${length}(value);
|
||||
}
|
||||
`;
|
||||
@ -146,7 +178,9 @@ const toUint = length => `\
|
||||
* _Available since v${version('toUint(int)', length)}._
|
||||
*/
|
||||
function toUint${length}(int${length} value) internal pure returns (uint${length}) {
|
||||
require(value >= 0, "SafeCast: value must be positive");
|
||||
if (value < 0) {
|
||||
revert SafeCastOverflowedIntToUint(${Math.min(length, 255)}, value);
|
||||
}
|
||||
return uint${length}(value);
|
||||
}
|
||||
`;
|
||||
@ -155,6 +189,7 @@ function toUint${length}(int${length} value) internal pure returns (uint${length
|
||||
module.exports = format(
|
||||
header.trimEnd(),
|
||||
'library SafeCast {',
|
||||
errors,
|
||||
[...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256)],
|
||||
'}',
|
||||
);
|
||||
|
||||
@ -38,7 +38,7 @@ pragma solidity ^0.8.19;
|
||||
* }
|
||||
*
|
||||
* function _setImplementation(address newImplementation) internal {
|
||||
* require(newImplementation.code.length > 0, "ERC1967: new implementation is not a contract");
|
||||
* require(newImplementation.code.length > 0);
|
||||
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
|
||||
* }
|
||||
* }
|
||||
|
||||
Reference in New Issue
Block a user