Open In App
Related Articles

Creating a Smart Contract that Returns Address and Balance of Owner using Solidity

Improve Article
Save Article
Like Article

Problem: Create a smart contract named MyContract having a state variable as owner. Create a constructor to fetch the address of the owner from msg and hold it into the state variable owner. Also, create a function getBalance() to show the current balance of the owner.

Solution: Every smart contract is owned by an address called as owner. A smart contract can know its owner’s address using sender property and its available balance using a special built-in object called msg.

Step 1: Open Remix-IDE.

Step 2: Select File Explorer from the left side icons and select Solidity in the environment. Click on New option below the Solidity environment. Enter the file name as MyContract.sol and Click on the OK button.

Step 3: Enter the following Solidity Code.


// Solidity program to
// retrieve address and
// balance of owner
pragma solidity ^0.6.8;     
// Creating a contract
contract MyContract
    // Private state variable
    address private owner;
     // Defining a constructor   
     constructor() public{   
    // Function to get 
    // address of owner
    function getOwner(
    ) public view returns (address) {    
        return owner;
    // Function to return 
    // current balance of owner
    function getBalance(
    ) public view returns(uint256){
        return owner.balance;

Step 4:  Compile the file MyContract.sol from the Solidity Compiler tab. 

Step 5: Deploy the smart contract from the Deploy and Run Transaction tab and you will get the balance and address of the owner.

Step 6: The output below shows the address and the balance of the owner.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 11 May, 2022
Like Article
Save Article
Similar Reads