1

I want to create a function which will accept address as argument and return balance of smart contract here is what i

function get_balance(address _add)
 {
     balance = balanceOf[_add];
     return balance;

 }

But it giving me following

 Different number of arguments in return statement than in returns declaration.
     return balance;

Please let me where i am wrong or is there any other way to return balance

Here is full code of contract

pragma solidity ^0.4.6; 
  contract tokenRecipient { function receiveApproval(address _from,    uint256 _value, address _token, bytes _extraData); } 
 contract RUBY { 
 string public standard = 'RUBY 1.0'; 
 string public name; 
 string public symbol; 
 uint8 public decimals; 
 uint256 public totalSupply; 
 uint256 public balance; 
 address public owner; 
 mapping (address => uint256) public balanceOf; 
 mapping (address => mapping (address => uint256)) public allowance; 
 event Transfer(address indexed from, address indexed to, uint256 value); 
 function RUBY() { 
     balanceOf[msg.sender] = 10000; 
     totalSupply = 10000; 
     name = 'RUBY'; 
     symbol = 'RUBY'; 
     decimals = 8; 
     owner=msg.sender; 
 } 
 modifier onlyOwner { 
     if (msg.sender != owner) throw; 
     _; 
 } 
function getBalance(address _addr) constant returns(uint) {
   return _addr.balance;
}

 function transfer(address _to, uint256 _value) { 
     if (_to == 0x0) throw; 
     if (balanceOf[msg.sender] < _value) throw; 
     if (balanceOf[_to] + _value < balanceOf[_to]) throw; 
     balanceOf[msg.sender] -= _value; 
     balanceOf[_to] += _value; 
     Transfer(msg.sender, _to, _value); 
 } 
 function approve(address _spender, uint256 _value) 
 returns (bool success) { 
     allowance[msg.sender][_spender] = _value; 
     return true; 
 } 
 function approveAndCall(address _spender, uint256 _value, bytes _extraData) 
     returns (bool success) { 
         tokenRecipient spender = tokenRecipient(_spender); 
         if (approve(_spender, _value)) { 
         spender.receiveApproval(msg.sender, _value, this, _extraData); 
         return true; 
     } 
 }
 function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { 
     if (_to == 0x0) throw; 
     if (balanceOf[_from] < _value) throw; 
     if (balanceOf[_to] + _value < balanceOf[_to]) throw; 
     if (_value > allowance[_from][msg.sender]) throw; 
     balanceOf[_from] -= _value; 
     balanceOf[_to] += _value; 
     allowance[_from][msg.sender] -= _value; 
    Transfer(_from, _to, _value); 
     return true; 
 } 
}
0

In your function definition, you're missing the returns param type:

function get_balance(address _add) returns(<balance_var_type>)
{
     balance = balanceOf[_add];
     return balance;

}

Try this way, it should work, anyway post the whole code of your contract. However, in that way you get the balance of your custom token, not the ETH balance of the wallet. If you need the ETH balance you have to do something like this:

function getEthBalance(address _addr) constant returns(uint) {
    return _addr.balance;
}

N.B: the balance result is in Wei not in ETH. Hope it helps.

Edit:

With the function get_balance() you can return the balance of RUBY token of an account.

enter image description here

| improve this answer | |
  • I have edit question and added full code of contract the function i have added is not accessible can you please check – Anil Baweja Jul 18 '17 at 15:32
  • Which function? getBalance()? – Paolo Guerra Jul 18 '17 at 17:02
  • I've tried your code and the getBalance() returns correctly the amount of ETH – Paolo Guerra Jul 18 '17 at 17:04
  • but i need the balance of RUBY(contract i have created) – Anil Baweja Jul 18 '17 at 20:08
  • So you're function is fine: function get_balance(address _add) returns(uint256) { balance = balanceOf[_add]; return balance; } – Paolo Guerra Jul 18 '17 at 21:39

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.