2

I have this contract and can't figure out where's the problem. Any ideas? (I'm a noob in solidity, just started a few days ago).

HelloWorld.sol file:

mapping (address => uint) balances;

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

function getBalance() public constant returns(uint) {
    return balances;
}

function sendCoin(address receiver, uint amount) public constant returns(bool sufficient) {
    if (balances[msg.sender] < amount) return false;

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

Moon.sol file:

mapping (address => uint) balances;

function Moon () {
    balances[tx.origin] = 1000;
}   

function getBalance() public constant returns(uint) {
    return balances;
}

In the Moon.sol file I missed out "mapping (address => uint) balances;".

Truffle compiler error:

$ truffle compile
Compiling .\contracts\HelloWorld.sol...
Compiling .\contracts\Moon.sol...

/C/Users/guerr/SmartContracs/firstPorject/contracts/Moon.sol:10:3: : 
Undeclared identifier.
            balances[tx.origin] = 1000;
            ^------^
Compiliation failed. See above.
  • Please paste the contents of the editor and terminal so the text is searchable/editable :) – Travis Jacobs Jul 6 '17 at 15:22
  • Also the error appears to have occurred in Moon.sol rather than HelloWorld.sol, so the contents of that file (moon) are needed as well – Travis Jacobs Jul 6 '17 at 15:24
1

Your code looks okay but you should change tx.origin to msg.sender for security reasons. tx.origin is almost always an error.

I would get rid of that moon.sol file and try again.

  • Yes, my bad didn't even see that was the Moon.sol file generating the error :( – Paolo Guerra Jul 6 '17 at 15:30

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.