diff --git a/packages/splits-v2/src/libraries/Cast.sol b/packages/splits-v2/src/libraries/Cast.sol index 7bd4837..2145e04 100644 --- a/packages/splits-v2/src/libraries/Cast.sol +++ b/packages/splits-v2/src/libraries/Cast.sol @@ -2,14 +2,18 @@ pragma solidity ^0.8.18; library Cast { - error CastOverflow(uint256 value); + error Overflow(); function toAddress(uint256 value) internal pure returns (address) { - if (value > type(uint160).max) revert CastOverflow(value); - return address(uint160(value)); + return address(toUint160(value)); } function toUint256(address value) internal pure returns (uint256) { return uint256(uint160(value)); } + + function toUint160(uint256 x) internal pure returns (uint160) { + if (x >= 1 << 160) revert Overflow(); + return uint160(x); + } } diff --git a/packages/splits-v2/test/warehouse/SplitsWarehouse.t.sol b/packages/splits-v2/test/warehouse/SplitsWarehouse.t.sol index 9d550c8..b2adcea 100644 --- a/packages/splits-v2/test/warehouse/SplitsWarehouse.t.sol +++ b/packages/splits-v2/test/warehouse/SplitsWarehouse.t.sol @@ -16,6 +16,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer { error ReentrancyGuardReentrantCall(); error FailedInnerCall(); error CastOverflow(uint256 value); + error Overflow(); event Withdraw( address indexed owner, address indexed token, address indexed withdrawer, uint256 amount, uint256 reward @@ -50,7 +51,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer { function test_name_Revert_whenTokenIDGreaterThanUint160() public { uint256 tokenId = uint256(type(uint160).max) + 1; - vm.expectRevert(abi.encodeWithSelector(CastOverflow.selector, tokenId)); + vm.expectRevert(Overflow.selector); warehouse.name(tokenId); } @@ -68,7 +69,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer { function test_symbol_Revert_whenTokenIDGreaterThanUint160() public { uint256 tokenId = uint256(type(uint160).max) + 1; - vm.expectRevert(abi.encodeWithSelector(CastOverflow.selector, tokenId)); + vm.expectRevert(Overflow.selector); warehouse.symbol(tokenId); } @@ -86,7 +87,7 @@ contract SplitsWarehouseTest is BaseTest, Fuzzer { function test_decimals_Revert_whenTokenIDGreaterThanUint160() public { uint256 tokenId = uint256(type(uint160).max) + 1; - vm.expectRevert(abi.encodeWithSelector(CastOverflow.selector, tokenId)); + vm.expectRevert(Overflow.selector); warehouse.decimals(tokenId); }