pragma solidity ^0.4.2;

       contract Transfer {

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

               // constructor
               function Transfer() {

               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();

The owner is printed correctly.Then used call() method to print balance.


This call method returned error.How can we print the value in balances[owner]?

The functions defined in contract is called as follows.



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]?

  • @Rob Hitchens How can we get the values stored in array? Jan 5, 2017 at 5:36

1 Answer 1


You've got the right idea with checking balances for one address at a time and using promises.

The square brackets are weird here. Looks like you're adapting array style but this is a function call that returns one number. You got a txhash because you sent a transaction to be mined ... different than call() which is local, read only, faster, free (no gas) and gives the returned values.




You're pretty close here. I just built it out a bit.

.(then(function(ownerReturned) {
  console.log("got owner", ownerReturned);
.catch(function(err) {
 console.error("problem getting owner", err;

and, with round brackets ...

.then(function(balanceReturned) {
  // balanceReturned will be in bigNumber format
  // try ...
  var bal = balanceReturned.toString(10);
  console.log("Balance returned", bal);   
.catch(function(error) {
  console.error("Error getting balance", error):

With luck, I haven't given you something with errors in it. It's a bit of scribble on my part.

For the transfer function, it needs to update the state, so drop call() like this, get a receipt and wait for it to be mined (not shown):

transferToUser(args, args).
.then(function(txn) {
  // Add something to wait for it to be mined.
  // after it's mined, this should work
  return contract.balance.call(args);
  .then(function(returnedBalance) {
  // carry on with the updated balance in hand

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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