Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][27.ABIEncode&Decode] Added abi.encodeCall #801

Open
1 task done
gsangeryee opened this issue Oct 2, 2024 · 1 comment
Open
1 task done

[Feature][27.ABIEncode&Decode] Added abi.encodeCall #801

gsangeryee opened this issue Oct 2, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@gsangeryee
Copy link

Details(细节)

  • abi.encodeCall: Syntactic sugar vesion of abi.encodeWithSelector and abi.encodeWithSignature

The most commonly used method in projects is abi.encodeCall.
Its advantage is that, compared to abi.encodeWithSignature and abi.encodeWithSelector, abi.encodeCall automatically checks the function signature and parameters at compile time, preventing spelling mistakes and parameter type mismatches. abi.encodeWithSignature dynamically accepts a string to represent the function signature, so when you use abi.encodeWithSignature, the compiler does not check if the function name or parameters are correct, as it treats the string as regular input data.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

interface IERC20 {
   function transfer(address, uint256) external;
}

contract Token {
   function transfer(address, uint256) external {}
}

contract AbiEncode {
   function test(address _contract, bytes calldata data) external {
      (bool ok,) = _contract.call(data);
      require(ok, "call failed");
   }

   function encodeWithSignature(address to, uint256 amount)
      external
      pure
      returns (bytes memory)
   {
      // Typo is not checked - "transfer(address, uint)"
      return abi.encodeWithSignature("transfer(address,uint256)", to, amount);
   }

   function encodeWithSelector(address to, uint256 amount)
      external
      pure
      returns (bytes memory)
   {
      // Type is not checked - (IERC20.transfer.selector, true, amount)
      return abi.encodeWithSelector(IERC20.transfer.selector, to, amount);
   }

   function encodeCall(address to, uint256 amount)
      external
      pure
      returns (bytes memory)
   {
      // Typo and type errors will not compile
      return abi.encodeCall(IERC20.transfer, (to, amount));
   }
}

Motivation(动机)

In practical projects, I often encounter scenarios where abi.encodeCall is used. Therefore, it is recommended to add this function to this chapter.

Are you willing to submit a PR?(你愿意提交PR吗?)

  • Yes I am willing to submit a PR!(是的我愿意)
@XdpCs
Copy link
Collaborator

XdpCs commented Nov 4, 2024

It seems good.

@XdpCs XdpCs added the enhancement New feature or request label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants
@gsangeryee @XdpCs and others