-
Notifications
You must be signed in to change notification settings - Fork 504
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
8 changed files
with
87 additions
and
6 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
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,10 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.0; | ||
|
||
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; | ||
|
||
/// @title Interface for ImmutableState | ||
interface IImmutableState { | ||
/// @notice The Uniswap v4 PoolManager contract | ||
function poolManager() external view returns (IPoolManager); | ||
} |
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,17 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
/// @title For calculating a percentage of an amount, using bips | ||
library BipsLibrary { | ||
uint256 internal constant BPS_DENOMINATOR = 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 > BPS_DENOMINATOR) revert InvalidBips(); | ||
return (amount * bips) / BPS_DENOMINATOR; | ||
} | ||
} |
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,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import "forge-std/Test.sol"; | ||
import {BipsLibrary} from "../../src/libraries/BipsLibrary.sol"; | ||
|
||
contract BipsLibraryTest is Test { | ||
using BipsLibrary for uint256; | ||
|
||
// The block gas limit set in foundry config is 300_000_000 (300M) for testing purposes | ||
uint256 BLOCK_GAS_LIMIT; | ||
|
||
function setUp() public { | ||
BLOCK_GAS_LIMIT = block.gaslimit; | ||
} | ||
|
||
function test_fuzz_calculatePortion(uint256 amount, uint256 bips) public { | ||
amount = bound(amount, 0, uint256(type(uint128).max)); | ||
if (bips > BipsLibrary.BPS_DENOMINATOR) { | ||
vm.expectRevert(BipsLibrary.InvalidBips.selector); | ||
amount.calculatePortion(bips); | ||
} else { | ||
assertEq(amount.calculatePortion(bips), amount * bips / BipsLibrary.BPS_DENOMINATOR); | ||
} | ||
} | ||
|
||
function test_fuzz_gasLimit(uint256 bips) public { | ||
if (bips > BipsLibrary.BPS_DENOMINATOR) { | ||
vm.expectRevert(BipsLibrary.InvalidBips.selector); | ||
block.gaslimit.calculatePortion(bips); | ||
} else { | ||
assertEq(block.gaslimit.calculatePortion(bips), BLOCK_GAS_LIMIT * bips / BipsLibrary.BPS_DENOMINATOR); | ||
} | ||
} | ||
|
||
function test_gasLimit_100_percent() public view { | ||
assertEq(block.gaslimit, block.gaslimit.calculatePortion(10_000)); | ||
} | ||
|
||
function test_gasLimit_1_percent() public view { | ||
// 100 bps = 1% | ||
// 1% of 30M is 300K | ||
assertEq(BLOCK_GAS_LIMIT / 100, block.gaslimit.calculatePortion(100)); | ||
} | ||
|
||
function test_gasLimit_1BP() public view { | ||
// 1bp is 0.01% | ||
// 0.01% of 30M is 300 | ||
assertEq(BLOCK_GAS_LIMIT / 10000, block.gaslimit.calculatePortion(1)); | ||
} | ||
} |
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