89DEVs

address and address payable in Solidity

The address data type in Solidity stores an address. It is a value type, when it is assigned to another variable the value is copied and not referenced. The address can be made payable by adding the payable keyword. This allows the address to receive Ether.

There are currently around 200 million unique addresses on the Ethereum blockchain
The address is technically the last 20 bytes of the keccak-256 hash of the public key in Ethereum. It has always the prefix 0x because it is a hexadecimal format. The total length of the address is 42 characters: 2 characters for the prefix and 40 characters for the address (20 bytes).

address and address payable

There are two types of addresses in Solidity:
  • address is a 20 byte value that represents an ethereum address
  • address payable is similar to address, but allows to use transfer, send and more
// declare address and address payable // assign zero address to variable addr1 address public addr1 = 0x0000000000000000000000000000000000000000; // assign contract address as payable to variable addr2 address payable public addr2 = payable(address(this)); In summary, the address payable data type is supposed to be an address that can receive Ether. The address data type, on the other hand, is not supposed to receive Ether. It can represent a smart contract that is not supposed to receive Ether for example. To get the address of the caller of our contract we use the global variable msg.sender address _caller = msg.sender

default value and zero address

The default value of the address data type in Solidity is 0x0000000000000000000000000000000000000000. The so-called zero address is used for burned tokens and holds currently over $180m in tokens. To use the zero address in Solidity we write: address(0)

members of address

  • .balance()
  • .code()
  • .codehash()
  • .call()
  • .delegatecall()
  • .staticcall()

additional members of address payable

  • .transfer()
  • .send()

        

Summary

Click to jump to section