Solidity Address

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

An address is a data type in Solidity which holds the value of a contract address or an externally owned address (EOA). Here is a smart contract that uses an address:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract addressExample{
    
    address public yourAddress = 0xd9145CCE52D386f254917e481eB44e9943F39138;
    address public zeroAddress = address(0);

    function whatIsMyAddress() public view returns(address){
        return msg.sender;
    }

} 

All addresses are hashes generated using the keccak-256 algorithm. All addresses contain 20 bytes of data, or the equivalent of 160 bits or 40 hexadecimal characters. All addresses start with “0x”, which is the universal notation that the follow characters are in hexadecimal format.

You can get the ETH balance of any address by using the .balance method. Here’s an example of a contract you can use to check your own address’s balance:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract addressExample{
    
    function whatIsMyAddress(address yourAddress) public view returns(uint){
        return yourAddress.balance;
    }

} 

There are other methods you can call on an address, but we won’t cover them in depth here:

  • .call
  • .transfer
  • .callcode
  • .delegatecall
  • .staticcall

Checksum Addresses

You might be wondering if capitalization matters in an Ethereum address. Proper addresses always have some letters capitalized, while others are not. It’s up to the developer of a project to determine if they want to use “checksum addresses”, which means that capitalization matters.

If you want to read more about Checksum Addresses, I recommend reading EIP-55.

Payable Addresses

Addresses can also be “payable”, which means that the defined address can receive ETH. An address without the “payable” keyword (which was introduced in Solidity 0.5.0) can’t receive ETH. Here is a contract that can be deployed with ETH, which can then be sent back to the caller of a function:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;

contract addressPayable{
    
    address payable yourAddress;

    constructor() payable{}
    //this line lets you deploy this contract with ETH

    function payMeBack() public{
        yourAddress = payable(msg.sender);
        yourAddress.transfer(address(this).balance);
    }

} 

In the function named “payMeBack”, the first line is assigning the variable “yourAddress” to a payable version of msg.sender, which means the address of the person calling the function. The second line uses “.transfer” to attempt to send ETH to value of “yourAddress”. You can see we also send all of the ETH available by using “address(this)” which returns the address of the contract, and then call the balance of that contract to end up with the balance of our deployed contract.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.