-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
7775 | ||
7995 |
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,17 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.19; | ||
|
||
/// @title For calculating a percentage of an amount, using bips | ||
library BipsLibrary { | ||
uint256 internal constant BIPS_BASE = 10_000; | ||
|
||
/// @notice emitted when an invalid percentage is provided | ||
error InvalidBips(); | ||
|
||
/// @param amount The total amount to calculate a percentage of | ||
/// @param bips The percentage to calculate, in bips | ||
function calculatePortion(uint256 amount, uint256 bips) internal pure returns (uint256) { | ||
if (bips > BIPS_BASE) revert InvalidBips(); | ||
return (amount * bips) / BIPS_BASE; | ||
} | ||
} |
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,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
import "forge-std/StdError.sol"; | ||
import {BipsLibrary} from "../../src/libraries/BipsLibrary.sol"; | ||
|
||
contract PositionConfigTest is Test { | ||
using BipsLibrary for uint256; | ||
|
||
function test_fuzz_calculatePortion(uint256 amount, uint256 bips) public { | ||
amount = bound(amount, 0, uint256(type(uint128).max)); | ||
if (bips > BipsLibrary.BIPS_BASE) { | ||
vm.expectRevert(BipsLibrary.InvalidBips.selector); | ||
amount.calculatePortion(bips); | ||
} else { | ||
assertEq(amount.calculatePortion(bips), amount * bips / BipsLibrary.BIPS_BASE); | ||
} | ||
} | ||
} |
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