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: MIT
pragma solidity 0.8.15;
contract ExampleAddress {
address public someAddress;
function setSomeAddress(address _someAddress) public {
someAddress = _someAddress;
}
function getAddressBalance() public view returns(uint) {
return someAddress.balance;
}
}
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:
- The Smart Contract is stored under its own address
- The Smart Contract can store an address in the variable āsomeAddressā, which can be its own address, but can be any other address as well.
- All information on the blochain is public, so we can retrieve the balance of the address stored in the variable āsomeAddressā
- The Smart Contract can transfer funds from his own address to another address. But it cannot transfer the funds from another address.
- Transferring Ether is fundamentally different than transferring ERC20 Tokens or NFTs, 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:
- Copy the Address from the Accounts-List
- Update the āsomeAddressā variable in the Smart Contract
- 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 āsetSomeAddressā button:
When you hit the āsomeAddressā button, it should show you your address. To check the balance, click on āgetBalanceOfAccountā button and it should show you the balance in Wei:
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 the important msg-object next!