pragma solidity ^0.4.2;
contract Transfer {
address public owner;
mapping (address => uint) balances;
// constructor
function Transfer() {
owner=msg.sender;
balances[owner]=1000;
}
function transferToUser(address _to, uint _value) returns (bool success) {
if(balances[msg.sender]< _value){
return false;
}
balances[msg.sender] -= _value;
balances[_to] += _value;
return true;
}
function getBalance(address _user) returns (uint _balance){
return balances[_user];
}
}
This is the code for contract i have deployed using testrpc and truffle.
I have used the following commands in truffle.
var contract=Transfer.deployed();
contract.owner.call().then(console.log);
The owner is printed correctly.Then used call() method to print balance.
contract.balances[owner].then(console.log);
This call method returned error.How can we print the value in balances[owner]?
The functions defined in contract is called as follows.
contract.transferToUser("0x4f91a3661a18328bc5d995a1f8b63cc69778a529",300).then(console.log)
contract.getBalance("0x4f91a3661a18328bc5d995a1f8b63cc69778a529").then(console.log)
Both functions returned hash value of transaction.How can we check the balances returned from the function getBalance() such as balances[msg.sender],balances[_to],balances[_user]?