Skip to content

Commit

Permalink
final decode in calldata (#363)
Browse files Browse the repository at this point in the history
* batch permit decode in calldata

* array of structs

* PR comment
  • Loading branch information
hensha256 authored Aug 2, 2024
1 parent 297efe5 commit b143c03
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 68 deletions.
16 changes: 12 additions & 4 deletions contracts/base/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ abstract contract Dispatcher is Payments, V2SwapRouter, V3SwapRouter, V3ToV4Migr
}
permit2TransferFrom(token, msgSender(), map(recipient), amount);
} else if (command == Commands.PERMIT2_PERMIT_BATCH) {
(IAllowanceTransfer.PermitBatch memory permitBatch,) =
abi.decode(inputs, (IAllowanceTransfer.PermitBatch, bytes));
IAllowanceTransfer.PermitBatch calldata permitBatch;
assembly {
// this is a variable length struct, so calldataload(inputs.offset) contains the
// offset from inputs.offset at which the struct begins
permitBatch := add(inputs.offset, calldataload(inputs.offset))
}
bytes calldata data = inputs.toBytes(1);
PERMIT2.permit(msgSender(), permitBatch, data);
} else if (command == Commands.SWEEP) {
Expand Down Expand Up @@ -188,8 +192,12 @@ abstract contract Dispatcher is Payments, V2SwapRouter, V3SwapRouter, V3ToV4Migr
}
Payments.unwrapWETH9(map(recipient), amountMin);
} else if (command == Commands.PERMIT2_TRANSFER_FROM_BATCH) {
(IAllowanceTransfer.AllowanceTransferDetails[] memory batchDetails) =
abi.decode(inputs, (IAllowanceTransfer.AllowanceTransferDetails[]));
IAllowanceTransfer.AllowanceTransferDetails[] calldata batchDetails;
(uint256 length, uint256 offset) = inputs.toLengthOffset(0);
assembly {
batchDetails.length := length
batchDetails.offset := offset
}
permit2TransferFrom(batchDetails, msgSender());
} else if (command == Commands.BALANCE_CHECK_ERC20) {
// equivalent: abi.decode(inputs, (address, address, uint256))
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/Permit2Payments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract contract Permit2Payments is Payments {

/// @notice Performs a batch transferFrom on Permit2
/// @param batchDetails An array detailing each of the transfers that should occur
function permit2TransferFrom(IAllowanceTransfer.AllowanceTransferDetails[] memory batchDetails, address owner)
function permit2TransferFrom(IAllowanceTransfer.AllowanceTransferDetails[] calldata batchDetails, address owner)
internal
{
uint256 batchLength = batchDetails.length;
Expand Down
2 changes: 1 addition & 1 deletion contracts/modules/uniswap/v2/UniswapV2Library.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ library UniswapV2Library {
/// @param path The path of the multi-hop trade
/// @return amount The input amount of the input token
/// @return pair The first pair in the trade
function getAmountInMultihop(address factory, bytes32 initCodeHash, uint256 amountOut, address[] memory path)
function getAmountInMultihop(address factory, bytes32 initCodeHash, uint256 amountOut, address[] calldata path)
internal
view
returns (uint256 amount, address pair)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`Check Ownership Gas gas: balance check ERC20 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 37644,
"gasUsed": 37639,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,48 @@
exports[`Payments Gas Tests Individual Command Tests gas: SWEEP with ERC20 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 37013,
"gasUsed": 36947,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: SWEEP_WITH_FEE 1`] = `
Object {
"calldataByteLength": 516,
"gasUsed": 65690,
"gasUsed": 65568,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: TRANSFER with ERC20 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 36038,
"gasUsed": 35972,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: TRANSFER with ETH 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 31609,
"gasUsed": 31543,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: UNWRAP_WETH 1`] = `
Object {
"calldataByteLength": 324,
"gasUsed": 44577,
"gasUsed": 44505,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: UNWRAP_WETH_WITH_FEE 1`] = `
Object {
"calldataByteLength": 644,
"gasUsed": 50905,
"gasUsed": 50721,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: WRAP_ETH 1`] = `
Object {
"calldataByteLength": 324,
"gasUsed": 53402,
"gasUsed": 53344,
}
`;
Loading

0 comments on commit b143c03

Please sign in to comment.