当用户为下面的代码片段调用
contract Sig {
function setA() public {
// How to get function signature of `setA()` at this location while calling through `setB()` in runtime
}
function setB() public {
setA();
}
}发布于 2023-01-11 07:53:32
您可以使用keccak256函数在setB()函数中获取setA()的签名,如下所示:
function setA() public {
// code here
}
function setB() public {
bytes4 signature = bytes4(keccak256("setA()"));
setA();
}发布于 2023-01-11 09:42:56
您可以将字符串" setA()“传递给keccak256()函数,以获得setA()的函数签名。使用abi.encodeWithSignature()对函数签名进行编码&返回函数签名的bytes4表示形式。
function setA() public {
bytes4 signature = keccak256(abi.encodeWithSignature("setA()"));
// use the signature variable as needed
}
function setB() public {
setA();
}https://ethereum.stackexchange.com/questions/142723
复制相似问题