0

I have a simple piece of code but I can't not get it to work correctly.

pragma solidity ^0.4.18;

contract Interface { function transfer(address _to, uint256 _value) public returns(bool); function balanceOf(address _owner) public constant returns(uint256); }

contract SolidContract is Interface { address owner; mapping(address => uint256) balances; function SolidContract() public { owner = msg.sender; balances[owner] = 100000; } function transfer(address _to, uint256 _value) public returns(bool) { if (balances[msg.sender] >= _value) { balances[msg.sender] -= _value; balances[_to] += _value; return true; } else { return false; } } function balanceOf(address _owner) public constant returns(uint256) { return balances[_owner]; } }

contract TestCallingContract { Interface c; function TestCallingContract(Interface _c) public{ c = _c; } function transfer(address _to, uint256 _value) public returns(bool) { return c.transfer(_to, _value); } function balanceOf(address _owner) public constant returns(uint256) { return c.balanceOf(_owner); } } When I call transfer from TestCallingContract, the if statement "if (balances[msg.sender] >= _value)" is always evaluated to false and therefore the transaction cannot go through. If I remove the if statement then everything works. Any idea how to fix this?

Thanks!

Chris

  • Actually after removing the if statement, even if the transfer returns true, the balance is not altered... – Chris Sun Mar 28 '18 at 9:29
0

contract Interface { function transfer(address, uint256) public returns(bool) {} function balanceOf(address) public constant returns(uint256) {} }

It is the syntax problem with the interface...

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.