Solidity

如何將 msg.sender 作為應付地址傳遞給結構?

  • August 15, 2021

目前正在遵循教程,並且此錯誤在我的程式碼中不斷重複。

我正在嘗試將msg.sender結構添加為應付地址,但錯誤:Invalid type for argument in function call. Invalid implicit conversion from address to address payable requested.在整個程式碼中重複。

只是好奇如何設置msg.sender結構。

非常感謝。

   struct Image {
       uint256 id;
       string hash;
       string description;
       uint256 tipAmount;
       address payable author; // < ---- Invalid implicit conversion from address to address payable requested.
   }

/////

   function uploadImage(string memory _imageHash, string memory _description) public {

       require(bytes(_imageHash).length > 0, "Must Have Hash!");

       require(bytes(_description).length > 0, "Must have description");

       require(msg.sender != address(0), "Must have author address");

       imageCount++;

       images[imageCount] = Image(
           imageCount,
           _imageHash,
           _description,
           0,
           msg.sender // < ---- Invalid implicit conversion from address to address payable requested.
       );

       emit ImageCreated(imageCount, _imageHash, _description, 0, msg.sender);
   }

您必須以這種方式轉換 msg.sender 地址:

address payable addr = address(uint160(msg.sender)); 

包裝 msg.sender payable(msg.sender) 為我工作了 Solidity 0.8.2

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