// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
interface IERC20 {
function transfer(address, uint) external;
}
contract Token {
mapping(address => uint) balances;
function transfer(address addr, uint amount) external {
balances[addr] += amount;
}
function getBalance(address addr) public view returns(uint balance) {
balance = balances[addr];
}
}
contract AbiEncode {
function test(address _contract, bytes calldata data) external {
(bool ok, ) = _contract.call(data);
require(ok, "call failed");
}
function encodeWithSignature(address to, uint 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, uint 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, uint amount) external pure returns (bytes memory) {
// Typo and type errors will not compile
return abi.encodeCall(IERC20.transfer, (to, amount));
}
}
https://solidity-by-example.org/abi-encode/