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

  • @Rob Hitchens How can we get the values stored in array? – Crissi Mariam Robert Jan 5 '17 at 5:36
4

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.

contract.balances[owner].then(console.log);

Try

contract.balances.call(owner).then(console.log);

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

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

and, with round brackets ...

contract.balance.call(owner)
.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, privacy policy and cookie policy

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