Solidity

無法使用接收功能在合約記憶體入乙太幣

  • October 27, 2022

程式碼是:

contract sendEther{
   //to send ether, we need to deposit some ether first in this contract
   //We can do it by declaring constructor as payable which enables us to deposit some ether shile deploying
   //OR we can declare a receive/fallback function to receive some ether after the contract is deployed
   //mainly we will use receive func coz we want to just receive ether
   //since we do not have a fallback func, any function called apart from sending ether will fail

   //constructor() payable {}

   receive() external payable {}

   function sendviaTransfer(address payable _to) external payable{
       _to.transfer(123); 
       
   }

   function sendviaSend(address payable _to) external payable{
       bool sent = _to.send(123);

       require(sent, "failed");
   }

   function sendviaCall(address payable _to) external payable{
       (bool sent, ) = _to.call{value : 123}(""); //will forward all available gas with 123 eth to execute this transaction
       require(sent, "failed");

   }
}

如果我不使用應付建構子而只使用接收應付然後我無法存入乙太和 sendViaX 函式失敗….請提出一個解決方案

我從這個影片中挑選了這個 https://www.youtube.com/watch?v=mlPc3EW-nNA&list=PLO5VPQH6OWdVQwpQfw9rZ67O6Pjfo6q-p&index=39

你試圖以什麼方式發送乙太幣?

部署合約後,您應該能夠在 remix 中設置值

在此處輸入圖像描述

然後點擊按鈕進行交易,這應該可以工作,由於接收功能,合約應該能夠接收乙太幣

在此處輸入圖像描述

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