Solidity

無法將乙太幣轉移到智能合約

  • February 27, 2018

我正在測試一個智能合約,需要向它發送一些乙太幣,但是當我嘗試這樣做時,我收到錯誤“似乎此交易將失敗。如果您送出它,它可能會消耗您提供的所有氣體”。有人可以建議如何解決它嗎?

錯誤截圖

這是原始碼,省略了基本的 Oraclize 類,因為我認為這無關緊要,但仍然有很多 Oraclize 相關的東西,對不起:

contract AdContractTest3 is usingOraclize {
   string public calc_script_base_url = "http://5.45.80.18/ad-price-calculator.php";
   uint public total_price_in_szabo;
   uint public szabo_per_placement;
   uint public szabo_per_view;
   address public client;
   address public blogger;

   event LogPriceUpdated(string price);
   event LogNewOraclizeQuery(string description);
   event LogNotEnoughEther();
   event LogEtherSent();
   event LogOnDeposit(address sender, uint amount);

   function AdContractTest3(uint price_in_szabo_per_placement, uint price_in_szabo_per_view, address client_address, address blogger_address) public payable {
       szabo_per_placement = price_in_szabo_per_placement;
       szabo_per_view = price_in_szabo_per_view;
       client = client_address;
       blogger = blogger_address;
   }

   function strConcat(string s1, string s2, string s3, string s4, string s5, string s6, string s7) internal pure returns (string)
   {
       return strConcat(strConcat(s1, s2, s3, s4), strConcat(s5, s6, s7));
   }

   function __callback(bytes32 myid, string result) public {
       require(msg.sender == oraclize_cbAddress());
       myid; // Silence compiler warnings
       total_price_in_szabo = parseInt(result);
       LogPriceUpdated(result);
   }

   function updatePrice(string video_url) public payable returns(string) {
       if (oraclize_getPrice("URL") > this.balance) {
           LogNewOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
       } else {
           var url = strConcat(calc_script_base_url, "?price_in_szabo_per_placement=", uint2str(szabo_per_placement), "&price_in_szabo_per_view=", uint2str(szabo_per_view), "&video_url=", video_url);
           oraclize_query("URL", strConcat("json(", url, ").total_price_in_szabo"));
           var log_msg = strConcat("Oraclize query '", url, "' was sent, standing by for the answer...");
           LogNewOraclizeQuery(log_msg);
       }
   }

   function payForWork() public {
       if (total_price_in_szabo * 1 szabo > this.balance) {
           LogNotEnoughEther();
       } else {
           blogger.transfer(total_price_in_szabo * 1 szabo);
           LogEtherSent();
       }
   }

   function onDeposit() public payable {
       LogOnDeposit(msg.sender, msg.value);
   }
}

當您在不提供任何其他數據的情況下向合約發送 ETH 時,合約會嘗試呼叫未命名的回退函式,該函式必須標記為payable. 這個函式看起來像這樣:

function() public payable {
   // handle ETH
}

您還沒有在契約中定義這樣的功能。如果您嘗試呼叫該onDeposit()函式,則不應簡單地“發送” ETH,而是傳遞相關數據以呼叫該函式,並將 ETH 包含在交易中。

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