Skip to Content
Courses

Address Types

One type, which is very specific to Ethereum and Solidity, is the type ā€œaddressā€.

View the Full Course Now 

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:

ethereum-blockchain-developer-image

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:

ethereum-blockchain-developer-image

Then paste it into the input field next to ā€œsetAddressā€ and then click the ā€œsetAddressā€ button:

ethereum-blockchain-developer-image

When you hit the ā€œmyAddressā€ button, it should show you your address:

ethereum-blockchain-developer-image

Now, let’s check the balance, click on ā€œgetBalanceOfAccountā€ button and it should show you the balance in Wei:

ethereum-blockchain-developer-image

Ethereum Denominations

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

UnitWei ExpWei
wei11
Kwei10^31,000
Mwei10^61,000,000
Gwei10^91,000,000,000
Ether10^181,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 updated on