Skip to content

Address Types

One type, which is very specific to Ethereum and Solidity, is the type "address".

Ethereum supports transfer of Ether and communication between Smart Contracts. Those reside on an address. Addresses can be stored in Smart Contracts and can be used to transfer Ether from the Smart Contract to to an address stored in a variable.

That's where variables of the type address come in.

In general, a variable of the type address holds 20 bytes. That's all that happens internally. Let's see what we can do with Solidity and addresses.

Smart Contract Example

Let's create a new Smart Contract to have an example.

// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.1;

contract AddressExample {
    address public myAddress;

    function setAddress(address _address) public {
        myAddress = _address;
    }

    function getBalanceOfAccount() public view returns(uint) {
        return myAddress.balance;
    }
}

Deploy the Smart Contract with Remix to the JavaScript VM:

Important Concepts

As you continue, please pay special attention to the following few concepts here which are really important and different than in any other programming language:

  1. The Smart Contract is stored under its own address
  2. The Smart Contract can store an address in the variable "myAddress", which can be its own address, but can be any other address as well.
  3. All information on the blochain is public, so we can retrieve the balance of the address stored in the variable "myAddress"
  4. The Smart Contract can transfer funds from his own address to another address. But it cannot transfer the funds from another address.
  5. Transferring Ether is fundamentally different than transferring ERC20 Tokens, as you will see later.

Before you continue, read the statements above and keep them in mind. These are the most mind-blowing facts for Ethereum newcomers.

Let's run the Smart Contract and get the balance of addresses programatically.

Run the Smart Contract

What we're going to do is to access the address in the accounts-list programatically from within the Smart Contract. We will:

  1. Copy the Address from the Accounts-List
  2. Update the "myAddress" variable in the Smart Contract
  3. Get the Balance of the address stored

Let's copy your first address from the Accounts-List. Click the little "copy" icon next to your Account:

Then paste it into the input field next to "setAddress" and then click the "setAddress" button:

When you hit the "myAddress" button, it should show you your address:

Now, let's check the balance, click on "getBalanceOfAccount" button and it should show you the balance in Wei:

Ethereum Denominations

A short reminder on Ethereum Denominations. Wei is the smallest, Ether = 10^18 Wei.

Unit Wei Exp Wei
wei 1 1
Kwei 10^3 1,000
Mwei 10^6 1,000,000
Gwei 10^9 1,000,000,000
Ether 10^18 1,000,000,000,000,000,000

Your balance will be very similar to the one in the picture above, probably around 99.999999-some Ether. Why not 100 Ether, or where do the Ether come from? The JavaScript VM is a simulated environment that will "give" you 100 Ether to play. Every transaction costs a little bit of Ether in Gas-Costs, which we will cover later.

Later on you will see how a Smart Contract can manage Ether which are sent to the address of the Smart Contract. Let's discuss an example using Strings next!


Last update: April 17, 2021