Ethereum-Wallet-Dapp

在乙太坊錢包中執行合約時,在估計費用消耗欄位中顯示“我們無法估計 gas”錯誤

  • October 25, 2018

我曾嘗試在乙太坊錢包中創建合約,但有時在執行這些合約時會顯示錯誤。錯誤說It seems this transaction will fail. If you submit it, it may consume all the gas you provide。誰能幫我解決這個問題?

我的智能合約是這樣的:

pragma solidity ^0.4.18;
contract MyToken {
       function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
   }

   contract TransferToken{
       function sendTheToken(address _from, address _to, uint256 _value)public returns(string) {
           address tokenContractAddress = <MyTokenaddress>
           MyToken token = MyToken (tokenContractAddress);
           bool sendSuccess = token.transferFrom(_from, _to, _value);
           if(sendSuccess=true) return 'success';
           else return 'Failure';
       }
   }

在上述合約中,MyToken 是我之前為加密貨幣創建的合約。

pragma solidity ^0.4.18;
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }

contract MyToken {
   string public name;
   string public symbol;
   uint8 public decimals = 2;
   uint256 public totalSupply;
   mapping (address => uint256) public balanceOf;
   mapping (address => mapping (address => uint256)) public allowance;
   event Transfer(address indexed from, address indexed to, uint256 value);

   function MyToken(
       uint256 initialSupply,
       string tokenName,
       string tokenSymbol
   ) public {
       totalSupply = initialSupply * 10 ** uint256(decimals);  
       balanceOf[msg.sender] = totalSupply;                
       name = tokenName;                                
       symbol = tokenSymbol;                               
   }


   function _transfer(address _from, address _to, uint _value) internal {
       require(_to != 0x0);
       require(balanceOf[_from] >= _value);
       require(balanceOf[_to] + _value > balanceOf[_to]);
       uint previousBalances = balanceOf[_from] + balanceOf[_to];
       balanceOf[_from] -= _value;
       balanceOf[_to] += _value;
       Transfer(_from, _to, _value);
       assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
   }


   function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
       require(_value <= allowance[_from][msg.sender]);  
       allowance[_from][msg.sender] -= _value;
       _transfer(_from, _to, _value);
       return true;
   }
}

您嘗試呼叫的方法可能存在一些錯誤。這主要發生在require()函式中的不滿足或發生其他一些可見錯誤時。如果您可以共享程式碼或至少共享功能,我可以為您提供幫助。

編輯(程式碼發布後):

transferFrom()一個require()需要滿足才能被執行。要求是呼叫地址應該有足夠的餘量,以便進行資金轉移。

雖然函式會檢查返回的值是真還是假,但只有當transferFrom()方法滿足時,require()它才會返回true。否則,該函式將只是 throw 而不是 return false。當它會拋出的場景時,就會"It seems this transaction will fail. If you submit it,it may consume all the gas you provide"發生錯誤。

推薦的方法是

contract TransferToken{
   function sendTheToken(address _from, address _to, uint256 _value)public returns(string) {
       address tokenContractAddress = <MyTokenaddress>
       MyToken token = MyToken (tokenContractAddress);
       token.transferFrom(_from, _to, _value);
   }
}

如果滿足要求,則函式呼叫將成功。如果失敗,它將拋出並返回剩餘的氣體。

如果您只是想調試合約並且不想修改sendTheToken()函式,則可以通過transferFrom()這種方式將方法修改為以下程式碼。

function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
       if(_value <= allowance[_from][msg.sender]) {
           allowance[_from][msg.sender] -= _value;
           _transfer(_from, _to, _value);
           return true;
       } else {
           return false;
       }
   }

**注意:**無論哪種方式,方法中都不需要 bool 成功transferFrom()。您可以直接返回布爾值。節省一些氣體。

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