From 41d70d7707dbde798c6f23fdf263fba2b1d452eb Mon Sep 17 00:00:00 2001 From: Anna Carroll Date: Wed, 17 Jul 2024 14:55:10 +0100 Subject: [PATCH] move helpers --- src/Orders.sol | 59 +++------------------------------------------ src/UsesPermit2.sol | 53 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/Orders.sol b/src/Orders.sol index 49ef11c..7d0e094 100644 --- a/src/Orders.sol +++ b/src/Orders.sol @@ -67,31 +67,11 @@ abstract contract OrderDestination is OutputPermit2 { /// @custom:emits Filled function fillPermit2(Output[] memory outputs, Permit2 calldata permit2) external { // via permit2 contract, transfer all tokens to the tokenRecipient (includes check on permit2 nonce & deadline) - _permitWitnessTransferFrom(outputs, _generateTransferDetails(outputs, permit2.permit.permitted), permit2); + _permitWitnessTransferFrom(outputs, _generateFillTransferDetails(outputs, permit2.permit.permitted), permit2); // emit emit Filled(outputs); } - - /// @notice transform Output and TokenPermissions structs to TransferDetails structs, for passing to permit2. - /// @dev always transfers the full permitted amount. - /// @param outputs - the Outputs to transform. - /// @param permitted - the TokenPermissions to transform. - /// @return transferDetails - the SignatureTransferDetails generated. - function _generateTransferDetails(Output[] memory outputs, ISignatureTransfer.TokenPermissions[] calldata permitted) - internal - pure - returns (ISignatureTransfer.SignatureTransferDetails[] memory transferDetails) - { - require(permitted.length == outputs.length); - transferDetails = new ISignatureTransfer.SignatureTransferDetails[](permitted.length); - for (uint256 i; i < permitted.length; i++) { - require(permitted[i].token == outputs[i].token); - require(permitted[i].amount == outputs[i].amount); - // ensure the tokens are being transferred to this address - transferDetails[i] = ISignatureTransfer.SignatureTransferDetails(outputs[i].recipient, outputs[i].amount); - } - } } /// @notice Contract capable of registering initiation of intent-based Orders. @@ -160,7 +140,9 @@ abstract contract OrderOrigin is OutputPermit2 { Output[] memory outputs, /* signed via permit2 witness */ Permit2 calldata permit2 ) external { - _permitWitnessTransferFrom(outputs, _generateTransferDetails(tokenRecipient, permit2.permit.permitted), permit2); + _permitWitnessTransferFrom( + outputs, _generateInitiateTransferDetails(tokenRecipient, permit2.permit.permitted), permit2 + ); // emit emit Order(permit2.permit.deadline, _generateInputs(permit2.permit.permitted), outputs); @@ -187,39 +169,6 @@ abstract contract OrderOrigin is OutputPermit2 { } emit Sweep(recipient, token, balance); } - - /// @notice transform TokenPermissions structs to TransferDetails structs, for passing to permit2. - /// @dev must provide a `tokenRecipient` field to receive the permitted tokens. - /// @dev always transfers the full permitted amount. - /// @param tokenRecipient - the `to` field for the TransferDetails that will receive the tokens. - /// @param permitted - the TokenPermissions to transform. - /// @return transferDetails - the SignatureTransferDetails generated. - function _generateTransferDetails(address tokenRecipient, ISignatureTransfer.TokenPermissions[] calldata permitted) - internal - pure - returns (ISignatureTransfer.SignatureTransferDetails[] memory transferDetails) - { - transferDetails = new ISignatureTransfer.SignatureTransferDetails[](permitted.length); - for (uint256 i; i < permitted.length; i++) { - // ensure the tokens are being transferred to this address - transferDetails[i] = ISignatureTransfer.SignatureTransferDetails(tokenRecipient, permitted[i].amount); - } - } - - /// @notice transform TokenPermissions structs to Inputs structs, for emitting. - /// @dev TokenPermissions and Inputs structs contain identical fields. - /// @param permitted - the TokenPermissions to transform. - /// @return inputs - the Inputs generated. - function _generateInputs(ISignatureTransfer.TokenPermissions[] calldata permitted) - internal - pure - returns (Input[] memory inputs) - { - inputs = new Input[](permitted.length); - for (uint256 i; i < permitted.length; i++) { - inputs[i] = Input(permitted[i].token, permitted[i].amount); - } - } } contract HostOrders is OrderDestination { diff --git a/src/UsesPermit2.sol b/src/UsesPermit2.sol index 6d5c73c..14f213b 100644 --- a/src/UsesPermit2.sol +++ b/src/UsesPermit2.sol @@ -2,7 +2,7 @@ pragma solidity ^0.8.24; import {ISignatureTransfer} from "./vendored/ISignatureTransfer.sol"; -import {Output} from "./Orders.sol"; +import {Input, Output} from "./Orders.sol"; /// @param permit - the permit2 batch token transfer details. includes a `deadline` and an unordered `nonce`. /// @param signer - the signer of the permit2, the owner of the tokens. @@ -49,4 +49,55 @@ abstract contract OutputPermit2 { } witness = keccak256(abi.encodePacked(hashes)); } + + /// @notice transform Output and TokenPermissions structs to TransferDetails structs, for passing to permit2. + /// @dev always transfers the full permitted amount. + /// @param outputs - the Outputs to transform. + /// @param permitted - the TokenPermissions to transform. + /// @return transferDetails - the SignatureTransferDetails generated. + function _generateFillTransferDetails( + Output[] memory outputs, + ISignatureTransfer.TokenPermissions[] calldata permitted + ) internal pure returns (ISignatureTransfer.SignatureTransferDetails[] memory transferDetails) { + require(permitted.length == outputs.length); + transferDetails = new ISignatureTransfer.SignatureTransferDetails[](permitted.length); + for (uint256 i; i < permitted.length; i++) { + require(permitted[i].token == outputs[i].token); + require(permitted[i].amount == outputs[i].amount); + // ensure the tokens are being transferred to this address + transferDetails[i] = ISignatureTransfer.SignatureTransferDetails(outputs[i].recipient, outputs[i].amount); + } + } + + /// @notice transform TokenPermissions structs to TransferDetails structs, for passing to permit2. + /// @dev must provide a `tokenRecipient` field to receive the permitted tokens. + /// @dev always transfers the full permitted amount. + /// @param tokenRecipient - the `to` field for the TransferDetails that will receive the tokens. + /// @param permitted - the TokenPermissions to transform. + /// @return transferDetails - the SignatureTransferDetails generated. + function _generateInitiateTransferDetails( + address tokenRecipient, + ISignatureTransfer.TokenPermissions[] calldata permitted + ) internal pure returns (ISignatureTransfer.SignatureTransferDetails[] memory transferDetails) { + transferDetails = new ISignatureTransfer.SignatureTransferDetails[](permitted.length); + for (uint256 i; i < permitted.length; i++) { + // ensure the tokens are being transferred to this address + transferDetails[i] = ISignatureTransfer.SignatureTransferDetails(tokenRecipient, permitted[i].amount); + } + } + + /// @notice transform TokenPermissions structs to Inputs structs, for emitting. + /// @dev TokenPermissions and Inputs structs contain identical fields. + /// @param permitted - the TokenPermissions to transform. + /// @return inputs - the Inputs generated. + function _generateInputs(ISignatureTransfer.TokenPermissions[] calldata permitted) + internal + pure + returns (Input[] memory inputs) + { + inputs = new Input[](permitted.length); + for (uint256 i; i < permitted.length; i++) { + inputs[i] = Input(permitted[i].token, permitted[i].amount); + } + } }