Solidity

如何更改從 0.4.6 升級的solidity 0.5.1 編譯器版本的approveAndCall() 函式?

  • February 28, 2019

這是我使用編譯器版本 0.4.6 時使用的程式碼片段:

function approveAndCall(address _spender, uint256 _value, bytes memory _extraData) public returns (bool success) {
       allowed[msg.sender][_spender] = _value;
       emit Approval(msg.sender, _spender, _value);
       if(!_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { 
           throw; 
       }
       return true;
   }

現在我正在用 0.5.1 版本編譯這段程式碼,我該怎麼辦?我已經提到 Read the Docs content : v0.5.0 Breaking Changes,但我真的不明白。

我不確定下面的程式碼是否可以解決您的問題。您可以嘗試以下方法並讓我知道結果。

function approveAndCall(address _spender, uint256 _value, bytes memory _extraData) public returns (bool success) {
   allowed[msg.sender][_spender] = _value;
   emit Approval(msg.sender, _spender, _value);
   (bool success, bytes memory data)  = address(_spender).call(abi.encode(
       bytes4(
           bytes32(
               keccak256(
                   abi.encodePacked(
                       "receiveApproval(address,uint256,address,bytes)"
                   )
               )
           )
       ),
       msg.sender, _value, this, _extraData))
   ;
   require(success);
   return true;
}

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