Solidity

如何將wei從合約轉移到特定地址?

  • September 4, 2019

我正在製作一個拍賣系統,我想確保當有人出價更高時,契約會將錢退回到先前出價的地址。

pragma solidity >=0.4.22 <0.6.0;

contract simpleAuction{
   uint currentValue = 0;
   address buyer;

   function set() public payable{
       uint sent = msg.value;
       require(sent>currentValue, "Bid less than current value.");

       buyer.transfer(currentValue);

       currentValue = sent;
       buyer = msg.sender;
   }

   function winner() public view returns (address addressWinner, uint newValue){
       addressWinner = buyer;
       newValue = currentValue;
   }
}

錯誤出現buyer.transfer (currentValue);transfer()僅適用於msg.sender.

如果您使用的是 Solidity 0.5.0 或更高版本,則需要創建buyer一個payable地址。您可以通過添加payablebuyer聲明來做到這一點:

address payable buyer;

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