-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRequireUtils.sol
More file actions
271 lines (234 loc) · 12.3 KB
/
RequireUtils.sol
File metadata and controls
271 lines (234 loc) · 12.3 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.27;
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {IERC721} from "openzeppelin-contracts/contracts/token/ERC721/IERC721.sol";
import {IERC1155} from "openzeppelin-contracts/contracts/token/ERC1155/IERC1155.sol";
/// @title RequireUtils
/// @notice A set of small, composable precondition checks intended to be called from an intent's call batch.
/// @dev Each function reverts with a custom error (cheap + structured) on failure.
contract RequireUtils {
/// @notice The transaction is expired.
error Expired(uint256 expiration, uint256 timestamp);
/// @notice The ERC20 balance is too low.
error ERC20BalanceTooLow(address token, address owner, uint256 balance, uint256 minBalance);
/// @notice The ERC20 allowance is too low.
error ERC20AllowanceTooLow(address token, address owner, address spender, uint256 allowance, uint256 minAllowance);
/// @notice The ERC721 is not owned.
error ERC721NotOwner(address token, uint256 tokenId, address owner, address requiredOwner);
/// @notice The ERC721 is not approved.
error ERC721NotApproved(address token, uint256 tokenId, address owner, address spender);
/// @notice The ERC1155 balance is too low.
error ERC1155BalanceTooLow(address token, address owner, uint256 tokenId, uint256 balance, uint256 minBalance);
/// @notice The length mismatch.
error LengthMismatch(uint256 a, uint256 b);
/// @notice The ERC1155 batch balance is too low.
error ERC1155BatchBalanceTooLow(uint256 index, uint256 balance, uint256 minBalance);
/// @notice The ERC1155 is not approved.
error ERC1155NotApproved(address token, address owner, address operator);
/// @notice The native balance is too low.
error NativeBalanceTooLow(address owner, uint256 balance, uint256 minBalance);
function _requireMinBalance(address owner, uint256 minBalance) private view {
uint256 balance = owner.balance;
if (balance < minBalance) {
revert NativeBalanceTooLow(owner, balance, minBalance);
}
}
function _requireMinERC20Balance(address token, address owner, uint256 minBalance) private view {
uint256 balance = IERC20(token).balanceOf(owner);
if (balance < minBalance) {
revert ERC20BalanceTooLow(token, owner, balance, minBalance);
}
}
function _requireMinERC20Allowance(address token, address owner, address spender, uint256 minAllowance) private view {
uint256 allowance = IERC20(token).allowance(owner, spender);
if (allowance < minAllowance) {
revert ERC20AllowanceTooLow(token, owner, spender, allowance, minAllowance);
}
}
function _requireERC721Owner(address token, address requiredOwner, uint256 tokenId) private view {
address owner = IERC721(token).ownerOf(tokenId);
if (owner != requiredOwner) {
revert ERC721NotOwner(token, tokenId, owner, requiredOwner);
}
}
function _requireERC721Approval(address token, address owner, address spender, uint256 tokenId) private view {
address approved = IERC721(token).getApproved(tokenId);
if (approved != spender && !IERC721(token).isApprovedForAll(owner, spender)) {
revert ERC721NotApproved(token, tokenId, owner, spender);
}
}
function _requireMinERC1155Balance(address token, address owner, uint256 tokenId, uint256 minBalance) private view {
uint256 balance = IERC1155(token).balanceOf(owner, tokenId);
if (balance < minBalance) {
revert ERC1155BalanceTooLow(token, owner, tokenId, balance, minBalance);
}
}
function _requireMinERC1155BalanceBatch(
address token,
address owner,
uint256[] calldata tokenIds,
uint256[] calldata minBalances
) private view {
if (tokenIds.length != minBalances.length) {
revert LengthMismatch(tokenIds.length, minBalances.length);
}
uint256 length = tokenIds.length;
address[] memory accounts = new address[](length);
for (uint256 i = 0; i < length; i++) {
accounts[i] = owner;
}
uint256[] memory balances = IERC1155(token).balanceOfBatch(accounts, tokenIds);
for (uint256 i = 0; i < length; i++) {
if (balances[i] < minBalances[i]) {
revert ERC1155BatchBalanceTooLow(i, balances[i], minBalances[i]);
}
}
}
function _requireERC1155Approval(address token, address owner, address operator) private view {
bool isApproved = IERC1155(token).isApprovedForAll(owner, operator);
if (!isApproved) {
revert ERC1155NotApproved(token, owner, operator);
}
}
/// @notice Reverts if `block.timestamp` is greater than or equal to `expiration`.
function requireNonExpired(uint256 expiration) external view {
if (block.timestamp >= expiration) {
revert Expired(expiration, block.timestamp);
}
}
/// @notice Reverts if `owner` has less than `minBalance` native tokens.
function requireMinBalance(address owner, uint256 minBalance) external view {
_requireMinBalance(owner, minBalance);
}
/// @notice Reverts if `msg.sender` has less than `minBalance` native tokens.
function requireMinBalanceSelf(uint256 minBalance) external view {
_requireMinBalance(msg.sender, minBalance);
}
/// @notice Reverts if `owner` has less than `minBalance` of `token`.
function requireMinERC20Balance(address token, address owner, uint256 minBalance) external view {
_requireMinERC20Balance(token, owner, minBalance);
}
/// @notice Reverts if `msg.sender` has less than `minBalance` of `token`.
function requireMinERC20BalanceSelf(address token, uint256 minBalance) external view {
_requireMinERC20Balance(token, msg.sender, minBalance);
}
/// @notice Reverts if `owner` has granted `spender` less than `minAllowance` for `token`.
function requireMinERC20Allowance(address token, address owner, address spender, uint256 minAllowance) external view {
_requireMinERC20Allowance(token, owner, spender, minAllowance);
}
/// @notice Reverts if `msg.sender` has granted `spender` less than `minAllowance` for `token`.
function requireMinERC20AllowanceSelf(address token, address spender, uint256 minAllowance) external view {
_requireMinERC20Allowance(token, msg.sender, spender, minAllowance);
}
/// @notice Reverts if `owner` has less than `minAmount` of `token` or granted `spender` less than `minAmount` for `token`.
function requireMinERC20BalanceAllowance(address token, address owner, address spender, uint256 minAmount)
external
view
{
_requireMinERC20Balance(token, owner, minAmount);
_requireMinERC20Allowance(token, owner, spender, minAmount);
}
/// @notice Reverts if `msg.sender` has less than `minAmount` of `token` or granted `spender` less than `minAmount` for `token`.
function requireMinERC20BalanceAllowanceSelf(address token, address spender, uint256 minAmount) external view {
_requireMinERC20Balance(token, msg.sender, minAmount);
_requireMinERC20Allowance(token, msg.sender, spender, minAmount);
}
/// @notice Reverts if `owner` is not the owner of `tokenId` on `token` (ERC721).
function requireERC721Owner(address token, address owner, uint256 tokenId) external view {
_requireERC721Owner(token, owner, tokenId);
}
/// @notice Reverts if `msg.sender` is not the owner of `tokenId` on `token` (ERC721).
function requireERC721OwnerSelf(address token, uint256 tokenId) external view {
_requireERC721Owner(token, msg.sender, tokenId);
}
/// @notice Reverts if `spender` is not approved to transfer `tokenId` from `owner` on `token` (ERC721).
function requireERC721Approval(address token, address owner, address spender, uint256 tokenId) external view {
_requireERC721Approval(token, owner, spender, tokenId);
}
/// @notice Reverts if `spender` is not approved to transfer `tokenId` from `msg.sender` on `token` (ERC721).
function requireERC721ApprovalSelf(address token, address spender, uint256 tokenId) external view {
_requireERC721Approval(token, msg.sender, spender, tokenId);
}
/// @notice Reverts if `owner` is not the owner of `tokenId` on `token` (ERC721) or `spender` is not approved to transfer `tokenId` from `owner` on `token`.
function requireERC721OwnerApproval(address token, address owner, address spender, uint256 tokenId) external view {
_requireERC721Owner(token, owner, tokenId);
_requireERC721Approval(token, owner, spender, tokenId);
}
/// @notice Reverts if `msg.sender` is not the owner of `tokenId` on `token` (ERC721) or `spender` is not approved to transfer `tokenId` from `msg.sender` on `token`.
function requireERC721OwnerApprovalSelf(address token, address spender, uint256 tokenId) external view {
_requireERC721Owner(token, msg.sender, tokenId);
_requireERC721Approval(token, msg.sender, spender, tokenId);
}
/// @notice Reverts if `owner` has less than `minBalance` of `tokenId` on `token` (ERC1155).
function requireMinERC1155Balance(address token, address owner, uint256 tokenId, uint256 minBalance) external view {
_requireMinERC1155Balance(token, owner, tokenId, minBalance);
}
/// @notice Reverts if `msg.sender` has less than `minBalance` of `tokenId` on `token` (ERC1155).
function requireMinERC1155BalanceSelf(address token, uint256 tokenId, uint256 minBalance) external view {
_requireMinERC1155Balance(token, msg.sender, tokenId, minBalance);
}
/// @notice Reverts if any `tokenIds[i]` balance of `owner` is below `minBalances[i]` (ERC1155 batch).
function requireMinERC1155BalanceBatch(
address token,
address owner,
uint256[] calldata tokenIds,
uint256[] calldata minBalances
) external view {
_requireMinERC1155BalanceBatch(token, owner, tokenIds, minBalances);
}
/// @notice Reverts if any `tokenIds[i]` balance of `msg.sender` is below `minBalances[i]` (ERC1155 batch).
function requireMinERC1155BalanceBatchSelf(address token, uint256[] calldata tokenIds, uint256[] calldata minBalances)
external
view
{
_requireMinERC1155BalanceBatch(token, msg.sender, tokenIds, minBalances);
}
/// @notice Reverts if `operator` is not approved for all of `owner`'s tokens on `token` (ERC1155).
function requireERC1155Approval(address token, address owner, address operator) external view {
_requireERC1155Approval(token, owner, operator);
}
/// @notice Reverts if `operator` is not approved for all of `msg.sender`'s tokens on `token` (ERC1155).
function requireERC1155ApprovalSelf(address token, address operator) external view {
_requireERC1155Approval(token, msg.sender, operator);
}
/// @notice Reverts if `owner` has less than `minBalance` of `tokenId` on `token` (ERC1155) or `operator` is not approved to transfer `tokenId` from `owner` on `token` (ERC1155).
function requireMinERC1155BalanceApproval(
address token,
address owner,
uint256 tokenId,
uint256 minBalance,
address operator
) external view {
_requireMinERC1155Balance(token, owner, tokenId, minBalance);
_requireERC1155Approval(token, owner, operator);
}
/// @notice Reverts if `msg.sender` has less than `minBalance` of `tokenId` on `token` (ERC1155) or `operator` is not approved to transfer `tokenId` from `msg.sender` on `token` (ERC1155).
function requireMinERC1155BalanceApprovalSelf(address token, uint256 tokenId, uint256 minBalance, address operator)
external
view
{
_requireMinERC1155Balance(token, msg.sender, tokenId, minBalance);
_requireERC1155Approval(token, msg.sender, operator);
}
/// @notice Reverts if any `tokenIds[i]` balance of `owner` is below `minBalances[i]` (ERC1155 batch) or `operator` is not approved to transfer `tokenIds[i]` from `owner` on `token` (ERC1155).
function requireMinERC1155BalanceApprovalBatch(
address token,
address owner,
uint256[] calldata tokenIds,
uint256[] calldata minBalances,
address operator
) external view {
_requireMinERC1155BalanceBatch(token, owner, tokenIds, minBalances);
_requireERC1155Approval(token, owner, operator);
}
/// @notice Reverts if any `tokenIds[i]` balance of `msg.sender` is below `minBalances[i]` (ERC1155 batch) or `operator` is not approved to transfer `tokenIds[i]` from `msg.sender` on `token` (ERC1155).
function requireMinERC1155BalanceApprovalBatchSelf(
address token,
uint256[] calldata tokenIds,
uint256[] calldata minBalances,
address operator
) external view {
_requireMinERC1155BalanceBatch(token, msg.sender, tokenIds, minBalances);
_requireERC1155Approval(token, msg.sender, operator);
}
}