Skip to content
Lattice

SafeCast

Git Source

Author: Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeCast.sol)

Wrappers over Solidity's uintXX/intXX/bool 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.

Using this library instead of the unchecked operations eliminates an entire class of bugs, so it's recommended to use it always.

Functions

toUint248

Returns the downcasted uint248 from uint256, reverting on overflow (when the input is greater than largest uint248).

Counterpart to Solidity's uint248 operator.

Requirements:

  • input must fit into 248 bits
function toUint248(uint256 value) internal pure returns (uint248);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint248

toUint240

Returns the downcasted uint240 from uint256, reverting on overflow (when the input is greater than largest uint240).

Counterpart to Solidity's uint240 operator.

Requirements:

  • input must fit into 240 bits
function toUint240(uint256 value) internal pure returns (uint240);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint240

toUint232

Returns the downcasted uint232 from uint256, reverting on overflow (when the input is greater than largest uint232).

Counterpart to Solidity's uint232 operator.

Requirements:

  • input must fit into 232 bits
function toUint232(uint256 value) internal pure returns (uint232);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint232

toUint224

Returns the downcasted uint224 from uint256, reverting on overflow (when the input is greater than largest uint224).

Counterpart to Solidity's uint224 operator.

Requirements:

  • input must fit into 224 bits
function toUint224(uint256 value) internal pure returns (uint224);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint224

toUint216

Returns the downcasted uint216 from uint256, reverting on overflow (when the input is greater than largest uint216).

Counterpart to Solidity's uint216 operator.

Requirements:

  • input must fit into 216 bits
function toUint216(uint256 value) internal pure returns (uint216);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint216

toUint208

Returns the downcasted uint208 from uint256, reverting on overflow (when the input is greater than largest uint208).

Counterpart to Solidity's uint208 operator.

Requirements:

  • input must fit into 208 bits
function toUint208(uint256 value) internal pure returns (uint208);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint208

toUint200

Returns the downcasted uint200 from uint256, reverting on overflow (when the input is greater than largest uint200).

Counterpart to Solidity's uint200 operator.

Requirements:

  • input must fit into 200 bits
function toUint200(uint256 value) internal pure returns (uint200);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint200

toUint192

Returns the downcasted uint192 from uint256, reverting on overflow (when the input is greater than largest uint192).

Counterpart to Solidity's uint192 operator.

Requirements:

  • input must fit into 192 bits
function toUint192(uint256 value) internal pure returns (uint192);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint192

toUint184

Returns the downcasted uint184 from uint256, reverting on overflow (when the input is greater than largest uint184).

Counterpart to Solidity's uint184 operator.

Requirements:

  • input must fit into 184 bits
function toUint184(uint256 value) internal pure returns (uint184);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint184

toUint176

Returns the downcasted uint176 from uint256, reverting on overflow (when the input is greater than largest uint176).

Counterpart to Solidity's uint176 operator.

Requirements:

  • input must fit into 176 bits
function toUint176(uint256 value) internal pure returns (uint176);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint176

toUint168

Returns the downcasted uint168 from uint256, reverting on overflow (when the input is greater than largest uint168).

Counterpart to Solidity's uint168 operator.

Requirements:

  • input must fit into 168 bits
function toUint168(uint256 value) internal pure returns (uint168);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint168

toUint160

Returns the downcasted uint160 from uint256, reverting on overflow (when the input is greater than largest uint160).

Counterpart to Solidity's uint160 operator.

Requirements:

  • input must fit into 160 bits
function toUint160(uint256 value) internal pure returns (uint160);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint160

toUint152

Returns the downcasted uint152 from uint256, reverting on overflow (when the input is greater than largest uint152).

Counterpart to Solidity's uint152 operator.

Requirements:

  • input must fit into 152 bits
function toUint152(uint256 value) internal pure returns (uint152);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint152

toUint144

Returns the downcasted uint144 from uint256, reverting on overflow (when the input is greater than largest uint144).

Counterpart to Solidity's uint144 operator.

Requirements:

  • input must fit into 144 bits
function toUint144(uint256 value) internal pure returns (uint144);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint144

toUint136

Returns the downcasted uint136 from uint256, reverting on overflow (when the input is greater than largest uint136).

Counterpart to Solidity's uint136 operator.

Requirements:

  • input must fit into 136 bits
function toUint136(uint256 value) internal pure returns (uint136);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint136

toUint128

Returns the downcasted uint128 from uint256, reverting on overflow (when the input is greater than largest uint128).

Counterpart to Solidity's uint128 operator.

Requirements:

  • input must fit into 128 bits
function toUint128(uint256 value) internal pure returns (uint128);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint128

toUint120

Returns the downcasted uint120 from uint256, reverting on overflow (when the input is greater than largest uint120).

Counterpart to Solidity's uint120 operator.

Requirements:

  • input must fit into 120 bits
function toUint120(uint256 value) internal pure returns (uint120);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint120

toUint112

Returns the downcasted uint112 from uint256, reverting on overflow (when the input is greater than largest uint112).

Counterpart to Solidity's uint112 operator.

Requirements:

  • input must fit into 112 bits
function toUint112(uint256 value) internal pure returns (uint112);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint112

toUint104

Returns the downcasted uint104 from uint256, reverting on overflow (when the input is greater than largest uint104).

Counterpart to Solidity's uint104 operator.

Requirements:

  • input must fit into 104 bits
function toUint104(uint256 value) internal pure returns (uint104);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint104

toUint96

Returns the downcasted uint96 from uint256, reverting on overflow (when the input is greater than largest uint96).

Counterpart to Solidity's uint96 operator.

Requirements:

  • input must fit into 96 bits
function toUint96(uint256 value) internal pure returns (uint96);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint96

toUint88

Returns the downcasted uint88 from uint256, reverting on overflow (when the input is greater than largest uint88).

Counterpart to Solidity's uint88 operator.

Requirements:

  • input must fit into 88 bits
function toUint88(uint256 value) internal pure returns (uint88);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint88

toUint80

Returns the downcasted uint80 from uint256, reverting on overflow (when the input is greater than largest uint80).

Counterpart to Solidity's uint80 operator.

Requirements:

  • input must fit into 80 bits
function toUint80(uint256 value) internal pure returns (uint80);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint80

toUint72

Returns the downcasted uint72 from uint256, reverting on overflow (when the input is greater than largest uint72).

Counterpart to Solidity's uint72 operator.

Requirements:

  • input must fit into 72 bits
function toUint72(uint256 value) internal pure returns (uint72);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint72

toUint64

Returns the downcasted uint64 from uint256, reverting on overflow (when the input is greater than largest uint64).

Counterpart to Solidity's uint64 operator.

Requirements:

  • input must fit into 64 bits
function toUint64(uint256 value) internal pure returns (uint64);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint64

toUint56

Returns the downcasted uint56 from uint256, reverting on overflow (when the input is greater than largest uint56).

Counterpart to Solidity's uint56 operator.

Requirements:

  • input must fit into 56 bits
function toUint56(uint256 value) internal pure returns (uint56);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint56

toUint48

Returns the downcasted uint48 from uint256, reverting on overflow (when the input is greater than largest uint48).

Counterpart to Solidity's uint48 operator.

Requirements:

  • input must fit into 48 bits
function toUint48(uint256 value) internal pure returns (uint48);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint48

toUint40

Returns the downcasted uint40 from uint256, reverting on overflow (when the input is greater than largest uint40).

Counterpart to Solidity's uint40 operator.

Requirements:

  • input must fit into 40 bits
function toUint40(uint256 value) internal pure returns (uint40);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint40

toUint32

Returns the downcasted uint32 from uint256, reverting on overflow (when the input is greater than largest uint32).

Counterpart to Solidity's uint32 operator.

Requirements:

  • input must fit into 32 bits
function toUint32(uint256 value) internal pure returns (uint32);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint32

toUint24

Returns the downcasted uint24 from uint256, reverting on overflow (when the input is greater than largest uint24).

Counterpart to Solidity's uint24 operator.

Requirements:

  • input must fit into 24 bits
function toUint24(uint256 value) internal pure returns (uint24);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint24

toUint16

Returns the downcasted uint16 from uint256, reverting on overflow (when the input is greater than largest uint16).

Counterpart to Solidity's uint16 operator.

Requirements:

  • input must fit into 16 bits
function toUint16(uint256 value) internal pure returns (uint16);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint16

toUint8

Returns the downcasted uint8 from uint256, reverting on overflow (when the input is greater than largest uint8).

Counterpart to Solidity's uint8 operator.

Requirements:

  • input must fit into 8 bits
function toUint8(uint256 value) internal pure returns (uint8);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>uint8

toUint256

Converts a signed int256 into an unsigned uint256.

Requirements:

  • input must be greater than or equal to 0.
function toUint256(int256 value) internal pure returns (uint256);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
<none>uint256

toInt248

Returns the downcasted int248 from int256, reverting on overflow (when the input is less than smallest int248 or greater than largest int248).

Counterpart to Solidity's int248 operator.

Requirements:

  • input must fit into 248 bits
function toInt248(int256 value) internal pure returns (int248 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint248

toInt240

Returns the downcasted int240 from int256, reverting on overflow (when the input is less than smallest int240 or greater than largest int240).

Counterpart to Solidity's int240 operator.

Requirements:

  • input must fit into 240 bits
function toInt240(int256 value) internal pure returns (int240 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint240

toInt232

Returns the downcasted int232 from int256, reverting on overflow (when the input is less than smallest int232 or greater than largest int232).

Counterpart to Solidity's int232 operator.

Requirements:

  • input must fit into 232 bits
function toInt232(int256 value) internal pure returns (int232 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint232

toInt224

Returns the downcasted int224 from int256, reverting on overflow (when the input is less than smallest int224 or greater than largest int224).

Counterpart to Solidity's int224 operator.

Requirements:

  • input must fit into 224 bits
function toInt224(int256 value) internal pure returns (int224 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint224

toInt216

Returns the downcasted int216 from int256, reverting on overflow (when the input is less than smallest int216 or greater than largest int216).

Counterpart to Solidity's int216 operator.

Requirements:

  • input must fit into 216 bits
function toInt216(int256 value) internal pure returns (int216 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint216

toInt208

Returns the downcasted int208 from int256, reverting on overflow (when the input is less than smallest int208 or greater than largest int208).

Counterpart to Solidity's int208 operator.

Requirements:

  • input must fit into 208 bits
function toInt208(int256 value) internal pure returns (int208 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint208

toInt200

Returns the downcasted int200 from int256, reverting on overflow (when the input is less than smallest int200 or greater than largest int200).

Counterpart to Solidity's int200 operator.

Requirements:

  • input must fit into 200 bits
function toInt200(int256 value) internal pure returns (int200 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint200

toInt192

Returns the downcasted int192 from int256, reverting on overflow (when the input is less than smallest int192 or greater than largest int192).

Counterpart to Solidity's int192 operator.

Requirements:

  • input must fit into 192 bits
function toInt192(int256 value) internal pure returns (int192 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint192

toInt184

Returns the downcasted int184 from int256, reverting on overflow (when the input is less than smallest int184 or greater than largest int184).

Counterpart to Solidity's int184 operator.

Requirements:

  • input must fit into 184 bits
function toInt184(int256 value) internal pure returns (int184 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint184

toInt176

Returns the downcasted int176 from int256, reverting on overflow (when the input is less than smallest int176 or greater than largest int176).

Counterpart to Solidity's int176 operator.

Requirements:

  • input must fit into 176 bits
function toInt176(int256 value) internal pure returns (int176 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint176

toInt168

Returns the downcasted int168 from int256, reverting on overflow (when the input is less than smallest int168 or greater than largest int168).

Counterpart to Solidity's int168 operator.

Requirements:

  • input must fit into 168 bits
function toInt168(int256 value) internal pure returns (int168 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint168

toInt160

Returns the downcasted int160 from int256, reverting on overflow (when the input is less than smallest int160 or greater than largest int160).

Counterpart to Solidity's int160 operator.

Requirements:

  • input must fit into 160 bits
function toInt160(int256 value) internal pure returns (int160 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint160

toInt152

Returns the downcasted int152 from int256, reverting on overflow (when the input is less than smallest int152 or greater than largest int152).

Counterpart to Solidity's int152 operator.

Requirements:

  • input must fit into 152 bits
function toInt152(int256 value) internal pure returns (int152 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint152

toInt144

Returns the downcasted int144 from int256, reverting on overflow (when the input is less than smallest int144 or greater than largest int144).

Counterpart to Solidity's int144 operator.

Requirements:

  • input must fit into 144 bits
function toInt144(int256 value) internal pure returns (int144 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint144

toInt136

Returns the downcasted int136 from int256, reverting on overflow (when the input is less than smallest int136 or greater than largest int136).

Counterpart to Solidity's int136 operator.

Requirements:

  • input must fit into 136 bits
function toInt136(int256 value) internal pure returns (int136 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint136

toInt128

Returns the downcasted int128 from int256, reverting on overflow (when the input is less than smallest int128 or greater than largest int128).

Counterpart to Solidity's int128 operator.

Requirements:

  • input must fit into 128 bits
function toInt128(int256 value) internal pure returns (int128 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint128

toInt120

Returns the downcasted int120 from int256, reverting on overflow (when the input is less than smallest int120 or greater than largest int120).

Counterpart to Solidity's int120 operator.

Requirements:

  • input must fit into 120 bits
function toInt120(int256 value) internal pure returns (int120 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint120

toInt112

Returns the downcasted int112 from int256, reverting on overflow (when the input is less than smallest int112 or greater than largest int112).

Counterpart to Solidity's int112 operator.

Requirements:

  • input must fit into 112 bits
function toInt112(int256 value) internal pure returns (int112 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint112

toInt104

Returns the downcasted int104 from int256, reverting on overflow (when the input is less than smallest int104 or greater than largest int104).

Counterpart to Solidity's int104 operator.

Requirements:

  • input must fit into 104 bits
function toInt104(int256 value) internal pure returns (int104 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint104

toInt96

Returns the downcasted int96 from int256, reverting on overflow (when the input is less than smallest int96 or greater than largest int96).

Counterpart to Solidity's int96 operator.

Requirements:

  • input must fit into 96 bits
function toInt96(int256 value) internal pure returns (int96 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint96

toInt88

Returns the downcasted int88 from int256, reverting on overflow (when the input is less than smallest int88 or greater than largest int88).

Counterpart to Solidity's int88 operator.

Requirements:

  • input must fit into 88 bits
function toInt88(int256 value) internal pure returns (int88 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint88

toInt80

Returns the downcasted int80 from int256, reverting on overflow (when the input is less than smallest int80 or greater than largest int80).

Counterpart to Solidity's int80 operator.

Requirements:

  • input must fit into 80 bits
function toInt80(int256 value) internal pure returns (int80 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint80

toInt72

Returns the downcasted int72 from int256, reverting on overflow (when the input is less than smallest int72 or greater than largest int72).

Counterpart to Solidity's int72 operator.

Requirements:

  • input must fit into 72 bits
function toInt72(int256 value) internal pure returns (int72 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint72

toInt64

Returns the downcasted int64 from int256, reverting on overflow (when the input is less than smallest int64 or greater than largest int64).

Counterpart to Solidity's int64 operator.

Requirements:

  • input must fit into 64 bits
function toInt64(int256 value) internal pure returns (int64 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint64

toInt56

Returns the downcasted int56 from int256, reverting on overflow (when the input is less than smallest int56 or greater than largest int56).

Counterpart to Solidity's int56 operator.

Requirements:

  • input must fit into 56 bits
function toInt56(int256 value) internal pure returns (int56 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint56

toInt48

Returns the downcasted int48 from int256, reverting on overflow (when the input is less than smallest int48 or greater than largest int48).

Counterpart to Solidity's int48 operator.

Requirements:

  • input must fit into 48 bits
function toInt48(int256 value) internal pure returns (int48 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint48

toInt40

Returns the downcasted int40 from int256, reverting on overflow (when the input is less than smallest int40 or greater than largest int40).

Counterpart to Solidity's int40 operator.

Requirements:

  • input must fit into 40 bits
function toInt40(int256 value) internal pure returns (int40 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint40

toInt32

Returns the downcasted int32 from int256, reverting on overflow (when the input is less than smallest int32 or greater than largest int32).

Counterpart to Solidity's int32 operator.

Requirements:

  • input must fit into 32 bits
function toInt32(int256 value) internal pure returns (int32 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint32

toInt24

Returns the downcasted int24 from int256, reverting on overflow (when the input is less than smallest int24 or greater than largest int24).

Counterpart to Solidity's int24 operator.

Requirements:

  • input must fit into 24 bits
function toInt24(int256 value) internal pure returns (int24 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint24

toInt16

Returns the downcasted int16 from int256, reverting on overflow (when the input is less than smallest int16 or greater than largest int16).

Counterpart to Solidity's int16 operator.

Requirements:

  • input must fit into 16 bits
function toInt16(int256 value) internal pure returns (int16 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint16

toInt8

Returns the downcasted int8 from int256, reverting on overflow (when the input is less than smallest int8 or greater than largest int8).

Counterpart to Solidity's int8 operator.

Requirements:

  • input must fit into 8 bits
function toInt8(int256 value) internal pure returns (int8 downcasted);

Parameters

NameTypeDescription
valueint256

Returns

NameTypeDescription
downcastedint8

toInt256

Converts an unsigned uint256 into a signed int256.

Requirements:

  • input must be less than or equal to maxInt256.
function toInt256(uint256 value) internal pure returns (int256);

Parameters

NameTypeDescription
valueuint256

Returns

NameTypeDescription
<none>int256

toUint

Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.

function toUint(bool b) internal pure returns (uint256 u);

Parameters

NameTypeDescription
bbool

Returns

NameTypeDescription
uuint256

Errors

SafeCastOverflowedUintDowncast

Value doesn't fit in a uint of bits size.

error SafeCastOverflowedUintDowncast(uint8 bits, uint256 value);

Parameters

NameTypeDescription
bitsuint8
valueuint256

SafeCastOverflowedIntToUint

An int value doesn't fit in a uint of bits size.

error SafeCastOverflowedIntToUint(int256 value);

Parameters

NameTypeDescription
valueint256

SafeCastOverflowedIntDowncast

Value doesn't fit in an int of bits size.

error SafeCastOverflowedIntDowncast(uint8 bits, int256 value);

Parameters

NameTypeDescription
bitsuint8
valueint256

SafeCastOverflowedUintToInt

A uint value doesn't fit in an int of bits size.

error SafeCastOverflowedUintToInt(uint256 value);

Parameters

NameTypeDescription
valueuint256