@1inch/solidity-utils 2.2.27 → 2.3.0
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.
- package/README.md +1 -0
- package/contracts/interfaces/IWETH.sol +4 -0
- package/contracts/libraries/SafeERC20.sol +187 -44
- package/dist/src/prelude.d.ts +1 -0
- package/dist/src/prelude.js +1 -0
- package/dist/src/prelude.js.map +1 -1
- package/dist/src/utils.d.ts +20 -1
- package/dist/src/utils.js +35 -2
- package/dist/src/utils.js.map +1 -1
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -40,6 +40,7 @@ This repository contains frequently used smart contracts, libraries and interfac
|
|
|
40
40
|
|utils|`fixSignature(signature)`|patchs ganache's signature to geth's version|
|
|
41
41
|
|utils|`signMessage(signer, messageHex) `|signs `messageHex` with `signer` and patchs ganache's signature to geth's version|
|
|
42
42
|
|utils|`countInstructions(txHash, instruction)`|counts amount of `instruction` in transaction with `txHash` hash|
|
|
43
|
+
|utils|`deployAndGetContract(contractName, constructorArgs, deployments, deployer, deploymentName, skipVerify, skipIfAlreadyDeployed, gasPrice, maxPriorityFeePerGas, maxFeePerGas, log, waitConfirmations)`|deploys contract `contractName` as `deploymentName` if not `skipIfAlreadyDeployed` with `constructorArgs` from `deployer` using `hardhat-deploy` `deployments` with additional gas options `gasPrice`, `maxPriorityFeePerGas` and `maxFeePerGas` and outputs results to the console if `log`. Tries to verify it after `waitConfirmations` on Etherscan if not `skipVerify`.|
|
|
43
44
|
|profileEVM|`profileEVM(txHash, instruction, optionalTraceFile)`|the same as the `countInstructions()` with option of writing all trace to `optionalTraceFile`|
|
|
44
45
|
|profileEVM|`gasspectEVM(txHash, options, optionalTraceFile)`|returns all used operations in `txHash` transaction with `options` and their costs with option of writing all trace to `optionalTraceFile`|
|
|
45
46
|
|
|
@@ -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,10 +26,18 @@ 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
|
|
|
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
|
+
*/
|
|
26
41
|
function safeBalanceOf(
|
|
27
42
|
IERC20 token,
|
|
28
43
|
address account
|
|
@@ -42,7 +57,16 @@ library SafeERC20 {
|
|
|
42
57
|
}
|
|
43
58
|
}
|
|
44
59
|
|
|
45
|
-
|
|
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
|
+
*/
|
|
46
70
|
function safeTransferFromUniversal(
|
|
47
71
|
IERC20 token,
|
|
48
72
|
address from,
|
|
@@ -57,7 +81,14 @@ library SafeERC20 {
|
|
|
57
81
|
}
|
|
58
82
|
}
|
|
59
83
|
|
|
60
|
-
|
|
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
|
+
*/
|
|
61
92
|
function safeTransferFrom(
|
|
62
93
|
IERC20 token,
|
|
63
94
|
address from,
|
|
@@ -87,7 +118,14 @@ library SafeERC20 {
|
|
|
87
118
|
if (!success) revert SafeTransferFromFailed();
|
|
88
119
|
}
|
|
89
120
|
|
|
90
|
-
|
|
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
|
+
*/
|
|
91
129
|
function safeTransferFromPermit2(
|
|
92
130
|
IERC20 token,
|
|
93
131
|
address from,
|
|
@@ -113,7 +151,13 @@ library SafeERC20 {
|
|
|
113
151
|
if (!success) revert SafeTransferFromFailed();
|
|
114
152
|
}
|
|
115
153
|
|
|
116
|
-
|
|
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
|
+
*/
|
|
117
161
|
function safeTransfer(
|
|
118
162
|
IERC20 token,
|
|
119
163
|
address to,
|
|
@@ -124,7 +168,13 @@ library SafeERC20 {
|
|
|
124
168
|
}
|
|
125
169
|
}
|
|
126
170
|
|
|
127
|
-
|
|
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
|
+
*/
|
|
128
178
|
function forceApprove(
|
|
129
179
|
IERC20 token,
|
|
130
180
|
address spender,
|
|
@@ -140,7 +190,14 @@ library SafeERC20 {
|
|
|
140
190
|
}
|
|
141
191
|
}
|
|
142
192
|
|
|
143
|
-
|
|
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
|
+
*/
|
|
144
201
|
function safeIncreaseAllowance(
|
|
145
202
|
IERC20 token,
|
|
146
203
|
address spender,
|
|
@@ -151,7 +208,14 @@ library SafeERC20 {
|
|
|
151
208
|
forceApprove(token, spender, allowance + value);
|
|
152
209
|
}
|
|
153
210
|
|
|
154
|
-
|
|
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
|
+
*/
|
|
155
219
|
function safeDecreaseAllowance(
|
|
156
220
|
IERC20 token,
|
|
157
221
|
address spender,
|
|
@@ -162,99 +226,150 @@ library SafeERC20 {
|
|
|
162
226
|
forceApprove(token, spender, allowance - value);
|
|
163
227
|
}
|
|
164
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
|
+
*/
|
|
165
236
|
function safePermit(IERC20 token, bytes calldata permit) internal {
|
|
166
237
|
if (!tryPermit(token, msg.sender, address(this), permit)) RevertReasonForwarder.reRevert();
|
|
167
238
|
}
|
|
168
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
|
+
*/
|
|
169
249
|
function safePermit(IERC20 token, address owner, address spender, bytes calldata permit) internal {
|
|
170
250
|
if (!tryPermit(token, owner, spender, permit)) RevertReasonForwarder.reRevert();
|
|
171
251
|
}
|
|
172
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
|
+
*/
|
|
173
260
|
function tryPermit(IERC20 token, bytes calldata permit) internal returns(bool success) {
|
|
174
261
|
return tryPermit(token, msg.sender, address(this), permit);
|
|
175
262
|
}
|
|
176
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
|
+
*/
|
|
177
279
|
function tryPermit(IERC20 token, address owner, address spender, bytes calldata permit) internal returns(bool success) {
|
|
280
|
+
// load function selectors for different permit standards
|
|
178
281
|
bytes4 permitSelector = IERC20Permit.permit.selector;
|
|
179
282
|
bytes4 daiPermitSelector = IDaiLikePermit.permit.selector;
|
|
180
283
|
bytes4 permit2Selector = IPermit2.permit.selector;
|
|
181
284
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
182
285
|
let ptr := mload(0x40)
|
|
286
|
+
|
|
287
|
+
// Switch case for different permit lengths, indicating different permit standards
|
|
183
288
|
switch permit.length
|
|
289
|
+
// Compact IERC20Permit
|
|
184
290
|
case 100 {
|
|
185
|
-
mstore(ptr, permitSelector)
|
|
186
|
-
mstore(add(ptr, 0x04), owner)
|
|
187
|
-
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
|
|
188
294
|
|
|
189
295
|
// Compact IERC20Permit.permit(uint256 value, uint32 deadline, uint256 r, uint256 vs)
|
|
190
296
|
{ // stack too deep
|
|
191
|
-
let deadline := shr(224, calldataload(add(permit.offset, 0x20)))
|
|
192
|
-
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
|
|
193
299
|
|
|
194
|
-
calldatacopy(add(ptr, 0x44), permit.offset, 0x20)
|
|
195
|
-
mstore(add(ptr, 0x64), sub(deadline, 1))
|
|
196
|
-
mstore(add(ptr, 0x84), add(27, shr(255, vs)))
|
|
197
|
-
calldatacopy(add(ptr, 0xa4), add(permit.offset, 0x24), 0x20) // r
|
|
198
|
-
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
|
|
199
305
|
}
|
|
200
306
|
// IERC20Permit.permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
|
|
201
307
|
success := call(gas(), token, 0, ptr, 0xe4, 0, 0)
|
|
202
308
|
}
|
|
309
|
+
// Compact IDaiLikePermit
|
|
203
310
|
case 72 {
|
|
204
|
-
mstore(ptr, daiPermitSelector)
|
|
205
|
-
mstore(add(ptr, 0x04), owner)
|
|
206
|
-
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
|
|
207
314
|
|
|
208
315
|
// Compact IDaiLikePermit.permit(uint32 nonce, uint32 expiry, uint256 r, uint256 vs)
|
|
209
316
|
{ // stack too deep
|
|
210
|
-
let expiry := shr(224, calldataload(add(permit.offset, 0x04)))
|
|
211
|
-
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
|
|
212
319
|
|
|
213
|
-
mstore(add(ptr, 0x44), shr(224, calldataload(permit.offset)))
|
|
214
|
-
mstore(add(ptr, 0x64), sub(expiry, 1))
|
|
215
|
-
mstore(add(ptr, 0x84), true)
|
|
216
|
-
mstore(add(ptr, 0xa4), add(27, shr(255, vs)))
|
|
217
|
-
calldatacopy(add(ptr, 0xc4), add(permit.offset, 0x08), 0x20)
|
|
218
|
-
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
|
|
219
326
|
}
|
|
220
327
|
// IDaiLikePermit.permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s)
|
|
221
328
|
success := call(gas(), token, 0, ptr, 0x104, 0, 0)
|
|
222
329
|
}
|
|
330
|
+
// IERC20Permit
|
|
223
331
|
case 224 {
|
|
224
332
|
mstore(ptr, permitSelector)
|
|
225
|
-
calldatacopy(add(ptr, 0x04), permit.offset, permit.length)
|
|
333
|
+
calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
|
|
226
334
|
// IERC20Permit.permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
|
|
227
335
|
success := call(gas(), token, 0, ptr, 0xe4, 0, 0)
|
|
228
336
|
}
|
|
337
|
+
// IDaiLikePermit
|
|
229
338
|
case 256 {
|
|
230
339
|
mstore(ptr, daiPermitSelector)
|
|
231
|
-
calldatacopy(add(ptr, 0x04), permit.offset, permit.length)
|
|
340
|
+
calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
|
|
232
341
|
// IDaiLikePermit.permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s)
|
|
233
342
|
success := call(gas(), token, 0, ptr, 0x104, 0, 0)
|
|
234
343
|
}
|
|
344
|
+
// Compact IPermit2
|
|
235
345
|
case 96 {
|
|
236
346
|
// Compact IPermit2.permit(uint160 amount, uint32 expiration, uint32 nonce, uint32 sigDeadline, uint256 r, uint256 vs)
|
|
237
|
-
mstore(ptr, permit2Selector)
|
|
238
|
-
mstore(add(ptr, 0x04), owner)
|
|
239
|
-
mstore(add(ptr, 0x24), token)
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
mstore(add(ptr,
|
|
244
|
-
mstore(add(ptr,
|
|
245
|
-
mstore(add(ptr,
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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
|
|
249
362
|
// IPermit2.permit(address owner, PermitSingle calldata permitSingle, bytes calldata signature)
|
|
250
363
|
success := call(gas(), _PERMIT2, 0, ptr, 0x164, 0, 0)
|
|
251
364
|
}
|
|
365
|
+
// IPermit2
|
|
252
366
|
case 352 {
|
|
253
367
|
mstore(ptr, permit2Selector)
|
|
254
|
-
calldatacopy(add(ptr, 0x04), permit.offset, permit.length)
|
|
368
|
+
calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
|
|
255
369
|
// IPermit2.permit(address owner, PermitSingle calldata permitSingle, bytes calldata signature)
|
|
256
370
|
success := call(gas(), _PERMIT2, 0, ptr, 0x164, 0, 0)
|
|
257
371
|
}
|
|
372
|
+
// Unknown
|
|
258
373
|
default {
|
|
259
374
|
mstore(ptr, _PERMIT_LENGTH_ERROR)
|
|
260
375
|
revert(ptr, 4)
|
|
@@ -262,6 +377,16 @@ library SafeERC20 {
|
|
|
262
377
|
}
|
|
263
378
|
}
|
|
264
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
|
+
*/
|
|
265
390
|
function _makeCall(
|
|
266
391
|
IERC20 token,
|
|
267
392
|
bytes4 selector,
|
|
@@ -287,6 +412,11 @@ library SafeERC20 {
|
|
|
287
412
|
}
|
|
288
413
|
}
|
|
289
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
|
+
*/
|
|
290
420
|
function safeDeposit(IWETH weth, uint256 amount) internal {
|
|
291
421
|
if (amount > 0) {
|
|
292
422
|
bytes4 selector = IWETH.deposit.selector;
|
|
@@ -300,6 +430,12 @@ library SafeERC20 {
|
|
|
300
430
|
}
|
|
301
431
|
}
|
|
302
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
|
+
*/
|
|
303
439
|
function safeWithdraw(IWETH weth, uint256 amount) internal {
|
|
304
440
|
bytes4 selector = IWETH.withdraw.selector;
|
|
305
441
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
@@ -313,6 +449,13 @@ library SafeERC20 {
|
|
|
313
449
|
}
|
|
314
450
|
}
|
|
315
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
|
+
*/
|
|
316
459
|
function safeWithdrawTo(IWETH weth, uint256 amount, address to) internal {
|
|
317
460
|
safeWithdraw(weth, amount);
|
|
318
461
|
if (to != address(this)) {
|
package/dist/src/prelude.d.ts
CHANGED
package/dist/src/prelude.js
CHANGED
package/dist/src/prelude.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prelude.js","sourceRoot":"","sources":["../../src/prelude.ts"],"names":[],"mappings":";;;AAAA,+BAAiF;
|
|
1
|
+
{"version":3,"file":"prelude.js","sourceRoot":"","sources":["../../src/prelude.ts"],"names":[],"mappings":";;;AAAA,+BAAiF;AAyBxE,0FAzBA,gBAAS,OAyBA;AAAE,+FAzBA,qBAAc,OAyBA;AAAE,uFAzBA,aAAM,OAyBA;AAAE,uFAzBA,aAAM,OAyBA;AAAE,uFAzBA,aAAM,OAyBA;AAAE,uFAzBA,aAAM,OAyBA;AAxBlE,4CAA8C;AAC9C,sFAAgE;AAgBvD,qFAhBA,8BAAI,OAgBA;AAdA,QAAA,SAAS,GAAG;IACrB,YAAY,EAAE,4CAA4C;IAC1D,WAAW,EAAE,4CAA4C;IACzD,YAAY,EAAE,oEAAoE;IAClF,WAAW,EAAE,EAAE,IAAI,IAAI,GAAG,EAAE;IAC5B,UAAU,EAAE,EAAE,IAAI,IAAI,GAAG,EAAE;IAC3B,UAAU,EAAE,EAAE,IAAI,GAAG,GAAG,EAAE;IAC1B,UAAU,EAAE,CAAC,CAAC,EAAE,IAAI,IAAI,CAAC;IACzB,WAAW,EAAE,EAAE,IAAI,IAAI,GAAG,EAAE;IAC5B,UAAU,EAAE,EAAE,IAAI,GAAG,GAAG,EAAE;IAC1B,UAAU,EAAE,CAAC,SAAS,EAAE,WAAW,CAAa;CAC1C,CAAC;AAKX,SAAgB,KAAK,CAAC,CAAS;IAC3B,OAAO,IAAA,kBAAU,EAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC;AACpC,CAAC;AAFD,sBAEC"}
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
import { constants } from './prelude';
|
|
2
|
-
import { providers, Wallet, Contract, Bytes, ContractTransaction, BigNumberish } from 'ethers';
|
|
2
|
+
import { providers, Wallet, Contract, Bytes, ContractTransaction, BigNumberish, BigNumber } from 'ethers';
|
|
3
|
+
import { DeployOptions, DeployResult } from 'hardhat-deploy/types';
|
|
4
|
+
interface DeployContractOptions {
|
|
5
|
+
contractName: string;
|
|
6
|
+
constructorArgs?: any[];
|
|
7
|
+
deployments: {
|
|
8
|
+
deploy: (name: string, options: DeployOptions) => Promise<DeployResult>;
|
|
9
|
+
};
|
|
10
|
+
deployer: string;
|
|
11
|
+
deploymentName?: string;
|
|
12
|
+
skipVerify?: boolean;
|
|
13
|
+
skipIfAlreadyDeployed?: boolean;
|
|
14
|
+
gasPrice?: BigNumber;
|
|
15
|
+
maxPriorityFeePerGas?: BigNumber;
|
|
16
|
+
maxFeePerGas?: BigNumber;
|
|
17
|
+
log?: boolean;
|
|
18
|
+
waitConfirmations?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function deployAndGetContract({ contractName, constructorArgs, deployments, deployer, deploymentName, skipVerify, skipIfAlreadyDeployed, gasPrice, maxPriorityFeePerGas, maxFeePerGas, log, waitConfirmations, }: DeployContractOptions): Promise<Contract>;
|
|
3
21
|
export declare function timeIncreaseTo(seconds: number | string): Promise<void>;
|
|
4
22
|
export declare function deployContract(name: string, parameters?: Array<BigNumberish>): Promise<Contract>;
|
|
5
23
|
export declare function trackReceivedTokenAndTx<T extends unknown[]>(provider: providers.JsonRpcProvider, token: Contract | {
|
|
@@ -10,3 +28,4 @@ export declare function trackReceivedTokenAndTx<T extends unknown[]>(provider: p
|
|
|
10
28
|
export declare function fixSignature(signature: string): string;
|
|
11
29
|
export declare function signMessage(signer: Wallet, messageHex?: string | Bytes): Promise<string>;
|
|
12
30
|
export declare function countInstructions(provider: providers.JsonRpcProvider, txHash: string, instructions: string[]): Promise<number[]>;
|
|
31
|
+
export {};
|
package/dist/src/utils.js
CHANGED
|
@@ -1,9 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.countInstructions = exports.signMessage = exports.fixSignature = exports.trackReceivedTokenAndTx = exports.deployContract = exports.timeIncreaseTo = void 0;
|
|
3
|
+
exports.countInstructions = exports.signMessage = exports.fixSignature = exports.trackReceivedTokenAndTx = exports.deployContract = exports.timeIncreaseTo = exports.deployAndGetContract = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
const prelude_1 = require("./prelude");
|
|
5
|
-
const hardhat_1 = require("hardhat");
|
|
6
|
+
const hardhat_1 = tslib_1.__importStar(require("hardhat"));
|
|
6
7
|
const hardhat_network_helpers_1 = require("@nomicfoundation/hardhat-network-helpers");
|
|
8
|
+
async function deployAndGetContract({ contractName, constructorArgs, deployments, deployer, deploymentName = contractName, skipVerify = false, skipIfAlreadyDeployed = true, gasPrice, maxPriorityFeePerGas, maxFeePerGas, log = true, waitConfirmations = prelude_1.constants.DEV_CHAINS.includes(hardhat_1.default.network.name) ? 1 : 6, }) {
|
|
9
|
+
/**
|
|
10
|
+
* Deploys contract and tries to verify it on Etherscan if requested.
|
|
11
|
+
* @remarks
|
|
12
|
+
* If the contract is deployed on a dev chain, verification is skipped.
|
|
13
|
+
* @returns Deployed contract instance
|
|
14
|
+
*/
|
|
15
|
+
const { deploy } = deployments;
|
|
16
|
+
const deployOptions = {
|
|
17
|
+
args: constructorArgs,
|
|
18
|
+
from: deployer,
|
|
19
|
+
contract: contractName,
|
|
20
|
+
skipIfAlreadyDeployed,
|
|
21
|
+
gasPrice,
|
|
22
|
+
maxPriorityFeePerGas,
|
|
23
|
+
maxFeePerGas,
|
|
24
|
+
log,
|
|
25
|
+
waitConfirmations,
|
|
26
|
+
};
|
|
27
|
+
const deployResult = await deploy(deploymentName, deployOptions);
|
|
28
|
+
if (!(skipVerify || prelude_1.constants.DEV_CHAINS.includes(hardhat_1.default.network.name))) {
|
|
29
|
+
await hardhat_1.default.run('verify:verify', {
|
|
30
|
+
address: deployResult.address,
|
|
31
|
+
constructorArguments: constructorArgs,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
console.log('Skipping verification');
|
|
36
|
+
}
|
|
37
|
+
return await hardhat_1.ethers.getContractAt(contractName, deployResult.address);
|
|
38
|
+
}
|
|
39
|
+
exports.deployAndGetContract = deployAndGetContract;
|
|
7
40
|
async function timeIncreaseTo(seconds) {
|
|
8
41
|
const delay = 1000 - new Date().getMilliseconds();
|
|
9
42
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
package/dist/src/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;;AAAA,uCAAsC;AACtC,2DAAsC;AACtC,sFAAgE;AAoBzD,KAAK,UAAU,oBAAoB,CAAC,EACvC,YAAY,EACZ,eAAe,EACf,WAAW,EACX,QAAQ,EACR,cAAc,GAAG,YAAY,EAC7B,UAAU,GAAG,KAAK,EAClB,qBAAqB,GAAG,IAAI,EAC5B,QAAQ,EACR,oBAAoB,EACpB,YAAY,EACZ,GAAG,GAAG,IAAI,EACV,iBAAiB,GAAG,mBAAS,CAAC,UAAU,CAAC,QAAQ,CAAC,iBAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA,CAAC,CAAC,CAA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|
|
5
5
|
"types": "dist/src/index.d.ts",
|
|
6
6
|
"repository": {
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@nomicfoundation/hardhat-chai-matchers": "1.0.6",
|
|
41
|
+
"@nomicfoundation/hardhat-verify": "1.0.1",
|
|
41
42
|
"@typechain/ethers-v5": "10.2.0",
|
|
42
43
|
"@typechain/hardhat": "6.1.5",
|
|
43
44
|
"@types/chai": "4.3.4",
|
|
@@ -61,6 +62,7 @@
|
|
|
61
62
|
"eslint-plugin-standard": "5.0.0",
|
|
62
63
|
"ethereumjs-wallet": "1.0.2",
|
|
63
64
|
"hardhat": "2.13.0",
|
|
65
|
+
"hardhat-deploy": "0.11.30",
|
|
64
66
|
"hardhat-gas-reporter": "1.0.9",
|
|
65
67
|
"hardhat-tracer": "2.1.2",
|
|
66
68
|
"prettier": "2.8.7",
|