177 - OZ SafeCast
OpenZeppelin SafeCast: Wrappers over Solidity's uintXX/intXX casting operators with added overflow checks. Downcasting from uint256/int256 in Solidity does not revert on overflow. This can easily result in undesired exploitation or bugs, since developers usually assume that overflows raise errors. SafeCast
restores this intuition by reverting the transaction when such an operation overflows.
-
toUint128(uint256 value) returns (uint128)
: Returns the downcasteduint128
fromuint256
, reverting on overflow (when the input is greater than largestuint128
). Similar functions are available fortoUint64(uint256 value)
,toUint32(uint256 value)
,toUint16(uint256 value)
,toUint8(uint256 value)
-
toInt128(int256 value) internal pure returns (int256)
: Returns the downcasted int128 fromint256
, reverting on overflow (when the input is less than smallestint128
or greater than largestint128
). Similar functions are available fortoInt64(int256 value)
,toInt32(int256 value)
,toInt16(int256 value)
,toInt8(int256 value)
. -
function toInt256(uint256 value) returns (int256)
: Converts an unsigneduint256
into a signedint256
-
function toUint256(int256 value) returns (uint256)
: Converts a signedint256
into an unsigneduint256
-
Similar functions downcasting to 224/96/64/32/16/8 bits for both unsigned and signed.
- Downcasting -> Overflow Safe Downcasting
- uint256 -> uint224/uint128/uint96/uint64/uint32/uint16/uint8
- int256 -> int224/int128/int96/int64/int32/int16/int8
- uint256 -> int256
- int256 -> uint256