Skip to content

Commit

Permalink
Internal audit part 2 (#11)
Browse files Browse the repository at this point in the history
* feat: update with latest v4-periphery

* feat: forge test after latest v4-periphery

* feat: s11
  • Loading branch information
ChefMist authored Sep 11, 2024
1 parent e7198f6 commit e4361a9
Show file tree
Hide file tree
Showing 32 changed files with 45 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
145907
145982
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122802
122905
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146869
146944
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178278
178353
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148663
148738
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182089
182159
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153006
153076
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151202
151291
Original file line number Diff line number Diff line change
@@ -1 +1 @@
172507
172582
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174511
174586
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181873
181948
Original file line number Diff line number Diff line change
@@ -1 +1 @@
247743
247830
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183378
183465
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185675
185764
Original file line number Diff line number Diff line change
@@ -1 +1 @@
250542
250612
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187221
187291
Original file line number Diff line number Diff line change
@@ -1 +1 @@
100775
117028
Original file line number Diff line number Diff line change
@@ -1 +1 @@
193236
193217
Original file line number Diff line number Diff line change
@@ -1 +1 @@
192692
192673
2 changes: 1 addition & 1 deletion .forge-snapshots/UniversalRouterBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23397
23464
Original file line number Diff line number Diff line change
@@ -1 +1 @@
607626
606007
Original file line number Diff line number Diff line change
@@ -1 +1 @@
594494
593395
Original file line number Diff line number Diff line change
@@ -1 +1 @@
570249
569221
Original file line number Diff line number Diff line change
@@ -1 +1 @@
631883
630193
2 changes: 1 addition & 1 deletion lib/pancake-v4-periphery
5 changes: 3 additions & 2 deletions src/base/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {IAllowanceTransfer} from "permit2/src/interfaces/IAllowanceTransfer.sol"
import {IERC721Permit} from "pancake-v4-periphery/src/pool-cl/interfaces/IERC721Permit.sol";
import {ActionConstants} from "pancake-v4-periphery/src/libraries/ActionConstants.sol";
import {BaseActionsRouter} from "pancake-v4-periphery/src/base/BaseActionsRouter.sol";
import {CalldataDecoder} from "pancake-v4-periphery/src/libraries/CalldataDecoder.sol";

/// @title Decodes and Executes Commands
/// @notice Called by the UniversalRouter contract to efficiently decode and execute a singular command
Expand All @@ -29,6 +30,7 @@ abstract contract Dispatcher is
Lock
{
using BytesLib for bytes;
using CalldataDecoder for bytes;

error InvalidCommandType(uint256 commandType);
error BalanceTooLow();
Expand Down Expand Up @@ -293,8 +295,7 @@ abstract contract Dispatcher is
} else {
// 0x21 <= command
if (command == Commands.EXECUTE_SUB_PLAN) {
bytes calldata _commands = inputs.toBytes(0);
bytes[] calldata _inputs = inputs.toBytesArray(1);
(bytes calldata _commands, bytes[] calldata _inputs) = inputs.decodeCommandsAndInputs();
(success, output) = (address(this)).call(abi.encodeCall(Dispatcher.execute, (_commands, _inputs)));
} else if (command == Commands.STABLE_SWAP_EXACT_IN) {
// equivalent: abi.decode(inputs, (address, uint256, uint256, bytes, bytes, bool))
Expand Down
59 changes: 11 additions & 48 deletions src/libraries/BytesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
pragma solidity ^0.8.0;

import {Constants} from "./Constants.sol";
import {CalldataDecoder} from "pancake-v4-periphery/src/libraries/CalldataDecoder.sol";

library BytesLib {
using CalldataDecoder for bytes;

error SliceOutOfBounds();

/// @notice Returns the address starting at byte 0
Expand Down Expand Up @@ -35,57 +38,11 @@ library BytesLib {
}
}

/// @notice Decode the `_arg`-th element in `_bytes` as a dynamic array
/// @dev The decoding of `length` and `offset` is universal,
/// whereas the type declaration of `res` instructs the compiler how to read it.
/// @param _bytes The input bytes string to slice
/// @param _arg The index of the argument to extract
/// @return length Length of the array
/// @return offset Pointer to the data part of the array
function toLengthOffset(bytes calldata _bytes, uint256 _arg)
internal
pure
returns (uint256 length, uint256 offset)
{
uint256 relativeOffset;
assembly {
// The offset of the `_arg`-th element is `32 * arg`, which stores the offset of the length pointer.
// shl(5, x) is equivalent to mul(32, x)
let lengthPtr := add(_bytes.offset, calldataload(add(_bytes.offset, shl(5, _arg))))
length := calldataload(lengthPtr)
offset := add(lengthPtr, 0x20)
relativeOffset := sub(offset, _bytes.offset)
}
if (_bytes.length < length + relativeOffset) revert SliceOutOfBounds();
}

/// @notice Decode the `_arg`-th element in `_bytes` as `bytes`
/// @param _bytes The input bytes string to extract a bytes string from
/// @param _arg The index of the argument to extract
function toBytes(bytes calldata _bytes, uint256 _arg) internal pure returns (bytes calldata res) {
(uint256 length, uint256 offset) = toLengthOffset(_bytes, _arg);
assembly {
res.length := length
res.offset := offset
}
}

/// @notice Decode the `_arg`-th element in `_bytes` as `address[]`
/// @param _bytes The input bytes string to extract an address array from
/// @param _arg The index of the argument to extract
function toAddressArray(bytes calldata _bytes, uint256 _arg) internal pure returns (address[] calldata res) {
(uint256 length, uint256 offset) = toLengthOffset(_bytes, _arg);
assembly {
res.length := length
res.offset := offset
}
}

/// @notice Decode the `_arg`-th element in `_bytes` as `bytes[]`
/// @param _bytes The input bytes string to extract a bytes array from
/// @param _arg The index of the argument to extract
function toBytesArray(bytes calldata _bytes, uint256 _arg) internal pure returns (bytes[] calldata res) {
(uint256 length, uint256 offset) = toLengthOffset(_bytes, _arg);
(uint256 length, uint256 offset) = _bytes.toLengthOffset(_arg);
assembly {
res.length := length
res.offset := offset
Expand All @@ -96,10 +53,16 @@ library BytesLib {
/// @param _bytes The input bytes string to extract an uint array from
/// @param _arg The index of the argument to extract
function toUintArray(bytes calldata _bytes, uint256 _arg) internal pure returns (uint256[] calldata res) {
(uint256 length, uint256 offset) = toLengthOffset(_bytes, _arg);
(uint256 length, uint256 offset) = _bytes.toLengthOffset(_arg);
assembly {
res.length := length
res.offset := offset
}
}

/// @notice Equivalent to abi.decode(bytes, bytes[])
/// @param _bytes The input bytes string to extract an parameters from
function decodeCommandsAndInputs(bytes calldata _bytes) internal pure returns (bytes calldata, bytes[] calldata) {
return _bytes.decodeActionsRouterParams();
}
}
2 changes: 2 additions & 0 deletions src/modules/pancakeswap/v3/V3SwapRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import {UniversalRouterHelper} from "../../../libraries/UniversalRouterHelper.so
import {RouterImmutables} from "../../../base/RouterImmutables.sol";
import {Permit2Payments} from "../../Permit2Payments.sol";
import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {CalldataDecoder} from "pancake-v4-periphery/src/libraries/CalldataDecoder.sol";

/// @title Router for PancakeSwap v3 Trades
abstract contract V3SwapRouter is RouterImmutables, Permit2Payments, IPancakeV3SwapCallback {
using UniversalRouterHelper for bytes;
using BytesLib for bytes;
using CalldataDecoder for bytes;
using SafeCast for uint256;

error V3InvalidSwap();
Expand Down
2 changes: 1 addition & 1 deletion test/V3ToV4Migration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ contract V3ToV4MigrationTest is BasePancakeSwapV4, OldVersionHelper, BinLiquidit
_approvePermit2ForCurrency(address(this), currency0, address(binPositionManager), permit2);
_approvePermit2ForCurrency(address(this), currency1, address(binPositionManager), permit2);

clPositionManager = new CLPositionManager(vault, clPoolManager, permit2);
clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000);
_approvePermit2ForCurrency(address(this), currency0, address(clPositionManager), permit2);
_approvePermit2ForCurrency(address(this), currency1, address(clPositionManager), permit2);

Expand Down
2 changes: 1 addition & 1 deletion test/V3ToV4MigrationNative.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ contract V3ToV4MigrationNativeTest is BasePancakeSwapV4, OldVersionHelper, BinLi
binPositionManager = new BinPositionManager(vault, binPoolManager, permit2);
_approvePermit2ForCurrency(address(this), currency1, address(binPositionManager), permit2);

clPositionManager = new CLPositionManager(vault, clPoolManager, permit2);
clPositionManager = new CLPositionManager(vault, clPoolManager, permit2, 100_000);
_approvePermit2ForCurrency(address(this), currency1, address(clPositionManager), permit2);

clPoolKey = PoolKey({
Expand Down
2 changes: 1 addition & 1 deletion test/v4/CLNativePancakeSwapV4.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract CLNativePancakeSwapV4Test is BasePancakeSwapV4 {

token1 = MockERC20(Currency.unwrap(currency1));

positionManager = new CLPositionManager(vault, poolManager, permit2);
positionManager = new CLPositionManager(vault, poolManager, permit2, 100_000);
_approvePermit2ForCurrency(address(this), currency1, address(positionManager), permit2);

RouterParameters memory params = RouterParameters({
Expand Down
2 changes: 1 addition & 1 deletion test/v4/CLPancakeSwapV4.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ contract CLPancakeSwapV4Test is BasePancakeSwapV4 {
token1 = MockERC20(Currency.unwrap(currency1));
token2 = MockERC20(Currency.unwrap(currency2));

positionManager = new CLPositionManager(vault, poolManager, permit2);
positionManager = new CLPositionManager(vault, poolManager, permit2, 100_000);
_approvePermit2ForCurrency(address(this), currency0, address(positionManager), permit2);
_approvePermit2ForCurrency(address(this), currency1, address(positionManager), permit2);
_approvePermit2ForCurrency(address(this), currency2, address(positionManager), permit2);
Expand Down

0 comments on commit e4361a9

Please sign in to comment.