generated from GenerationSoftware/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PermitAndMulticall.sol
66 lines (59 loc) · 1.96 KB
/
PermitAndMulticall.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import { IERC20Permit } from "openzeppelin/token/ERC20/extensions/IERC20Permit.sol";
import { Address } from "openzeppelin/utils/Address.sol";
/**
* @notice Allows a user to permit token spend and then call multiple functions on a contract.
*/
contract PermitAndMulticall {
/**
* @notice Secp256k1 signature values.
* @param deadline Timestamp at which the signature expires
* @param v `v` portion of the signature
* @param r `r` portion of the signature
* @param s `s` portion of the signature
*/
struct Signature {
uint256 deadline;
uint8 v;
bytes32 r;
bytes32 s;
}
/**
* @notice Allows a user to call multiple functions on the same contract. Useful for EOA who want to batch transactions.
* @param _data An array of encoded function calls. The calls must be abi-encoded calls to this contract.
* @return The results from each function call
*/
function _multicall(bytes[] calldata _data) internal virtual returns (bytes[] memory) {
uint256 _dataLength = _data.length;
bytes[] memory results = new bytes[](_dataLength);
for (uint256 i; i < _dataLength; i++) {
results[i] = Address.functionDelegateCall(address(this), _data[i]);
}
return results;
}
/**
* @notice Allow a user to approve an ERC20 token and run various calls in one transaction.
* @param _permitToken Address of the ERC20 token
* @param _amount Amount of tickets to approve
* @param _permitSignature Permit signature
* @param _data Datas to call with `functionDelegateCall`
*/
function _permitAndMulticall(
IERC20Permit _permitToken,
uint256 _amount,
Signature calldata _permitSignature,
bytes[] calldata _data
) internal {
_permitToken.permit(
msg.sender,
address(this),
_amount,
_permitSignature.deadline,
_permitSignature.v,
_permitSignature.r,
_permitSignature.s
);
_multicall(_data);
}
}