-
Notifications
You must be signed in to change notification settings - Fork 149
/
CometMath.sol
61 lines (50 loc) · 1.61 KB
/
CometMath.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.15;
/**
* @title Compound's Comet Math Contract
* @dev Pure math functions
* @author Compound
*/
contract CometMath {
/** Custom errors **/
error InvalidUInt64();
error InvalidUInt104();
error InvalidUInt128();
error InvalidInt104();
error InvalidInt256();
error NegativeNumber();
function safe64(uint n) internal pure returns (uint64) {
if (n > type(uint64).max) revert InvalidUInt64();
return uint64(n);
}
function safe104(uint n) internal pure returns (uint104) {
if (n > type(uint104).max) revert InvalidUInt104();
return uint104(n);
}
function safe128(uint n) internal pure returns (uint128) {
if (n > type(uint128).max) revert InvalidUInt128();
return uint128(n);
}
function signed104(uint104 n) internal pure returns (int104) {
if (n > uint104(type(int104).max)) revert InvalidInt104();
return int104(n);
}
function signed256(uint256 n) internal pure returns (int256) {
if (n > uint256(type(int256).max)) revert InvalidInt256();
return int256(n);
}
function unsigned104(int104 n) internal pure returns (uint104) {
if (n < 0) revert NegativeNumber();
return uint104(n);
}
function unsigned256(int256 n) internal pure returns (uint256) {
if (n < 0) revert NegativeNumber();
return uint256(n);
}
function toUInt8(bool x) internal pure returns (uint8) {
return x ? 1 : 0;
}
function toBool(uint8 x) internal pure returns (bool) {
return x != 0;
}
}