Solidity

address.transfer 不起作用

  • January 21, 2021
pragma solidity ^0.4.23;

contract ApprovalContract {
address public sender;
address public receiver;

address public constant approver = 0x5AEDA56215b167893e80B4fE645BA6d5Bab767DE;

constructor() public {
// tbd
}
function deposit(address _receiver) external payable {
   require(msg.value > 0);
   sender = msg.sender;
   receiver = _receiver;
}

function viewApprover() external pure returns(address){
   return(approver);
}

function viewRe() external pure returns(address){
   return(receiver);
}


function approve() external payable{
   require(msg.sender == approver);

   receiver.transfer(address(this).balance); // this line not working

}
}

我的傳輸變數不可見。

在地址中進行與參數相關的查找後,未找到成員“轉移”或不可見。

採用

address payable public receiver;

在您希望在合約中轉移乙太幣的地址的聲明中。根據可靠性文件:

地址類型有兩種風格,它們基本相同:

address:保存一個 20 字節的值(乙太坊地址的大小)。

address payable: 與地址相同,但有附加成員傳輸和發送。這種區別背後的想法是,應付地址是您可以發送乙太幣的地址,而普通地址不能發送乙太幣。

您可以隨時address轉換為payable address

address(uint160(receiver)).transfer(...);

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