Skip to content

Commit 4fd2f8b

Browse files
balajipachaiAmxx
andauthored
Replace abi.encodeWithSelector & abi.encodeWithSignature with abi.encodeCall (#4293)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
1 parent 85696d8 commit 4fd2f8b

File tree

8 files changed

+14
-13
lines changed

8 files changed

+14
-13
lines changed

.changeset/big-plums-cover.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'openzeppelin-solidity': major
3+
---
4+
Use `abi.encodeCall` in place of `abi.encodeWithSelector` and `abi.encodeWithSignature` for improved type-checking of parameters

contracts/mocks/MulticallTest.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ contract MulticallTest {
1212
) external {
1313
bytes[] memory calls = new bytes[](recipients.length);
1414
for (uint256 i = 0; i < recipients.length; i++) {
15-
calls[i] = abi.encodeWithSignature("transfer(address,uint256)", recipients[i], amounts[i]);
15+
calls[i] = abi.encodeCall(multicallToken.transfer, (recipients[i], amounts[i]));
1616
}
1717

1818
bytes[] memory results = multicallToken.multicall(calls);

contracts/mocks/ReentrancyMock.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ contract ReentrancyMock is ReentrancyGuard {
2626
function countThisRecursive(uint256 n) public nonReentrant {
2727
if (n > 0) {
2828
_count();
29-
(bool success, ) = address(this).call(abi.encodeWithSignature("countThisRecursive(uint256)", n - 1));
29+
(bool success, ) = address(this).call(abi.encodeCall(this.countThisRecursive, (n - 1)));
3030
require(success, "ReentrancyMock: failed call");
3131
}
3232
}

contracts/mocks/proxy/UUPSLegacy.sol

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ contract UUPSUpgradeableLegacyMock is UUPSUpgradeableMock {
3131
if (!rollbackTesting.value) {
3232
// Trigger rollback using upgradeTo from the new implementation
3333
rollbackTesting.value = true;
34-
Address.functionDelegateCall(
35-
newImplementation,
36-
abi.encodeWithSignature("upgradeTo(address)", oldImplementation)
37-
);
34+
Address.functionDelegateCall(newImplementation, abi.encodeCall(this.upgradeTo, (oldImplementation)));
3835
rollbackTesting.value = false;
3936
// Check rollback was effective
4037
require(oldImplementation == _getImplementation(), "ERC1967Upgrade: upgrade breaks further upgrades");

contracts/token/ERC20/extensions/ERC4626.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ abstract contract ERC4626 is ERC20, IERC4626 {
6767
*/
6868
function _tryGetAssetDecimals(IERC20 asset_) private view returns (bool, uint8) {
6969
(bool success, bytes memory encodedDecimals) = address(asset_).staticcall(
70-
abi.encodeWithSelector(IERC20Metadata.decimals.selector)
70+
abi.encodeCall(IERC20Metadata.decimals, ())
7171
);
7272
if (success && encodedDecimals.length >= 32) {
7373
uint256 returnedDecimals = abi.decode(encodedDecimals, (uint256));

contracts/token/ERC20/utils/SafeERC20.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ library SafeERC20 {
2424
* non-reverting calls are assumed to be successful.
2525
*/
2626
function safeTransfer(IERC20 token, address to, uint256 value) internal {
27-
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
27+
_callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
2828
}
2929

3030
/**
3131
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
3232
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
3333
*/
3434
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
35-
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
35+
_callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
3636
}
3737

3838
/**
@@ -62,10 +62,10 @@ library SafeERC20 {
6262
* 0 before setting it to a non-zero value.
6363
*/
6464
function forceApprove(IERC20 token, address spender, uint256 value) internal {
65-
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
65+
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
6666

6767
if (!_callOptionalReturnBool(token, approvalCall)) {
68-
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
68+
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
6969
_callOptionalReturn(token, approvalCall);
7070
}
7171
}

contracts/utils/cryptography/SignatureChecker.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ library SignatureChecker {
4141
bytes memory signature
4242
) internal view returns (bool) {
4343
(bool success, bytes memory result) = signer.staticcall(
44-
abi.encodeWithSelector(IERC1271.isValidSignature.selector, hash, signature)
44+
abi.encodeCall(IERC1271.isValidSignature, (hash, signature))
4545
);
4646
return (success &&
4747
result.length >= 32 &&

contracts/utils/introspection/ERC165Checker.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ library ERC165Checker {
109109
*/
110110
function supportsERC165InterfaceUnchecked(address account, bytes4 interfaceId) internal view returns (bool) {
111111
// prepare call
112-
bytes memory encodedParams = abi.encodeWithSelector(IERC165.supportsInterface.selector, interfaceId);
112+
bytes memory encodedParams = abi.encodeCall(IERC165.supportsInterface, (interfaceId));
113113

114114
// perform static call
115115
bool success;

0 commit comments

Comments
 (0)