0

So I get the error below when attempting to send the coins to another address, and with the debugger, I can't figure out where's the problem. What am I doing wrong? (I just started learning solidity a few days ago).

pragma 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;
    }
}

Error:

enter image description here

0

There is no error with your Contract from question deployed to testrpc, for example, with the last stable version of compiler:

enter image description here

| improve this answer | |
0

Ok, I fixed it by replacing msg.sender with the owner address var declared in the constructor. Does anyone know why now it works?

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;
    }
}
| improve this answer | |
  • 1
    The only thing I can see is that you are probably using Solidity compiler version 0.4.0, whereas @ViktorBaranov tested the same code with version 0.4.13. Although this seems to be very simple code, it might be that the earlier compiler had a bug. – Ajoy Bhatia Jul 7 '17 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.