-
-
Save PaulRBerg/cdc99146726a3dba404ccab1cd795c33 to your computer and use it in GitHub Desktop.
UD60x18 user-defined value type in Solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: Unlicense | |
pragma solidity >=0.8.9; | |
/// @notice The unsigned 60.18-decimal fixed-point representation. | |
/// Can have up to 60 digits and up to 18 decimals. | |
type UD60x18 is uint256; | |
using { add, and, eq, gt, gte, leftShift, lt, lte, isZero, rightShift, uncheckedAdd } for UD60x18 global; | |
/// @notice Implements checked addition in the UD60x18 type. | |
function add(UD60x18 x, UD60x18 y) pure returns (UD60x18) { | |
return UD60x18.wrap(UD60x18.unwrap(x) + UD60x18.unwrap(y)); | |
} | |
/// @notice Implements the AND bitwise operation in the UD60x18 type. | |
function and(UD60x18 x, uint256 bits) pure returns (UD60x18) { | |
return UD60x18.wrap(UD60x18.unwrap(x) & bits); | |
} | |
/// @notice Implements the equal (=) operation in the UD60x18 type. | |
function eq(UD60x18 x, UD60x18 y) pure returns (bool) { | |
return UD60x18.unwrap(x) == UD60x18.unwrap(y); | |
} | |
function eq(UD60x18 x, uint256 y) pure returns (bool) { | |
return UD60x18.unwrap(x) == UD60x18.unwrap(y); | |
} | |
/// @notice Implements the greater than (>) operation in the UD60x18 type. | |
function gt(UD60x18 x, UD60x18 y) pure returns (bool) { | |
return UD60x18.unwrap(x) > UD60x18.unwrap(y); | |
} | |
/// @notice Implements the greater than or equal to (>=) operation in the UD60x18 type. | |
function gte(UD60x18 x, UD60x18 y) pure returns (bool) { | |
return UD60x18.unwrap(x) >= UD60x18.unwrap(y); | |
} | |
/// @notice Implements a zero comparison check function in the UD60x18 type. | |
function isZero(UD60x18 x) pure returns (bool) { | |
return UD60x18.unwrap(x) == 0; | |
} | |
/// @notice Implements the left shift operation in the UD60x18 type. | |
function leftShift(UD60x18 x, uint256 bits) pure returns (UD60x18) { | |
return UD60x18.wrap(UD60x18.unwrap(x) << bits); | |
} | |
/// @notice Implements the lower than (<) operation in the UD60x18 type. | |
function lt(UD60x18 x, UD60x18 y) pure returns (bool) { | |
return UD60x18.unwrap(x) < UD60x18.unwrap(y); | |
} | |
/// @notice Implements the lower than or equal to (<=) operation in the UD60x18 type. | |
function lte(UD60x18 x, UD60x18 y) pure returns (bool) { | |
return UD60x18.unwrap(x) <= UD60x18.unwrap(y); | |
} | |
/// @notice Implements the right shift operation in the UD60x18 type. | |
function rightShift(UD60x18 x, uint256 bits) pure returns (UD60x18) { | |
return UD60x18.wrap(UD60x18.unwrap(x) >> bits); | |
} | |
/// @notice Implements unchecked addition in the UD60x18 type. | |
function uncheckedAdd(UD60x18 x, UD60x18 y) pure returns (UD60x18) { | |
unchecked { | |
return UD60x18.wrap(UD60x18.unwrap(x) + UD60x18.unwrap(y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment