@1inch/solidity-utils 2.2.26 → 2.2.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,6 +5,10 @@ pragma solidity ^0.8.0;
|
|
|
5
5
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
6
6
|
|
|
7
7
|
interface IWETH is IERC20 {
|
|
8
|
+
event Deposit(address indexed dst, uint wad);
|
|
9
|
+
|
|
10
|
+
event Withdrawal(address indexed src, uint wad);
|
|
11
|
+
|
|
8
12
|
function deposit() external payable;
|
|
9
13
|
|
|
10
14
|
function withdraw(uint256 amount) external;
|
|
@@ -9,7 +9,14 @@ import "../interfaces/IPermit2.sol";
|
|
|
9
9
|
import "../interfaces/IWETH.sol";
|
|
10
10
|
import "../libraries/RevertReasonForwarder.sol";
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @title Implements efficient safe methods for ERC20 interface.
|
|
14
|
+
* @notice Compared to the standard ERC20, this implementation offers several enhancements:
|
|
15
|
+
* 1. more gas-efficient, providing significant savings in transaction costs.
|
|
16
|
+
* 2. support for different permit implementations
|
|
17
|
+
* 3. forceApprove functionality
|
|
18
|
+
* 4. support for WETH deposit and withdraw
|
|
19
|
+
*/
|
|
13
20
|
library SafeERC20 {
|
|
14
21
|
error SafeTransferFailed();
|
|
15
22
|
error SafeTransferFromFailed();
|
|
@@ -19,11 +26,47 @@ library SafeERC20 {
|
|
|
19
26
|
error SafePermitBadLength();
|
|
20
27
|
error Permit2TransferAmountTooHigh();
|
|
21
28
|
|
|
29
|
+
// Uniswap Permit2 address
|
|
22
30
|
address private constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
|
|
23
31
|
bytes4 private constant _PERMIT_LENGTH_ERROR = 0x68275857; // SafePermitBadLength.selector
|
|
24
32
|
uint256 private constant _RAW_CALL_GAS_LIMIT = 5000;
|
|
25
33
|
|
|
26
|
-
|
|
34
|
+
/**
|
|
35
|
+
* @notice Fetches the balance of a specific ERC20 token held by an account.
|
|
36
|
+
* Consumes less gas then regular `ERC20.balanceOf`.
|
|
37
|
+
* @param token The IERC20 token contract for which the balance will be fetched.
|
|
38
|
+
* @param account The address of the account whose token balance will be fetched.
|
|
39
|
+
* @return tokenBalance The balance of the specified ERC20 token held by the account.
|
|
40
|
+
*/
|
|
41
|
+
function safeBalanceOf(
|
|
42
|
+
IERC20 token,
|
|
43
|
+
address account
|
|
44
|
+
) internal view returns(uint256 tokenBalance) {
|
|
45
|
+
bytes4 selector = IERC20.balanceOf.selector;
|
|
46
|
+
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
47
|
+
mstore(0x00, selector)
|
|
48
|
+
mstore(0x04, account)
|
|
49
|
+
let success := staticcall(gas(), token, 0x00, 0x24, 0x00, 0x20)
|
|
50
|
+
tokenBalance := mload(0)
|
|
51
|
+
|
|
52
|
+
if or(iszero(success), lt(returndatasize(), 0x20)) {
|
|
53
|
+
let ptr := mload(0x40)
|
|
54
|
+
returndatacopy(ptr, 0, returndatasize())
|
|
55
|
+
revert(ptr, returndatasize())
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @notice Attempts to safely transfer tokens from one address to another.
|
|
62
|
+
* @dev If permit2 is true, uses the Permit2 standard; otherwise uses the standard ERC20 transferFrom.
|
|
63
|
+
* Either requires `true` in return data, or requires target to be smart-contract and empty return data.
|
|
64
|
+
* @param token The IERC20 token contract from which the tokens will be transferred.
|
|
65
|
+
* @param from The address from which the tokens will be transferred.
|
|
66
|
+
* @param to The address to which the tokens will be transferred.
|
|
67
|
+
* @param amount The amount of tokens to transfer.
|
|
68
|
+
* @param permit2 If true, uses the Permit2 standard for the transfer; otherwise uses the standard ERC20 transferFrom.
|
|
69
|
+
*/
|
|
27
70
|
function safeTransferFromUniversal(
|
|
28
71
|
IERC20 token,
|
|
29
72
|
address from,
|
|
@@ -32,14 +75,20 @@ library SafeERC20 {
|
|
|
32
75
|
bool permit2
|
|
33
76
|
) internal {
|
|
34
77
|
if (permit2) {
|
|
35
|
-
|
|
36
|
-
safeTransferFromPermit2(token, from, to, uint160(amount));
|
|
78
|
+
safeTransferFromPermit2(token, from, to, amount);
|
|
37
79
|
} else {
|
|
38
80
|
safeTransferFrom(token, from, to, amount);
|
|
39
81
|
}
|
|
40
82
|
}
|
|
41
83
|
|
|
42
|
-
|
|
84
|
+
/**
|
|
85
|
+
* @notice Attempts to safely transfer tokens from one address to another using the ERC20 standard.
|
|
86
|
+
* @dev Either requires `true` in return data, or requires target to be smart-contract and empty return data.
|
|
87
|
+
* @param token The IERC20 token contract from which the tokens will be transferred.
|
|
88
|
+
* @param from The address from which the tokens will be transferred.
|
|
89
|
+
* @param to The address to which the tokens will be transferred.
|
|
90
|
+
* @param amount The amount of tokens to transfer.
|
|
91
|
+
*/
|
|
43
92
|
function safeTransferFrom(
|
|
44
93
|
IERC20 token,
|
|
45
94
|
address from,
|
|
@@ -69,13 +118,21 @@ library SafeERC20 {
|
|
|
69
118
|
if (!success) revert SafeTransferFromFailed();
|
|
70
119
|
}
|
|
71
120
|
|
|
72
|
-
|
|
121
|
+
/**
|
|
122
|
+
* @notice Attempts to safely transfer tokens from one address to another using the Permit2 standard.
|
|
123
|
+
* @dev Either requires `true` in return data, or requires target to be smart-contract and empty return data.
|
|
124
|
+
* @param token The IERC20 token contract from which the tokens will be transferred.
|
|
125
|
+
* @param from The address from which the tokens will be transferred.
|
|
126
|
+
* @param to The address to which the tokens will be transferred.
|
|
127
|
+
* @param amount The amount of tokens to transfer.
|
|
128
|
+
*/
|
|
73
129
|
function safeTransferFromPermit2(
|
|
74
130
|
IERC20 token,
|
|
75
131
|
address from,
|
|
76
132
|
address to,
|
|
77
|
-
|
|
133
|
+
uint256 amount
|
|
78
134
|
) internal {
|
|
135
|
+
if (amount > type(uint160).max) revert Permit2TransferAmountTooHigh();
|
|
79
136
|
bytes4 selector = IPermit2.transferFrom.selector;
|
|
80
137
|
bool success;
|
|
81
138
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
@@ -94,7 +151,13 @@ library SafeERC20 {
|
|
|
94
151
|
if (!success) revert SafeTransferFromFailed();
|
|
95
152
|
}
|
|
96
153
|
|
|
97
|
-
|
|
154
|
+
/**
|
|
155
|
+
* @notice Attempts to safely transfer tokens to another address.
|
|
156
|
+
* @dev Either requires `true` in return data, or requires target to be smart-contract and empty return data.
|
|
157
|
+
* @param token The IERC20 token contract from which the tokens will be transferred.
|
|
158
|
+
* @param to The address to which the tokens will be transferred.
|
|
159
|
+
* @param value The amount of tokens to transfer.
|
|
160
|
+
*/
|
|
98
161
|
function safeTransfer(
|
|
99
162
|
IERC20 token,
|
|
100
163
|
address to,
|
|
@@ -105,7 +168,13 @@ library SafeERC20 {
|
|
|
105
168
|
}
|
|
106
169
|
}
|
|
107
170
|
|
|
108
|
-
|
|
171
|
+
/**
|
|
172
|
+
* @notice Attempts to approve a spender to spend a certain amount of tokens.
|
|
173
|
+
* @dev If `approve(from, to, amount)` fails, it tries to set the allowance to zero, and retries the `approve` call.
|
|
174
|
+
* @param token The IERC20 token contract on which the call will be made.
|
|
175
|
+
* @param spender The address which will spend the funds.
|
|
176
|
+
* @param value The amount of tokens to be spent.
|
|
177
|
+
*/
|
|
109
178
|
function forceApprove(
|
|
110
179
|
IERC20 token,
|
|
111
180
|
address spender,
|
|
@@ -121,7 +190,14 @@ library SafeERC20 {
|
|
|
121
190
|
}
|
|
122
191
|
}
|
|
123
192
|
|
|
124
|
-
|
|
193
|
+
/**
|
|
194
|
+
* @notice Safely increases the allowance of a spender.
|
|
195
|
+
* @dev Increases with safe math check. Checks if the increased allowance will overflow, if yes, then it reverts the transaction.
|
|
196
|
+
* Then uses `forceApprove` to increase the allowance.
|
|
197
|
+
* @param token The IERC20 token contract on which the call will be made.
|
|
198
|
+
* @param spender The address which will spend the funds.
|
|
199
|
+
* @param value The amount of tokens to increase the allowance by.
|
|
200
|
+
*/
|
|
125
201
|
function safeIncreaseAllowance(
|
|
126
202
|
IERC20 token,
|
|
127
203
|
address spender,
|
|
@@ -132,7 +208,14 @@ library SafeERC20 {
|
|
|
132
208
|
forceApprove(token, spender, allowance + value);
|
|
133
209
|
}
|
|
134
210
|
|
|
135
|
-
|
|
211
|
+
/**
|
|
212
|
+
* @notice Safely decreases the allowance of a spender.
|
|
213
|
+
* @dev Decreases with safe math check. Checks if the decreased allowance will underflow, if yes, then it reverts the transaction.
|
|
214
|
+
* Then uses `forceApprove` to increase the allowance.
|
|
215
|
+
* @param token The IERC20 token contract on which the call will be made.
|
|
216
|
+
* @param spender The address which will spend the funds.
|
|
217
|
+
* @param value The amount of tokens to decrease the allowance by.
|
|
218
|
+
*/
|
|
136
219
|
function safeDecreaseAllowance(
|
|
137
220
|
IERC20 token,
|
|
138
221
|
address spender,
|
|
@@ -143,99 +226,150 @@ library SafeERC20 {
|
|
|
143
226
|
forceApprove(token, spender, allowance - value);
|
|
144
227
|
}
|
|
145
228
|
|
|
229
|
+
/**
|
|
230
|
+
* @notice Attempts to execute the `permit` function on the provided token with the sender and contract as parameters.
|
|
231
|
+
* Permit type is determined automatically based on permit calldata (IERC20Permit, IDaiLikePermit, and IPermit2).
|
|
232
|
+
* @dev Wraps `tryPermit` function and forwards revert reason if permit fails.
|
|
233
|
+
* @param token The IERC20 token to execute the permit function on.
|
|
234
|
+
* @param permit The permit data to be used in the function call.
|
|
235
|
+
*/
|
|
146
236
|
function safePermit(IERC20 token, bytes calldata permit) internal {
|
|
147
237
|
if (!tryPermit(token, msg.sender, address(this), permit)) RevertReasonForwarder.reRevert();
|
|
148
238
|
}
|
|
149
239
|
|
|
240
|
+
/**
|
|
241
|
+
* @notice Attempts to execute the `permit` function on the provided token with custom owner and spender parameters.
|
|
242
|
+
* Permit type is determined automatically based on permit calldata (IERC20Permit, IDaiLikePermit, and IPermit2).
|
|
243
|
+
* @dev Wraps `tryPermit` function and forwards revert reason if permit fails.
|
|
244
|
+
* @param token The IERC20 token to execute the permit function on.
|
|
245
|
+
* @param owner The owner of the tokens for which the permit is made.
|
|
246
|
+
* @param spender The spender allowed to spend the tokens by the permit.
|
|
247
|
+
* @param permit The permit data to be used in the function call.
|
|
248
|
+
*/
|
|
150
249
|
function safePermit(IERC20 token, address owner, address spender, bytes calldata permit) internal {
|
|
151
250
|
if (!tryPermit(token, owner, spender, permit)) RevertReasonForwarder.reRevert();
|
|
152
251
|
}
|
|
153
252
|
|
|
253
|
+
/**
|
|
254
|
+
* @notice Attempts to execute the `permit` function on the provided token with the sender and contract as parameters.
|
|
255
|
+
* @dev Invokes `tryPermit` with sender as owner and contract as spender.
|
|
256
|
+
* @param token The IERC20 token to execute the permit function on.
|
|
257
|
+
* @param permit The permit data to be used in the function call.
|
|
258
|
+
* @return success Returns true if the permit function was successfully executed, false otherwise.
|
|
259
|
+
*/
|
|
154
260
|
function tryPermit(IERC20 token, bytes calldata permit) internal returns(bool success) {
|
|
155
261
|
return tryPermit(token, msg.sender, address(this), permit);
|
|
156
262
|
}
|
|
157
263
|
|
|
264
|
+
/**
|
|
265
|
+
* @notice The function attempts to call the permit function on a given ERC20 token.
|
|
266
|
+
* @dev The function is designed to support a variety of permit functions, namely: IERC20Permit, IDaiLikePermit, and IPermit2.
|
|
267
|
+
* It accommodates both Compact and Full formats of these permit types.
|
|
268
|
+
* Please note, it is expected that the `expiration` parameter for the compact Permit2 and the `deadline` parameter
|
|
269
|
+
* for the compact Permit are to be incremented by one before invoking this function. This approach is motivated by
|
|
270
|
+
* gas efficiency considerations; as the unlimited expiration period is likely to be the most common scenario, and
|
|
271
|
+
* zeros are cheaper to pass in terms of gas cost. Thus, callers should increment the expiration or deadline by one
|
|
272
|
+
* before invocation for optimized performance.
|
|
273
|
+
* @param token The address of the ERC20 token on which to call the permit function.
|
|
274
|
+
* @param owner The owner of the tokens. This address should have signed the off-chain permit.
|
|
275
|
+
* @param spender The address which will be approved for transfer of tokens.
|
|
276
|
+
* @param permit The off-chain permit data, containing different fields depending on the type of permit function.
|
|
277
|
+
* @return success A boolean indicating whether the permit call was successful.
|
|
278
|
+
*/
|
|
158
279
|
function tryPermit(IERC20 token, address owner, address spender, bytes calldata permit) internal returns(bool success) {
|
|
280
|
+
// load function selectors for different permit standards
|
|
159
281
|
bytes4 permitSelector = IERC20Permit.permit.selector;
|
|
160
282
|
bytes4 daiPermitSelector = IDaiLikePermit.permit.selector;
|
|
161
283
|
bytes4 permit2Selector = IPermit2.permit.selector;
|
|
162
284
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
163
285
|
let ptr := mload(0x40)
|
|
286
|
+
|
|
287
|
+
// Switch case for different permit lengths, indicating different permit standards
|
|
164
288
|
switch permit.length
|
|
289
|
+
// Compact IERC20Permit
|
|
165
290
|
case 100 {
|
|
166
|
-
mstore(ptr, permitSelector)
|
|
167
|
-
mstore(add(ptr, 0x04), owner)
|
|
168
|
-
mstore(add(ptr, 0x24), spender)
|
|
291
|
+
mstore(ptr, permitSelector) // store selector
|
|
292
|
+
mstore(add(ptr, 0x04), owner) // store owner
|
|
293
|
+
mstore(add(ptr, 0x24), spender) // store spender
|
|
169
294
|
|
|
170
295
|
// Compact IERC20Permit.permit(uint256 value, uint32 deadline, uint256 r, uint256 vs)
|
|
171
296
|
{ // stack too deep
|
|
172
|
-
let deadline := shr(224, calldataload(add(permit.offset, 0x20)))
|
|
173
|
-
let vs := calldataload(add(permit.offset, 0x44))
|
|
297
|
+
let deadline := shr(224, calldataload(add(permit.offset, 0x20))) // loads permit.offset 0x20..0x23
|
|
298
|
+
let vs := calldataload(add(permit.offset, 0x44)) // loads permit.offset 0x44..0x63
|
|
174
299
|
|
|
175
|
-
calldatacopy(add(ptr, 0x44), permit.offset, 0x20)
|
|
176
|
-
mstore(add(ptr, 0x64), sub(deadline, 1))
|
|
177
|
-
mstore(add(ptr, 0x84), add(27, shr(255, vs)))
|
|
178
|
-
calldatacopy(add(ptr, 0xa4), add(permit.offset, 0x24), 0x20) // r
|
|
179
|
-
mstore(add(ptr, 0xc4), shr(1, shl(1, vs)))
|
|
300
|
+
calldatacopy(add(ptr, 0x44), permit.offset, 0x20) // store value = copy permit.offset 0x00..0x19
|
|
301
|
+
mstore(add(ptr, 0x64), sub(deadline, 1)) // store deadline = deadline - 1
|
|
302
|
+
mstore(add(ptr, 0x84), add(27, shr(255, vs))) // store v = most significant bit of vs + 27 (27 or 28)
|
|
303
|
+
calldatacopy(add(ptr, 0xa4), add(permit.offset, 0x24), 0x20) // store r = copy permit.offset 0x24..0x43
|
|
304
|
+
mstore(add(ptr, 0xc4), shr(1, shl(1, vs))) // store s = vs without most significant bit
|
|
180
305
|
}
|
|
181
306
|
// IERC20Permit.permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
|
|
182
307
|
success := call(gas(), token, 0, ptr, 0xe4, 0, 0)
|
|
183
308
|
}
|
|
309
|
+
// Compact IDaiLikePermit
|
|
184
310
|
case 72 {
|
|
185
|
-
mstore(ptr, daiPermitSelector)
|
|
186
|
-
mstore(add(ptr, 0x04), owner)
|
|
187
|
-
mstore(add(ptr, 0x24), spender)
|
|
311
|
+
mstore(ptr, daiPermitSelector) // store selector
|
|
312
|
+
mstore(add(ptr, 0x04), owner) // store owner
|
|
313
|
+
mstore(add(ptr, 0x24), spender) // store spender
|
|
188
314
|
|
|
189
315
|
// Compact IDaiLikePermit.permit(uint32 nonce, uint32 expiry, uint256 r, uint256 vs)
|
|
190
316
|
{ // stack too deep
|
|
191
|
-
let expiry := shr(224, calldataload(add(permit.offset, 0x04)))
|
|
192
|
-
let vs := calldataload(add(permit.offset, 0x28))
|
|
317
|
+
let expiry := shr(224, calldataload(add(permit.offset, 0x04))) // loads permit.offset 0x04..0x07
|
|
318
|
+
let vs := calldataload(add(permit.offset, 0x28)) // loads permit.offset 0x28..0x47
|
|
193
319
|
|
|
194
|
-
mstore(add(ptr, 0x44), shr(224, calldataload(permit.offset)))
|
|
195
|
-
mstore(add(ptr, 0x64), sub(expiry, 1))
|
|
196
|
-
mstore(add(ptr, 0x84), true)
|
|
197
|
-
mstore(add(ptr, 0xa4), add(27, shr(255, vs)))
|
|
198
|
-
calldatacopy(add(ptr, 0xc4), add(permit.offset, 0x08), 0x20)
|
|
199
|
-
mstore(add(ptr, 0xe4), shr(1, shl(1, vs)))
|
|
320
|
+
mstore(add(ptr, 0x44), shr(224, calldataload(permit.offset))) // store nonce = copy permit.offset 0x00..0x03
|
|
321
|
+
mstore(add(ptr, 0x64), sub(expiry, 1)) // store expiry = expiry - 1
|
|
322
|
+
mstore(add(ptr, 0x84), true) // store allowed = true
|
|
323
|
+
mstore(add(ptr, 0xa4), add(27, shr(255, vs))) // store v = most significant bit of vs + 27 (27 or 28)
|
|
324
|
+
calldatacopy(add(ptr, 0xc4), add(permit.offset, 0x08), 0x20) // store r = copy permit.offset 0x08..0x27
|
|
325
|
+
mstore(add(ptr, 0xe4), shr(1, shl(1, vs))) // store s = vs without most significant bit
|
|
200
326
|
}
|
|
201
327
|
// IDaiLikePermit.permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s)
|
|
202
328
|
success := call(gas(), token, 0, ptr, 0x104, 0, 0)
|
|
203
329
|
}
|
|
330
|
+
// IERC20Permit
|
|
204
331
|
case 224 {
|
|
205
332
|
mstore(ptr, permitSelector)
|
|
206
|
-
calldatacopy(add(ptr, 0x04), permit.offset, permit.length)
|
|
333
|
+
calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
|
|
207
334
|
// IERC20Permit.permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
|
|
208
335
|
success := call(gas(), token, 0, ptr, 0xe4, 0, 0)
|
|
209
336
|
}
|
|
337
|
+
// IDaiLikePermit
|
|
210
338
|
case 256 {
|
|
211
339
|
mstore(ptr, daiPermitSelector)
|
|
212
|
-
calldatacopy(add(ptr, 0x04), permit.offset, permit.length)
|
|
340
|
+
calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
|
|
213
341
|
// IDaiLikePermit.permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s)
|
|
214
342
|
success := call(gas(), token, 0, ptr, 0x104, 0, 0)
|
|
215
343
|
}
|
|
344
|
+
// Compact IPermit2
|
|
216
345
|
case 96 {
|
|
217
346
|
// Compact IPermit2.permit(uint160 amount, uint32 expiration, uint32 nonce, uint32 sigDeadline, uint256 r, uint256 vs)
|
|
218
|
-
mstore(ptr, permit2Selector)
|
|
219
|
-
mstore(add(ptr, 0x04), owner)
|
|
220
|
-
mstore(add(ptr, 0x24), token)
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
mstore(add(ptr,
|
|
225
|
-
mstore(add(ptr,
|
|
226
|
-
mstore(add(ptr,
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
347
|
+
mstore(ptr, permit2Selector) // store selector
|
|
348
|
+
mstore(add(ptr, 0x04), owner) // store owner
|
|
349
|
+
mstore(add(ptr, 0x24), token) // store token
|
|
350
|
+
|
|
351
|
+
calldatacopy(add(ptr, 0x50), permit.offset, 0x14) // store amount = copy permit.offset 0x00..0x13
|
|
352
|
+
// and(0xffffffffffff, ...) - conversion to uint48
|
|
353
|
+
mstore(add(ptr, 0x64), and(0xffffffffffff, sub(shr(224, calldataload(add(permit.offset, 0x14))), 1))) // store expiration = ((permit.offset 0x14..0x17 - 1) & 0xffffffffffff)
|
|
354
|
+
mstore(add(ptr, 0x84), shr(224, calldataload(add(permit.offset, 0x18)))) // store nonce = copy permit.offset 0x18..0x1b
|
|
355
|
+
mstore(add(ptr, 0xa4), spender) // store spender
|
|
356
|
+
// and(0xffffffffffff, ...) - conversion to uint48
|
|
357
|
+
mstore(add(ptr, 0xc4), and(0xffffffffffff, sub(shr(224, calldataload(add(permit.offset, 0x1c))), 1))) // store sigDeadline = ((permit.offset 0x1c..0x1f - 1) & 0xffffffffffff)
|
|
358
|
+
mstore(add(ptr, 0xe4), 0x100) // store offset = 256
|
|
359
|
+
mstore(add(ptr, 0x104), 0x40) // store length = 64
|
|
360
|
+
calldatacopy(add(ptr, 0x124), add(permit.offset, 0x20), 0x20) // store r = copy permit.offset 0x20..0x3f
|
|
361
|
+
calldatacopy(add(ptr, 0x144), add(permit.offset, 0x40), 0x20) // store vs = copy permit.offset 0x40..0x5f
|
|
230
362
|
// IPermit2.permit(address owner, PermitSingle calldata permitSingle, bytes calldata signature)
|
|
231
363
|
success := call(gas(), _PERMIT2, 0, ptr, 0x164, 0, 0)
|
|
232
364
|
}
|
|
365
|
+
// IPermit2
|
|
233
366
|
case 352 {
|
|
234
367
|
mstore(ptr, permit2Selector)
|
|
235
|
-
calldatacopy(add(ptr, 0x04), permit.offset, permit.length)
|
|
368
|
+
calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
|
|
236
369
|
// IPermit2.permit(address owner, PermitSingle calldata permitSingle, bytes calldata signature)
|
|
237
370
|
success := call(gas(), _PERMIT2, 0, ptr, 0x164, 0, 0)
|
|
238
371
|
}
|
|
372
|
+
// Unknown
|
|
239
373
|
default {
|
|
240
374
|
mstore(ptr, _PERMIT_LENGTH_ERROR)
|
|
241
375
|
revert(ptr, 4)
|
|
@@ -243,6 +377,16 @@ library SafeERC20 {
|
|
|
243
377
|
}
|
|
244
378
|
}
|
|
245
379
|
|
|
380
|
+
/**
|
|
381
|
+
* @dev Executes a low level call to a token contract, making it resistant to reversion and erroneous boolean returns.
|
|
382
|
+
* @param token The IERC20 token contract on which the call will be made.
|
|
383
|
+
* @param selector The function signature that is to be called on the token contract.
|
|
384
|
+
* @param to The address to which the token amount will be transferred.
|
|
385
|
+
* @param amount The token amount to be transferred.
|
|
386
|
+
* @return success A boolean indicating if the call was successful. Returns 'true' on success and 'false' on failure.
|
|
387
|
+
* In case of success but no returned data, validates that the contract code exists.
|
|
388
|
+
* In case of returned data, ensures that it's a boolean `true`.
|
|
389
|
+
*/
|
|
246
390
|
function _makeCall(
|
|
247
391
|
IERC20 token,
|
|
248
392
|
bytes4 selector,
|
|
@@ -268,6 +412,11 @@ library SafeERC20 {
|
|
|
268
412
|
}
|
|
269
413
|
}
|
|
270
414
|
|
|
415
|
+
/**
|
|
416
|
+
* @notice Safely deposits a specified amount of Ether into the IWETH contract. Consumes less gas then regular `IWETH.deposit`.
|
|
417
|
+
* @param weth The IWETH token contract.
|
|
418
|
+
* @param amount The amount of Ether to deposit into the IWETH contract.
|
|
419
|
+
*/
|
|
271
420
|
function safeDeposit(IWETH weth, uint256 amount) internal {
|
|
272
421
|
if (amount > 0) {
|
|
273
422
|
bytes4 selector = IWETH.deposit.selector;
|
|
@@ -281,6 +430,12 @@ library SafeERC20 {
|
|
|
281
430
|
}
|
|
282
431
|
}
|
|
283
432
|
|
|
433
|
+
/**
|
|
434
|
+
* @notice Safely withdraws a specified amount of wrapped Ether from the IWETH contract. Consumes less gas then regular `IWETH.withdraw`.
|
|
435
|
+
* @dev Uses inline assembly to interact with the IWETH contract.
|
|
436
|
+
* @param weth The IWETH token contract.
|
|
437
|
+
* @param amount The amount of wrapped Ether to withdraw from the IWETH contract.
|
|
438
|
+
*/
|
|
284
439
|
function safeWithdraw(IWETH weth, uint256 amount) internal {
|
|
285
440
|
bytes4 selector = IWETH.withdraw.selector;
|
|
286
441
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
@@ -294,6 +449,13 @@ library SafeERC20 {
|
|
|
294
449
|
}
|
|
295
450
|
}
|
|
296
451
|
|
|
452
|
+
/**
|
|
453
|
+
* @notice Safely withdraws a specified amount of wrapped Ether from the IWETH contract to a specified recipient.
|
|
454
|
+
* Consumes less gas then regular `IWETH.withdraw`.
|
|
455
|
+
* @param weth The IWETH token contract.
|
|
456
|
+
* @param amount The amount of wrapped Ether to withdraw from the IWETH contract.
|
|
457
|
+
* @param to The recipient of the withdrawn Ether.
|
|
458
|
+
*/
|
|
297
459
|
function safeWithdrawTo(IWETH weth, uint256 amount, address to) internal {
|
|
298
460
|
safeWithdraw(weth, amount);
|
|
299
461
|
if (to != address(this)) {
|