Replace revert strings with custom errors (#4261)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
@ -77,6 +77,28 @@ 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.
|
||||
*/
|
||||
error SafeCastOverflowedIntToUint(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.
|
||||
*/
|
||||
error SafeCastOverflowedUintToInt(uint256 value);
|
||||
`;
|
||||
|
||||
const toUintDownCast = length => `\
|
||||
/**
|
||||
* @dev Returns the downcasted uint${length} from uint256, reverting on
|
||||
@ -91,7 +113,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 +137,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 +156,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(value);
|
||||
}
|
||||
return int${length}(value);
|
||||
}
|
||||
`;
|
||||
@ -146,7 +174,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(value);
|
||||
}
|
||||
return uint${length}(value);
|
||||
}
|
||||
`;
|
||||
@ -155,6 +185,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)],
|
||||
'}',
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user