1

I am a newbie in Solidity. I am learning by reading official docs, i.e. Solidity I am trying to get the balance of an address in solidity using the simple balance method. Below is my code:

pragma solidity ^0.5.1;


contract Firstry {
    address abc = 0xCBD6832Ebc203e49E2B771897067fce3c58575ac ;
    abc.balance(unit 256) ; 
}

Upon trying to compile it, I am getting the following error:

ParseError : Expected Identifier but got '('.

Can anyone please help me with what I am doing wrong?

3
  • No , actually I wanted to know what mistake is in this command . I am new to coding and don't know much about geth or "this" .
    – Brijesh
    Commented Jan 8, 2022 at 11:41
  • The question really does look like a duplicate without your clarifications. I edited it to make it more specific. I hope it conveys your intention better.
    – cameel
    Commented Jan 8, 2022 at 14:31
  • @Brijesh The error is that you are not using the correct syntax, the compiler doesn't understand the code and fails.
    – Ismael
    Commented Jan 9, 2022 at 18:59

2 Answers 2

1

what you're doing wrong is how you are trying to get balance.

.balance is a simple readable property of address not a function so you can will have to call it just by using

address.balance; instead of address.balance();

that was the main problem.

now you will have to assign the value to a variable and then get that value using a function.

here is the complete code

contract test {
        address myAddress = 0xCBD6832Ebc203e49E2B771897067fce3c58575ac; 
        uint256 balance = myAddress.balance;

        function getBalance() public view returns (uint256) {
            return balance;
        }
}

Here is link to docs of solidity version 5.17

if you're starting just now i would advise you to learn using latest docs instead of much older version

2
  • I apologize if my query sounds silly , but can you once shed some light on the difference between usage of address.balance; & address.balance(); . Even in the solodity link the syntax is like "<address>.balance (uint256) " , so I used same format.
    – Brijesh
    Commented Jan 8, 2022 at 12:52
  • @Brijesh there are two types of properties readable properties like balance <address>.balance (uint256) and function properties that execute some operation and return some data like <address payable>.transfer(uint256 amount) in documentation there are red curly braces and black curly braces black curly braces just show that these are return types of calls and not supposed to be passed as values whereas red show how a function should be called and what it returns as well in this case eth docs are not beginner friendly and confusing. so best of luck for further learning. Commented Jan 8, 2022 at 13:08
0

you are trying to call balance on a variable. This would be equivalent of doing something like:

let address = "0xCBD6832Ebc203e49E2B771897067fce3c58575ac";
console.log(address.balance(uint256 1);

To get the contract balance you can do this:

address(this).balance

This question explains what this means here

4
  • Actually this address is the address of my metamask wallet and it has ether in it ,it is not related to any contract. I put my wallet address into this variable and then am trying to compile the programme but it is showing error.
    – Brijesh
    Commented Jan 8, 2022 at 11:54
  • You cannot get eth balance of a wallet in a smart contract, eth is not erc20 compliant, you would have to use a token that is, like WETH @Brijesh
    – nemo
    Commented Jan 8, 2022 at 12:14
  • @dogboy This answer is misleading, OP is looking to get balance of a wallet address whereas this answer shows how get balance of address. Also you can totally get ETH balance in smartcontract. Commented Jan 8, 2022 at 13:18
  • @MuhammadFaizanUlHaq looking at the comments he was asked if it was a duplication of another question which asked if he wanted to get the contract address and he only said he wanted to know what it means. Also please elaborate on the latter part
    – nemo
    Commented Jan 8, 2022 at 15:51

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