Solidity

堅固性呼叫(abi.encodeWithSignature)不起作用

  • July 27, 2021

我在使用“address.call(abi.encodeWithSignature(….))”方法時遇到問題。下面是我在 Remix 中測試的程式碼:

pragma solidity >=0.7.0 <0.9.0;


library lib {
     event check(bool, bytes);

     function remoteCall(address c) internal
     {
          (bool success, bytes memory data) = c.call(abi.encodeWithSignature("callback(bool)", true));
           emit check(success, data);
     }
 }

 contract main{
    using lib for *;
    event test(bool);

    function useCallBack() external{
        lib.remoteCall(address(this));
    }

    function callback(bool b) internal {
        emit test(b);
    }

}

我總是得到:success = false,並且永遠不會觸發“測試”事件。

有誰知道我的程式碼有什麼問題?

函式回調的可見性應更改為公共或外部。由於是內部的,地址類型 c 無法通過低級呼叫訪問該函式。

library lib {
 event check(bool, bytes);

 function remoteCall(address c) internal
 {
      (bool success, bytes memory data) = c.call(abi.encodeWithSignature("callback(bool)", true));
       emit check(success, data);
 }}

contract main{
using lib for *;
event test(bool);

function useCallBack() external{
    lib.remoteCall(address(this));
}

function callback(bool b) public{
    emit test(b);
}}

引用自:https://ethereum.stackexchange.com/questions/104167