Solidity

即使在適當的條件下,乙太坊錢包也不允許交易

  • March 5, 2018

即使我在合約中使用了 if/else 來確保轉賬價值不高於合約餘額,乙太坊錢包仍顯示“似乎此交易將失敗……”。

 function mySales( address a) constant public canWithdraw returns (uint){
   return totalSales[a];
}

function topSalesPerson() constant public returns ( address ){
   return topsales;
}

event bonusDepleted(string);

function submitSales() public payable canWithdraw{

   uint amount = msg.value;
   uint bonus_temp;
   if (this.balance >0){
       if(amount > 50000000000000000000){
           bonus_temp = amount/10; // 10% bonus
       }
       if(bonus_temp > 20000000000000000000){
           bonus_temp = 20000000000000000000;  // max bonus = 20 ether
       }

       if (this.balance > bonus_temp){
            msg.sender.transfer(bonus_temp);  // transfer bonus in case of enough balance
       }
      else{
           msg.sender.transfer(this.balance); // else transfer remaining amount as Bonus 
      }
       owner.transfer(amount);

       Bonus[msg.sender] += bonus_temp;
       totalSales[msg.sender] += msg.value;

       if( totalSales[topsales] < totalSales[msg.sender]){
           topsales = msg.sender;
       }
   }

   if(this.balance == 0){
       bonusDepleted("Bonus has reached its limit and no bonus will be provided for future sales");
   }
}`

因此,如果合約有 3 個乙太幣,那麼對於 50 個乙太幣,它允許 txn,但對於 60 個乙太幣,它會顯示警告和錯誤“內在氣體太低”,因為它應該剛剛退還最後 3 個乙太幣。

同樣對於像“mySales”這樣的只讀函式,如何從“ethereum -wallet”呼叫它們,因為在“從合約部分讀取”下將地址輸入此函式時它不會返回任何內容。我可以從命令行使用它。

我認為問題在這裡:

if (this.balance > bonus_temp){
   msg.sender.transfer(bonus_temp);  // transfer bonus in case of enough balance
}
else {
   msg.sender.transfer(this.balance); // else transfer remaining amount as Bonus 
}
owner.transfer(amount);

無法保證有足夠的資金transfer用於owner. 特別是,任何時間amount都是正數並且您處理該else條款,然後合約的餘額將為零,並且它之後的行將恢復交易。

一個合理的解決方法可能是在計算獎金之前transfer執行此操作,但這實際上取決於您想要的行為。owner

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