-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add value types SD1x18 and UD2x18
- Loading branch information
Showing
8 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.13; | ||
|
||
/// @notice The signed 1.18-decimal fixed-point number representation, which can have up to 1 digit and up to 18 decimals. | ||
/// The values of this are bound by the minimum and the maximum values permitted by the underlying Solidity type int64. | ||
/// This is useful when end users want to use int64 to save gas, e.g. with tight variable packing in contract storage. | ||
type SD1x18 is int64; | ||
|
||
/*////////////////////////////////////////////////////////////////////////// | ||
CONVERSION FUNCTIONS | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
|
||
/// @notice Wraps a signed integer into the SD1x18 type. | ||
function sd1x18(int64 x) pure returns (SD1x18 result) { | ||
result = SD1x18.wrap(x); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.13; | ||
|
||
/// @notice The unsigned 2.18-decimal fixed-point number representation, which can have up to 2 digits and up to 18 decimals. | ||
/// The values of this are bound by the minimum and the maximum values permitted by the underlying Solidity type uint64. | ||
/// This is useful when end users want to use uint64 to save gas, e.g. with tight variable packing in contract storage. | ||
type UD2x18 is uint64; | ||
|
||
/*////////////////////////////////////////////////////////////////////////// | ||
CONVERSION FUNCTIONS | ||
//////////////////////////////////////////////////////////////////////////*/ | ||
|
||
/// @notice Wraps a signed integer into the UD2x18 type. | ||
function ud2x18(uint64 x) pure returns (UD2x18 result) { | ||
result = UD2x18.wrap(x); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.13; | ||
|
||
import "src/SD1x18.sol"; | ||
import { BaseTest } from "../../BaseTest.t.sol"; | ||
|
||
/// @dev Collection of tests for the conversion functions available in the SD1x18 type. | ||
contract SD1x18__ConversionTest is BaseTest { | ||
function testSd1x18(int64 x) external { | ||
SD1x18 actual = sd1x18(x); | ||
SD1x18 expected = SD1x18.wrap(x); | ||
assertEq(actual, expected); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.13; | ||
|
||
import "src/UD2x18.sol"; | ||
import { BaseTest } from "../../BaseTest.t.sol"; | ||
|
||
/// @dev Collection of tests for the conversion functions available in the UD2x18 type. | ||
contract UD2x18__ConversionTest is BaseTest { | ||
function testUd2x18(uint64 x) external { | ||
UD2x18 actual = ud2x18(x); | ||
UD2x18 expected = UD2x18.wrap(x); | ||
assertEq(actual, expected); | ||
} | ||
} |