Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permit2 signature transfer #64

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Parameters
Licensor: DePay AG, Switzerland

Licensed Work: DePay EVM Payment Router
The Licensed Work is (c) 2023 DePay AG
The Licensed Work is (c) 2023-2024 DePay AG

Change Date: The earlier of 2025-01-01 or a date specified at
Change Date: The earlier of 2027-01-01 or a date specified at

Change License: MIT License

Expand Down
61 changes: 55 additions & 6 deletions contracts/DePayRouterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,44 @@ contract DePayRouterV2 is Ownable2Step {
return _pay(payment);
}

/// @dev Handles the payment process with permit2 approval (internal).
/// @dev Handles the payment process with permit2 SignatureTransfer.
/// @param payment The payment data.
/// @param permitTransferFromAndSignature The PermitTransferFrom and signature.
/// @return Returns true if successful.
function _pay(
IDePayRouterV2.Payment calldata payment,
IDePayRouterV2.PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) internal returns(bool){
uint256 balanceInBefore;
uint256 balanceOutBefore;

(balanceInBefore, balanceOutBefore) = _validatePreConditions(payment);
_payIn(payment, permitTransferFromAndSignature);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);

return true;
}

/// @notice Handles the payment process with permit2 SignatureTransfer for external callers.
/// @param payment The payment data.
/// @param permitTransferFromAndSignature The PermitTransferFrom and signature.
/// @return Returns true if successful.
function pay(
IDePayRouterV2.Payment calldata payment,
IDePayRouterV2.PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) external payable returns(bool){
return _pay(payment, permitTransferFromAndSignature);
}

/// @dev Handles the payment process with permit2 AllowanceTransfer.
/// @param payment The payment data.
/// @param permitSingle The permit single data.
/// @param signature The permit signature.
/// @return Returns true if successful.
function _pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) internal returns(bool){
uint256 balanceInBefore;
Expand All @@ -106,14 +136,14 @@ contract DePayRouterV2 is Ownable2Step {
return true;
}

/// @notice Handles the payment process with permit2 approval for external callers.
/// @notice Handles the payment process with permit2 AllowanceTransfer for external callers.
/// @param payment The payment data.
/// @param permitSingle The permit single data.
/// @param signature The permit signature.
/// @return Returns true if successful.
function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) external payable returns(bool){
return _pay(payment, permitSingle, signature);
Expand Down Expand Up @@ -148,7 +178,7 @@ contract DePayRouterV2 is Ownable2Step {
/// @param permitSingle The permit single data.
/// @param signature The permit signature.
function _permit(
IPermit2.PermitSingle memory permitSingle,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) internal {

Expand All @@ -164,8 +194,8 @@ contract DePayRouterV2 is Ownable2Step {
function _payIn(
IDePayRouterV2.Payment calldata payment
) internal {
// Make sure that the sender has paid in the correct token & amount
if(payment.tokenInAddress == NATIVE) {
// Make sure that the sender has paid in the correct token & amount
if(msg.value != payment.amountIn) {
revert WrongAmountPaidIn();
}
Expand All @@ -176,6 +206,25 @@ contract DePayRouterV2 is Ownable2Step {
}
}

/// @dev Processes the payIn operations (exlusively for permit2 SignatureTransfer).
/// @param payment The payment data.
/// @param permitTransferFromAndSignature permitTransferFromAndSignature for permit2 permitTransferFrom.
function _payIn(
IDePayRouterV2.Payment calldata payment,
IDePayRouterV2.PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) internal {

IPermit2(PERMIT2).permitTransferFrom(
permitTransferFromAndSignature.permitTransferFrom,
IPermit2.SignatureTransferDetails({
to: address(this),
requestedAmount: payment.amountIn
}),
msg.sender,
permitTransferFromAndSignature.signature
);
}

/// @dev Processes the payment.
/// @param payment The payment data.
function _performPayment(IDePayRouterV2.Payment calldata payment) internal {
Expand Down
12 changes: 11 additions & 1 deletion contracts/interfaces/IDePayRouterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ interface IDePayRouterV2 {
uint256 deadline;
}

struct PermitTransferFromAndSignature {
IPermit2.PermitTransferFrom permitTransferFrom;
bytes signature;
}

function pay(
Payment calldata payment
) external payable returns(bool);

function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) external payable returns(bool);

function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) external payable returns(bool);

Expand Down
18 changes: 18 additions & 0 deletions contracts/interfaces/IPermit2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ interface IPermit2 {
uint256 sigDeadline;
}

struct PermitTransferFrom {
TokenPermissions permitted;
uint256 nonce;
uint256 deadline;
}

struct TokenPermissions {
address token;
uint256 amount;
}

struct SignatureTransferDetails {
address to;
uint256 requestedAmount;
}

function permit(address owner, PermitSingle memory permitSingle, bytes calldata signature) external;

function transferFrom(address from, address to, uint160 amount, address token) external;

function permitTransferFrom(PermitTransferFrom memory permit, SignatureTransferDetails calldata transferDetails, address owner, bytes calldata signature) external;

function allowance(address user, address token, address spender) external view returns (uint160 amount, uint48 expiration, uint48 nonce);

}
30 changes: 29 additions & 1 deletion flatten/DePayForwarderV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,28 @@ interface IPermit2 {
uint256 sigDeadline;
}

struct PermitTransferFrom {
TokenPermissions permitted;
uint256 nonce;
uint256 deadline;
}

struct TokenPermissions {
address token;
uint256 amount;
}

struct SignatureTransferDetails {
address to;
uint256 requestedAmount;
}

function permit(address owner, PermitSingle memory permitSingle, bytes calldata signature) external;

function transferFrom(address from, address to, uint160 amount, address token) external;

function permitTransferFrom(PermitTransferFrom memory permit, SignatureTransferDetails calldata transferDetails, address owner, bytes calldata signature) external;

function allowance(address user, address token, address spender) external view returns (uint160 amount, uint48 expiration, uint48 nonce);

}
Expand Down Expand Up @@ -764,13 +782,23 @@ interface IDePayRouterV2 {
uint256 deadline;
}

struct PermitTransferFromAndSignature {
IPermit2.PermitTransferFrom permitTransferFrom;
bytes signature;
}

function pay(
Payment calldata payment
) external payable returns(bool);

function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) external payable returns(bool);

function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) external payable returns(bool);

Expand Down
91 changes: 84 additions & 7 deletions flatten/DePayRouterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,28 @@ interface IPermit2 {
uint256 sigDeadline;
}

struct PermitTransferFrom {
TokenPermissions permitted;
uint256 nonce;
uint256 deadline;
}

struct TokenPermissions {
address token;
uint256 amount;
}

struct SignatureTransferDetails {
address to;
uint256 requestedAmount;
}

function permit(address owner, PermitSingle memory permitSingle, bytes calldata signature) external;

function transferFrom(address from, address to, uint160 amount, address token) external;

function permitTransferFrom(PermitTransferFrom memory permit, SignatureTransferDetails calldata transferDetails, address owner, bytes calldata signature) external;

function allowance(address user, address token, address spender) external view returns (uint160 amount, uint48 expiration, uint48 nonce);

}
Expand Down Expand Up @@ -764,13 +782,23 @@ interface IDePayRouterV2 {
uint256 deadline;
}

struct PermitTransferFromAndSignature {
IPermit2.PermitTransferFrom permitTransferFrom;
bytes signature;
}

function pay(
Payment calldata payment
) external payable returns(bool);

function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) external payable returns(bool);

function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) external payable returns(bool);

Expand Down Expand Up @@ -894,14 +922,44 @@ contract DePayRouterV2 is Ownable2Step {
return _pay(payment);
}

/// @dev Handles the payment process with permit2 approval (internal).
/// @dev Handles the payment process with permit2 SignatureTransfer.
/// @param payment The payment data.
/// @param permitTransferFromAndSignature The PermitTransferFrom and signature.
/// @return Returns true if successful.
function _pay(
IDePayRouterV2.Payment calldata payment,
IDePayRouterV2.PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) internal returns(bool){
uint256 balanceInBefore;
uint256 balanceOutBefore;

(balanceInBefore, balanceOutBefore) = _validatePreConditions(payment);
_payIn(payment, permitTransferFromAndSignature);
_performPayment(payment);
_validatePostConditions(payment, balanceInBefore, balanceOutBefore);

return true;
}

/// @notice Handles the payment process with permit2 SignatureTransfer for external callers.
/// @param payment The payment data.
/// @param permitTransferFromAndSignature The PermitTransferFrom and signature.
/// @return Returns true if successful.
function pay(
IDePayRouterV2.Payment calldata payment,
IDePayRouterV2.PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) external payable returns(bool){
return _pay(payment, permitTransferFromAndSignature);
}

/// @dev Handles the payment process with permit2 AllowanceTransfer.
/// @param payment The payment data.
/// @param permitSingle The permit single data.
/// @param signature The permit signature.
/// @return Returns true if successful.
function _pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) internal returns(bool){
uint256 balanceInBefore;
Expand All @@ -916,14 +974,14 @@ contract DePayRouterV2 is Ownable2Step {
return true;
}

/// @notice Handles the payment process with permit2 approval for external callers.
/// @notice Handles the payment process with permit2 AllowanceTransfer for external callers.
/// @param payment The payment data.
/// @param permitSingle The permit single data.
/// @param signature The permit signature.
/// @return Returns true if successful.
function pay(
IDePayRouterV2.Payment calldata payment,
IPermit2.PermitSingle memory permitSingle,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) external payable returns(bool){
return _pay(payment, permitSingle, signature);
Expand Down Expand Up @@ -958,7 +1016,7 @@ contract DePayRouterV2 is Ownable2Step {
/// @param permitSingle The permit single data.
/// @param signature The permit signature.
function _permit(
IPermit2.PermitSingle memory permitSingle,
IPermit2.PermitSingle calldata permitSingle,
bytes calldata signature
) internal {

Expand All @@ -974,8 +1032,8 @@ contract DePayRouterV2 is Ownable2Step {
function _payIn(
IDePayRouterV2.Payment calldata payment
) internal {
// Make sure that the sender has paid in the correct token & amount
if(payment.tokenInAddress == NATIVE) {
// Make sure that the sender has paid in the correct token & amount
if(msg.value != payment.amountIn) {
revert WrongAmountPaidIn();
}
Expand All @@ -986,6 +1044,25 @@ contract DePayRouterV2 is Ownable2Step {
}
}

/// @dev Processes the payIn operations (exlusively for permit2 SignatureTransfer).
/// @param payment The payment data.
/// @param permitTransferFromAndSignature permitTransferFromAndSignature for permit2 permitTransferFrom.
function _payIn(
IDePayRouterV2.Payment calldata payment,
IDePayRouterV2.PermitTransferFromAndSignature calldata permitTransferFromAndSignature
) internal {

IPermit2(PERMIT2).permitTransferFrom(
permitTransferFromAndSignature.permitTransferFrom,
IPermit2.SignatureTransferDetails({
to: address(this),
requestedAmount: payment.amountIn
}),
msg.sender,
permitTransferFromAndSignature.signature
);
}

/// @dev Processes the payment.
/// @param payment The payment data.
function _performPayment(IDePayRouterV2.Payment calldata payment) internal {
Expand Down
Loading
Loading