@1inch/solidity-utils 4.0.0 → 4.2.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 +9 -208
- package/contracts/interfaces/ICreate3Deployer.sol +16 -0
- package/contracts/interfaces/IDaiLikePermit.sol +13 -0
- package/contracts/interfaces/IERC20MetadataUppercase.sol +12 -0
- package/contracts/interfaces/IERC7597Permit.sol +13 -0
- package/contracts/interfaces/IPermit2.sol +45 -12
- package/contracts/interfaces/IWETH.sol +17 -0
- package/contracts/libraries/AddressArray.sol +73 -25
- package/contracts/libraries/AddressLib.sol +2 -1
- package/contracts/libraries/AddressSet.sol +52 -14
- package/contracts/libraries/BySigTraits.sol +37 -5
- package/contracts/libraries/BytesMemory.sol +2 -1
- package/contracts/libraries/BytesStorage.sol +2 -1
- package/contracts/libraries/ECDSA.sol +111 -0
- package/contracts/libraries/RevertReasonForwarder.sol +13 -3
- package/contracts/libraries/RevertReasonParser.sol +11 -2
- package/contracts/libraries/SafeERC20.sol +1 -1
- package/contracts/libraries/StringUtil.sol +21 -3
- package/contracts/libraries/UniERC20.sol +57 -12
- package/contracts/mixins/BySig.sol +220 -0
- package/contracts/mixins/EthReceiver.sol +27 -0
- package/contracts/mixins/OnlyWethReceiver.sol +31 -0
- package/contracts/mixins/PermitAndCall.sol +39 -0
- package/contracts/mixins/SelfdestructEthSender.sol +32 -0
- package/contracts/mocks/ERC20PermitMock.sol +12 -0
- package/contracts/mocks/SelfdestructEthSenderMock.sol +27 -0
- package/contracts/mocks/TokenCustomDecimalsMock.sol +25 -4
- package/contracts/mocks/TokenMock.sol +19 -0
- package/dist/docgen/index.d.ts +2 -0
- package/dist/docgen/index.js +7 -0
- package/dist/docgen/index.js.map +1 -0
- package/dist/docgen/templates.d.ts +6 -0
- package/dist/docgen/templates.js +15 -0
- package/dist/docgen/templates.js.map +1 -0
- package/dist/hardhat-setup/networks.d.ts +23 -0
- package/dist/hardhat-setup/networks.js +22 -0
- package/dist/hardhat-setup/networks.js.map +1 -1
- package/dist/src/expect.d.ts +12 -0
- package/dist/src/expect.js +12 -0
- package/dist/src/expect.js.map +1 -1
- package/dist/src/permit.d.ts +131 -2
- package/dist/src/permit.js +126 -9
- package/dist/src/permit.js.map +1 -1
- package/dist/src/prelude.d.ts +6 -0
- package/dist/src/prelude.js +6 -0
- package/dist/src/prelude.js.map +1 -1
- package/dist/src/profileEVM.d.ts +26 -0
- package/dist/src/profileEVM.js +30 -3
- package/dist/src/profileEVM.js.map +1 -1
- package/dist/src/utils.d.ts +79 -0
- package/dist/src/utils.js +92 -1
- package/dist/src/utils.js.map +1 -1
- package/dist/typechain-types/contracts/mixins/SelfdestructEthSender.d.ts +29 -0
- package/dist/typechain-types/contracts/mixins/SelfdestructEthSender.js +3 -0
- package/dist/typechain-types/contracts/mixins/SelfdestructEthSender.js.map +1 -0
- package/dist/typechain-types/contracts/mixins/index.d.ts +1 -0
- package/dist/typechain-types/contracts/mocks/SelfdestructEthSenderMock.d.ts +37 -0
- package/dist/typechain-types/contracts/mocks/SelfdestructEthSenderMock.js +3 -0
- package/dist/typechain-types/contracts/mocks/SelfdestructEthSenderMock.js.map +1 -0
- package/dist/typechain-types/contracts/mocks/TokenCustomDecimalsMock.d.ts +1 -5
- package/dist/typechain-types/contracts/mocks/index.d.ts +1 -0
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/Address__factory.d.ts +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/Address__factory.js +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/ShortStrings__factory.d.ts +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/ShortStrings__factory.js +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/Strings__factory.d.ts +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/Strings__factory.js +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/cryptography/ECDSA__factory.d.ts +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/cryptography/ECDSA__factory.js +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/math/Math__factory.d.ts +1 -1
- package/dist/typechain-types/factories/@openzeppelin/contracts/utils/math/Math__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/AddressArray__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/AddressArray__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/BySigTraits__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/BySigTraits__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/BytesMemory__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/BytesMemory__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/BytesStorage__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/BytesStorage__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/RevertReasonParser__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/RevertReasonParser__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/SafeERC20__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/SafeERC20__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/libraries/UniERC20__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/libraries/UniERC20__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/mixins/SelfdestructEthSender__factory.d.ts +17 -0
- package/dist/typechain-types/factories/contracts/mixins/SelfdestructEthSender__factory.js +33 -0
- package/dist/typechain-types/factories/contracts/mixins/SelfdestructEthSender__factory.js.map +1 -0
- package/dist/typechain-types/factories/contracts/mixins/index.d.ts +1 -0
- package/dist/typechain-types/factories/contracts/mixins/index.js +3 -1
- package/dist/typechain-types/factories/contracts/mixins/index.js.map +1 -1
- package/dist/typechain-types/factories/contracts/mocks/ERC20PermitMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/mocks/ERC20PermitMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/mocks/ERC20PermitMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/mocks/SelfdestructEthSenderMock__factory.d.ts +49 -0
- package/dist/typechain-types/factories/contracts/mocks/SelfdestructEthSenderMock__factory.js +75 -0
- package/dist/typechain-types/factories/contracts/mocks/SelfdestructEthSenderMock__factory.js.map +1 -0
- package/dist/typechain-types/factories/contracts/mocks/TokenCustomDecimalsMock__factory.d.ts +1 -11
- package/dist/typechain-types/factories/contracts/mocks/TokenCustomDecimalsMock__factory.js +1 -14
- package/dist/typechain-types/factories/contracts/mocks/TokenCustomDecimalsMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/mocks/TokenMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/mocks/TokenMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/mocks/TokenMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/mocks/index.d.ts +1 -0
- package/dist/typechain-types/factories/contracts/mocks/index.js +3 -1
- package/dist/typechain-types/factories/contracts/mocks/index.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/ECDSATest__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/ECDSATest__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/ECDSATest__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/RevertReasonParserTest__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/RevertReasonParserTest__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/RevertReasonParserTest__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/StringUtilTest__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/StringUtilTest__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/StringUtilTest__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressArrayMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressArrayMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressArrayMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressLibMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressLibMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressLibMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressSetMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressSetMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/AddressSetMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BySigTraitsMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BySigTraitsMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BySigTraitsMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BytesMemoryMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BytesMemoryMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BytesMemoryMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BytesStorageMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BytesStorageMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/BytesStorageMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/DaiLikePermitMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/DaiLikePermitMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/DaiLikePermitMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/ERC1271WalletMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/ERC1271WalletMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/ERC1271WalletMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/EthReceiverMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/EthReceiverMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/EthSenderMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/EthSenderMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/EthSenderMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/PermitAndCallMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/PermitAndCallMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/PermitAndCallMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/PermitableMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/PermitableMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/PermitableMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20NoReturnMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20NoReturnMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20NoReturnMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20PermitNoRevertMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20PermitNoRevertMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20PermitNoRevertMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ReturnFalseMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ReturnFalseMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ReturnFalseMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ReturnTrueMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ReturnTrueMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ReturnTrueMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ThroughZeroApprove__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ThroughZeroApprove__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20ThroughZeroApprove__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20WithSafeBalance__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20WithSafeBalance__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/ERC20WithSafeBalance__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/Permit2ReturnTrueMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/Permit2ReturnTrueMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/Permit2ReturnTrueMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/SafeERC20Wrapper__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/SafeERC20Wrapper__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/SafeERC20Wrapper__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/SafeWETHWrapper__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/SafeWETHWrapper__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/SafeERC20Helper.sol/SafeWETHWrapper__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/TokenWithBySig__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/TokenWithBySig__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/TokenWithBySig__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/USDCLikePermitMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/USDCLikePermitMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/USDCLikePermitMock__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20Capitals__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20Capitals__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20Capitals__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20bytes32Capitals__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20bytes32Capitals__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20bytes32Capitals__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20bytes32__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20bytes32__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ERC20bytes32__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ETHBadReceiver__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ETHBadReceiver__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/ETHBadReceiver__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/UniERC20Wrapper__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/UniERC20Wrapper__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/UniERC20Helper.sol/UniERC20Wrapper__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/WETH__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/WETH__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/WETH__factory.js.map +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/WethReceiverMock__factory.d.ts +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/WethReceiverMock__factory.js +1 -1
- package/dist/typechain-types/factories/contracts/tests/mocks/WethReceiverMock__factory.js.map +1 -1
- package/dist/typechain-types/index.d.ts +4 -0
- package/dist/typechain-types/index.js +6 -2
- package/dist/typechain-types/index.js.map +1 -1
- package/package.json +29 -21
- package/utils/README.md +198 -0
- package/utils/docify.utils.js +3 -34
- package/utils/file-dependencies.js +0 -0
- package/utils/test-docgen.js +0 -0
- package/utils/contract.hbs +0 -69
|
@@ -4,50 +4,79 @@ pragma solidity ^0.8.0;
|
|
|
4
4
|
|
|
5
5
|
import "./AddressArray.sol";
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* 3. contains
|
|
7
|
+
/**
|
|
8
|
+
* @title AddressSet
|
|
9
|
+
* @notice Library for managing sets of addresses, allowing operations such as add, remove, and contains.
|
|
10
|
+
* Utilizes the AddressArray library for underlying data storage.
|
|
12
11
|
*/
|
|
13
12
|
library AddressSet {
|
|
14
13
|
using AddressArray for AddressArray.Data;
|
|
15
14
|
|
|
16
15
|
uint256 internal constant _NULL_INDEX = type(uint256).max;
|
|
17
16
|
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* @dev Data struct from AddressArray.Data items
|
|
19
|
+
* and lookup mapping address => index in data array.
|
|
20
|
+
*/
|
|
19
21
|
struct Data {
|
|
20
22
|
AddressArray.Data items;
|
|
21
23
|
mapping(address => uint256) lookup;
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
|
|
26
|
+
/**
|
|
27
|
+
* @notice Determines the number of addresses in the set.
|
|
28
|
+
* @param s The set of addresses.
|
|
29
|
+
* @return The number of addresses in the set.
|
|
30
|
+
*/
|
|
25
31
|
function length(Data storage s) internal view returns (uint256) {
|
|
26
32
|
return s.items.length();
|
|
27
33
|
}
|
|
28
34
|
|
|
29
|
-
|
|
35
|
+
/**
|
|
36
|
+
* @notice Retrieves the address at a specified index in the set.
|
|
37
|
+
* @param s The set of addresses.
|
|
38
|
+
* @param index The index of the address to retrieve.
|
|
39
|
+
* @return The address at the specified index.
|
|
40
|
+
*/
|
|
30
41
|
function at(Data storage s, uint256 index) internal view returns (address) {
|
|
31
42
|
return s.items.at(index);
|
|
32
43
|
}
|
|
33
44
|
|
|
34
|
-
|
|
45
|
+
/**
|
|
46
|
+
* @notice Checks if the set contains the specified address.
|
|
47
|
+
* @param s The set of addresses.
|
|
48
|
+
* @param item The address to check for.
|
|
49
|
+
* @return True if the set contains the address, false otherwise.
|
|
50
|
+
*/
|
|
35
51
|
function contains(Data storage s, address item) internal view returns (bool) {
|
|
36
52
|
uint256 index = s.lookup[item];
|
|
37
53
|
return index != 0 && index != _NULL_INDEX;
|
|
38
54
|
}
|
|
39
55
|
|
|
40
|
-
|
|
56
|
+
/**
|
|
57
|
+
* @notice Returns list of addresses from storage `s`.
|
|
58
|
+
* @param s The set of addresses.
|
|
59
|
+
* @return The array of addresses stored in `s`.
|
|
60
|
+
*/
|
|
41
61
|
function get(Data storage s) internal view returns (address[] memory) {
|
|
42
62
|
return s.items.get();
|
|
43
63
|
}
|
|
44
64
|
|
|
45
|
-
|
|
65
|
+
/**
|
|
66
|
+
* @notice Puts list of addresses from `s` storage into `output` array.
|
|
67
|
+
* @param s The set of addresses.
|
|
68
|
+
* @return The provided output array filled with addresses.
|
|
69
|
+
*/
|
|
46
70
|
function get(Data storage s, address[] memory input) internal view returns (address[] memory) {
|
|
47
71
|
return s.items.get(input);
|
|
48
72
|
}
|
|
49
73
|
|
|
50
|
-
|
|
74
|
+
/**
|
|
75
|
+
* @notice Adds an address to the set if it is not already present.
|
|
76
|
+
* @param s The set of addresses.
|
|
77
|
+
* @param item The address to add.
|
|
78
|
+
* @return True if the address was added to the set, false if it was already present.
|
|
79
|
+
*/
|
|
51
80
|
function add(Data storage s, address item) internal returns (bool) {
|
|
52
81
|
uint256 index = s.lookup[item];
|
|
53
82
|
if (index != 0 && index != _NULL_INDEX) {
|
|
@@ -57,7 +86,12 @@ library AddressSet {
|
|
|
57
86
|
return true;
|
|
58
87
|
}
|
|
59
88
|
|
|
60
|
-
|
|
89
|
+
/**
|
|
90
|
+
* @notice Removes an address from the set if it exists.
|
|
91
|
+
* @param s The set of addresses.
|
|
92
|
+
* @param item The address to remove.
|
|
93
|
+
* @return True if the address was removed from the set, false if it was not found.
|
|
94
|
+
*/
|
|
61
95
|
function remove(Data storage s, address item) internal returns (bool) {
|
|
62
96
|
uint256 index = s.lookup[item];
|
|
63
97
|
s.lookup[item] = _NULL_INDEX;
|
|
@@ -75,7 +109,11 @@ library AddressSet {
|
|
|
75
109
|
return true;
|
|
76
110
|
}
|
|
77
111
|
|
|
78
|
-
|
|
112
|
+
/**
|
|
113
|
+
* @notice Erases set from storage `s`.
|
|
114
|
+
* @param s The set of addresses.
|
|
115
|
+
* @return items All removed items.
|
|
116
|
+
*/
|
|
79
117
|
function erase(Data storage s) internal returns(address[] memory items) {
|
|
80
118
|
items = s.items.get();
|
|
81
119
|
uint256 len = items.length;
|
|
@@ -2,14 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
pragma solidity ^0.8.0;
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @title BySigTraits
|
|
7
|
+
* @notice Provides utility functions for decoding and working with `BySig` call traits encoded in a single `uint256` value.
|
|
8
|
+
* @dev This library allows for the compact representation and manipulation of various call traits such as nonce type,
|
|
9
|
+
* deadline, relayer allowance, and nonce value using bit manipulation techniques.
|
|
10
|
+
*/
|
|
5
11
|
library BySigTraits {
|
|
12
|
+
/// @notice Thrown when an invalid nonce type is encountered.
|
|
6
13
|
error WrongNonceType();
|
|
7
14
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
/**
|
|
16
|
+
* @dev Represents the encoded traits of a call, packed within a single `uint256`.
|
|
17
|
+
* The encoding is as follows:
|
|
18
|
+
* [255-254] - 2 bits - Nonce type (e.g., Account, Selector, Unique).
|
|
19
|
+
* [253-248] - 6 bits - Reserved for future use.
|
|
20
|
+
* [247-208] - 40 bits - Deadline (Unix timestamp at which the call becomes invalid).
|
|
21
|
+
* [207-128] - 80 bits - Relayer address's lower bits. A value of 0 indicates that any relayer is allowed.
|
|
22
|
+
* [127-0] - 128 bits - Nonce value (used for ensuring calls are executed in order and not replayed).
|
|
23
|
+
*/
|
|
13
24
|
type Value is uint256;
|
|
14
25
|
|
|
15
26
|
enum NonceType {
|
|
@@ -25,21 +36,42 @@ library BySigTraits {
|
|
|
25
36
|
uint256 constant internal RELAYER_BIT_MASK = (1 << 80) - 1;
|
|
26
37
|
uint256 constant internal NONCE_MASK = (1 << 128) - 1;
|
|
27
38
|
|
|
39
|
+
/**
|
|
40
|
+
* @notice Decodes and returns the nonce type from the traits.
|
|
41
|
+
* @param traits The encoded call traits.
|
|
42
|
+
* @return The decoded nonce type as an enum.
|
|
43
|
+
*/
|
|
28
44
|
function nonceType(Value traits) internal pure returns(NonceType) {
|
|
29
45
|
uint256 _type = Value.unwrap(traits) >> TYPE_BIT_SHIFT;
|
|
30
46
|
if (_type > uint256(NonceType.Unique)) revert WrongNonceType();
|
|
31
47
|
return NonceType(_type);
|
|
32
48
|
}
|
|
33
49
|
|
|
50
|
+
/**
|
|
51
|
+
* @notice Decodes and returns the deadline from the traits.
|
|
52
|
+
* @param traits The encoded call traits.
|
|
53
|
+
* @return The decoded deadline timestamp.
|
|
54
|
+
*/
|
|
34
55
|
function deadline(Value traits) internal pure returns(uint256) {
|
|
35
56
|
return (Value.unwrap(traits) >> DEADLINE_BIT_SHIFT) & DEADLINE_BIT_MASK;
|
|
36
57
|
}
|
|
37
58
|
|
|
59
|
+
/**
|
|
60
|
+
* @notice Checks if a given relayer address is allowed to relay the call based on the traits.
|
|
61
|
+
* @param traits The encoded call traits.
|
|
62
|
+
* @param relayer The address of the relayer to check.
|
|
63
|
+
* @return True if the relayer is allowed, false otherwise.
|
|
64
|
+
*/
|
|
38
65
|
function isRelayerAllowed(Value traits, address relayer) internal pure returns(bool) {
|
|
39
66
|
uint256 relayerBits = (Value.unwrap(traits) >> RELAYER_BIT_SHIFT) & RELAYER_BIT_MASK;
|
|
40
67
|
return relayerBits == 0 || (uint160(relayer) & RELAYER_BIT_MASK) == relayerBits;
|
|
41
68
|
}
|
|
42
69
|
|
|
70
|
+
/**
|
|
71
|
+
* @notice Decodes and returns the nonce value from the traits.
|
|
72
|
+
* @param traits The encoded call traits.
|
|
73
|
+
* @return The decoded nonce value.
|
|
74
|
+
*/
|
|
43
75
|
function nonce(Value traits) internal pure returns(uint256) {
|
|
44
76
|
return Value.unwrap(traits) & NONCE_MASK;
|
|
45
77
|
}
|
|
@@ -4,6 +4,10 @@ pragma solidity ^0.8.0;
|
|
|
4
4
|
|
|
5
5
|
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @title ECDSA signature operations
|
|
9
|
+
* @notice Provides functions for recovering addresses from signatures and verifying signatures, including support for EIP-2098 compact signatures.
|
|
10
|
+
*/
|
|
7
11
|
library ECDSA {
|
|
8
12
|
// EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
|
|
9
13
|
// unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
|
|
@@ -18,6 +22,15 @@ library ECDSA {
|
|
|
18
22
|
uint256 private constant _COMPACT_S_MASK = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
|
|
19
23
|
uint256 private constant _COMPACT_V_SHIFT = 255;
|
|
20
24
|
|
|
25
|
+
/**
|
|
26
|
+
* @notice Recovers the signer's address from the signature.
|
|
27
|
+
* @dev Recovers the address that has signed a hash with `(v, r, s)` signature.
|
|
28
|
+
* @param hash The keccak256 hash of the data signed.
|
|
29
|
+
* @param v The recovery byte of the signature.
|
|
30
|
+
* @param r The first 32 bytes of the signature.
|
|
31
|
+
* @param s The second 32 bytes of the signature.
|
|
32
|
+
* @return signer The address of the signer.
|
|
33
|
+
*/
|
|
21
34
|
function recover(
|
|
22
35
|
bytes32 hash,
|
|
23
36
|
uint8 v,
|
|
@@ -39,6 +52,14 @@ library ECDSA {
|
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
54
|
|
|
55
|
+
/**
|
|
56
|
+
* @notice Recovers the signer's address from the signature using `r` and `vs` components.
|
|
57
|
+
* @dev Recovers the address that has signed a hash with `r` and `vs`, where `vs` combines `v` and `s`.
|
|
58
|
+
* @param hash The keccak256 hash of the data signed.
|
|
59
|
+
* @param r The first 32 bytes of the signature.
|
|
60
|
+
* @param vs The combined `v` and `s` values of the signature.
|
|
61
|
+
* @return signer The address of the signer.
|
|
62
|
+
*/
|
|
42
63
|
function recover(
|
|
43
64
|
bytes32 hash,
|
|
44
65
|
bytes32 r,
|
|
@@ -60,6 +81,12 @@ library ECDSA {
|
|
|
60
81
|
}
|
|
61
82
|
}
|
|
62
83
|
|
|
84
|
+
/**
|
|
85
|
+
* @notice Recovers the signer's address from a hash and a signature.
|
|
86
|
+
* @param hash The keccak256 hash of the signed data.
|
|
87
|
+
* @param signature The full signature from which the signer will be recovered.
|
|
88
|
+
* @return signer The address of the signer.
|
|
89
|
+
*/
|
|
63
90
|
/// @dev WARNING!!!
|
|
64
91
|
/// There is a known signature malleability issue with two representations of signatures!
|
|
65
92
|
/// Even though this function is able to verify both standard 65-byte and compact 64-byte EIP-2098 signatures
|
|
@@ -102,6 +129,14 @@ library ECDSA {
|
|
|
102
129
|
}
|
|
103
130
|
}
|
|
104
131
|
|
|
132
|
+
/**
|
|
133
|
+
* @notice Verifies the signature for a hash, either by recovering the signer or using EIP-1271's `isValidSignature` function.
|
|
134
|
+
* @dev Attempts to recover the signer's address from the signature; if the address is non-zero, checks if it's valid according to EIP-1271.
|
|
135
|
+
* @param signer The address to validate the signature against.
|
|
136
|
+
* @param hash The hash of the signed data.
|
|
137
|
+
* @param signature The signature to verify.
|
|
138
|
+
* @return success True if the signature is verified, false otherwise.
|
|
139
|
+
*/
|
|
105
140
|
function recoverOrIsValidSignature(
|
|
106
141
|
address signer,
|
|
107
142
|
bytes32 hash,
|
|
@@ -114,6 +149,16 @@ library ECDSA {
|
|
|
114
149
|
return isValidSignature(signer, hash, signature);
|
|
115
150
|
}
|
|
116
151
|
|
|
152
|
+
/**
|
|
153
|
+
* @notice Verifies the signature for a hash, either by recovering the signer or using EIP-1271's `isValidSignature` function.
|
|
154
|
+
* @dev Attempts to recover the signer's address from the signature; if the address is non-zero, checks if it's valid according to EIP-1271.
|
|
155
|
+
* @param signer The address to validate the signature against.
|
|
156
|
+
* @param hash The hash of the signed data.
|
|
157
|
+
* @param v The recovery byte of the signature.
|
|
158
|
+
* @param r The first 32 bytes of the signature.
|
|
159
|
+
* @param s The second 32 bytes of the signature.
|
|
160
|
+
* @return success True if the signature is verified, false otherwise.
|
|
161
|
+
*/
|
|
117
162
|
function recoverOrIsValidSignature(
|
|
118
163
|
address signer,
|
|
119
164
|
bytes32 hash,
|
|
@@ -128,6 +173,15 @@ library ECDSA {
|
|
|
128
173
|
return isValidSignature(signer, hash, v, r, s);
|
|
129
174
|
}
|
|
130
175
|
|
|
176
|
+
/**
|
|
177
|
+
* @notice Verifies the signature for a hash, either by recovering the signer or using EIP-1271's `isValidSignature` function.
|
|
178
|
+
* @dev Attempts to recover the signer's address from the signature; if the address is non-zero, checks if it's valid according to EIP-1271.
|
|
179
|
+
* @param signer The address to validate the signature against.
|
|
180
|
+
* @param hash The hash of the signed data.
|
|
181
|
+
* @param r The first 32 bytes of the signature.
|
|
182
|
+
* @param vs The combined `v` and `s` values of the signature.
|
|
183
|
+
* @return success True if the signature is verified, false otherwise.
|
|
184
|
+
*/
|
|
131
185
|
function recoverOrIsValidSignature(
|
|
132
186
|
address signer,
|
|
133
187
|
bytes32 hash,
|
|
@@ -141,6 +195,15 @@ library ECDSA {
|
|
|
141
195
|
return isValidSignature(signer, hash, r, vs);
|
|
142
196
|
}
|
|
143
197
|
|
|
198
|
+
/**
|
|
199
|
+
* @notice Verifies the signature for a given hash, attempting to recover the signer's address or validates it using EIP-1271 for 65-byte signatures.
|
|
200
|
+
* @dev Attempts to recover the signer's address from the signature. If the address is a contract, checks if the signature is valid according to EIP-1271.
|
|
201
|
+
* @param signer The expected signer's address.
|
|
202
|
+
* @param hash The keccak256 hash of the signed data.
|
|
203
|
+
* @param r The first 32 bytes of the signature.
|
|
204
|
+
* @param vs The last 32 bytes of the signature, with the last byte being the recovery id.
|
|
205
|
+
* @return success True if the signature is valid, false otherwise.
|
|
206
|
+
*/
|
|
144
207
|
function recoverOrIsValidSignature65(
|
|
145
208
|
address signer,
|
|
146
209
|
bytes32 hash,
|
|
@@ -154,6 +217,14 @@ library ECDSA {
|
|
|
154
217
|
return isValidSignature65(signer, hash, r, vs);
|
|
155
218
|
}
|
|
156
219
|
|
|
220
|
+
/**
|
|
221
|
+
* @notice Validates a signature for a hash using EIP-1271, if `signer` is a contract.
|
|
222
|
+
* @dev Makes a static call to `signer` with `isValidSignature` function selector from EIP-1271.
|
|
223
|
+
* @param signer The address of the signer to validate against, which could be an EOA or a contract.
|
|
224
|
+
* @param hash The hash of the signed data.
|
|
225
|
+
* @param signature The signature to validate.
|
|
226
|
+
* @return success True if the signature is valid according to EIP-1271, false otherwise.
|
|
227
|
+
*/
|
|
157
228
|
function isValidSignature(
|
|
158
229
|
address signer,
|
|
159
230
|
bytes32 hash,
|
|
@@ -176,6 +247,16 @@ library ECDSA {
|
|
|
176
247
|
}
|
|
177
248
|
}
|
|
178
249
|
|
|
250
|
+
/**
|
|
251
|
+
* @notice Validates a signature for a hash using EIP-1271, if `signer` is a contract.
|
|
252
|
+
* @dev Makes a static call to `signer` with `isValidSignature` function selector from EIP-1271.
|
|
253
|
+
* @param signer The address of the signer to validate against, which could be an EOA or a contract.
|
|
254
|
+
* @param hash The hash of the signed data.
|
|
255
|
+
* @param v The recovery byte of the signature.
|
|
256
|
+
* @param r The first 32 bytes of the signature.
|
|
257
|
+
* @param s The second 32 bytes of the signature.
|
|
258
|
+
* @return success True if the signature is valid according to EIP-1271, false otherwise.
|
|
259
|
+
*/
|
|
179
260
|
function isValidSignature(
|
|
180
261
|
address signer,
|
|
181
262
|
bytes32 hash,
|
|
@@ -200,6 +281,15 @@ library ECDSA {
|
|
|
200
281
|
}
|
|
201
282
|
}
|
|
202
283
|
|
|
284
|
+
/**
|
|
285
|
+
* @notice Validates a signature for a hash using EIP-1271, if `signer` is a contract.
|
|
286
|
+
* @dev Makes a static call to `signer` with `isValidSignature` function selector from EIP-1271.
|
|
287
|
+
* @param signer The address of the signer to validate against, which could be an EOA or a contract.
|
|
288
|
+
* @param hash The hash of the signed data.
|
|
289
|
+
* @param r The first 32 bytes of the signature.
|
|
290
|
+
* @param vs The last 32 bytes of the signature, with the last byte being the recovery id.
|
|
291
|
+
* @return success True if the signature is valid according to EIP-1271, false otherwise.
|
|
292
|
+
*/
|
|
203
293
|
function isValidSignature(
|
|
204
294
|
address signer,
|
|
205
295
|
bytes32 hash,
|
|
@@ -224,6 +314,14 @@ library ECDSA {
|
|
|
224
314
|
}
|
|
225
315
|
}
|
|
226
316
|
|
|
317
|
+
/**
|
|
318
|
+
* @notice Verifies if a 65-byte signature is valid for a given hash, according to EIP-1271.
|
|
319
|
+
* @param signer The address of the signer to validate against, which could be an EOA or a contract.
|
|
320
|
+
* @param hash The hash of the signed data.
|
|
321
|
+
* @param r The first 32 bytes of the signature.
|
|
322
|
+
* @param vs The combined `v` (recovery id) and `s` component of the signature, packed into the last 32 bytes.
|
|
323
|
+
* @return success True if the signature is valid according to EIP-1271, false otherwise.
|
|
324
|
+
*/
|
|
227
325
|
function isValidSignature65(
|
|
228
326
|
address signer,
|
|
229
327
|
bytes32 hash,
|
|
@@ -249,6 +347,12 @@ library ECDSA {
|
|
|
249
347
|
}
|
|
250
348
|
}
|
|
251
349
|
|
|
350
|
+
/**
|
|
351
|
+
* @notice Generates a hash compatible with Ethereum's signed message format.
|
|
352
|
+
* @dev Prepends the hash with Ethereum's message prefix before hashing it.
|
|
353
|
+
* @param hash The hash of the data to sign.
|
|
354
|
+
* @return res The Ethereum signed message hash.
|
|
355
|
+
*/
|
|
252
356
|
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 res) {
|
|
253
357
|
// 32 is the length in bytes of hash, enforced by the type signature above
|
|
254
358
|
// return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
|
|
@@ -259,6 +363,13 @@ library ECDSA {
|
|
|
259
363
|
}
|
|
260
364
|
}
|
|
261
365
|
|
|
366
|
+
/**
|
|
367
|
+
* @notice Generates an EIP-712 compliant hash.
|
|
368
|
+
* @dev Encodes the domain separator and the struct hash according to EIP-712.
|
|
369
|
+
* @param domainSeparator The EIP-712 domain separator.
|
|
370
|
+
* @param structHash The EIP-712 struct hash.
|
|
371
|
+
* @return res The EIP-712 compliant hash.
|
|
372
|
+
*/
|
|
262
373
|
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 res) {
|
|
263
374
|
// return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
|
|
264
375
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
pragma solidity ^0.8.0;
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @title RevertReasonForwarder
|
|
7
|
+
* @notice Provides utilities for forwarding and retrieving revert reasons from failed external calls.
|
|
8
|
+
*/
|
|
6
9
|
library RevertReasonForwarder {
|
|
7
|
-
|
|
10
|
+
/**
|
|
11
|
+
* @dev Forwards the revert reason from the latest external call.
|
|
12
|
+
* This method allows propagating the revert reason of a failed external call to the caller.
|
|
13
|
+
*/
|
|
8
14
|
function reRevert() internal pure {
|
|
9
15
|
// bubble up revert reason from latest external call
|
|
10
16
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
@@ -14,7 +20,11 @@ library RevertReasonForwarder {
|
|
|
14
20
|
}
|
|
15
21
|
}
|
|
16
22
|
|
|
17
|
-
|
|
23
|
+
/**
|
|
24
|
+
* @dev Retrieves the revert reason from the latest external call.
|
|
25
|
+
* This method enables capturing the revert reason of a failed external call for inspection or processing.
|
|
26
|
+
* @return reason The latest external call revert reason.
|
|
27
|
+
*/
|
|
18
28
|
function reReason() internal pure returns (bytes memory reason) {
|
|
19
29
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
20
30
|
reason := mload(0x40)
|
|
@@ -4,7 +4,9 @@ pragma solidity ^0.8.0;
|
|
|
4
4
|
|
|
5
5
|
import "./StringUtil.sol";
|
|
6
6
|
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* @title RevertReasonParser
|
|
9
|
+
* @notice Library that allows to parse unsuccessful arbitrary calls revert reasons.
|
|
8
10
|
* See https://solidity.readthedocs.io/en/latest/control-structures.html#revert for details.
|
|
9
11
|
* Note that we assume revert reason being abi-encoded as Error(string) so it may fail to parse reason
|
|
10
12
|
* if structured reverts appear in the future.
|
|
@@ -20,7 +22,14 @@ library RevertReasonParser {
|
|
|
20
22
|
bytes4 private constant _ERROR_SELECTOR = bytes4(keccak256("Error(string)"));
|
|
21
23
|
bytes4 private constant _PANIC_SELECTOR = bytes4(keccak256("Panic(uint256)"));
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @dev Parses revert reason from failed calls, returning it with a `prefix`.
|
|
27
|
+
* Handles standard `Error(string)` and `Panic(uint256)` formats, defaulting to `Unknown(data)` for unrecognized patterns.
|
|
28
|
+
*
|
|
29
|
+
* @param data The revert data to parse.
|
|
30
|
+
* @param prefix String to add before the parsed reason for context.
|
|
31
|
+
* @return The formatted revert reason.
|
|
32
|
+
*/
|
|
24
33
|
function parse(bytes memory data, string memory prefix) internal pure returns (string memory) {
|
|
25
34
|
// https://solidity.readthedocs.io/en/latest/control-structures.html#revert
|
|
26
35
|
// We assume that revert reason is abi-encoded as Error(string)
|
|
@@ -282,7 +282,7 @@ library SafeERC20 {
|
|
|
282
282
|
|
|
283
283
|
/**
|
|
284
284
|
* @notice The function attempts to call the permit function on a given ERC20 token.
|
|
285
|
-
* @dev The function is designed to support a variety of permit functions, namely: IERC20Permit, IDaiLikePermit, and IPermit2.
|
|
285
|
+
* @dev The function is designed to support a variety of permit functions, namely: IERC20Permit, IDaiLikePermit, IERC7597Permit and IPermit2.
|
|
286
286
|
* It accommodates both Compact and Full formats of these permit types.
|
|
287
287
|
* Please note, it is expected that the `expiration` parameter for the compact Permit2 and the `deadline` parameter
|
|
288
288
|
* for the compact Permit are to be incremented by one before invoking this function. This approach is motivated by
|
|
@@ -2,18 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
pragma solidity ^0.8.0;
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @title StringUtil
|
|
7
|
+
* @dev Library with gas-efficient string operations.
|
|
8
|
+
*/
|
|
6
9
|
library StringUtil {
|
|
10
|
+
/**
|
|
11
|
+
* @notice Converts a uint256 value to its hexadecimal string representation.
|
|
12
|
+
* @param value The uint256 value to convert.
|
|
13
|
+
* @return The hexadecimal string representation of the input value.
|
|
14
|
+
*/
|
|
7
15
|
function toHex(uint256 value) internal pure returns (string memory) {
|
|
8
16
|
return toHex(abi.encodePacked(value));
|
|
9
17
|
}
|
|
10
18
|
|
|
19
|
+
/**
|
|
20
|
+
* @notice Converts an address to its hexadecimal string representation.
|
|
21
|
+
* @param value The address to convert.
|
|
22
|
+
* @return The hexadecimal string representation of the input address.
|
|
23
|
+
*/
|
|
11
24
|
function toHex(address value) internal pure returns (string memory) {
|
|
12
25
|
return toHex(abi.encodePacked(value));
|
|
13
26
|
}
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
28
|
+
/**
|
|
29
|
+
* @dev Converts arbitrary bytes to their hexadecimal string representation.
|
|
30
|
+
* This is an assembly adaptation of highly optimized toHex16 code by Mikhail Vladimirov.
|
|
31
|
+
* Reference: https://stackoverflow.com/a/69266989
|
|
32
|
+
* @param data The bytes to be converted to hexadecimal string.
|
|
33
|
+
* @return result The hexadecimal string representation of the input bytes.
|
|
34
|
+
*/
|
|
17
35
|
function toHex(bytes memory data) internal pure returns (string memory result) {
|
|
18
36
|
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
|
|
19
37
|
function _toHex16(input) -> output {
|
|
@@ -8,7 +8,11 @@ import "../interfaces/IERC20MetadataUppercase.sol";
|
|
|
8
8
|
import "./SafeERC20.sol";
|
|
9
9
|
import "./StringUtil.sol";
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @title UniERC20
|
|
13
|
+
* @dev Library to abstract the handling of ETH and ERC20 tokens, enabling unified interaction with both. It allows usage of ETH as ERC20.
|
|
14
|
+
* Utilizes SafeERC20 for ERC20 interactions and provides additional utility functions.
|
|
15
|
+
*/
|
|
12
16
|
library UniERC20 {
|
|
13
17
|
using SafeERC20 for IERC20;
|
|
14
18
|
|
|
@@ -23,12 +27,21 @@ library UniERC20 {
|
|
|
23
27
|
IERC20 private constant _ETH_ADDRESS = IERC20(0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE);
|
|
24
28
|
IERC20 private constant _ZERO_ADDRESS = IERC20(address(0));
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
/**
|
|
31
|
+
* @dev Determines if the specified token is ETH.
|
|
32
|
+
* @param token The token to check.
|
|
33
|
+
* @return bool True if the token is ETH, false otherwise.
|
|
34
|
+
*/
|
|
27
35
|
function isETH(IERC20 token) internal pure returns (bool) {
|
|
28
36
|
return (token == _ZERO_ADDRESS || token == _ETH_ADDRESS);
|
|
29
37
|
}
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
/**
|
|
40
|
+
* @dev Retrieves the balance of the specified token for an account.
|
|
41
|
+
* @param token The token to query the balance of.
|
|
42
|
+
* @param account The address of the account.
|
|
43
|
+
* @return uint256 The balance of the token for the specified account.
|
|
44
|
+
*/
|
|
32
45
|
function uniBalanceOf(IERC20 token, address account) internal view returns (uint256) {
|
|
33
46
|
if (isETH(token)) {
|
|
34
47
|
return account.balance;
|
|
@@ -37,8 +50,13 @@ library UniERC20 {
|
|
|
37
50
|
}
|
|
38
51
|
}
|
|
39
52
|
|
|
40
|
-
|
|
41
|
-
|
|
53
|
+
/**
|
|
54
|
+
* @dev Transfers a specified amount of the token to a given address.
|
|
55
|
+
* Note: Does nothing if the amount is zero.
|
|
56
|
+
* @param token The token to transfer.
|
|
57
|
+
* @param to The address to transfer the token to.
|
|
58
|
+
* @param amount The amount of the token to transfer.
|
|
59
|
+
*/
|
|
42
60
|
function uniTransfer(
|
|
43
61
|
IERC20 token,
|
|
44
62
|
address payable to,
|
|
@@ -56,8 +74,14 @@ library UniERC20 {
|
|
|
56
74
|
}
|
|
57
75
|
}
|
|
58
76
|
|
|
59
|
-
|
|
60
|
-
|
|
77
|
+
/**
|
|
78
|
+
* @dev Transfers a specified amount of the token from one address to another.
|
|
79
|
+
* Note: Does nothing if the amount is zero.
|
|
80
|
+
* @param token The token to transfer.
|
|
81
|
+
* @param from The address to transfer the token from.
|
|
82
|
+
* @param to The address to transfer the token to.
|
|
83
|
+
* @param amount The amount of the token to transfer.
|
|
84
|
+
*/
|
|
61
85
|
function uniTransferFrom(
|
|
62
86
|
IERC20 token,
|
|
63
87
|
address payable from,
|
|
@@ -83,17 +107,31 @@ library UniERC20 {
|
|
|
83
107
|
}
|
|
84
108
|
}
|
|
85
109
|
|
|
86
|
-
|
|
110
|
+
/**
|
|
111
|
+
* @dev Retrieves the symbol from ERC20 metadata of the specified token.
|
|
112
|
+
* @param token The token to retrieve the symbol of.
|
|
113
|
+
* @return string The symbol of the token.
|
|
114
|
+
*/
|
|
87
115
|
function uniSymbol(IERC20 token) internal view returns (string memory) {
|
|
88
116
|
return _uniDecode(token, IERC20Metadata.symbol.selector, IERC20MetadataUppercase.SYMBOL.selector);
|
|
89
117
|
}
|
|
90
118
|
|
|
91
|
-
|
|
119
|
+
/**
|
|
120
|
+
* @dev Retrieves the name from ERC20 metadata of the specified token.
|
|
121
|
+
* @param token The token to retrieve the name of.
|
|
122
|
+
* @return string The name of the token.
|
|
123
|
+
*/
|
|
92
124
|
function uniName(IERC20 token) internal view returns (string memory) {
|
|
93
125
|
return _uniDecode(token, IERC20Metadata.name.selector, IERC20MetadataUppercase.NAME.selector);
|
|
94
126
|
}
|
|
95
127
|
|
|
96
|
-
|
|
128
|
+
/**
|
|
129
|
+
* @dev forceApprove the specified amount of the token to a given address.
|
|
130
|
+
* Reverts if the token is ETH.
|
|
131
|
+
* @param token The token to approve.
|
|
132
|
+
* @param to The address to approve the token to.
|
|
133
|
+
* @param amount The amount of the token to approve.
|
|
134
|
+
*/
|
|
97
135
|
function uniApprove(
|
|
98
136
|
IERC20 token,
|
|
99
137
|
address to,
|
|
@@ -104,8 +142,15 @@ library UniERC20 {
|
|
|
104
142
|
token.forceApprove(to, amount);
|
|
105
143
|
}
|
|
106
144
|
|
|
107
|
-
|
|
108
|
-
|
|
145
|
+
/**
|
|
146
|
+
* @dev Internal function to decode token metadata (name or symbol).
|
|
147
|
+
* 20K gas is provided to account for possible implementations of name/symbol
|
|
148
|
+
* (token implementation might be behind proxy or store the value in storage)
|
|
149
|
+
* @param token The token to decode metadata for.
|
|
150
|
+
* @param lowerCaseSelector The selector for the lowercase metadata function.
|
|
151
|
+
* @param upperCaseSelector The selector for the uppercase metadata function.
|
|
152
|
+
* @return result The decoded metadata value.
|
|
153
|
+
*/
|
|
109
154
|
function _uniDecode(
|
|
110
155
|
IERC20 token,
|
|
111
156
|
bytes4 lowerCaseSelector,
|