Solidity

將硬幣發送到地址時出現 Solidity 操作碼錯誤

  • July 7, 2017

因此,當我嘗試將硬幣發送到另一個地址時,我收到以下錯誤,並且使用調試器,我無法弄清楚問題出在哪裡。我究竟做錯了什麼?(我幾天前才開始學習solidity)。

編譯指示 ^0.4.0;

contract HelloWorld {

   mapping (address => uint) balances;
   address owner;

   event Transfer(address indexed _from, address indexed _to, uint256 _value);

   function HelloWorld() {
       balances[msg.sender] = 1500;
       owner = msg.sender;
   }

   function getBalances(address addr) returns(uint) {
       return balances[addr];
   }

   function getOwner() returns(address) {
       return owner;
   }

   function sendCoin(address receiver, uint amount) returns(bool success) {

       if(balances[msg.sender] < amount) return false; // Check amount of coins

       balances[msg.sender] -= amount;
       balances[receiver] += amount;

       Transfer(msg.sender, receiver, amount);
       return true;
   }
}

錯誤:

在此處輸入圖像描述

從部署到 testrpc 的問題中,您的契約沒有錯誤,例如,使用最後一個穩定版本的編譯器:

在此處輸入圖像描述

好的,我通過將 msg.sender 替換為建構子中聲明的所有者地址 var 來修復它。有誰知道為什麼現在它有效?

contract HelloWorld {

   mapping (address => uint) balances;
   address owner;

   event Transfer(address indexed _from, address indexed _to, uint256 _value);

   function HelloWorld() {
       balances[msg.sender] = 1500;
       owner = msg.sender;
   }

   function getBalance(address addr) returns(uint) {
       return balances[addr];
   }

   function getOwner() returns(address) {
       return owner;
   }

   function sendCoin(address receiver, uint amount) returns(bool success) {

       if(amount > balances[owner]) return false; // Check balance

       balances[owner] -= amount;
       balances[receiver] += amount;

       Transfer(owner, receiver, amount);
       return true;
   }
}

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