Skip to content

Commit

Permalink
move helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-carroll committed Jul 17, 2024
1 parent 5aa70a3 commit 41d70d7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
59 changes: 4 additions & 55 deletions src/Orders.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
53 changes: 52 additions & 1 deletion src/UsesPermit2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 41d70d7

Please sign in to comment.