Skip to content

Commit

Permalink
add calldata decoder fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snreynolds committed Oct 24, 2024
1 parent eaeba07 commit 6a66d73
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/libraries/CalldataDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,40 @@ contract CalldataDecoderTest is Test {
assertEq(mintParams.tickUpper, _config.tickUpper);
}

function test_fuzz_decodeMintFromAmountsParams(
PositionConfig calldata _config,
uint128 _amount0,
uint128 _amount1,
uint128 _amount0Max,
uint128 _amount1Max,
address _owner,
bytes calldata _hookData
) public view {
bytes memory params = abi.encode(
_config.poolKey,
_config.tickLower,
_config.tickUpper,
_amount0,
_amount1,
_amount0Max,
_amount1Max,
_owner,
_hookData
);

(MockCalldataDecoder.MintFromAmountsParams memory mintParams) = decoder.decodeMintFromAmountsParams(params);

_assertEq(mintParams.poolKey, _config.poolKey);
assertEq(mintParams.tickLower, _config.tickLower);
assertEq(mintParams.tickUpper, _config.tickUpper);
assertEq(mintParams.amount0, _amount0);
assertEq(mintParams.amount1, _amount1);
assertEq(mintParams.amount0Max, _amount0Max);
assertEq(mintParams.amount1Max, _amount1Max);
assertEq(mintParams.owner, _owner);
assertEq(mintParams.hookData, _hookData);
}

function test_fuzz_decodeSwapExactInParams(IV4Router.ExactInputParams calldata _swapParams) public view {
bytes memory params = abi.encode(_swapParams);
IV4Router.ExactInputParams memory swapParams = decoder.decodeSwapExactInParams(params);
Expand Down Expand Up @@ -232,6 +266,32 @@ contract CalldataDecoderTest is Test {
assertEq(amount, _amount);
}

function test_fuzz_decodeIncreaseLiquidityFromAmountsParams(
uint256 _tokenId,
uint128 _amount0,
uint128 _amount1,
uint128 _amount0Max,
uint128 _amount1Max,
bytes calldata _hookData
) public view {
bytes memory params = abi.encode(_tokenId, _amount0, _amount1, _amount0Max, _amount1Max, _hookData);

(
uint256 tokenId,
uint128 amount0,
uint128 amount1,
uint128 amount0Max,
uint128 amount1Max,
bytes memory hookData
) = decoder.decodeIncreaseLiquidityFromAmountsParams(params);
assertEq(_tokenId, tokenId);
assertEq(_amount0, amount0);
assertEq(_amount1, amount1);
assertEq(_amount0Max, amount0Max);
assertEq(_amount1Max, amount1Max);
assertEq(_hookData, hookData);
}

function _assertEq(PathKey[] memory path1, PathKey[] memory path2) internal pure {
assertEq(path1.length, path2.length);
for (uint256 i = 0; i < path1.length; i++) {
Expand Down
56 changes: 56 additions & 0 deletions test/mocks/MockCalldataDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ contract MockCalldataDecoder {
bytes hookData;
}

struct MintFromAmountsParams {
PoolKey poolKey;
int24 tickLower;
int24 tickUpper;
uint128 amount0;
uint128 amount1;
uint128 amount0Max;
uint128 amount1Max;
address owner;
bytes hookData;
}

function decodeActionsRouterParams(bytes calldata params)
external
pure
Expand Down Expand Up @@ -136,4 +148,48 @@ contract MockCalldataDecoder {
{
return params.decodeCurrencyAddressAndUint256();
}

function decodeIncreaseLiquidityFromAmountsParams(bytes calldata params)
external
pure
returns (
uint256 tokenId,
uint128 amount0,
uint128 amount1,
uint128 amount0Max,
uint128 amount1Max,
bytes calldata hookData
)
{
return params.decodeIncreaseLiquidityFromAmountsParams();
}

function decodeMintFromAmountsParams(bytes calldata params)
external
pure
returns (MintFromAmountsParams memory mintParams)
{
(
PoolKey memory poolKey,
int24 tickLower,
int24 tickUpper,
uint128 amount0,
uint128 amount1,
uint128 amount0Max,
uint128 amount1Max,
address owner,
bytes memory hookData
) = params.decodeMintFromAmountsParams();
return MintFromAmountsParams({
poolKey: poolKey,
tickLower: tickLower,
tickUpper: tickUpper,
amount0: amount0,
amount1: amount1,
amount0Max: amount0Max,
amount1Max: amount1Max,
owner: owner,
hookData: hookData
});
}
}

0 comments on commit 6a66d73

Please sign in to comment.