Solidity

預期{但發現不斷的plzz幫助

  • September 11, 2022

   function equity_in_Hadcoins(address investor) external constant returns (uint) {
       return equity_Hadcoins[investor];
   } 
   // getting the equity in USD of an investor
   function equity_in_usd(address investor) external constant returns (uint) {
       return equity_usd[investor];
   } 
   //buying Hadcoins
   function buy_Hadcoins(address investor, uint usd_invested) external  can_buy_Hadcoins(usd_invested) {
       uint  Hadcoins_bought = usd_invested * usd_to_Hadcoins;
       equity_Hadcoins[investor] += Hadcoins_bought;
       equity_usd[investor] = equity_Hadcoins[investor] / 1000;
       total_Hadcoins_bought ;
   }

constant關鍵字不能用於裝飾函式,在定義這些函式時刪除它們。

在較新版本的 Solidity 中,您需要在函式上使用view關鍵字來表示該函式將僅從合約狀態中讀取,或者pure表示該函式不會讀取並且不會修改儲存。

您的程式碼重構如下:

   function equity_in_Hadcoins(address investor) external view returns (uint) {
       return equity_Hadcoins[investor];
   } 
   // getting the equity in USD of an investor
   function equity_in_usd(address investor) external view returns (uint) {
       return equity_usd[investor];
   } 
   //buying Hadcoins
   function buy_Hadcoins(address investor, uint usd_invested) external  can_buy_Hadcoins(usd_invested) {
       uint  Hadcoins_bought = usd_invested * usd_to_Hadcoins;
       equity_Hadcoins[investor] += Hadcoins_bought;
       equity_usd[investor] = equity_Hadcoins[investor] / 1000;
       total_Hadcoins_bought ;
   }

檢查view關鍵字文件:https ://docs.soliditylang.org/en/v0.8.17/contracts.html?highlight=pure#view-functions

檢查pure關鍵字文件:https ://docs.soliditylang.org/en/v0.8.17/contracts.html?highlight=pure#pure-functions

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