abi.encodeWithSignature(....)的前四个字节就是函数选择器,也就是msg.sig,
也可这这样计算 bytes4(keccak256(bytes(_func)))
这种方法可以直接调用合约的方法
eg:
addr.call(abi.encodeWithSignature("transfer(address,uint256)", SomeAddress, 123))
bytes4(keccak256("set(uint256)"))
eg2:
function foo(string memory _message, uint _x) public payable returns (uint) {
emit Received(msg.sender, msg.value, _message);
return _x + 1;
}
(bool success, bytes memory data) = _addr.call{value: msg.value, gas: 5000}(
abi.encodeWithSignature("foo(string,uint256)", "call foo", 123)
);
Sort: Trending