Payable

從合約地址轉移到發件人

  • September 1, 2021

我正在編寫一份眾籌契約,其中發件人有兩種選擇。首先,payIn 呼叫到合約地址的轉賬。其次,withdraw 呼叫從合約地址到發送者的轉賬。但是,我不能轉移到契約的地址,因為它不是payable。我怎樣才能使契約應付?

pragma solidity >=0.7.0 <0.9.0;

contract Crowdfunding{
   // Crowdfunding settings.
   address payable owner;
   uint256 goal;
   bool isOpen;
   
   // Store the crowd funding values
   mapping(address => uint256) public crowd;

   constructor(uint256 _goal) {
       owner = payable(msg.sender);
       isOpen = true;
       goal = _goal;
   }
   
   function payIn() public fundingIsOpen payable {
       address(this).transfer(msg.value);
   }
   
   function withdraw() public fundingIsOpen payable {
       address(msg.sender).transfer(crowd[msg.sender]);
   }

...

有很多方法可以解決這個問題,一個可行的方法是

function withdraw() public fundingIsOpen payable {
   payable(msg.sender).transfer(amount);
}

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