Solidity Tutorial : all about Addresses – Jean Cvllr

You define a variable as an address type by simply specifying the keyword address in front of the variable name.
address user = msg.sender
Here we use the Solidity built-in function msg.sender to retrieve the address of the account that interacted with the smart contract.
Address literals
Address literals are the hexadecimal representation of an Ethereum address (prefixed with 0x) that pass the checksum test. (Apparently their size range from 39 to 41 digits). You can declare address literals in Solidity as follow :
address owner = 0xc0ffee254729296a45a3885639AC7E10F9d54979
Address literals that do not pass the checksum test bring up a warning and are, additionally, treated a regular rational number intervals.
The mixed-case address checksum format is defined in EIP-55
Address literals (hexadecimal representation of an address) are by default set as address payable .
address vs address payable ?
The distinction between address and address payable was introduced with version 0.5.0 of Solidity.
Tdhe idea behind this distinction is that address payable is an address you can send Ether to, while a plain address cannot be sent Ether.
When assigning the keyword payable to an address variable in Solidity, the address (variable) can send and receive Ethers. As a result, all the other methods (transfer, send, call, delegatecall and staticcall) become available for this variable.
Type conversion between address andaddress payable
- Implicit conversions from
address payabletoaddressare allowed
Give an example of user list.
- Implicit conversions from
addresstoaddress payableare not possible (except for address payable).
NB: the only way to convert from
addresstoaddress payableis by using an intermediate conversion touint160(160 bits = 20 bytes, the size of an Ethereum address).
- Explicit conversion from and to
addressare allowed for : integers, integer literals,bytes20and contract type. - Explicit conversion in the form
address payable(x)(wherex= integers, integer literal,bytesor contract type). - The result of a conversion in the form
address(x)has the typeaddress payable, ifxis of integer or fixed bytes type, a literal or a contract with a payable fallback function.
Contracts as address
Before version 0.5.0, contracts directly derived from the address type (since there was no distinction between address and address payable ).
Starting from version 0.5.0 of Solidity, contracts do not derive from the address type anymore. However, they can still be explicitly converted to and from address and address payable, if they have a payable fallback function.
Let’s assume the following code :
contract NotPayable contract Payable }
contract HelloWorld }
Let’s look at our Hello World contract to understand :
NotPayableis a contract without a payable fallback function. The the variablexwhich is assigned the valueaddress(NotPayable)will be of typeaddress.Payableis a contract with a payable fallback function. The the variableywhich is assigned the valueaddress(Payable)will be of typeaddress payable.
NB : The conversion is still performed using
address(variable)and not usingaddress payable(variable).
- An external function signatures
addressis used for both theaddressand theaddress payabletype.
address conversion using operators
The following operators are available with addresses : <=, <, ==, !=, >= and > .
Published at Thu, 01 Aug 2019 13:09:06 +0000
{flickr|100|campaign}
