-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLibSafeCast.sol
65 lines (57 loc) · 1.98 KB
/
LibSafeCast.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// SPDX-License-Identifier: Beta Software
pragma solidity ^0.8;
library LibSafeCast {
error Uint256ToUint96CastOutOfRange(uint256 v);
error Uint256ToInt192CastOutOfRange(uint256 v);
error Int192ToUint96CastOutOfRange(int192 i192);
error Uint256ToInt128CastOutOfRangeError(uint256 u256);
error Uint256ToUint128CastOutOfRangeError(uint256 u256);
error Uint256ToUint40CastOutOfRangeError(uint256 u256);
function safeCastUint256ToUint96(uint256 v) internal pure returns (uint96) {
if (v > uint256(type(uint96).max)) {
revert Uint256ToUint96CastOutOfRange(v);
}
return uint96(v);
}
function safeCastUint256ToUint128(uint256 v) internal pure returns (uint128) {
if (v > uint256(type(uint128).max)) {
revert Uint256ToUint128CastOutOfRangeError(v);
}
return uint128(v);
}
function safeCastUint256ToInt192(uint256 v) internal pure returns (int192) {
if (v > uint256(uint192(type(int192).max))) {
revert Uint256ToInt192CastOutOfRange(v);
}
return int192(uint192(v));
}
function safeCastUint96ToInt192(uint96 v) internal pure returns (int192) {
return int192(uint192(v));
}
function safeCastInt192ToUint96(int192 i192) internal pure returns (uint96) {
if (i192 < 0 || i192 > int192(uint192(type(uint96).max))) {
revert Int192ToUint96CastOutOfRange(i192);
}
return uint96(uint192(i192));
}
function safeCastUint256ToInt128(uint256 x)
internal
pure
returns (int128)
{
if (x > uint256(uint128(type(int128).max))) {
revert Uint256ToInt128CastOutOfRangeError(x);
}
return int128(uint128(x));
}
function safeCastUint256ToUint40(uint256 x)
internal
pure
returns (uint40)
{
if (x > uint256(type(uint40).max)) {
revert Uint256ToUint40CastOutOfRangeError(x);
}
return uint40(x);
}
}